source: subversion/trunk/roundcubemail/program/include/rcube_message.php @ 1736

Last change on this file since 1736 was 1736, checked in by thomasb, 5 years ago

Respect Content-Location headers in multipart/related messages (#1484946)

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