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); |
|
|
|
|
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
|
|
|
|