| | |
| | | +-----------------------------------------------------------------------+ |
| | | | Author: Thomas Bruederli <roundcube@gmail.com> | |
| | | +-----------------------------------------------------------------------+ |
| | | |
| | | $Id$ |
| | | |
| | | */ |
| | | |
| | | |
| | |
| | | class rcube_message |
| | | { |
| | | /** |
| | | * Instace of rcmail. |
| | | * Instace of framework class. |
| | | * |
| | | * @var rcmail |
| | | * @var rcube |
| | | */ |
| | | private $app; |
| | | |
| | |
| | | */ |
| | | private $mime; |
| | | private $opt = array(); |
| | | private $inline_parts = array(); |
| | | private $parse_alternative = false; |
| | | |
| | | public $uid = null; |
| | | public $uid; |
| | | public $folder; |
| | | public $headers; |
| | | public $parts = array(); |
| | | public $mime_parts = array(); |
| | | public $inline_parts = array(); |
| | | public $attachments = array(); |
| | | public $subject = ''; |
| | | public $sender = null; |
| | |
| | | * |
| | | * Provide a uid, and parse message structure. |
| | | * |
| | | * @param string $uid The message UID. |
| | | * @param string $uid The message UID. |
| | | * @param string $folder Folder name |
| | | * |
| | | * @see self::$app, self::$storage, self::$opt, self::$parts |
| | | */ |
| | | function __construct($uid) |
| | | function __construct($uid, $folder = null) |
| | | { |
| | | $this->uid = $uid; |
| | | $this->app = rcube::get_instance(); |
| | | $this->storage = $this->app->get_storage(); |
| | | $this->folder = strlen($folder) ? $folder : $this->storage->get_folder(); |
| | | $this->storage->set_options(array('all_headers' => true)); |
| | | |
| | | // Set current folder |
| | | $this->storage->set_folder($this->folder); |
| | | |
| | | $this->headers = $this->storage->get_message($uid); |
| | | |
| | |
| | | } |
| | | return $fp ? true : $part->body; |
| | | } |
| | | |
| | | // get from IMAP |
| | | $this->storage->set_folder($this->folder); |
| | | |
| | | return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv); |
| | | } else |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | if (!$recursive) { |
| | | $level = explode('.', $part->mime_id); |
| | | |
| | | // Level too high |
| | | if (count($level) > 2) { |
| | | // Skip if level too deep or part has a file name |
| | | if (count($level) > 2 || $part->filename) { |
| | | continue; |
| | | } |
| | | |
| | | // HTML part can be on the lower level, if not... |
| | | if (count($level) > 1) { |
| | | // It can be an alternative or related message part |
| | | $parent = $this->mime_parts[0]; |
| | | array_pop($level); |
| | | $parent = $this->mime_parts[join('.', $level)]; |
| | | // ... parent isn't multipart/alternative or related |
| | | if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') { |
| | | continue; |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Checks if part of the message is an attachment (or part of it) |
| | | * |
| | | * @param rcube_message_part $part Message part |
| | | * |
| | | * @return bool True if the part is an attachment part |
| | | */ |
| | | public function is_attachment($part) |
| | | { |
| | | foreach ($this->attachments as $att_part) { |
| | | if ($att_part->mime_id == $part->mime_id) { |
| | | return true; |
| | | } |
| | | |
| | | // check if the part is a subpart of another attachment part (message/rfc822) |
| | | if ($att_part->mimetype == 'message/rfc822') { |
| | | if (in_array($part, (array)$att_part->parts)) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Read the message structure returend by the IMAP server |
| | | * and build flat lists of content parts and attachments |
| | | * |
| | |
| | | |
| | | foreach ($structure->parts as $p => $sub_part) { |
| | | $sub_mimetype = $sub_part->mimetype; |
| | | |
| | | // skip empty text parts |
| | | if (!$sub_part->size && preg_match('#^text/(plain|html|enriched)$#', $sub_mimetype)) { |
| | | continue; |
| | | } |
| | | |
| | | // check if sub part is |
| | | if ($sub_mimetype == 'text/plain') |
| | |
| | | } |
| | | |
| | | // list as attachment as well |
| | | if (!empty($mail_part->filename)) |
| | | if (!empty($mail_part->filename)) { |
| | | $this->attachments[] = $mail_part; |
| | | } |
| | | // list html part as attachment (here the part is most likely inside a multipart/related part) |
| | | else if ($this->parse_alternative && ($secondary_type == 'html' && !$this->opt['prefer_html'])) { |
| | | $this->attachments[] = $mail_part; |
| | | } |
| | | } |
| | | // part message/* |
| | | else if ($primary_type == 'message') { |
| | |
| | | function tnef_decode(&$part) |
| | | { |
| | | // @TODO: attachment may be huge, hadle it via file |
| | | if (!isset($part->body)) |
| | | if (!isset($part->body)) { |
| | | $this->storage->set_folder($this->folder); |
| | | $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part); |
| | | } |
| | | |
| | | $parts = array(); |
| | | $tnef = new tnef_decoder; |
| | |
| | | function uu_decode(&$part) |
| | | { |
| | | // @TODO: messages may be huge, hadle body via file |
| | | if (!isset($part->body)) |
| | | if (!isset($part->body)) { |
| | | $this->storage->set_folder($this->folder); |
| | | $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part); |
| | | } |
| | | |
| | | $parts = array(); |
| | | // FIXME: line length is max.65? |
| | | $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n(([\x21-\x7E]{0,65}\n)+)`\nend/s'; |
| | | $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n/s'; |
| | | |
| | | if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) { |
| | | // remove attachments bodies from the message body |
| | | $part->body = preg_replace($uu_regexp, '', $part->body); |
| | | // update message content-type |
| | | $part->ctype_primary = 'multipart'; |
| | | $part->ctype_secondary = 'mixed'; |
| | | $part->mimetype = $part->ctype_primary . '/' . $part->ctype_secondary; |
| | | $uu_endstring = "`\nend\n"; |
| | | |
| | | // add attachments to the structure |
| | | foreach ($matches as $pid => $att) { |
| | | $startpos = strpos($part->body, $att[1]) + strlen($att[1]) + 1; // "\n" |
| | | $endpos = strpos($part->body, $uu_endstring); |
| | | $filebody = substr($part->body, $startpos, $endpos-$startpos); |
| | | |
| | | // remove attachments bodies from the message body |
| | | $part->body = substr_replace($part->body, "", $startpos, $endpos+strlen($uu_endstring)-$startpos); |
| | | |
| | | $uupart = new rcube_message_part; |
| | | |
| | | $uupart->filename = trim($att[1]); |
| | | $uupart->encoding = 'stream'; |
| | | $uupart->body = convert_uudecode($att[2]); |
| | | $uupart->body = convert_uudecode($filebody); |
| | | $uupart->size = strlen($uupart->body); |
| | | $uupart->mime_id = 'uu.' . $part->mime_id . '.' . $pid; |
| | | |
| | |
| | | $parts[] = $uupart; |
| | | unset($matches[$pid]); |
| | | } |
| | | |
| | | // remove attachments bodies from the message body |
| | | $part->body = preg_replace($uu_regexp, '', $part->body); |
| | | } |
| | | |
| | | return $parts; |