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