GetAttachment   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Handle() 0 32 5
1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2007-2016 Zarafa Deutschland GmbH
5
 * SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH
6
 *
7
 * Provides the GETATTACHMENT command
8
 */
9
10
class GetAttachment extends RequestProcessor {
11
	/**
12
	 * Handles the GetAttachment command.
13
	 *
14
	 * @param int $commandCode
15
	 *
16
	 * @return bool
17
	 */
18
	public function Handle($commandCode) {
19
		$attname = Request::GetGETAttachmentName();
20
		if (!$attname) {
21
			return false;
22
		}
23
24
		try {
25
			$attachment = self::$backend->GetAttachmentData($attname);
26
			$stream = $attachment->data;
27
			SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleGetAttachment(): attachment stream from backend: %s", $stream));
28
29
			// need to check for a resource here, as eg. feof('Error') === false and causing infinite loop in while!
30
			if (!is_resource($stream)) {
31
				throw new StatusException(sprintf("HandleGetAttachment(): No stream resource returned by backend for attachment: %s", $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
0 ignored issues
show
Bug introduced by
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

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