1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Christoph Wurst <[email protected]> |
5
|
|
|
* @author Christoph Wurst <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Thomas Müller <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Mail |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\Mail; |
26
|
|
|
|
27
|
|
|
use Horde_Imap_Client_Data_Fetch; |
28
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
29
|
|
|
|
30
|
|
|
class Attachment { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Horde_Imap_Client_Socket $conn |
34
|
|
|
* @param \Horde_Imap_Client_Mailbox $mailBox |
35
|
|
|
* @param int $messageId |
36
|
|
|
* @param string $attachmentId |
37
|
|
|
*/ |
38
|
|
|
public function __construct($conn, $mailBox, $messageId, $attachmentId) { |
39
|
|
|
$this->conn = $conn; |
40
|
|
|
$this->mailBox = $mailBox; |
41
|
|
|
$this->messageId = $messageId; |
42
|
|
|
$this->attachmentId = $attachmentId; |
43
|
|
|
|
44
|
|
|
$this->load(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Horde_Imap_Client_Socket |
49
|
|
|
*/ |
50
|
|
|
private $conn; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Horde_Imap_Client_Mailbox |
54
|
|
|
*/ |
55
|
|
|
private $mailBox; |
56
|
|
|
private $messageId; |
57
|
|
|
private $attachmentId; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \Horde_Mime_Part |
61
|
|
|
*/ |
62
|
|
|
private $mimePart; |
63
|
|
|
|
64
|
|
|
private function load() { |
65
|
|
|
$headers = []; |
66
|
|
|
|
67
|
|
|
$fetch_query = new \Horde_Imap_Client_Fetch_Query(); |
68
|
|
|
$fetch_query->bodyPart($this->attachmentId); |
69
|
|
|
$fetch_query->mimeHeader($this->attachmentId); |
70
|
|
|
|
71
|
|
|
$headers = array_merge($headers, [ |
72
|
|
|
'importance', |
73
|
|
|
'list-post', |
74
|
|
|
'x-priority' |
75
|
|
|
]); |
76
|
|
|
$headers[] = 'content-type'; |
77
|
|
|
|
78
|
|
|
$fetch_query->headers('imp', $headers, [ |
79
|
|
|
'cache' => true |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
// $list is an array of Horde_Imap_Client_Data_Fetch objects. |
83
|
|
|
$ids = new \Horde_Imap_Client_Ids($this->messageId); |
84
|
|
|
$headers = $this->conn->fetch($this->mailBox, $fetch_query, ['ids' => $ids]); |
85
|
|
|
/** @var $fetch Horde_Imap_Client_Data_Fetch */ |
86
|
|
|
if (!isset($headers[$this->messageId])) { |
87
|
|
|
throw new DoesNotExistException('Unable to load the attachment.'); |
88
|
|
|
} |
89
|
|
|
$fetch = $headers[$this->messageId]; |
90
|
|
|
$mimeHeaders = $fetch->getMimeHeader($this->attachmentId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE); |
91
|
|
|
|
92
|
|
|
$this->mimePart = new \Horde_Mime_Part(); |
93
|
|
|
|
94
|
|
|
// To prevent potential problems with the SOP we serve all files with the |
95
|
|
|
// MIME type "application/octet-stream" |
96
|
|
|
$this->mimePart->setType('application/octet-stream'); |
97
|
|
|
|
98
|
|
|
// Serve all files with a content-disposition of "attachment" to prevent Cross-Site Scripting |
99
|
|
|
$this->mimePart->setDisposition('attachment'); |
100
|
|
|
|
101
|
|
|
// Extract headers from part |
102
|
|
|
$contentDisposition = $mimeHeaders->getValue('content-disposition', \Horde_Mime_Headers::VALUE_PARAMS); |
103
|
|
|
if (!is_null($contentDisposition)) { |
104
|
|
|
$vars = ['filename']; |
105
|
|
|
foreach ($contentDisposition as $key => $val) { |
106
|
|
|
if(in_array($key, $vars)) { |
107
|
|
|
$this->mimePart->setDispositionParameter($key, $val); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
$contentDisposition = $mimeHeaders->getValue('content-type', \Horde_Mime_Headers::VALUE_PARAMS); |
112
|
|
|
$vars = ['name']; |
113
|
|
|
foreach ($contentDisposition as $key => $val) { |
114
|
|
|
if(in_array($key, $vars)) { |
115
|
|
|
$this->mimePart->setContentTypeParameter($key, $val); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/* Content transfer encoding. */ |
121
|
|
|
if ($tmp = $mimeHeaders->getValue('content-transfer-encoding')) { |
122
|
|
|
$this->mimePart->setTransferEncoding($tmp); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$body = $fetch->getBodyPart($this->attachmentId); |
126
|
|
|
$this->mimePart->setContents($body); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getContents() { |
133
|
|
|
return $this->mimePart->getContents(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getName() { |
140
|
|
|
return $this->mimePart->getName(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getType() { |
147
|
|
|
return $this->mimePart->getType(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|