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

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

Complete implementation of #1484601: add link for saving sender to address book and reload message

File size: 13.2 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    $this->set_safe(0);
61   
62    $this->opt = array(
63      'safe' => $this->is_safe,
64      'prefer_html' => $this->app->config->get('prefer_html'),
65      'get_url' => rcmail_url('get', array('_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid))
66    );
67   
68    if ($this->structure = $this->imap->get_structure($uid)) {
69      $this->get_mime_numbers($this->structure);
70      $this->parse_structure($this->structure);
71    }
72    else {
73      $this->body = $this->imap->get_body($uid);
74    }
75  }
76 
77 
78  /**
79   * Return a (decoded) message header
80   *
81   * @param string Header name
82   * @param bool   Don't mime-decode the value
83   * @return string Header value
84   */
85  public function get_header($name, $raw = false)
86  {
87    $value = $this->headers->$name;
88    return $raw ? $value : $this->imap->decode_header($value);
89  }
90 
91  /**
92   * Set is_safe var and session data
93   *
94   * @param bool enable/disable
95   */
96  public function set_safe($safe = true)
97  {
98    $this->is_safe = $safe;
99    $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
100  }
101 
102  /**
103   * Compose a valid URL for getting a message part
104   *
105   * @param string Part MIME-ID
106   * @return string URL or false if part does not exist
107   */
108  public function get_part_url($mime_id)
109  {
110    if ($this->mime_parts[$mime_id])
111      return $this->opt['get_url'] . "&_part=" . $mime_id;
112    else
113      return false;
114  }
115 
116 
117  /**
118   * Get content of a specific part of this message
119   *
120   * @param string Part MIME-ID
121   * @return string Part content
122   */
123  public function get_part_content($mime_id)
124  {
125    if ($part = $this->mime_parts[$mime_id])
126      return $this->imap->get_message_part($this->uid, $mime_id, $part);
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    // message contains alternative parts
235    else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
236      // get html/plaintext parts
237      $plain_part = $html_part = $print_part = $related_part = null;
238
239      foreach ($structure->parts as $p => $sub_part) {
240        $rel_parts = $attachmnts = null;
241        $sub_ctype_primary = strtolower($sub_part->ctype_primary);
242        $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
243       
244        // check if sub part is
245        if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
246          $plain_part = $p;
247        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
248          $html_part = $p;
249        else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
250          $enriched_part = $p;
251        else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
252          $related_part = $p;
253      }
254
255      // parse related part (alternative part could be in here)
256      if ($related_part !== null && !$this->parse_alternative) {
257        $this->parse_alternative = true;
258        $this->parse_structure($structure->parts[$related_part], true);
259        $this->parse_alternative = false;
260       
261        // if plain part was found, we should unset it if html is preferred
262        if ($this->opt['prefer_html'] && count($this->parts))
263          $plain_part = null;
264      }
265
266      // choose html/plain part to print
267      if ($html_part !== null && $this->opt['prefer_html']) {
268        $print_part = &$structure->parts[$html_part];
269      }
270      else if ($enriched_part !== null) {
271        $print_part = &$structure->parts[$enriched_part];
272      }
273      else if ($plain_part !== null) {
274        $print_part = &$structure->parts[$plain_part];
275      }
276
277      // add the right message body
278      if (is_object($print_part)) {
279        $print_part->type = 'content';
280        $this->parts[] = $print_part;
281      }
282      // show plaintext warning
283      else if ($html_part !== nullL && empty($this->parts)) {
284        $c = new stdClass;
285        $c->type = 'content';
286        $c->body = rcube_label('htmlmessage');
287        $c->ctype_primary = 'text';
288        $c->ctype_secondary = 'plain';
289
290        $this->parts[] = $c;
291      }
292
293      // add html part as attachment
294      if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
295        $html_part = &$structure->parts[$html_part];
296        $html_part->filename = rcube_label('htmlmessage');
297        $html_part->mimetype = 'text/html';
298
299        $this->attachments[] = $html_part;
300      }
301    }
302    // this is an ecrypted message -> create a plaintext body with the according message
303    else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
304      $p = new stdClass;
305      $p->type = 'content';
306      $p->ctype_primary = 'text';
307      $p->ctype_secondary = 'plain';
308      $p->body = rcube_label('encryptedmessage');
309     
310      $this->parts[] = $p;
311    }
312    // message contains multiple parts
313    else if (is_array($structure->parts) && !empty($structure->parts)) {
314      // iterate over parts
315      for ($i=0; $i < count($structure->parts); $i++) {
316        $mail_part = &$structure->parts[$i];
317        $primary_type = strtolower($mail_part->ctype_primary);
318        $secondary_type = strtolower($mail_part->ctype_secondary);
319
320        // multipart/alternative
321        if ($primary_type=='multipart') {
322          $this->parse_structure($mail_part, true);
323        }
324        // part text/[plain|html] OR message/delivery-status
325        else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
326                 ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
327         
328          // add text part if we're not in alternative mode or if it matches the prefs
329          if (!$this->parse_alternative ||
330              ($secondary_type == 'html' && $this->opt['prefer_html']) ||
331              ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
332            $mail_part->type = 'content';
333            $this->parts[] = $mail_part;
334          }
335         
336          // list as attachment as well
337          if (!empty($mail_part->filename))
338            $this->attachments[] = $mail_part;
339        }
340        // part message/*
341        else if ($primary_type=='message') {
342          $this->parse_structure($mail_part, true);
343        }
344        // ignore "virtual" protocol parts
345        else if ($primary_type == 'protocol')
346          continue;
347
348        // part is file/attachment
349        else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
350                 $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
351          // skip apple resource forks
352          if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
353            continue;
354
355          // part belongs to a related message
356          if ($message_ctype_secondary == 'related' && $mail_part->headers['content-id']) {
357            $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
358            $this->inline_parts[] = $mail_part;
359          }
360          // is regular attachment
361          else {
362            if (!$mail_part->filename)
363              $mail_part->filename = 'Part '.$mail_part->mime_id;
364            $this->attachments[] = $mail_part;
365          }
366        }
367      }
368
369      // if this was a related part try to resolve references
370      if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
371        $a_replaces = array();
372
373        foreach ($this->inline_parts as $inline_object) {
374          $a_replaces['cid:'.$inline_object->content_id] = $this->get_part_url($inline_object->mime_id);
375        }
376
377        // add replace array to each content part
378        // (will be applied later when part body is available)
379        foreach ($this->parts as $i => $part) {
380          if ($part->type == 'content')
381            $this->parts[$i]->replaces = $a_replaces;
382        }
383      }
384    }
385
386    // message is single part non-text
387    else if ($structure->filename) {
388      $this->attachments[] = $structure;
389    }
390  }
391
392
393  /**
394   * Fill aflat array with references to all parts, indexed by part numbers
395   *
396   * @param object rcube_message_part Message body structure
397   */
398  private function get_mime_numbers(&$part)
399  {
400    if (strlen($part->mime_id))
401      $this->mime_parts[$part->mime_id] = &$part;
402     
403    if (is_array($part->parts))
404      for ($i=0; $i<count($part->parts); $i++)
405        $this->get_mime_numbers($part->parts[$i]);
406  }
407
408
409}
410
Note: See TracBrowser for help on using the repository browser.