Issues (1502)

lib/request/getattachment.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * SPDX-License-Identifier: AGPL-3.0-only
5
 * SPDX-FileCopyrightText: Copyright 2007-2016 Zarafa Deutschland GmbH
6
 * SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH
7
 *
8
 * Provides the GETATTACHMENT command
9
 */
10
11
class GetAttachment extends RequestProcessor {
12
	/**
13
	 * Handles the GetAttachment command.
14
	 *
15
	 * @param int $commandCode
16
	 *
17
	 * @return bool
18
	 */
19
	public function Handle($commandCode) {
20
		$attname = Request::GetGETAttachmentName();
21
		if (!$attname) {
22
			return false;
23
		}
24
25
		try {
26
			$attachment = self::$backend->GetAttachmentData($attname);
27
			$stream = $attachment->data;
28
			SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleGetAttachment(): attachment stream from backend: %s", $stream));
29
30
			// need to check for a resource here, as eg. feof('Error') === false and causing infinite loop in while!
31
			if (!is_resource($stream)) {
32
				throw new StatusException(sprintf("HandleGetAttachment(): No stream resource returned by backend for attachment: %s", $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
0 ignored issues
show
It seems like $attname can also be of type true; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
				throw new StatusException(sprintf("HandleGetAttachment(): No stream resource returned by backend for attachment: %s", /** @scrutinizer ignore-type */ $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
Loading history...
33
			}
34
35
			header("Content-Type: application/octet-stream");
36
			self::$topCollector->AnnounceInformation("Starting attachment streaming", true);
37
			$l = fpassthru($stream);
38
			fclose($stream);
39
			if ($l === false) {
40
				throw new FatalException("HandleGetAttachment(): fpassthru === false !!!");
41
			}
42
			self::$topCollector->AnnounceInformation(sprintf("Streamed %d KB attachment", round($l / 1024)), true);
43
			SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleGetAttachment(): attachment with %d KB sent to mobile", round($l / 1024)));
44
		}
45
		catch (StatusException $s) {
46
			// StatusException already logged so we just need to pass it upwards to send a HTTP error
47
			throw new HTTPReturnCodeException($s->getMessage(), HTTP_CODE_500, null, LOGLEVEL_DEBUG);
48
		}
49
50
		return true;
51
	}
52
}
53