source: github/program/include/rcube_message.php @ b0d56e9

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since b0d56e9 was b0d56e9, checked in by alecpl <alec@…>, 4 years ago
  • simple fix for malformed Content-Disposition (#1485965)
  • Property mode set to 100644
File size: 14.9 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/include/rcube_message.php                                     |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Logical representation of a mail message with all its data          |
13 |   and related functions                                               |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id: rcube_imap.php 1344 2008-04-30 08:21:42Z thomasb $
19
20*/
21
22
23/**
24 * Logical representation of a mail message with all its data
25 * and related functions
26 *
27 * @package    Mail
28 * @author     Thomas Bruederli <roundcube@gmail.com>
29 */
30class rcube_message
31{
32  private $app;
33  private $imap;
34  private $opt = array();
35  private $inline_parts = array();
36  private $parse_alternative = false;
37 
38  public $uid = null;
39  public $headers;
40  public $structure;
41  public $parts = array();
42  public $mime_parts = array();
43  public $attachments = array();
44  public $subject = '';
45  public $sender = null;
46  public $is_safe = false;
47 
48
49  /**
50   * __construct
51   *
52   * Provide a uid, and parse message structure.
53   *
54   * @param string $uid The message UID.
55   *
56   * @uses rcmail::get_instance()
57   * @uses rcube_imap::decode_mime_string()
58   * @uses self::set_safe()
59   *
60   * @see self::$app, self::$imap, self::$opt, self::$structure
61   */
62  function __construct($uid)
63  {
64    $this->app = rcmail::get_instance();
65    $this->imap = $this->app->imap;
66   
67    $this->uid = $uid;
68    $this->headers = $this->imap->get_headers($uid, NULL, true, true);
69
70    $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
71    list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
72   
73    $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
74    $this->opt = array(
75      'safe' => $this->is_safe,
76      'prefer_html' => $this->app->config->get('prefer_html'),
77      'get_url' => rcmail_url('get', array('_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid))
78    );
79
80    if ($this->structure = $this->imap->get_structure($uid, $this->headers->body_structure)) {
81      $this->get_mime_numbers($this->structure);
82      $this->parse_structure($this->structure);
83    }
84    else {
85      $this->body = $this->imap->get_body($uid);
86    }
87   
88    // notify plugins and let them analyze this structured message object
89    $this->app->plugins->exec_hook('message_load', array('object' => $this));
90  }
91 
92 
93  /**
94   * Return a (decoded) message header
95   *
96   * @param string Header name
97   * @param bool   Don't mime-decode the value
98   * @return string Header value
99   */
100  public function get_header($name, $raw = false)
101  {
102    $value = $this->headers->$name;
103    return $raw ? $value : $this->imap->decode_header($value);
104  }
105 
106  /**
107   * Set is_safe var and session data
108   *
109   * @param bool enable/disable
110   */
111  public function set_safe($safe = true)
112  {
113    $this->is_safe = $safe;
114    $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
115  }
116 
117  /**
118   * Compose a valid URL for getting a message part
119   *
120   * @param string Part MIME-ID
121   * @return string URL or false if part does not exist
122   */
123  public function get_part_url($mime_id)
124  {
125    if ($this->mime_parts[$mime_id])
126      return $this->opt['get_url'] . "&_part=" . $mime_id;
127    else
128      return false;
129  }
130 
131 
132  /**
133   * Get content of a specific part of this message
134   *
135   * @param string Part MIME-ID
136   * @param resource File pointer to save the message part
137   * @return string Part content
138   */
139  public function get_part_content($mime_id, $fp=NULL)
140  {
141    if ($part = $this->mime_parts[$mime_id])
142      return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
143    else
144      return null;
145  }
146 
147 
148  /**
149   * Determine if the message contains a HTML part
150   *
151   * @return bool True if a HTML is available, False if not
152   */
153  function has_html_part()
154  {
155     // check all message parts
156     foreach ($this->parts as $pid => $part) {
157        $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
158        if ($mimetype == 'text/html')
159           return true;
160     }
161
162     return false;
163  }
164
165  /**
166   * Return the first HTML part of this message
167   *
168   * @return string HTML message part content
169   */
170  function first_html_part()
171    {
172    // check all message parts
173    foreach ($this->mime_parts as $mime_id => $part) {
174      $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
175      if ($mimetype == 'text/html') {
176        return $this->imap->get_message_part($this->uid, $mime_id, $part);
177      }
178    }
179  }
180
181
182  /**
183   * Return the first text part of this message
184   *
185   * @return string Plain text message/part content
186   */
187  function first_text_part()
188    {
189    // no message structure, return complete body
190    if (empty($this->parts))
191      return $this->body;
192     
193    $out = null;
194
195    // check all message parts
196    foreach ($this->mime_parts as $mime_id => $part) {
197      $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
198
199      if ($mimetype == 'text/plain') {
200        $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
201        break;
202      }
203      else if ($mimetype == 'text/html') {
204        $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
205
206        // remove special chars encoding
207        $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
208        $html_part = strtr($html_part, $trans);
209
210        // create instance of html2text class
211        $txt = new html2text($html_part);
212        $out = $txt->get_text();
213        break;
214      }
215    }
216
217    return $out;
218  }
219
220
221  /**
222   * Raad the message structure returend by the IMAP server
223   * and build flat lists of content parts and attachments
224   *
225   * @param object rcube_message_part Message structure node
226   * @param bool  True when called recursively
227   */
228  private function parse_structure($structure, $recursive = false)
229  {
230    $message_ctype_primary = strtolower($structure->ctype_primary);
231    $message_ctype_secondary = strtolower($structure->ctype_secondary);
232
233    // show message headers
234    if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
235      $c = new stdClass;
236      $c->type = 'headers';
237      $c->headers = &$structure->headers;
238      $this->parts[] = $c;
239    }
240
241    // print body if message doesn't have multiple parts
242    if ($message_ctype_primary == 'text' && !$recursive) {
243      $structure->type = 'content';
244      $this->parts[] = &$structure;
245    }
246    // the same for pgp signed messages
247    else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
248      $structure->type = 'content';
249      $this->parts[] = &$structure;
250    }
251    // message contains alternative parts
252    else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
253      // get html/plaintext parts
254      $plain_part = $html_part = $print_part = $related_part = null;
255
256      foreach ($structure->parts as $p => $sub_part) {
257        $rel_parts = $attachmnts = null;
258        $sub_ctype_primary = strtolower($sub_part->ctype_primary);
259        $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
260       
261        // check if sub part is
262        if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
263          $plain_part = $p;
264        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
265          $html_part = $p;
266        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
267          $enriched_part = $p;
268        else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
269          $related_part = $p;
270      }
271
272      // parse related part (alternative part could be in here)
273      if ($related_part !== null && !$this->parse_alternative) {
274        $this->parse_alternative = true;
275        $this->parse_structure($structure->parts[$related_part], true);
276        $this->parse_alternative = false;
277       
278        // if plain part was found, we should unset it if html is preferred
279        if ($this->opt['prefer_html'] && count($this->parts))
280          $plain_part = null;
281      }
282
283      // choose html/plain part to print
284      if ($html_part !== null && $this->opt['prefer_html']) {
285        $print_part = &$structure->parts[$html_part];
286      }
287      else if ($enriched_part !== null) {
288        $print_part = &$structure->parts[$enriched_part];
289      }
290      else if ($plain_part !== null) {
291        $print_part = &$structure->parts[$plain_part];
292      }
293
294      // add the right message body
295      if (is_object($print_part)) {
296        $print_part->type = 'content';
297        $this->parts[] = $print_part;
298      }
299      // show plaintext warning
300      else if ($html_part !== null && empty($this->parts)) {
301        $c = new stdClass;
302        $c->type = 'content';
303        $c->body = rcube_label('htmlmessage');
304        $c->ctype_primary = 'text';
305        $c->ctype_secondary = 'plain';
306
307        $this->parts[] = $c;
308      }
309
310      // add html part as attachment
311      if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
312        $html_part = &$structure->parts[$html_part];
313        $html_part->filename = rcube_label('htmlmessage');
314        $html_part->mimetype = 'text/html';
315
316        $this->attachments[] = $html_part;
317      }
318    }
319    // this is an ecrypted message -> create a plaintext body with the according message
320    else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
321      $p = new stdClass;
322      $p->type = 'content';
323      $p->ctype_primary = 'text';
324      $p->ctype_secondary = 'plain';
325      $p->body = rcube_label('encryptedmessage');
326     
327      $this->parts[] = $p;
328    }
329    // message contains multiple parts
330    else if (is_array($structure->parts) && !empty($structure->parts)) {
331      // iterate over parts
332      for ($i=0; $i < count($structure->parts); $i++) {
333        $mail_part = &$structure->parts[$i];
334        $primary_type = strtolower($mail_part->ctype_primary);
335        $secondary_type = strtolower($mail_part->ctype_secondary);
336
337        // multipart/alternative
338        if ($primary_type=='multipart') {
339          $this->parse_structure($mail_part, true);
340        }
341        // part text/[plain|html] OR message/delivery-status
342        else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
343                 ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
344
345          // add text part if we're not in alternative mode or if it matches the prefs
346          if (!$this->parse_alternative ||
347              ($secondary_type == 'html' && $this->opt['prefer_html']) ||
348              ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
349            $mail_part->type = 'content';
350            $this->parts[] = $mail_part;
351          }
352
353          // list as attachment as well
354          if (!empty($mail_part->filename))
355            $this->attachments[] = $mail_part;
356        }
357        // part message/*
358        else if ($primary_type=='message') {
359          $this->parse_structure($mail_part, true);
360         
361          // list as attachment as well (mostly .eml)
362          if (!empty($mail_part->filename))
363            $this->attachments[] = $mail_part;
364        }
365        // ignore "virtual" protocol parts
366        else if ($primary_type == 'protocol')
367          continue;
368         
369        // part is Microsoft Outlook TNEF (winmail.dat)
370        else if ($primary_type == 'application' && $secondary_type == 'ms-tnef') {
371          foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) {
372            $this->mime_parts[$tnef_part->mime_id] = $tnef_part;
373            $this->attachments[] = $tnef_part;
374          }
375        }
376
377        // part is a file/attachment
378        else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
379                 $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
380
381          // skip apple resource forks
382          if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
383            continue;
384
385          // part belongs to a related message
386          if ($message_ctype_secondary == 'related') {
387            if ($mail_part->headers['content-id'])
388              $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
389            if ($mail_part->headers['content-location'])
390              $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
391           
392            if ($mail_part->content_id || $mail_part->content_location) {
393              $this->inline_parts[] = $mail_part;
394            }
395          }
396          // is a regular attachment
397          else {
398            if (!$mail_part->filename)
399              $mail_part->filename = 'Part '.$mail_part->mime_id;
400            $this->attachments[] = $mail_part;
401          }
402        }
403      }
404
405      // if this was a related part try to resolve references
406      if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
407        $a_replaces = array();
408
409        foreach ($this->inline_parts as $inline_object) {
410          $part_url = $this->get_part_url($inline_object->mime_id);
411          if ($inline_object->content_id)
412            $a_replaces['cid:'.$inline_object->content_id] = $part_url;
413          if ($inline_object->content_location)
414            $a_replaces[$inline_object->content_location] = $part_url;
415        }
416
417        // add replace array to each content part
418        // (will be applied later when part body is available)
419        foreach ($this->parts as $i => $part) {
420          if ($part->type == 'content')
421            $this->parts[$i]->replaces = $a_replaces;
422        }
423      }
424    }
425
426    // message is single part non-text
427    else if ($structure->filename) {
428      $this->attachments[] = $structure;
429    }
430  }
431
432
433  /**
434   * Fill aflat array with references to all parts, indexed by part numbers
435   *
436   * @param object rcube_message_part Message body structure
437   */
438  private function get_mime_numbers(&$part)
439  {
440    if (strlen($part->mime_id))
441      $this->mime_parts[$part->mime_id] = &$part;
442     
443    if (is_array($part->parts))
444      for ($i=0; $i<count($part->parts); $i++)
445        $this->get_mime_numbers($part->parts[$i]);
446  }
447
448
449}
450
Note: See TracBrowser for help on using the repository browser.