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

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

Also list message/rfc822 parts as attachment if a filename is specified

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