source: github/program/include/rcube_message.php @ 7a229b9

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 7a229b9 was 7a229b9, checked in by alecpl <alec@…>, 4 years ago
  • Improve messages display performance
  • Property mode set to 100644
File size: 14.5 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, 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 
89 
90  /**
91   * Return a (decoded) message header
92   *
93   * @param string Header name
94   * @param bool   Don't mime-decode the value
95   * @return string Header value
96   */
97  public function get_header($name, $raw = false)
98  {
99    $value = $this->headers->$name;
100    return $raw ? $value : $this->imap->decode_header($value);
101  }
102 
103  /**
104   * Set is_safe var and session data
105   *
106   * @param bool enable/disable
107   */
108  public function set_safe($safe = true)
109  {
110    $this->is_safe = $safe;
111    $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
112  }
113 
114  /**
115   * Compose a valid URL for getting a message part
116   *
117   * @param string Part MIME-ID
118   * @return string URL or false if part does not exist
119   */
120  public function get_part_url($mime_id)
121  {
122    if ($this->mime_parts[$mime_id])
123      return $this->opt['get_url'] . "&_part=" . $mime_id;
124    else
125      return false;
126  }
127 
128 
129  /**
130   * Get content of a specific part of this message
131   *
132   * @param string Part MIME-ID
133   * @param resource File pointer to save the message part
134   * @return string Part content
135   */
136  public function get_part_content($mime_id, $fp=NULL)
137  {
138    if ($part = $this->mime_parts[$mime_id])
139      return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
140    else
141      return null;
142  }
143 
144 
145  /**
146   * Determine if the message contains a HTML part
147   *
148   * @return bool True if a HTML is available, False if not
149   */
150  function has_html_part()
151  {
152     // check all message parts
153     foreach ($this->parts as $pid => $part) {
154        $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
155        if ($mimetype == 'text/html')
156           return true;
157     }
158
159     return false;
160  }
161
162  /**
163   * Return the first HTML part of this message
164   *
165   * @return string HTML message part content
166   */
167  function first_html_part()
168    {
169    $html_part = null;
170
171    // check all message parts
172    foreach ($this->mime_parts as $mime_id => $part) {
173      $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
174      if ($mimetype == 'text/html') {
175        $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
176      }
177    }
178
179    return $html_part;
180  }
181
182
183  /**
184   * Return the first text part of this message
185   *
186   * @return string Plain text message/part content
187   */
188  function first_text_part()
189    {
190    // no message structure, return complete body
191    if (empty($this->parts))
192      return $this->body;
193     
194    $out = null;
195
196    // check all message parts
197    foreach ($this->mime_parts as $mime_id => $part) {
198      $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
199
200      if ($mimetype == 'text/plain') {
201        $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
202        break;
203      }
204      else if ($mimetype == 'text/html') {
205        $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
206
207        // remove special chars encoding
208        $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
209        $html_part = strtr($html_part, $trans);
210
211        // create instance of html2text class
212        $txt = new html2text($html_part);
213        $out = $txt->get_text();
214        break;
215      }
216    }
217
218    return $out;
219  }
220
221
222  /**
223   * Raad the message structure returend by the IMAP server
224   * and build flat lists of content parts and attachments
225   *
226   * @param object rcube_message_part Message structure node
227   * @param bool  True when called recursively
228   */
229  private function parse_structure($structure, $recursive = false)
230  {
231    $message_ctype_primary = strtolower($structure->ctype_primary);
232    $message_ctype_secondary = strtolower($structure->ctype_secondary);
233
234    // show message headers
235    if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
236      $c = new stdClass;
237      $c->type = 'headers';
238      $c->headers = &$structure->headers;
239      $this->parts[] = $c;
240    }
241
242    // print body if message doesn't have multiple parts
243    if ($message_ctype_primary == 'text' && !$recursive) {
244      $structure->type = 'content';
245      $this->parts[] = &$structure;
246    }
247    // the same for pgp signed messages
248    else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
249      $structure->type = 'content';
250      $this->parts[] = &$structure;
251    }
252    // message contains alternative parts
253    else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
254      // get html/plaintext parts
255      $plain_part = $html_part = $print_part = $related_part = null;
256
257      foreach ($structure->parts as $p => $sub_part) {
258        $rel_parts = $attachmnts = null;
259        $sub_ctype_primary = strtolower($sub_part->ctype_primary);
260        $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
261       
262        // check if sub part is
263        if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
264          $plain_part = $p;
265        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
266          $html_part = $p;
267        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
268          $enriched_part = $p;
269        else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
270          $related_part = $p;
271      }
272
273      // parse related part (alternative part could be in here)
274      if ($related_part !== null && !$this->parse_alternative) {
275        $this->parse_alternative = true;
276        $this->parse_structure($structure->parts[$related_part], true);
277        $this->parse_alternative = false;
278       
279        // if plain part was found, we should unset it if html is preferred
280        if ($this->opt['prefer_html'] && count($this->parts))
281          $plain_part = null;
282      }
283
284      // choose html/plain part to print
285      if ($html_part !== null && $this->opt['prefer_html']) {
286        $print_part = &$structure->parts[$html_part];
287      }
288      else if ($enriched_part !== null) {
289        $print_part = &$structure->parts[$enriched_part];
290      }
291      else if ($plain_part !== null) {
292        $print_part = &$structure->parts[$plain_part];
293      }
294
295      // add the right message body
296      if (is_object($print_part)) {
297        $print_part->type = 'content';
298        $this->parts[] = $print_part;
299      }
300      // show plaintext warning
301      else if ($html_part !== null && empty($this->parts)) {
302        $c = new stdClass;
303        $c->type = 'content';
304        $c->body = rcube_label('htmlmessage');
305        $c->ctype_primary = 'text';
306        $c->ctype_secondary = 'plain';
307
308        $this->parts[] = $c;
309      }
310
311      // add html part as attachment
312      if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
313        $html_part = &$structure->parts[$html_part];
314        $html_part->filename = rcube_label('htmlmessage');
315        $html_part->mimetype = 'text/html';
316
317        $this->attachments[] = $html_part;
318      }
319    }
320    // this is an ecrypted message -> create a plaintext body with the according message
321    else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
322      $p = new stdClass;
323      $p->type = 'content';
324      $p->ctype_primary = 'text';
325      $p->ctype_secondary = 'plain';
326      $p->body = rcube_label('encryptedmessage');
327     
328      $this->parts[] = $p;
329    }
330    // message contains multiple parts
331    else if (is_array($structure->parts) && !empty($structure->parts)) {
332      // iterate over parts
333      for ($i=0; $i < count($structure->parts); $i++) {
334        $mail_part = &$structure->parts[$i];
335        $primary_type = strtolower($mail_part->ctype_primary);
336        $secondary_type = strtolower($mail_part->ctype_secondary);
337
338        // multipart/alternative
339        if ($primary_type=='multipart') {
340          $this->parse_structure($mail_part, true);
341        }
342        // part text/[plain|html] OR message/delivery-status
343        else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
344                 ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
345         
346          // add text part if we're not in alternative mode or if it matches the prefs
347          if (!$this->parse_alternative ||
348              ($secondary_type == 'html' && $this->opt['prefer_html']) ||
349              ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
350            $mail_part->type = 'content';
351            $this->parts[] = $mail_part;
352          }
353
354          // list as attachment as well
355          if (!empty($mail_part->filename))
356            $this->attachments[] = $mail_part;
357        }
358        // part message/*
359        else if ($primary_type=='message') {
360          $this->parse_structure($mail_part, true);
361         
362          // list as attachment as well (mostly .eml)
363          if (!empty($mail_part->filename))
364            $this->attachments[] = $mail_part;
365        }
366        // ignore "virtual" protocol parts
367        else if ($primary_type == 'protocol')
368          continue;
369
370        // part is file/attachment
371        else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
372                 $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
373          // skip apple resource forks
374          if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
375            continue;
376
377          // part belongs to a related message
378          if ($message_ctype_secondary == 'related') {
379            if ($mail_part->headers['content-id'])
380              $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
381            if ($mail_part->headers['content-location'])
382              $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
383           
384            if ($mail_part->content_id || $mail_part->content_location) {
385              $this->inline_parts[] = $mail_part;
386            }
387          }
388          // is regular attachment
389          else {
390            if (!$mail_part->filename)
391              $mail_part->filename = 'Part '.$mail_part->mime_id;
392            $this->attachments[] = $mail_part;
393          }
394        }
395      }
396
397      // if this was a related part try to resolve references
398      if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
399        $a_replaces = array();
400
401        foreach ($this->inline_parts as $inline_object) {
402          $part_url = $this->get_part_url($inline_object->mime_id);
403          if ($inline_object->content_id)
404            $a_replaces['cid:'.$inline_object->content_id] = $part_url;
405          if ($inline_object->content_location)
406            $a_replaces[$inline_object->content_location] = $part_url;
407        }
408
409        // add replace array to each content part
410        // (will be applied later when part body is available)
411        foreach ($this->parts as $i => $part) {
412          if ($part->type == 'content')
413            $this->parts[$i]->replaces = $a_replaces;
414        }
415      }
416    }
417
418    // message is single part non-text
419    else if ($structure->filename) {
420      $this->attachments[] = $structure;
421    }
422  }
423
424
425  /**
426   * Fill aflat array with references to all parts, indexed by part numbers
427   *
428   * @param object rcube_message_part Message body structure
429   */
430  private function get_mime_numbers(&$part)
431  {
432    if (strlen($part->mime_id))
433      $this->mime_parts[$part->mime_id] = &$part;
434     
435    if (is_array($part->parts))
436      for ($i=0; $i<count($part->parts); $i++)
437        $this->get_mime_numbers($part->parts[$i]);
438  }
439
440
441}
442
Note: See TracBrowser for help on using the repository browser.