source: github/program/include/rcube_message.php @ 8fa58e7

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 8fa58e7 was 8fa58e7, checked in by thomascube <thomas@…>, 5 years ago

New class rcube_message representing a mail message; changed global $MESSAGE from array to object

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