commit | author | age
|
8fa58e
|
1 |
<?php |
T |
2 |
|
a95874
|
3 |
/** |
8fa58e
|
4 |
+-----------------------------------------------------------------------+ |
e019f2
|
5 |
| This file is part of the Roundcube Webmail client | |
48ba44
|
6 |
| Copyright (C) 2008-2014, 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; |
9af8e2
|
56 |
public $sender; |
323fa2
|
57 |
public $context; |
9af8e2
|
58 |
public $parts = array(); |
AM |
59 |
public $mime_parts = array(); |
287eff
|
60 |
public $inline_parts = array(); |
9af8e2
|
61 |
public $attachments = array(); |
AM |
62 |
public $subject = ''; |
|
63 |
public $is_safe = false; |
48ba44
|
64 |
|
AM |
65 |
const BODY_MAX_SIZE = 1048576; // 1MB |
2ae58f
|
66 |
|
193fb4
|
67 |
|
d311d8
|
68 |
/** |
A |
69 |
* __construct |
|
70 |
* |
|
71 |
* Provide a uid, and parse message structure. |
|
72 |
* |
63e793
|
73 |
* @param string $uid The message UID. |
AM |
74 |
* @param string $folder Folder name |
|
75 |
* @param bool $is_safe Security flag |
d311d8
|
76 |
* |
8b92d2
|
77 |
* @see self::$app, self::$storage, self::$opt, self::$parts |
d311d8
|
78 |
*/ |
63e793
|
79 |
function __construct($uid, $folder = null, $is_safe = false) |
8fa58e
|
80 |
{ |
e8cb51
|
81 |
// decode combined UID-folder identifier |
323fa2
|
82 |
if (preg_match('/^[0-9.]+-.+/', $uid)) { |
1d1fdc
|
83 |
list($uid, $folder) = explode('-', $uid, 2); |
e8cb51
|
84 |
} |
TB |
85 |
|
323fa2
|
86 |
if (preg_match('/^([0-9]+)\.([0-9.]+)$/', $uid, $matches)) { |
AM |
87 |
$uid = $matches[1]; |
|
88 |
$context = $matches[2]; |
|
89 |
} |
|
90 |
|
63e793
|
91 |
$this->uid = $uid; |
323fa2
|
92 |
$this->context = $context; |
63e793
|
93 |
$this->app = rcube::get_instance(); |
c321a9
|
94 |
$this->storage = $this->app->get_storage(); |
10562d
|
95 |
$this->folder = strlen($folder) ? $folder : $this->storage->get_folder(); |
AM |
96 |
|
|
97 |
// Set current folder |
|
98 |
$this->storage->set_folder($this->folder); |
63e793
|
99 |
$this->storage->set_options(array('all_headers' => true)); |
64e3e8
|
100 |
|
c321a9
|
101 |
$this->headers = $this->storage->get_message($uid); |
64e3e8
|
102 |
|
4fdaa0
|
103 |
if (!$this->headers) { |
64e3e8
|
104 |
return; |
4fdaa0
|
105 |
} |
64e3e8
|
106 |
|
63e793
|
107 |
$this->set_safe($is_safe || $_SESSION['safe_messages'][$this->folder.':'.$uid]); |
d311d8
|
108 |
$this->opt = array( |
59d11d
|
109 |
'safe' => $this->is_safe, |
d311d8
|
110 |
'prefer_html' => $this->app->config->get('prefer_html'), |
59d11d
|
111 |
'get_url' => $this->app->url(array( |
AM |
112 |
'action' => 'get', |
|
113 |
'mbox' => $this->folder, |
4a4088
|
114 |
'uid' => $uid), |
TB |
115 |
false, false, true) |
d311d8
|
116 |
); |
8fa58e
|
117 |
|
80152b
|
118 |
if (!empty($this->headers->structure)) { |
A |
119 |
$this->get_mime_numbers($this->headers->structure); |
|
120 |
$this->parse_structure($this->headers->structure); |
aa16b4
|
121 |
} |
323fa2
|
122 |
else if ($this->context === null) { |
c321a9
|
123 |
$this->body = $this->storage->get_body($uid); |
d311d8
|
124 |
} |
323fa2
|
125 |
|
AM |
126 |
$this->mime = new rcube_mime($this->headers->charset); |
|
127 |
$this->subject = $this->headers->get('subject'); |
|
128 |
list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1)); |
d311d8
|
129 |
|
A |
130 |
// notify plugins and let them analyze this structured message object |
|
131 |
$this->app->plugins->exec_hook('message_load', array('object' => $this)); |
|
132 |
} |
2ae58f
|
133 |
|
d311d8
|
134 |
/** |
A |
135 |
* Return a (decoded) message header |
|
136 |
* |
5c461b
|
137 |
* @param string $name Header name |
A |
138 |
* @param bool $row Don't mime-decode the value |
d311d8
|
139 |
* @return string Header value |
A |
140 |
*/ |
|
141 |
public function get_header($name, $raw = false) |
|
142 |
{ |
4fdaa0
|
143 |
if (empty($this->headers)) { |
1c4f23
|
144 |
return null; |
4fdaa0
|
145 |
} |
1c4f23
|
146 |
|
4fdaa0
|
147 |
return $this->headers->get($name, !$raw); |
d311d8
|
148 |
} |
A |
149 |
|
|
150 |
/** |
|
151 |
* Set is_safe var and session data |
|
152 |
* |
5c461b
|
153 |
* @param bool $safe enable/disable |
d311d8
|
154 |
*/ |
A |
155 |
public function set_safe($safe = true) |
|
156 |
{ |
f11142
|
157 |
$_SESSION['safe_messages'][$this->folder.':'.$this->uid] = $this->is_safe = $safe; |
d311d8
|
158 |
} |
A |
159 |
|
|
160 |
/** |
|
161 |
* Compose a valid URL for getting a message part |
|
162 |
* |
5c461b
|
163 |
* @param string $mime_id Part MIME-ID |
a021d6
|
164 |
* @param mixed $embed Mimetype class for parts to be embedded |
d311d8
|
165 |
* @return string URL or false if part does not exist |
A |
166 |
*/ |
57486f
|
167 |
public function get_part_url($mime_id, $embed = false) |
d311d8
|
168 |
{ |
A |
169 |
if ($this->mime_parts[$mime_id]) |
a021d6
|
170 |
return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1&_mimeclass=' . $embed : ''); |
aa16b4
|
171 |
else |
d311d8
|
172 |
return false; |
8fa58e
|
173 |
} |
d311d8
|
174 |
|
A |
175 |
/** |
|
176 |
* Get content of a specific part of this message |
|
177 |
* |
71950d
|
178 |
* @param string $mime_id Part MIME-ID |
e8b6e7
|
179 |
* @param resource $fp File pointer to save the message part |
71950d
|
180 |
* @param boolean $skip_charset_conv Disables charset conversion |
dff2c7
|
181 |
* @param int $max_bytes Only read this number of bytes |
ae8533
|
182 |
* @param boolean $formatted Enables formatting of text/* parts bodies |
71950d
|
183 |
* |
d311d8
|
184 |
* @return string Part content |
48ba44
|
185 |
* @deprecated |
d311d8
|
186 |
*/ |
ae8533
|
187 |
public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false, $max_bytes = 0, $formatted = true) |
d311d8
|
188 |
{ |
A |
189 |
if ($part = $this->mime_parts[$mime_id]) { |
|
190 |
// stored in message structure (winmail/inline-uuencode) |
8b92d2
|
191 |
if (!empty($part->body) || $part->encoding == 'stream') { |
d311d8
|
192 |
if ($fp) { |
A |
193 |
fwrite($fp, $part->body); |
|
194 |
} |
|
195 |
return $fp ? true : $part->body; |
|
196 |
} |
10562d
|
197 |
|
d311d8
|
198 |
// get from IMAP |
10562d
|
199 |
$this->storage->set_folder($this->folder); |
AM |
200 |
|
ae8533
|
201 |
return $this->storage->get_message_part($this->uid, $mime_id, $part, |
AM |
202 |
NULL, $fp, $skip_charset_conv, $max_bytes, $formatted); |
10562d
|
203 |
} |
48ba44
|
204 |
} |
AM |
205 |
|
|
206 |
/** |
|
207 |
* Get content of a specific part of this message |
|
208 |
* |
|
209 |
* @param string $mime_id Part ID |
|
210 |
* @param boolean $formatted Enables formatting of text/* parts bodies |
|
211 |
* @param int $max_bytes Only return/read this number of bytes |
|
212 |
* @param mixed $mode NULL to return a string, -1 to print body |
|
213 |
* or file pointer to save the body into |
|
214 |
* |
|
215 |
* @return string|bool Part content or operation status |
|
216 |
*/ |
|
217 |
public function get_part_body($mime_id, $formatted = false, $max_bytes = 0, $mode = null) |
|
218 |
{ |
|
219 |
if (!($part = $this->mime_parts[$mime_id])) { |
|
220 |
return; |
|
221 |
} |
9af8e2
|
222 |
|
AM |
223 |
// allow plugins to modify part body |
|
224 |
$plugin = $this->app->plugins->exec_hook('message_part_body', |
|
225 |
array('object' => $this, 'part' => $part)); |
48ba44
|
226 |
|
AM |
227 |
// only text parts can be formatted |
|
228 |
$formatted = $formatted && $part->ctype_primary == 'text'; |
|
229 |
|
|
230 |
// part body not fetched yet... save in memory if it's small enough |
|
231 |
if ($part->body === null && is_numeric($mime_id) && $part->size < self::BODY_MAX_SIZE) { |
d93019
|
232 |
$this->storage->set_folder($this->folder); |
48ba44
|
233 |
// Warning: body here should be always unformatted |
AM |
234 |
$part->body = $this->storage->get_message_part($this->uid, $mime_id, $part, |
|
235 |
null, null, true, 0, false); |
|
236 |
} |
|
237 |
|
|
238 |
// body stored in message structure (winmail/inline-uuencode) |
|
239 |
if ($part->body !== null || $part->encoding == 'stream') { |
|
240 |
$body = $part->body; |
|
241 |
|
|
242 |
if ($formatted && $body) { |
|
243 |
$body = self::format_part_body($body, $part, $this->headers->charset); |
|
244 |
} |
|
245 |
|
|
246 |
if ($max_bytes && strlen($body) > $max_bytes) { |
|
247 |
$body = substr($body, 0, $max_bytes); |
|
248 |
} |
|
249 |
|
|
250 |
if (is_resource($mode)) { |
|
251 |
if ($body !== false) { |
|
252 |
fwrite($mode, $body); |
|
253 |
rewind($mode); |
|
254 |
} |
|
255 |
|
|
256 |
return $body !== false; |
|
257 |
} |
|
258 |
|
|
259 |
if ($mode === -1) { |
|
260 |
if ($body !== false) { |
|
261 |
print($body); |
|
262 |
} |
|
263 |
|
|
264 |
return $body !== false; |
|
265 |
} |
|
266 |
|
|
267 |
return $body; |
|
268 |
} |
|
269 |
|
|
270 |
// get the body from IMAP |
|
271 |
$this->storage->set_folder($this->folder); |
|
272 |
|
|
273 |
$body = $this->storage->get_message_part($this->uid, $mime_id, $part, |
68c41f
|
274 |
$mode === -1, is_resource($mode) ? $mode : null, |
AM |
275 |
!($mode && $formatted), $max_bytes, $mode && $formatted); |
48ba44
|
276 |
|
AM |
277 |
if (is_resource($mode)) { |
|
278 |
rewind($mode); |
|
279 |
return $body !== false; |
|
280 |
} |
|
281 |
|
68c41f
|
282 |
if (!$mode && $body && $formatted) { |
AM |
283 |
$body = self::format_part_body($body, $part, $this->headers->charset); |
|
284 |
} |
|
285 |
|
48ba44
|
286 |
return $body; |
AM |
287 |
} |
|
288 |
|
|
289 |
/** |
|
290 |
* Format text message part for display |
|
291 |
* |
|
292 |
* @param string $body Part body |
|
293 |
* @param rcube_message_part $part Part object |
|
294 |
* @param string $default_charset Fallback charset if part charset is not specified |
|
295 |
* |
|
296 |
* @return string Formatted body |
|
297 |
*/ |
|
298 |
public static function format_part_body($body, $part, $default_charset = null) |
|
299 |
{ |
|
300 |
// remove useless characters |
|
301 |
$body = preg_replace('/[\t\r\0\x0B]+\n/', "\n", $body); |
|
302 |
|
|
303 |
// remove NULL characters if any (#1486189) |
|
304 |
if (strpos($body, "\x00") !== false) { |
|
305 |
$body = str_replace("\x00", '', $body); |
|
306 |
} |
|
307 |
|
|
308 |
// detect charset... |
|
309 |
if (!$part->charset || strtoupper($part->charset) == 'US-ASCII') { |
|
310 |
// try to extract charset information from HTML meta tag (#1488125) |
|
311 |
if ($part->ctype_secondary == 'html' && preg_match('/<meta[^>]+charset=([a-z0-9-_]+)/i', $body, $m)) { |
|
312 |
$part->charset = strtoupper($m[1]); |
|
313 |
} |
|
314 |
else if ($default_charset) { |
|
315 |
$part->charset = $default_charset; |
|
316 |
} |
|
317 |
else { |
|
318 |
$rcube = rcube::get_instance(); |
|
319 |
$part->charset = $rcube->config->get('default_charset', RCUBE_CHARSET); |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
// ..convert charset encoding |
|
324 |
$body = rcube_charset::convert($body, $part->charset); |
|
325 |
|
|
326 |
return $body; |
8fa58e
|
327 |
} |
T |
328 |
|
d311d8
|
329 |
/** |
5c26bd
|
330 |
* Determine if the message contains a HTML part. This must to be |
AM |
331 |
* a real part not an attachment (or its part) |
d311d8
|
332 |
* |
b92a66
|
333 |
* @param bool $enriched Enables checking for text/enriched parts too |
AM |
334 |
* @param rcube_message_part &$part Reference to the part if found |
33423a
|
335 |
* |
d311d8
|
336 |
* @return bool True if a HTML is available, False if not |
A |
337 |
*/ |
b92a66
|
338 |
public function has_html_part($enriched = false, &$part = null) |
d311d8
|
339 |
{ |
A |
340 |
// check all message parts |
574928
|
341 |
foreach ($this->mime_parts as $part) { |
52d0d9
|
342 |
if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) { |
0ef894
|
343 |
// Skip if part is an attachment, don't use is_attachment() here |
AM |
344 |
if ($part->filename) { |
ce3105
|
345 |
continue; |
AM |
346 |
} |
|
347 |
|
|
348 |
if (!$part->size) { |
|
349 |
continue; |
|
350 |
} |
|
351 |
|
|
352 |
if (!$this->check_context($part)) { |
5c26bd
|
353 |
continue; |
AM |
354 |
} |
33423a
|
355 |
|
5c26bd
|
356 |
$level = explode('.', $part->mime_id); |
f8101f
|
357 |
$depth = count($level); |
4c0cb9
|
358 |
$last = ''; |
5c26bd
|
359 |
|
5a2d2a
|
360 |
// Check if the part belongs to higher-level's multipart part |
f8101f
|
361 |
// this can be alternative/related/signed/encrypted or mixed |
5c26bd
|
362 |
while (array_pop($level) !== null) { |
f8101f
|
363 |
$parent_depth = count($level); |
TB |
364 |
if (!$parent_depth) { |
5c26bd
|
365 |
return true; |
33423a
|
366 |
} |
A |
367 |
|
ce3105
|
368 |
$parent = $this->mime_parts[join('.', $level)]; |
4c0cb9
|
369 |
|
ce3105
|
370 |
if (!$this->check_context($parent)) { |
AM |
371 |
return true; |
|
372 |
} |
|
373 |
|
|
374 |
$max_delta = $depth - (1 + ($last == 'multipart/alternative' ? 1 : 0)); |
|
375 |
$last = $parent->real_mimetype ?: $parent->mimetype; |
|
376 |
|
|
377 |
if (!preg_match('/^multipart\/(alternative|related|signed|encrypted|mixed)$/', $last) |
|
378 |
|| ($last == 'multipart/mixed' && $parent_depth < $max_delta)) { |
5c26bd
|
379 |
continue 2; |
33423a
|
380 |
} |
A |
381 |
} |
|
382 |
|
ce3105
|
383 |
return true; |
5c26bd
|
384 |
} |
AM |
385 |
} |
|
386 |
|
b92a66
|
387 |
$part = null; |
AM |
388 |
|
5c26bd
|
389 |
return false; |
AM |
390 |
} |
|
391 |
|
|
392 |
/** |
|
393 |
* Determine if the message contains a text/plain part. This must to be |
|
394 |
* a real part not an attachment (or its part) |
|
395 |
* |
b92a66
|
396 |
* @param rcube_message_part &$part Reference to the part if found |
AM |
397 |
* |
5c26bd
|
398 |
* @return bool True if a plain text part is available, False if not |
AM |
399 |
*/ |
b92a66
|
400 |
public function has_text_part(&$part = null) |
5c26bd
|
401 |
{ |
AM |
402 |
// check all message parts |
574928
|
403 |
foreach ($this->mime_parts as $part) { |
5c26bd
|
404 |
if ($part->mimetype == 'text/plain') { |
0ef894
|
405 |
// Skip if part is an attachment, don't use is_attachment() here |
AM |
406 |
if ($part->filename) { |
323fa2
|
407 |
continue; |
AM |
408 |
} |
|
409 |
|
ce3105
|
410 |
if (!$part->size) { |
AM |
411 |
continue; |
|
412 |
} |
|
413 |
|
323fa2
|
414 |
if (!$this->check_context($part)) { |
5c26bd
|
415 |
continue; |
AM |
416 |
} |
|
417 |
|
|
418 |
$level = explode('.', $part->mime_id); |
|
419 |
|
|
420 |
// Check if the part belongs to higher-level's alternative/related |
|
421 |
while (array_pop($level) !== null) { |
|
422 |
if (!count($level)) { |
|
423 |
return true; |
|
424 |
} |
|
425 |
|
|
426 |
$parent = $this->mime_parts[join('.', $level)]; |
ce3105
|
427 |
|
AM |
428 |
if (!$this->check_context($parent)) { |
|
429 |
return true; |
|
430 |
} |
|
431 |
|
5c26bd
|
432 |
if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') { |
AM |
433 |
continue 2; |
|
434 |
} |
|
435 |
} |
|
436 |
|
ce3105
|
437 |
return true; |
33423a
|
438 |
} |
d311d8
|
439 |
} |
A |
440 |
|
b92a66
|
441 |
$part = null; |
AM |
442 |
|
d311d8
|
443 |
return false; |
A |
444 |
} |
|
445 |
|
|
446 |
/** |
|
447 |
* Return the first HTML part of this message |
|
448 |
* |
b92a66
|
449 |
* @param rcube_message_part &$part Reference to the part if found |
AM |
450 |
* @param bool $enriched Enables checking for text/enriched parts too |
|
451 |
* |
d311d8
|
452 |
* @return string HTML message part content |
A |
453 |
*/ |
b92a66
|
454 |
public function first_html_part(&$part = null, $enriched = false) |
d311d8
|
455 |
{ |
b92a66
|
456 |
if ($this->has_html_part($enriched, $part)) { |
AM |
457 |
$body = $this->get_part_body($part->mime_id, true); |
|
458 |
|
|
459 |
if ($part->mimetype == 'text/enriched') { |
|
460 |
$body = rcube_enriched::to_html($body); |
d311d8
|
461 |
} |
b92a66
|
462 |
|
AM |
463 |
return $body; |
d311d8
|
464 |
} |
A |
465 |
} |
|
466 |
|
|
467 |
/** |
b92a66
|
468 |
* Return the first text part of this message. |
AM |
469 |
* If there's no text/plain part but $strict=true and text/html part |
|
470 |
* exists, it will be returned in text/plain format. |
d311d8
|
471 |
* |
b92a66
|
472 |
* @param rcube_message_part &$part Reference to the part if found |
AM |
473 |
* @param bool $strict Check only text/plain parts |
|
474 |
* |
d311d8
|
475 |
* @return string Plain text message/part content |
A |
476 |
*/ |
b92a66
|
477 |
public function first_text_part(&$part = null, $strict = false) |
d311d8
|
478 |
{ |
A |
479 |
// no message structure, return complete body |
b92a66
|
480 |
if (empty($this->parts)) { |
d311d8
|
481 |
return $this->body; |
A |
482 |
} |
|
483 |
|
b92a66
|
484 |
if ($this->has_text_part($part)) { |
AM |
485 |
return $this->get_part_body($part->mime_id, true); |
|
486 |
} |
|
487 |
|
|
488 |
if (!$strict && ($body = $this->first_html_part($part, true))) { |
|
489 |
// create instance of html2text class |
|
490 |
$h2t = new rcube_html2text($body); |
|
491 |
return $h2t->get_text(); |
|
492 |
} |
d311d8
|
493 |
} |
A |
494 |
|
|
495 |
/** |
ce3105
|
496 |
* Return message parts in current context |
AM |
497 |
*/ |
|
498 |
public function mime_parts() |
|
499 |
{ |
|
500 |
if ($this->context === null) { |
|
501 |
return $this->mime_parts; |
|
502 |
} |
|
503 |
|
|
504 |
$parts = array(); |
|
505 |
|
|
506 |
foreach ($this->mime_parts as $part_id => $part) { |
|
507 |
if ($this->check_context($part)) { |
|
508 |
$parts[$part_id] = $part; |
|
509 |
} |
|
510 |
} |
|
511 |
|
|
512 |
return $parts; |
|
513 |
} |
|
514 |
|
|
515 |
/** |
3efc74
|
516 |
* Checks if part of the message is an attachment (or part of it) |
AM |
517 |
* |
|
518 |
* @param rcube_message_part $part Message part |
|
519 |
* |
|
520 |
* @return bool True if the part is an attachment part |
|
521 |
*/ |
|
522 |
public function is_attachment($part) |
|
523 |
{ |
|
524 |
foreach ($this->attachments as $att_part) { |
|
525 |
if ($att_part->mime_id == $part->mime_id) { |
|
526 |
return true; |
|
527 |
} |
|
528 |
|
|
529 |
// check if the part is a subpart of another attachment part (message/rfc822) |
|
530 |
if ($att_part->mimetype == 'message/rfc822') { |
|
531 |
if (in_array($part, (array)$att_part->parts)) { |
|
532 |
return true; |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
|
|
537 |
return false; |
|
538 |
} |
|
539 |
|
|
540 |
/** |
f7f75f
|
541 |
* In a multipart/encrypted encrypted message, |
TB |
542 |
* find the encrypted message payload part. |
|
543 |
* |
|
544 |
* @return rcube_message_part |
|
545 |
*/ |
|
546 |
public function get_multipart_encrypted_part() |
|
547 |
{ |
|
548 |
foreach ($this->mime_parts as $mime_id => $mpart) { |
|
549 |
if ($mpart->mimetype == 'multipart/encrypted') { |
|
550 |
$this->pgp_mime = true; |
|
551 |
} |
|
552 |
if ($this->pgp_mime && ($mpart->mimetype == 'application/octet-stream' || |
|
553 |
(!empty($mpart->filename) && $mpart->filename != 'version.txt'))) { |
|
554 |
$this->encrypted_part = $mime_id; |
|
555 |
return $mpart; |
|
556 |
} |
|
557 |
} |
|
558 |
|
|
559 |
return false; |
|
560 |
} |
|
561 |
|
|
562 |
/** |
8b92d2
|
563 |
* Read the message structure returend by the IMAP server |
d311d8
|
564 |
* and build flat lists of content parts and attachments |
A |
565 |
* |
5c461b
|
566 |
* @param rcube_message_part $structure Message structure node |
A |
567 |
* @param bool $recursive True when called recursively |
d311d8
|
568 |
*/ |
A |
569 |
private function parse_structure($structure, $recursive = false) |
|
570 |
{ |
|
571 |
// real content-type of message/rfc822 part |
a8a72e
|
572 |
if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype) { |
5ced9c
|
573 |
$mimetype = $structure->real_mimetype; |
a8a72e
|
574 |
|
TB |
575 |
// parse headers from message/rfc822 part |
7ae7cd
|
576 |
if (!isset($structure->headers['subject']) && !isset($structure->headers['from'])) { |
48ba44
|
577 |
list($headers, ) = explode("\r\n\r\n", $this->get_part_body($structure->mime_id, false, 32768)); |
a8a72e
|
578 |
$structure->headers = rcube_mime::parse_headers($headers); |
323fa2
|
579 |
|
AM |
580 |
if ($this->context == $structure->mime_id) { |
|
581 |
$this->headers = rcube_message_header::from_array($structure->headers); |
|
582 |
} |
a8a72e
|
583 |
} |
TB |
584 |
} |
9af8e2
|
585 |
else { |
5ced9c
|
586 |
$mimetype = $structure->mimetype; |
9af8e2
|
587 |
} |
d311d8
|
588 |
|
A |
589 |
// show message headers |
ddfdd8
|
590 |
if ($recursive && is_array($structure->headers) && |
323fa2
|
591 |
(isset($structure->headers['subject']) || $structure->headers['from'] || $structure->headers['to']) |
AM |
592 |
) { |
d311d8
|
593 |
$c = new stdClass; |
A |
594 |
$c->type = 'headers'; |
c5d7c9
|
595 |
$c->headers = $structure->headers; |
323fa2
|
596 |
$this->add_part($c); |
d311d8
|
597 |
} |
5ced9c
|
598 |
|
A |
599 |
// Allow plugins to handle message parts |
|
600 |
$plugin = $this->app->plugins->exec_hook('message_part_structure', |
|
601 |
array('object' => $this, 'structure' => $structure, |
|
602 |
'mimetype' => $mimetype, 'recursive' => $recursive)); |
|
603 |
|
9af8e2
|
604 |
if ($plugin['abort']) { |
5ced9c
|
605 |
return; |
9af8e2
|
606 |
} |
5ced9c
|
607 |
|
A |
608 |
$structure = $plugin['structure']; |
9af8e2
|
609 |
$mimetype = $plugin['mimetype']; |
AM |
610 |
$recursive = $plugin['recursive']; |
|
611 |
|
|
612 |
list($message_ctype_primary, $message_ctype_secondary) = explode('/', $mimetype); |
d311d8
|
613 |
|
A |
614 |
// print body if message doesn't have multiple parts |
|
615 |
if ($message_ctype_primary == 'text' && !$recursive) { |
c23dc8
|
616 |
// parts with unsupported type add to attachments list |
AM |
617 |
if (!in_array($message_ctype_secondary, array('plain', 'html', 'enriched'))) { |
323fa2
|
618 |
$this->add_part($structure, 'attachment'); |
c23dc8
|
619 |
return; |
AM |
620 |
} |
|
621 |
|
d311d8
|
622 |
$structure->type = 'content'; |
323fa2
|
623 |
$this->add_part($structure); |
8757f5
|
624 |
|
d311d8
|
625 |
// Parse simple (plain text) message body |
c23dc8
|
626 |
if ($message_ctype_secondary == 'plain') { |
d311d8
|
627 |
foreach ((array)$this->uu_decode($structure) as $uupart) { |
A |
628 |
$this->mime_parts[$uupart->mime_id] = $uupart; |
323fa2
|
629 |
$this->add_part($uupart, 'attachment'); |
d311d8
|
630 |
} |
c23dc8
|
631 |
} |
d311d8
|
632 |
} |
A |
633 |
// the same for pgp signed messages |
|
634 |
else if ($mimetype == 'application/pgp' && !$recursive) { |
|
635 |
$structure->type = 'content'; |
323fa2
|
636 |
$this->add_part($structure); |
d311d8
|
637 |
} |
e730cd
|
638 |
// message contains (more than one!) alternative parts |
A |
639 |
else if ($mimetype == 'multipart/alternative' |
|
640 |
&& is_array($structure->parts) && count($structure->parts) > 1 |
|
641 |
) { |
c5d7c9
|
642 |
// get html/plaintext parts, other add to attachments list |
d311d8
|
643 |
foreach ($structure->parts as $p => $sub_part) { |
A |
644 |
$sub_mimetype = $sub_part->mimetype; |
cb0f03
|
645 |
$is_multipart = preg_match('/^multipart\/(related|relative|mixed|alternative)/', $sub_mimetype); |
8757f5
|
646 |
|
5fbfde
|
647 |
// skip empty text parts |
c5d7c9
|
648 |
if (!$sub_part->size && !$is_multipart) { |
5f4095
|
649 |
continue; |
5fbfde
|
650 |
} |
5f4095
|
651 |
|
170702
|
652 |
// We've encountered (malformed) messages with more than |
AM |
653 |
// one text/plain or text/html part here. There's no way to choose |
|
654 |
// which one is better, so we'll display first of them and add |
|
655 |
// others as attachments (#1489358) |
|
656 |
|
d311d8
|
657 |
// check if sub part is |
c5d7c9
|
658 |
if ($is_multipart) |
AM |
659 |
$related_part = $p; |
170702
|
660 |
else if ($sub_mimetype == 'text/plain' && !$plain_part) |
d311d8
|
661 |
$plain_part = $p; |
5b737d
|
662 |
else if ($sub_mimetype == 'text/html' && !$html_part) { |
d311d8
|
663 |
$html_part = $p; |
5b737d
|
664 |
$this->got_html_part = true; |
AM |
665 |
} |
170702
|
666 |
else if ($sub_mimetype == 'text/enriched' && !$enriched_part) |
d311d8
|
667 |
$enriched_part = $p; |
170702
|
668 |
else { |
AM |
669 |
// add unsupported/unrecognized parts to attachments list |
323fa2
|
670 |
$this->add_part($sub_part, 'attachment'); |
170702
|
671 |
} |
d311d8
|
672 |
} |
A |
673 |
|
|
674 |
// parse related part (alternative part could be in here) |
|
675 |
if ($related_part !== null && !$this->parse_alternative) { |
|
676 |
$this->parse_alternative = true; |
|
677 |
$this->parse_structure($structure->parts[$related_part], true); |
|
678 |
$this->parse_alternative = false; |
8757f5
|
679 |
|
d311d8
|
680 |
// if plain part was found, we should unset it if html is preferred |
A |
681 |
if ($this->opt['prefer_html'] && count($this->parts)) |
|
682 |
$plain_part = null; |
|
683 |
} |
|
684 |
|
|
685 |
// choose html/plain part to print |
|
686 |
if ($html_part !== null && $this->opt['prefer_html']) { |
c5d7c9
|
687 |
$print_part = $structure->parts[$html_part]; |
d311d8
|
688 |
} |
A |
689 |
else if ($enriched_part !== null) { |
c5d7c9
|
690 |
$print_part = $structure->parts[$enriched_part]; |
d311d8
|
691 |
} |
A |
692 |
else if ($plain_part !== null) { |
c5d7c9
|
693 |
$print_part = $structure->parts[$plain_part]; |
d311d8
|
694 |
} |
A |
695 |
|
|
696 |
// add the right message body |
|
697 |
if (is_object($print_part)) { |
|
698 |
$print_part->type = 'content'; |
323fa2
|
699 |
$this->add_part($print_part); |
d311d8
|
700 |
} |
A |
701 |
// show plaintext warning |
|
702 |
else if ($html_part !== null && empty($this->parts)) { |
|
703 |
$c = new stdClass; |
|
704 |
$c->type = 'content'; |
|
705 |
$c->ctype_primary = 'text'; |
|
706 |
$c->ctype_secondary = 'plain'; |
0c2596
|
707 |
$c->mimetype = 'text/plain'; |
A |
708 |
$c->realtype = 'text/html'; |
d311d8
|
709 |
|
323fa2
|
710 |
$this->add_part($c); |
d311d8
|
711 |
} |
A |
712 |
} |
|
713 |
// this is an ecrypted message -> create a plaintext body with the according message |
|
714 |
else if ($mimetype == 'multipart/encrypted') { |
|
715 |
$p = new stdClass; |
|
716 |
$p->type = 'content'; |
|
717 |
$p->ctype_primary = 'text'; |
|
718 |
$p->ctype_secondary = 'plain'; |
0c2596
|
719 |
$p->mimetype = 'text/plain'; |
A |
720 |
$p->realtype = 'multipart/encrypted'; |
ef2915
|
721 |
$p->mime_id = $structure->mime_id; |
c054ec
|
722 |
|
323fa2
|
723 |
$this->add_part($p); |
ef2915
|
724 |
|
TB |
725 |
// add encrypted payload part as attachment |
|
726 |
if (is_array($structure->parts)) { |
|
727 |
for ($i=0; $i < count($structure->parts); $i++) { |
|
728 |
$subpart = $structure->parts[$i]; |
|
729 |
if ($subpart->mimetype == 'application/octet-stream' || !empty($subpart->filename)) { |
323fa2
|
730 |
$this->add_part($subpart, 'attachment'); |
ef2915
|
731 |
} |
TB |
732 |
} |
|
733 |
} |
d311d8
|
734 |
} |
ee89c6
|
735 |
// this is an S/MIME ecrypted message -> create a plaintext body with the according message |
AM |
736 |
else if ($mimetype == 'application/pkcs7-mime') { |
|
737 |
$p = new stdClass; |
|
738 |
$p->type = 'content'; |
|
739 |
$p->ctype_primary = 'text'; |
|
740 |
$p->ctype_secondary = 'plain'; |
|
741 |
$p->mimetype = 'text/plain'; |
|
742 |
$p->realtype = 'application/pkcs7-mime'; |
ef2915
|
743 |
$p->mime_id = $structure->mime_id; |
ee89c6
|
744 |
|
323fa2
|
745 |
$this->add_part($p); |
ef2915
|
746 |
|
TB |
747 |
if (!empty($structure->filename)) { |
323fa2
|
748 |
$this->add_part($structure, 'attachment'); |
ef2915
|
749 |
} |
ee89c6
|
750 |
} |
d311d8
|
751 |
// message contains multiple parts |
A |
752 |
else if (is_array($structure->parts) && !empty($structure->parts)) { |
|
753 |
// iterate over parts |
|
754 |
for ($i=0; $i < count($structure->parts); $i++) { |
|
755 |
$mail_part = &$structure->parts[$i]; |
|
756 |
$primary_type = $mail_part->ctype_primary; |
|
757 |
$secondary_type = $mail_part->ctype_secondary; |
be3460
|
758 |
$part_mimetype = $mail_part->mimetype; |
8fa58e
|
759 |
|
be3460
|
760 |
// multipart/alternative or message/rfc822 |
AM |
761 |
if ($primary_type == 'multipart' || $part_mimetype == 'message/rfc822') { |
d311d8
|
762 |
$this->parse_structure($mail_part, true); |
A |
763 |
|
|
764 |
// list message/rfc822 as attachment as well (mostly .eml) |
be3460
|
765 |
if ($primary_type == 'message' && !empty($mail_part->filename)) { |
323fa2
|
766 |
$this->add_part($mail_part, 'attachment'); |
be3460
|
767 |
} |
d311d8
|
768 |
} |
f22ea7
|
769 |
// part text/[plain|html] or delivery status |
d311d8
|
770 |
else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') || |
f22ea7
|
771 |
in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification')) |
d311d8
|
772 |
) { |
1a2f83
|
773 |
// Allow plugins to handle also this part |
A |
774 |
$plugin = $this->app->plugins->exec_hook('message_part_structure', |
|
775 |
array('object' => $this, 'structure' => $mail_part, |
|
776 |
'mimetype' => $part_mimetype, 'recursive' => true)); |
|
777 |
|
be3460
|
778 |
if ($plugin['abort']) { |
1a2f83
|
779 |
continue; |
be3460
|
780 |
} |
1a2f83
|
781 |
|
9c299e
|
782 |
if ($part_mimetype == 'text/html' && $mail_part->size) { |
5b737d
|
783 |
$this->got_html_part = true; |
63f9de
|
784 |
} |
A |
785 |
|
1a2f83
|
786 |
$mail_part = $plugin['structure']; |
A |
787 |
list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']); |
|
788 |
|
d311d8
|
789 |
// add text part if it matches the prefs |
A |
790 |
if (!$this->parse_alternative || |
|
791 |
($secondary_type == 'html' && $this->opt['prefer_html']) || |
|
792 |
($secondary_type == 'plain' && !$this->opt['prefer_html']) |
|
793 |
) { |
|
794 |
$mail_part->type = 'content'; |
323fa2
|
795 |
$this->add_part($mail_part); |
d311d8
|
796 |
} |
fd371a
|
797 |
|
d311d8
|
798 |
// list as attachment as well |
f7c11e
|
799 |
if (!empty($mail_part->filename)) { |
323fa2
|
800 |
$this->add_part($mail_part, 'attachment'); |
f7c11e
|
801 |
} |
d311d8
|
802 |
} |
A |
803 |
// ignore "virtual" protocol parts |
|
804 |
else if ($primary_type == 'protocol') { |
|
805 |
continue; |
|
806 |
} |
|
807 |
// part is Microsoft Outlook TNEF (winmail.dat) |
|
808 |
else if ($part_mimetype == 'application/ms-tnef') { |
292292
|
809 |
$tnef_parts = (array) $this->tnef_decode($mail_part); |
AM |
810 |
foreach ($tnef_parts as $tpart) { |
d311d8
|
811 |
$this->mime_parts[$tpart->mime_id] = $tpart; |
323fa2
|
812 |
$this->add_part($tpart, 'attachment'); |
d311d8
|
813 |
} |
292292
|
814 |
|
AM |
815 |
// add winmail.dat to the list if it's content is unknown |
|
816 |
if (empty($tnef_parts) && !empty($mail_part->filename)) { |
|
817 |
$this->mime_parts[$mail_part->mime_id] = $mail_part; |
323fa2
|
818 |
$this->add_part($mail_part, 'attachment'); |
292292
|
819 |
} |
d311d8
|
820 |
} |
A |
821 |
// part is a file/attachment |
|
822 |
else if (preg_match('/^(inline|attach)/', $mail_part->disposition) || |
8794f1
|
823 |
$mail_part->headers['content-id'] || |
A |
824 |
($mail_part->filename && |
|
825 |
(empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition))) |
d311d8
|
826 |
) { |
A |
827 |
// skip apple resource forks |
|
828 |
if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile') |
|
829 |
continue; |
|
830 |
|
|
831 |
// part belongs to a related message and is linked |
cb0f03
|
832 |
if (preg_match('/^multipart\/(related|relative)/', $mimetype) |
be3460
|
833 |
&& ($mail_part->headers['content-id'] || $mail_part->headers['content-location']) |
AM |
834 |
) { |
d311d8
|
835 |
if ($mail_part->headers['content-id']) |
A |
836 |
$mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']); |
|
837 |
if ($mail_part->headers['content-location']) |
|
838 |
$mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location']; |
|
839 |
|
323fa2
|
840 |
$this->add_part($mail_part, 'inline'); |
d311d8
|
841 |
} |
b38925
|
842 |
// regular attachment with valid content type |
A |
843 |
// (content-type name regexp according to RFC4288.4.2) |
2ae58f
|
844 |
else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) { |
323fa2
|
845 |
$this->add_part($mail_part, 'attachment'); |
b38925
|
846 |
} |
A |
847 |
// attachment with invalid content type |
|
848 |
// replace malformed content type with application/octet-stream (#1487767) |
|
849 |
else if ($mail_part->filename) { |
|
850 |
$mail_part->ctype_primary = 'application'; |
|
851 |
$mail_part->ctype_secondary = 'octet-stream'; |
|
852 |
$mail_part->mimetype = 'application/octet-stream'; |
|
853 |
|
323fa2
|
854 |
$this->add_part($mail_part, 'attachment'); |
d311d8
|
855 |
} |
8757f5
|
856 |
} |
98e461
|
857 |
// calendar part not marked as attachment (#1490325) |
AM |
858 |
else if ($part_mimetype == 'text/calendar') { |
|
859 |
if (!$mail_part->filename) { |
|
860 |
$mail_part->filename = 'calendar.ics'; |
|
861 |
} |
|
862 |
|
323fa2
|
863 |
$this->add_part($mail_part, 'attachment'); |
98e461
|
864 |
} |
d311d8
|
865 |
} |
A |
866 |
|
|
867 |
// if this was a related part try to resolve references |
cb0f03
|
868 |
if (preg_match('/^multipart\/(related|relative)/', $mimetype) && sizeof($this->inline_parts)) { |
d311d8
|
869 |
$a_replaces = array(); |
89d19c
|
870 |
$img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/'; |
d311d8
|
871 |
|
A |
872 |
foreach ($this->inline_parts as $inline_object) { |
a021d6
|
873 |
$part_url = $this->get_part_url($inline_object->mime_id, $inline_object->ctype_primary); |
8cfba1
|
874 |
if (isset($inline_object->content_id)) |
d311d8
|
875 |
$a_replaces['cid:'.$inline_object->content_id] = $part_url; |
63f9de
|
876 |
if ($inline_object->content_location) { |
d311d8
|
877 |
$a_replaces[$inline_object->content_location] = $part_url; |
63f9de
|
878 |
} |
89d19c
|
879 |
|
A |
880 |
if (!empty($inline_object->filename)) { |
|
881 |
// MS Outlook sends sometimes non-related attachments as related |
|
882 |
// In this case multipart/related message has only one text part |
|
883 |
// We'll add all such attachments to the attachments list |
5b737d
|
884 |
if (!isset($this->got_html_part)) { |
323fa2
|
885 |
$this->add_part($inline_object, 'attachment'); |
89d19c
|
886 |
} |
A |
887 |
// MS Outlook sometimes also adds non-image attachments as related |
|
888 |
// We'll add all such attachments to the attachments list |
|
889 |
// Warning: some browsers support pdf in <img/> |
|
890 |
else if (!preg_match($img_regexp, $inline_object->mimetype)) { |
323fa2
|
891 |
$this->add_part($inline_object, 'attachment'); |
89d19c
|
892 |
} |
A |
893 |
// @TODO: we should fetch HTML body and find attachment's content-id |
|
894 |
// to handle also image attachments without reference in the body |
|
895 |
// @TODO: should we list all image attachments in text mode? |
02b6e6
|
896 |
} |
d311d8
|
897 |
} |
A |
898 |
|
|
899 |
// add replace array to each content part |
|
900 |
// (will be applied later when part body is available) |
|
901 |
foreach ($this->parts as $i => $part) { |
|
902 |
if ($part->type == 'content') |
|
903 |
$this->parts[$i]->replaces = $a_replaces; |
|
904 |
} |
|
905 |
} |
|
906 |
} |
|
907 |
// message is a single part non-text |
|
908 |
else if ($structure->filename) { |
323fa2
|
909 |
$this->add_part($structure, $attachment); |
d311d8
|
910 |
} |
3e58bf
|
911 |
// message is a single part non-text (without filename) |
A |
912 |
else if (preg_match('/application\//i', $mimetype)) { |
323fa2
|
913 |
$this->add_part($structure, 'attachment'); |
c5d7c9
|
914 |
} |
AM |
915 |
} |
|
916 |
|
|
917 |
/** |
323fa2
|
918 |
* Fill a flat array with references to all parts, indexed by part numbers |
d311d8
|
919 |
* |
5c461b
|
920 |
* @param rcube_message_part $part Message body structure |
d311d8
|
921 |
*/ |
A |
922 |
private function get_mime_numbers(&$part) |
|
923 |
{ |
|
924 |
if (strlen($part->mime_id)) |
|
925 |
$this->mime_parts[$part->mime_id] = &$part; |
f19d86
|
926 |
|
d311d8
|
927 |
if (is_array($part->parts)) |
A |
928 |
for ($i=0; $i<count($part->parts); $i++) |
|
929 |
$this->get_mime_numbers($part->parts[$i]); |
|
930 |
} |
|
931 |
|
|
932 |
/** |
323fa2
|
933 |
* Add a part to object parts array(s) (with context check) |
AM |
934 |
*/ |
|
935 |
private function add_part($part, $type = null) |
|
936 |
{ |
|
937 |
if ($this->check_context($part)) { |
|
938 |
switch ($type) { |
|
939 |
case 'inline': $this->inline_parts[] = $part; break; |
|
940 |
case 'attachment': $this->attachments[] = $part; break; |
|
941 |
default: $this->parts[] = $part; break; |
|
942 |
} |
|
943 |
} |
|
944 |
} |
|
945 |
|
|
946 |
/** |
|
947 |
* Check if specified part belongs to the current context |
|
948 |
*/ |
|
949 |
private function check_context($part) |
|
950 |
{ |
|
951 |
return $this->context === null || strpos($part->mime_id, $this->context . '.') === 0; |
|
952 |
} |
|
953 |
|
|
954 |
/** |
d311d8
|
955 |
* Decode a Microsoft Outlook TNEF part (winmail.dat) |
A |
956 |
* |
5c461b
|
957 |
* @param rcube_message_part $part Message part to decode |
A |
958 |
* @return array |
d311d8
|
959 |
*/ |
A |
960 |
function tnef_decode(&$part) |
|
961 |
{ |
48ba44
|
962 |
// @TODO: attachment may be huge, handle body via file |
AM |
963 |
$body = $this->get_part_body($part->mime_id); |
2883fc
|
964 |
$tnef = new rcube_tnef_decoder; |
48ba44
|
965 |
$tnef_arr = $tnef->decompress($body); |
AM |
966 |
$parts = array(); |
d311d8
|
967 |
|
48ba44
|
968 |
unset($body); |
d311d8
|
969 |
|
A |
970 |
foreach ($tnef_arr as $pid => $winatt) { |
|
971 |
$tpart = new rcube_message_part; |
|
972 |
|
2da830
|
973 |
$tpart->filename = $this->fix_attachment_name(trim($winatt['name']), $part); |
d311d8
|
974 |
$tpart->encoding = 'stream'; |
f19d86
|
975 |
$tpart->ctype_primary = trim(strtolower($winatt['type'])); |
A |
976 |
$tpart->ctype_secondary = trim(strtolower($winatt['subtype'])); |
d311d8
|
977 |
$tpart->mimetype = $tpart->ctype_primary . '/' . $tpart->ctype_secondary; |
A |
978 |
$tpart->mime_id = 'winmail.' . $part->mime_id . '.' . $pid; |
|
979 |
$tpart->size = $winatt['size']; |
|
980 |
$tpart->body = $winatt['stream']; |
|
981 |
|
|
982 |
$parts[] = $tpart; |
|
983 |
unset($tnef_arr[$pid]); |
|
984 |
} |
f19d86
|
985 |
|
d311d8
|
986 |
return $parts; |
A |
987 |
} |
|
988 |
|
|
989 |
/** |
|
990 |
* Parse message body for UUencoded attachments bodies |
|
991 |
* |
5c461b
|
992 |
* @param rcube_message_part $part Message part to decode |
A |
993 |
* @return array |
d311d8
|
994 |
*/ |
A |
995 |
function uu_decode(&$part) |
|
996 |
{ |
48ba44
|
997 |
// @TODO: messages may be huge, handle body via file |
AM |
998 |
$part->body = $this->get_part_body($part->mime_id); |
|
999 |
$parts = array(); |
|
1000 |
$pid = 0; |
d311d8
|
1001 |
|
A |
1002 |
// FIXME: line length is max.65? |
48ba44
|
1003 |
$uu_regexp_begin = '/begin [0-7]{3,4} ([^\r\n]+)\r?\n/s'; |
AM |
1004 |
$uu_regexp_end = '/`\r?\nend((\r?\n)|($))/s'; |
d311d8
|
1005 |
|
48ba44
|
1006 |
while (preg_match($uu_regexp_begin, $part->body, $matches, PREG_OFFSET_CAPTURE)) { |
AM |
1007 |
$startpos = $matches[0][1]; |
d311d8
|
1008 |
|
48ba44
|
1009 |
if (!preg_match($uu_regexp_end, $part->body, $m, PREG_OFFSET_CAPTURE, $startpos)) { |
AM |
1010 |
break; |
d311d8
|
1011 |
} |
14f22f
|
1012 |
|
48ba44
|
1013 |
$endpos = $m[0][1]; |
AM |
1014 |
$begin_len = strlen($matches[0][0]); |
|
1015 |
$end_len = strlen($m[0][0]); |
|
1016 |
|
|
1017 |
// extract attachment body |
|
1018 |
$filebody = substr($part->body, $startpos + $begin_len, $endpos - $startpos - $begin_len - 1); |
|
1019 |
$filebody = str_replace("\r\n", "\n", $filebody); |
|
1020 |
|
|
1021 |
// remove attachment body from the message body |
|
1022 |
$part->body = substr_replace($part->body, '', $startpos, $endpos + $end_len - $startpos); |
2268aa
|
1023 |
// mark body as modified so it will not be cached by rcube_imap_cache |
AM |
1024 |
$part->body_modified = true; |
48ba44
|
1025 |
|
AM |
1026 |
// add attachments to the structure |
|
1027 |
$uupart = new rcube_message_part; |
|
1028 |
$uupart->filename = trim($matches[1][0]); |
|
1029 |
$uupart->encoding = 'stream'; |
|
1030 |
$uupart->body = convert_uudecode($filebody); |
|
1031 |
$uupart->size = strlen($uupart->body); |
|
1032 |
$uupart->mime_id = 'uu.' . $part->mime_id . '.' . $pid; |
|
1033 |
|
|
1034 |
$ctype = rcube_mime::file_content_type($uupart->body, $uupart->filename, 'application/octet-stream', true); |
|
1035 |
$uupart->mimetype = $ctype; |
|
1036 |
list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype); |
|
1037 |
|
|
1038 |
$parts[] = $uupart; |
|
1039 |
$pid++; |
d311d8
|
1040 |
} |
f19d86
|
1041 |
|
d311d8
|
1042 |
return $parts; |
A |
1043 |
} |
b91f04
|
1044 |
|
2da830
|
1045 |
/** |
AM |
1046 |
* Fix attachment name encoding if needed/possible |
|
1047 |
*/ |
|
1048 |
protected function fix_attachment_name($name, $part) |
|
1049 |
{ |
|
1050 |
if ($name == rcube_charset::clean($name)) { |
|
1051 |
return $name; |
|
1052 |
} |
|
1053 |
|
|
1054 |
// find charset from part or its parent(s) |
|
1055 |
if ($part->charset) { |
|
1056 |
$charsets[] = $part->charset; |
|
1057 |
} |
|
1058 |
else { |
|
1059 |
// check first part (common case) |
|
1060 |
$n = strpos($part->mime_id, '.') ? preg_replace('/\.[0-9]+$/', '', $part->mime_id) . '.1' : 1; |
|
1061 |
if (($_part = $this->mime_parts[$n]) && $_part->charset) { |
|
1062 |
$charsets[] = $_part->charset; |
|
1063 |
} |
|
1064 |
|
|
1065 |
// check parents' charset |
|
1066 |
$items = explode('.', $part->mime_id); |
|
1067 |
for ($i = count($items)-1; $i > 0; $i--) { |
|
1068 |
$last = array_pop($items); |
|
1069 |
$parent = $this->mime_parts[join('.', $items)]; |
|
1070 |
|
|
1071 |
if ($parent && $parent->charset) { |
|
1072 |
$charsets[] = $parent->charset; |
|
1073 |
} |
|
1074 |
} |
|
1075 |
} |
|
1076 |
|
|
1077 |
if ($this->headers->charset) { |
|
1078 |
$charsets[] = $this->headers->charset; |
|
1079 |
} |
|
1080 |
|
|
1081 |
if (empty($charsets)) { |
|
1082 |
$rcube = rcube::get_instance(); |
|
1083 |
$charsets[] = rcube_charset::detect($name, $rcube->config->get('default_charset', RCUBE_CHARSET)); |
|
1084 |
} |
|
1085 |
|
|
1086 |
foreach (array_unique($charsets) as $charset) { |
|
1087 |
$_name = rcube_charset::convert($name, $charset); |
|
1088 |
|
|
1089 |
if ($_name == rcube_charset::clean($_name)) { |
|
1090 |
if (!$part->charset) { |
|
1091 |
$part->charset = $charset; |
|
1092 |
} |
|
1093 |
|
|
1094 |
return $_name; |
|
1095 |
} |
|
1096 |
} |
|
1097 |
|
|
1098 |
return $name; |
|
1099 |
} |
b91f04
|
1100 |
|
T |
1101 |
/** |
|
1102 |
* Deprecated methods (to be removed) |
|
1103 |
*/ |
|
1104 |
|
|
1105 |
public static function unfold_flowed($text) |
|
1106 |
{ |
|
1107 |
return rcube_mime::unfold_flowed($text); |
|
1108 |
} |
|
1109 |
|
|
1110 |
public static function format_flowed($text, $length = 72) |
|
1111 |
{ |
|
1112 |
return rcube_mime::format_flowed($text, $length); |
|
1113 |
} |
8fa58e
|
1114 |
} |