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