commit | author | age
|
8fa58e
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
e019f2
|
5 |
| This file is part of the Roundcube Webmail client | |
f5e7b3
|
6 |
| Copyright (C) 2008-2010, The Roundcube Dev Team | |
7fe381
|
7 |
| | |
T |
8 |
| Licensed under the GNU General Public License version 3 or | |
|
9 |
| any later version with exceptions for skins & plugins. | |
|
10 |
| See the README file for a full license statement. | |
8fa58e
|
11 |
| | |
T |
12 |
| PURPOSE: | |
|
13 |
| Logical representation of a mail message with all its data | |
|
14 |
| and related functions | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
17 |
+-----------------------------------------------------------------------+ |
|
18 |
*/ |
|
19 |
|
|
20 |
/** |
45f56c
|
21 |
* Logical representation of a mail message with all its data |
T |
22 |
* and related functions |
8fa58e
|
23 |
* |
9ab346
|
24 |
* @package Framework |
AM |
25 |
* @subpackage Storage |
8fa58e
|
26 |
* @author Thomas Bruederli <roundcube@gmail.com> |
T |
27 |
*/ |
|
28 |
class rcube_message |
|
29 |
{ |
5c461b
|
30 |
/** |
be98df
|
31 |
* Instace of framework class. |
5c461b
|
32 |
* |
be98df
|
33 |
* @var rcube |
5c461b
|
34 |
*/ |
d311d8
|
35 |
private $app; |
5c461b
|
36 |
|
A |
37 |
/** |
8b92d2
|
38 |
* Instance of storage class |
5c461b
|
39 |
* |
8b92d2
|
40 |
* @var rcube_storage |
5c461b
|
41 |
*/ |
8b92d2
|
42 |
private $storage; |
1c4f23
|
43 |
|
A |
44 |
/** |
|
45 |
* Instance of mime class |
|
46 |
* |
|
47 |
* @var rcube_mime |
|
48 |
*/ |
|
49 |
private $mime; |
d311d8
|
50 |
private $opt = array(); |
A |
51 |
private $parse_alternative = false; |
2ae58f
|
52 |
|
10562d
|
53 |
public $uid; |
AM |
54 |
public $folder; |
d311d8
|
55 |
public $headers; |
A |
56 |
public $parts = array(); |
|
57 |
public $mime_parts = array(); |
287eff
|
58 |
public $inline_parts = array(); |
d311d8
|
59 |
public $attachments = array(); |
A |
60 |
public $subject = ''; |
|
61 |
public $sender = null; |
|
62 |
public $is_safe = false; |
2ae58f
|
63 |
|
193fb4
|
64 |
|
d311d8
|
65 |
/** |
A |
66 |
* __construct |
|
67 |
* |
|
68 |
* Provide a uid, and parse message structure. |
|
69 |
* |
10562d
|
70 |
* @param string $uid The message UID. |
AM |
71 |
* @param string $folder Folder name |
d311d8
|
72 |
* |
8b92d2
|
73 |
* @see self::$app, self::$storage, self::$opt, self::$parts |
d311d8
|
74 |
*/ |
10562d
|
75 |
function __construct($uid, $folder = null) |
8fa58e
|
76 |
{ |
1c4f23
|
77 |
$this->uid = $uid; |
0c2596
|
78 |
$this->app = rcube::get_instance(); |
c321a9
|
79 |
$this->storage = $this->app->get_storage(); |
10562d
|
80 |
$this->folder = strlen($folder) ? $folder : $this->storage->get_folder(); |
c321a9
|
81 |
$this->storage->set_options(array('all_headers' => true)); |
10562d
|
82 |
|
AM |
83 |
// Set current folder |
|
84 |
$this->storage->set_folder($this->folder); |
64e3e8
|
85 |
|
c321a9
|
86 |
$this->headers = $this->storage->get_message($uid); |
64e3e8
|
87 |
|
A |
88 |
if (!$this->headers) |
|
89 |
return; |
8fa58e
|
90 |
|
1c4f23
|
91 |
$this->mime = new rcube_mime($this->headers->charset); |
A |
92 |
|
|
93 |
$this->subject = $this->mime->decode_mime_string($this->headers->subject); |
|
94 |
list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1)); |
64e3e8
|
95 |
|
d311d8
|
96 |
$this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid])); |
A |
97 |
$this->opt = array( |
|
98 |
'safe' => $this->is_safe, |
|
99 |
'prefer_html' => $this->app->config->get('prefer_html'), |
0c2596
|
100 |
'get_url' => $this->app->url(array( |
A |
101 |
'action' => 'get', |
|
102 |
'mbox' => $this->storage->get_folder(), |
|
103 |
'uid' => $uid)) |
d311d8
|
104 |
); |
8fa58e
|
105 |
|
80152b
|
106 |
if (!empty($this->headers->structure)) { |
A |
107 |
$this->get_mime_numbers($this->headers->structure); |
|
108 |
$this->parse_structure($this->headers->structure); |
aa16b4
|
109 |
} |
d311d8
|
110 |
else { |
c321a9
|
111 |
$this->body = $this->storage->get_body($uid); |
d311d8
|
112 |
} |
A |
113 |
|
|
114 |
// notify plugins and let them analyze this structured message object |
|
115 |
$this->app->plugins->exec_hook('message_load', array('object' => $this)); |
|
116 |
} |
2ae58f
|
117 |
|
A |
118 |
|
d311d8
|
119 |
/** |
A |
120 |
* Return a (decoded) message header |
|
121 |
* |
5c461b
|
122 |
* @param string $name Header name |
A |
123 |
* @param bool $row Don't mime-decode the value |
d311d8
|
124 |
* @return string Header value |
A |
125 |
*/ |
|
126 |
public function get_header($name, $raw = false) |
|
127 |
{ |
1c4f23
|
128 |
if (empty($this->headers)) |
A |
129 |
return null; |
|
130 |
|
e25a35
|
131 |
if ($this->headers->$name) |
A |
132 |
$value = $this->headers->$name; |
|
133 |
else if ($this->headers->others[$name]) |
|
134 |
$value = $this->headers->others[$name]; |
|
135 |
|
1c4f23
|
136 |
return $raw ? $value : $this->mime->decode_header($value); |
d311d8
|
137 |
} |
A |
138 |
|
2ae58f
|
139 |
|
d311d8
|
140 |
/** |
A |
141 |
* Set is_safe var and session data |
|
142 |
* |
5c461b
|
143 |
* @param bool $safe enable/disable |
d311d8
|
144 |
*/ |
A |
145 |
public function set_safe($safe = true) |
|
146 |
{ |
|
147 |
$this->is_safe = $safe; |
|
148 |
$_SESSION['safe_messages'][$this->uid] = $this->is_safe; |
|
149 |
} |
|
150 |
|
|
151 |
|
|
152 |
/** |
|
153 |
* Compose a valid URL for getting a message part |
|
154 |
* |
5c461b
|
155 |
* @param string $mime_id Part MIME-ID |
d311d8
|
156 |
* @return string URL or false if part does not exist |
A |
157 |
*/ |
57486f
|
158 |
public function get_part_url($mime_id, $embed = false) |
d311d8
|
159 |
{ |
A |
160 |
if ($this->mime_parts[$mime_id]) |
57486f
|
161 |
return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1' : ''); |
aa16b4
|
162 |
else |
d311d8
|
163 |
return false; |
8fa58e
|
164 |
} |
T |
165 |
|
d311d8
|
166 |
|
A |
167 |
/** |
|
168 |
* Get content of a specific part of this message |
|
169 |
* |
71950d
|
170 |
* @param string $mime_id Part MIME-ID |
A |
171 |
* @param resource $fp File pointer to save the message part |
|
172 |
* @param boolean $skip_charset_conv Disables charset conversion |
dff2c7
|
173 |
* @param int $max_bytes Only read this number of bytes |
71950d
|
174 |
* |
d311d8
|
175 |
* @return string Part content |
A |
176 |
*/ |
dff2c7
|
177 |
public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false, $max_bytes = 0) |
d311d8
|
178 |
{ |
A |
179 |
if ($part = $this->mime_parts[$mime_id]) { |
|
180 |
// stored in message structure (winmail/inline-uuencode) |
8b92d2
|
181 |
if (!empty($part->body) || $part->encoding == 'stream') { |
d311d8
|
182 |
if ($fp) { |
A |
183 |
fwrite($fp, $part->body); |
|
184 |
} |
|
185 |
return $fp ? true : $part->body; |
|
186 |
} |
10562d
|
187 |
|
d311d8
|
188 |
// get from IMAP |
10562d
|
189 |
$this->storage->set_folder($this->folder); |
AM |
190 |
|
dff2c7
|
191 |
return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv, $max_bytes); |
10562d
|
192 |
} |
8fa58e
|
193 |
} |
T |
194 |
|
|
195 |
|
d311d8
|
196 |
/** |
A |
197 |
* Determine if the message contains a HTML part |
|
198 |
* |
33423a
|
199 |
* @param bool $recursive Enables checking in all levels of the structure |
52d0d9
|
200 |
* @param bool $enriched Enables checking for text/enriched parts too |
33423a
|
201 |
* |
d311d8
|
202 |
* @return bool True if a HTML is available, False if not |
A |
203 |
*/ |
52d0d9
|
204 |
function has_html_part($recursive = true, $enriched = false) |
d311d8
|
205 |
{ |
A |
206 |
// check all message parts |
33423a
|
207 |
foreach ($this->parts as $part) { |
52d0d9
|
208 |
if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) { |
33423a
|
209 |
// Level check, we'll skip e.g. HTML attachments |
A |
210 |
if (!$recursive) { |
|
211 |
$level = explode('.', $part->mime_id); |
|
212 |
|
271efe
|
213 |
// Skip if level too deep or part has a file name |
TB |
214 |
if (count($level) > 2 || $part->filename) { |
33423a
|
215 |
continue; |
A |
216 |
} |
|
217 |
|
|
218 |
// HTML part can be on the lower level, if not... |
|
219 |
if (count($level) > 1) { |
271efe
|
220 |
array_pop($level); |
TB |
221 |
$parent = $this->mime_parts[join('.', $level)]; |
|
222 |
// ... parent isn't multipart/alternative or related |
33423a
|
223 |
if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') { |
A |
224 |
continue; |
|
225 |
} |
|
226 |
} |
|
227 |
} |
|
228 |
|
d311d8
|
229 |
return true; |
33423a
|
230 |
} |
d311d8
|
231 |
} |
A |
232 |
|
|
233 |
return false; |
|
234 |
} |
|
235 |
|
|
236 |
|
|
237 |
/** |
|
238 |
* Return the first HTML part of this message |
|
239 |
* |
|
240 |
* @return string HTML message part content |
|
241 |
*/ |
|
242 |
function first_html_part() |
|
243 |
{ |
|
244 |
// check all message parts |
33423a
|
245 |
foreach ($this->mime_parts as $pid => $part) { |
A |
246 |
if ($part->mimetype == 'text/html') { |
|
247 |
return $this->get_part_content($pid); |
d311d8
|
248 |
} |
A |
249 |
} |
|
250 |
} |
|
251 |
|
|
252 |
|
|
253 |
/** |
|
254 |
* Return the first text part of this message |
|
255 |
* |
868deb
|
256 |
* @param rcube_message_part $part Reference to the part if found |
d311d8
|
257 |
* @return string Plain text message/part content |
A |
258 |
*/ |
868deb
|
259 |
function first_text_part(&$part=null) |
d311d8
|
260 |
{ |
A |
261 |
// no message structure, return complete body |
|
262 |
if (empty($this->parts)) |
|
263 |
return $this->body; |
|
264 |
|
|
265 |
// check all message parts |
|
266 |
foreach ($this->mime_parts as $mime_id => $part) { |
33423a
|
267 |
if ($part->mimetype == 'text/plain') { |
8b92d2
|
268 |
return $this->get_part_content($mime_id); |
d311d8
|
269 |
} |
33423a
|
270 |
else if ($part->mimetype == 'text/html') { |
8b92d2
|
271 |
$out = $this->get_part_content($mime_id); |
d311d8
|
272 |
|
A |
273 |
// create instance of html2text class |
66afd7
|
274 |
$txt = new rcube_html2text($out); |
868deb
|
275 |
return $txt->get_text(); |
d311d8
|
276 |
} |
A |
277 |
} |
|
278 |
|
868deb
|
279 |
$part = null; |
A |
280 |
return null; |
d311d8
|
281 |
} |
A |
282 |
|
|
283 |
|
|
284 |
/** |
3efc74
|
285 |
* Checks if part of the message is an attachment (or part of it) |
AM |
286 |
* |
|
287 |
* @param rcube_message_part $part Message part |
|
288 |
* |
|
289 |
* @return bool True if the part is an attachment part |
|
290 |
*/ |
|
291 |
public function is_attachment($part) |
|
292 |
{ |
|
293 |
foreach ($this->attachments as $att_part) { |
|
294 |
if ($att_part->mime_id == $part->mime_id) { |
|
295 |
return true; |
|
296 |
} |
|
297 |
|
|
298 |
// check if the part is a subpart of another attachment part (message/rfc822) |
|
299 |
if ($att_part->mimetype == 'message/rfc822') { |
|
300 |
if (in_array($part, (array)$att_part->parts)) { |
|
301 |
return true; |
|
302 |
} |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
return false; |
|
307 |
} |
|
308 |
|
|
309 |
|
|
310 |
/** |
8b92d2
|
311 |
* Read the message structure returend by the IMAP server |
d311d8
|
312 |
* and build flat lists of content parts and attachments |
A |
313 |
* |
5c461b
|
314 |
* @param rcube_message_part $structure Message structure node |
A |
315 |
* @param bool $recursive True when called recursively |
d311d8
|
316 |
*/ |
A |
317 |
private function parse_structure($structure, $recursive = false) |
|
318 |
{ |
|
319 |
// real content-type of message/rfc822 part |
a8a72e
|
320 |
if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype) { |
5ced9c
|
321 |
$mimetype = $structure->real_mimetype; |
a8a72e
|
322 |
|
TB |
323 |
// parse headers from message/rfc822 part |
|
324 |
if (!isset($structure->headers['subject'])) { |
|
325 |
list($headers, $dump) = explode("\r\n\r\n", $this->get_part_content($structure->mime_id, null, true, 4096)); |
|
326 |
$structure->headers = rcube_mime::parse_headers($headers); |
|
327 |
} |
|
328 |
} |
5ced9c
|
329 |
else |
A |
330 |
$mimetype = $structure->mimetype; |
d311d8
|
331 |
|
A |
332 |
// show message headers |
|
333 |
if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) { |
|
334 |
$c = new stdClass; |
|
335 |
$c->type = 'headers'; |
c5d7c9
|
336 |
$c->headers = $structure->headers; |
d311d8
|
337 |
$this->parts[] = $c; |
A |
338 |
} |
5ced9c
|
339 |
|
A |
340 |
// Allow plugins to handle message parts |
|
341 |
$plugin = $this->app->plugins->exec_hook('message_part_structure', |
|
342 |
array('object' => $this, 'structure' => $structure, |
|
343 |
'mimetype' => $mimetype, 'recursive' => $recursive)); |
|
344 |
|
|
345 |
if ($plugin['abort']) |
|
346 |
return; |
|
347 |
|
|
348 |
$structure = $plugin['structure']; |
|
349 |
list($message_ctype_primary, $message_ctype_secondary) = explode('/', $plugin['mimetype']); |
d311d8
|
350 |
|
A |
351 |
// print body if message doesn't have multiple parts |
|
352 |
if ($message_ctype_primary == 'text' && !$recursive) { |
c23dc8
|
353 |
// parts with unsupported type add to attachments list |
AM |
354 |
if (!in_array($message_ctype_secondary, array('plain', 'html', 'enriched'))) { |
|
355 |
$this->attachments[] = $structure; |
|
356 |
return; |
|
357 |
} |
|
358 |
|
d311d8
|
359 |
$structure->type = 'content'; |
c5d7c9
|
360 |
$this->parts[] = $structure; |
8757f5
|
361 |
|
d311d8
|
362 |
// Parse simple (plain text) message body |
c23dc8
|
363 |
if ($message_ctype_secondary == 'plain') { |
d311d8
|
364 |
foreach ((array)$this->uu_decode($structure) as $uupart) { |
A |
365 |
$this->mime_parts[$uupart->mime_id] = $uupart; |
|
366 |
$this->attachments[] = $uupart; |
|
367 |
} |
c23dc8
|
368 |
} |
d311d8
|
369 |
} |
A |
370 |
// the same for pgp signed messages |
|
371 |
else if ($mimetype == 'application/pgp' && !$recursive) { |
|
372 |
$structure->type = 'content'; |
c5d7c9
|
373 |
$this->parts[] = $structure; |
d311d8
|
374 |
} |
e730cd
|
375 |
// message contains (more than one!) alternative parts |
A |
376 |
else if ($mimetype == 'multipart/alternative' |
|
377 |
&& is_array($structure->parts) && count($structure->parts) > 1 |
|
378 |
) { |
c5d7c9
|
379 |
$plain_part = null; |
AM |
380 |
$html_part = null; |
|
381 |
$print_part = null; |
|
382 |
$related_part = null; |
|
383 |
$attach_part = null; |
d311d8
|
384 |
|
c5d7c9
|
385 |
// get html/plaintext parts, other add to attachments list |
d311d8
|
386 |
foreach ($structure->parts as $p => $sub_part) { |
A |
387 |
$sub_mimetype = $sub_part->mimetype; |
c5d7c9
|
388 |
$is_multipart = in_array($sub_mimetype, array('multipart/related', 'multipart/mixed', 'multipart/alternative')); |
8757f5
|
389 |
|
5fbfde
|
390 |
// skip empty text parts |
c5d7c9
|
391 |
if (!$sub_part->size && !$is_multipart) { |
5f4095
|
392 |
continue; |
5fbfde
|
393 |
} |
5f4095
|
394 |
|
d311d8
|
395 |
// check if sub part is |
c5d7c9
|
396 |
if ($is_multipart) |
AM |
397 |
$related_part = $p; |
|
398 |
else if ($sub_mimetype == 'text/plain') |
d311d8
|
399 |
$plain_part = $p; |
A |
400 |
else if ($sub_mimetype == 'text/html') |
|
401 |
$html_part = $p; |
|
402 |
else if ($sub_mimetype == 'text/enriched') |
|
403 |
$enriched_part = $p; |
c5d7c9
|
404 |
else |
AM |
405 |
$attach_part = $p; |
d311d8
|
406 |
} |
A |
407 |
|
|
408 |
// parse related part (alternative part could be in here) |
|
409 |
if ($related_part !== null && !$this->parse_alternative) { |
|
410 |
$this->parse_alternative = true; |
|
411 |
$this->parse_structure($structure->parts[$related_part], true); |
|
412 |
$this->parse_alternative = false; |
8757f5
|
413 |
|
d311d8
|
414 |
// if plain part was found, we should unset it if html is preferred |
A |
415 |
if ($this->opt['prefer_html'] && count($this->parts)) |
|
416 |
$plain_part = null; |
|
417 |
} |
|
418 |
|
|
419 |
// choose html/plain part to print |
|
420 |
if ($html_part !== null && $this->opt['prefer_html']) { |
c5d7c9
|
421 |
$print_part = $structure->parts[$html_part]; |
d311d8
|
422 |
} |
A |
423 |
else if ($enriched_part !== null) { |
c5d7c9
|
424 |
$print_part = $structure->parts[$enriched_part]; |
d311d8
|
425 |
} |
A |
426 |
else if ($plain_part !== null) { |
c5d7c9
|
427 |
$print_part = $structure->parts[$plain_part]; |
d311d8
|
428 |
} |
A |
429 |
|
|
430 |
// add the right message body |
|
431 |
if (is_object($print_part)) { |
|
432 |
$print_part->type = 'content'; |
|
433 |
$this->parts[] = $print_part; |
|
434 |
} |
|
435 |
// show plaintext warning |
|
436 |
else if ($html_part !== null && empty($this->parts)) { |
|
437 |
$c = new stdClass; |
|
438 |
$c->type = 'content'; |
|
439 |
$c->ctype_primary = 'text'; |
|
440 |
$c->ctype_secondary = 'plain'; |
0c2596
|
441 |
$c->mimetype = 'text/plain'; |
A |
442 |
$c->realtype = 'text/html'; |
d311d8
|
443 |
|
A |
444 |
$this->parts[] = $c; |
|
445 |
} |
|
446 |
|
|
447 |
// add html part as attachment |
|
448 |
if ($html_part !== null && $structure->parts[$html_part] !== $print_part) { |
c5d7c9
|
449 |
$html_part = $structure->parts[$html_part]; |
d311d8
|
450 |
$html_part->mimetype = 'text/html'; |
A |
451 |
|
|
452 |
$this->attachments[] = $html_part; |
c5d7c9
|
453 |
} |
AM |
454 |
|
|
455 |
// add unsupported/unrecognized parts to attachments list |
|
456 |
if ($attach_part) { |
|
457 |
$this->attachments[] = $structure->parts[$attach_part]; |
d311d8
|
458 |
} |
A |
459 |
} |
|
460 |
// this is an ecrypted message -> create a plaintext body with the according message |
|
461 |
else if ($mimetype == 'multipart/encrypted') { |
|
462 |
$p = new stdClass; |
|
463 |
$p->type = 'content'; |
|
464 |
$p->ctype_primary = 'text'; |
|
465 |
$p->ctype_secondary = 'plain'; |
0c2596
|
466 |
$p->mimetype = 'text/plain'; |
A |
467 |
$p->realtype = 'multipart/encrypted'; |
c054ec
|
468 |
|
A |
469 |
$this->parts[] = $p; |
d311d8
|
470 |
} |
A |
471 |
// message contains multiple parts |
|
472 |
else if (is_array($structure->parts) && !empty($structure->parts)) { |
|
473 |
// iterate over parts |
|
474 |
for ($i=0; $i < count($structure->parts); $i++) { |
|
475 |
$mail_part = &$structure->parts[$i]; |
|
476 |
$primary_type = $mail_part->ctype_primary; |
|
477 |
$secondary_type = $mail_part->ctype_secondary; |
8fa58e
|
478 |
|
d311d8
|
479 |
// real content-type of message/rfc822 |
A |
480 |
if ($mail_part->real_mimetype) { |
|
481 |
$part_orig_mimetype = $mail_part->mimetype; |
|
482 |
$part_mimetype = $mail_part->real_mimetype; |
|
483 |
list($primary_type, $secondary_type) = explode('/', $part_mimetype); |
|
484 |
} |
|
485 |
else |
|
486 |
$part_mimetype = $mail_part->mimetype; |
6b6f2e
|
487 |
|
d311d8
|
488 |
// multipart/alternative |
A |
489 |
if ($primary_type == 'multipart') { |
|
490 |
$this->parse_structure($mail_part, true); |
|
491 |
|
|
492 |
// list message/rfc822 as attachment as well (mostly .eml) |
|
493 |
if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename)) |
|
494 |
$this->attachments[] = $mail_part; |
|
495 |
} |
f22ea7
|
496 |
// part text/[plain|html] or delivery status |
d311d8
|
497 |
else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') || |
f22ea7
|
498 |
in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification')) |
d311d8
|
499 |
) { |
1a2f83
|
500 |
// Allow plugins to handle also this part |
A |
501 |
$plugin = $this->app->plugins->exec_hook('message_part_structure', |
|
502 |
array('object' => $this, 'structure' => $mail_part, |
|
503 |
'mimetype' => $part_mimetype, 'recursive' => true)); |
|
504 |
|
|
505 |
if ($plugin['abort']) |
|
506 |
continue; |
|
507 |
|
9c299e
|
508 |
if ($part_mimetype == 'text/html' && $mail_part->size) { |
63f9de
|
509 |
$got_html_part = true; |
A |
510 |
} |
|
511 |
|
1a2f83
|
512 |
$mail_part = $plugin['structure']; |
A |
513 |
list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']); |
|
514 |
|
d311d8
|
515 |
// add text part if it matches the prefs |
A |
516 |
if (!$this->parse_alternative || |
|
517 |
($secondary_type == 'html' && $this->opt['prefer_html']) || |
|
518 |
($secondary_type == 'plain' && !$this->opt['prefer_html']) |
|
519 |
) { |
|
520 |
$mail_part->type = 'content'; |
|
521 |
$this->parts[] = $mail_part; |
|
522 |
} |
fd371a
|
523 |
|
d311d8
|
524 |
// list as attachment as well |
f7c11e
|
525 |
if (!empty($mail_part->filename)) { |
d311d8
|
526 |
$this->attachments[] = $mail_part; |
f7c11e
|
527 |
} |
AM |
528 |
// list html part as attachment (here the part is most likely inside a multipart/related part) |
|
529 |
else if ($this->parse_alternative && ($secondary_type == 'html' && !$this->opt['prefer_html'])) { |
|
530 |
$this->attachments[] = $mail_part; |
|
531 |
} |
d311d8
|
532 |
} |
A |
533 |
// part message/* |
8757f5
|
534 |
else if ($primary_type == 'message') { |
d311d8
|
535 |
$this->parse_structure($mail_part, true); |
A |
536 |
|
|
537 |
// list as attachment as well (mostly .eml) |
|
538 |
if (!empty($mail_part->filename)) |
|
539 |
$this->attachments[] = $mail_part; |
|
540 |
} |
|
541 |
// ignore "virtual" protocol parts |
|
542 |
else if ($primary_type == 'protocol') { |
|
543 |
continue; |
|
544 |
} |
|
545 |
// part is Microsoft Outlook TNEF (winmail.dat) |
|
546 |
else if ($part_mimetype == 'application/ms-tnef') { |
|
547 |
foreach ((array)$this->tnef_decode($mail_part) as $tpart) { |
|
548 |
$this->mime_parts[$tpart->mime_id] = $tpart; |
|
549 |
$this->attachments[] = $tpart; |
|
550 |
} |
|
551 |
} |
|
552 |
// part is a file/attachment |
|
553 |
else if (preg_match('/^(inline|attach)/', $mail_part->disposition) || |
8794f1
|
554 |
$mail_part->headers['content-id'] || |
A |
555 |
($mail_part->filename && |
|
556 |
(empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition))) |
d311d8
|
557 |
) { |
A |
558 |
// skip apple resource forks |
|
559 |
if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile') |
|
560 |
continue; |
|
561 |
|
|
562 |
// part belongs to a related message and is linked |
|
563 |
if ($mimetype == 'multipart/related' |
|
564 |
&& ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) { |
|
565 |
if ($mail_part->headers['content-id']) |
|
566 |
$mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']); |
|
567 |
if ($mail_part->headers['content-location']) |
|
568 |
$mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location']; |
|
569 |
|
|
570 |
$this->inline_parts[] = $mail_part; |
|
571 |
} |
|
572 |
// attachment encapsulated within message/rfc822 part needs further decoding (#1486743) |
|
573 |
else if ($part_orig_mimetype == 'message/rfc822') { |
|
574 |
$this->parse_structure($mail_part, true); |
fd371a
|
575 |
|
A |
576 |
// list as attachment as well (mostly .eml) |
|
577 |
if (!empty($mail_part->filename)) |
|
578 |
$this->attachments[] = $mail_part; |
d311d8
|
579 |
} |
b38925
|
580 |
// regular attachment with valid content type |
A |
581 |
// (content-type name regexp according to RFC4288.4.2) |
2ae58f
|
582 |
else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) { |
b38925
|
583 |
$this->attachments[] = $mail_part; |
A |
584 |
} |
|
585 |
// attachment with invalid content type |
|
586 |
// replace malformed content type with application/octet-stream (#1487767) |
|
587 |
else if ($mail_part->filename) { |
|
588 |
$mail_part->ctype_primary = 'application'; |
|
589 |
$mail_part->ctype_secondary = 'octet-stream'; |
|
590 |
$mail_part->mimetype = 'application/octet-stream'; |
|
591 |
|
d311d8
|
592 |
$this->attachments[] = $mail_part; |
A |
593 |
} |
|
594 |
} |
8757f5
|
595 |
// attachment part as message/rfc822 (#1488026) |
A |
596 |
else if ($mail_part->mimetype == 'message/rfc822') { |
|
597 |
$this->parse_structure($mail_part); |
|
598 |
} |
d311d8
|
599 |
} |
A |
600 |
|
|
601 |
// if this was a related part try to resolve references |
|
602 |
if ($mimetype == 'multipart/related' && sizeof($this->inline_parts)) { |
|
603 |
$a_replaces = array(); |
89d19c
|
604 |
$img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/'; |
d311d8
|
605 |
|
A |
606 |
foreach ($this->inline_parts as $inline_object) { |
57486f
|
607 |
$part_url = $this->get_part_url($inline_object->mime_id, true); |
d311d8
|
608 |
if ($inline_object->content_id) |
A |
609 |
$a_replaces['cid:'.$inline_object->content_id] = $part_url; |
63f9de
|
610 |
if ($inline_object->content_location) { |
d311d8
|
611 |
$a_replaces[$inline_object->content_location] = $part_url; |
63f9de
|
612 |
} |
89d19c
|
613 |
|
A |
614 |
if (!empty($inline_object->filename)) { |
|
615 |
// MS Outlook sends sometimes non-related attachments as related |
|
616 |
// In this case multipart/related message has only one text part |
|
617 |
// We'll add all such attachments to the attachments list |
|
618 |
if (!isset($got_html_part) && empty($inline_object->content_id)) { |
|
619 |
$this->attachments[] = $inline_object; |
|
620 |
} |
|
621 |
// MS Outlook sometimes also adds non-image attachments as related |
|
622 |
// We'll add all such attachments to the attachments list |
|
623 |
// Warning: some browsers support pdf in <img/> |
|
624 |
else if (!preg_match($img_regexp, $inline_object->mimetype)) { |
|
625 |
$this->attachments[] = $inline_object; |
|
626 |
} |
|
627 |
// @TODO: we should fetch HTML body and find attachment's content-id |
|
628 |
// to handle also image attachments without reference in the body |
|
629 |
// @TODO: should we list all image attachments in text mode? |
02b6e6
|
630 |
} |
d311d8
|
631 |
} |
A |
632 |
|
|
633 |
// add replace array to each content part |
|
634 |
// (will be applied later when part body is available) |
|
635 |
foreach ($this->parts as $i => $part) { |
|
636 |
if ($part->type == 'content') |
|
637 |
$this->parts[$i]->replaces = $a_replaces; |
|
638 |
} |
|
639 |
} |
|
640 |
} |
|
641 |
// message is a single part non-text |
|
642 |
else if ($structure->filename) { |
|
643 |
$this->attachments[] = $structure; |
|
644 |
} |
3e58bf
|
645 |
// message is a single part non-text (without filename) |
A |
646 |
else if (preg_match('/application\//i', $mimetype)) { |
|
647 |
$this->attachments[] = $structure; |
c5d7c9
|
648 |
} |
AM |
649 |
} |
|
650 |
|
|
651 |
|
|
652 |
/** |
d311d8
|
653 |
* Fill aflat array with references to all parts, indexed by part numbers |
A |
654 |
* |
5c461b
|
655 |
* @param rcube_message_part $part Message body structure |
d311d8
|
656 |
*/ |
A |
657 |
private function get_mime_numbers(&$part) |
|
658 |
{ |
|
659 |
if (strlen($part->mime_id)) |
|
660 |
$this->mime_parts[$part->mime_id] = &$part; |
f19d86
|
661 |
|
d311d8
|
662 |
if (is_array($part->parts)) |
A |
663 |
for ($i=0; $i<count($part->parts); $i++) |
|
664 |
$this->get_mime_numbers($part->parts[$i]); |
|
665 |
} |
|
666 |
|
|
667 |
|
|
668 |
/** |
|
669 |
* Decode a Microsoft Outlook TNEF part (winmail.dat) |
|
670 |
* |
5c461b
|
671 |
* @param rcube_message_part $part Message part to decode |
A |
672 |
* @return array |
d311d8
|
673 |
*/ |
A |
674 |
function tnef_decode(&$part) |
|
675 |
{ |
|
676 |
// @TODO: attachment may be huge, hadle it via file |
10562d
|
677 |
if (!isset($part->body)) { |
AM |
678 |
$this->storage->set_folder($this->folder); |
c321a9
|
679 |
$part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part); |
10562d
|
680 |
} |
d311d8
|
681 |
|
A |
682 |
$parts = array(); |
f19d86
|
683 |
$tnef = new tnef_decoder; |
A |
684 |
$tnef_arr = $tnef->decompress($part->body); |
d311d8
|
685 |
|
A |
686 |
foreach ($tnef_arr as $pid => $winatt) { |
|
687 |
$tpart = new rcube_message_part; |
|
688 |
|
|
689 |
$tpart->filename = trim($winatt['name']); |
|
690 |
$tpart->encoding = 'stream'; |
f19d86
|
691 |
$tpart->ctype_primary = trim(strtolower($winatt['type'])); |
A |
692 |
$tpart->ctype_secondary = trim(strtolower($winatt['subtype'])); |
d311d8
|
693 |
$tpart->mimetype = $tpart->ctype_primary . '/' . $tpart->ctype_secondary; |
A |
694 |
$tpart->mime_id = 'winmail.' . $part->mime_id . '.' . $pid; |
|
695 |
$tpart->size = $winatt['size']; |
|
696 |
$tpart->body = $winatt['stream']; |
|
697 |
|
|
698 |
$parts[] = $tpart; |
|
699 |
unset($tnef_arr[$pid]); |
|
700 |
} |
f19d86
|
701 |
|
d311d8
|
702 |
return $parts; |
A |
703 |
} |
|
704 |
|
|
705 |
|
|
706 |
/** |
|
707 |
* Parse message body for UUencoded attachments bodies |
|
708 |
* |
5c461b
|
709 |
* @param rcube_message_part $part Message part to decode |
A |
710 |
* @return array |
d311d8
|
711 |
*/ |
A |
712 |
function uu_decode(&$part) |
|
713 |
{ |
|
714 |
// @TODO: messages may be huge, hadle body via file |
10562d
|
715 |
if (!isset($part->body)) { |
AM |
716 |
$this->storage->set_folder($this->folder); |
c321a9
|
717 |
$part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part); |
10562d
|
718 |
} |
d311d8
|
719 |
|
A |
720 |
$parts = array(); |
|
721 |
// FIXME: line length is max.65? |
14f22f
|
722 |
$uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n/s'; |
d311d8
|
723 |
|
A |
724 |
if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) { |
|
725 |
// update message content-type |
|
726 |
$part->ctype_primary = 'multipart'; |
|
727 |
$part->ctype_secondary = 'mixed'; |
|
728 |
$part->mimetype = $part->ctype_primary . '/' . $part->ctype_secondary; |
14f22f
|
729 |
$uu_endstring = "`\nend\n"; |
d311d8
|
730 |
|
A |
731 |
// add attachments to the structure |
|
732 |
foreach ($matches as $pid => $att) { |
14f22f
|
733 |
$startpos = strpos($part->body, $att[1]) + strlen($att[1]) + 1; // "\n" |
GB |
734 |
$endpos = strpos($part->body, $uu_endstring); |
|
735 |
$filebody = substr($part->body, $startpos, $endpos-$startpos); |
|
736 |
|
|
737 |
// remove attachments bodies from the message body |
|
738 |
$part->body = substr_replace($part->body, "", $startpos, $endpos+strlen($uu_endstring)-$startpos); |
|
739 |
|
d311d8
|
740 |
$uupart = new rcube_message_part; |
A |
741 |
|
|
742 |
$uupart->filename = trim($att[1]); |
|
743 |
$uupart->encoding = 'stream'; |
14f22f
|
744 |
$uupart->body = convert_uudecode($filebody); |
d311d8
|
745 |
$uupart->size = strlen($uupart->body); |
A |
746 |
$uupart->mime_id = 'uu.' . $part->mime_id . '.' . $pid; |
|
747 |
|
0c2596
|
748 |
$ctype = rcube_mime::content_type($uupart->body, $uupart->filename, 'application/octet-stream', true); |
d311d8
|
749 |
$uupart->mimetype = $ctype; |
A |
750 |
list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype); |
|
751 |
|
|
752 |
$parts[] = $uupart; |
|
753 |
unset($matches[$pid]); |
|
754 |
} |
14f22f
|
755 |
|
GB |
756 |
// remove attachments bodies from the message body |
|
757 |
$part->body = preg_replace($uu_regexp, '', $part->body); |
d311d8
|
758 |
} |
f19d86
|
759 |
|
d311d8
|
760 |
return $parts; |
A |
761 |
} |
b91f04
|
762 |
|
T |
763 |
|
|
764 |
/** |
|
765 |
* Deprecated methods (to be removed) |
|
766 |
*/ |
|
767 |
|
|
768 |
public static function unfold_flowed($text) |
|
769 |
{ |
|
770 |
return rcube_mime::unfold_flowed($text); |
|
771 |
} |
|
772 |
|
|
773 |
public static function format_flowed($text, $length = 72) |
|
774 |
{ |
|
775 |
return rcube_mime::format_flowed($text, $length); |
|
776 |
} |
|
777 |
|
8fa58e
|
778 |
} |