| | |
| | | * @var rcube_imap |
| | | */ |
| | | private $imap; |
| | | |
| | | /** |
| | | * Instance of mime class |
| | | * |
| | | * @var rcube_mime |
| | | */ |
| | | private $mime; |
| | | private $opt = array(); |
| | | private $inline_parts = array(); |
| | | private $parse_alternative = false; |
| | | |
| | | public $uid = null; |
| | | public $headers; |
| | | public $structure; |
| | | public $parts = array(); |
| | | public $mime_parts = array(); |
| | | public $attachments = array(); |
| | |
| | | * |
| | | * @param string $uid The message UID. |
| | | * |
| | | * @uses rcmail::get_instance() |
| | | * @uses rcube_imap::decode_mime_string() |
| | | * @uses self::set_safe() |
| | | * |
| | | * @see self::$app, self::$imap, self::$opt, self::$structure |
| | | */ |
| | | function __construct($uid) |
| | | { |
| | | $this->app = rcmail::get_instance(); |
| | | $this->uid = $uid; |
| | | $this->app = rcmail::get_instance(); |
| | | $this->imap = $this->app->imap; |
| | | $this->imap->get_all_headers = true; |
| | | |
| | | $this->uid = $uid; |
| | | $this->headers = $this->imap->get_headers($uid, NULL, true, true); |
| | | $this->headers = $this->imap->get_message($uid); |
| | | |
| | | if (!$this->headers) |
| | | return; |
| | | |
| | | $this->subject = rcube_imap::decode_mime_string( |
| | | $this->headers->subject, $this->headers->charset); |
| | | list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from)); |
| | | $this->mime = new rcube_mime($this->headers->charset); |
| | | |
| | | $this->subject = $this->mime->decode_mime_string($this->headers->subject); |
| | | list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1)); |
| | | |
| | | $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid])); |
| | | $this->opt = array( |
| | |
| | | '_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid)) |
| | | ); |
| | | |
| | | if ($this->structure = $this->imap->get_structure($uid, $this->headers->body_structure)) { |
| | | $this->get_mime_numbers($this->structure); |
| | | $this->parse_structure($this->structure); |
| | | if (!empty($this->headers->structure)) { |
| | | $this->get_mime_numbers($this->headers->structure); |
| | | $this->parse_structure($this->headers->structure); |
| | | } |
| | | else { |
| | | $this->body = $this->imap->get_body($uid); |
| | |
| | | */ |
| | | public function get_header($name, $raw = false) |
| | | { |
| | | if (empty($this->headers)) |
| | | return null; |
| | | |
| | | if ($this->headers->$name) |
| | | $value = $this->headers->$name; |
| | | else if ($this->headers->others[$name]) |
| | | $value = $this->headers->others[$name]; |
| | | |
| | | return $raw ? $value : $this->imap->decode_header($value); |
| | | return $raw ? $value : $this->mime->decode_header($value); |
| | | } |
| | | |
| | | |
| | |
| | | * @param string $mime_id Part MIME-ID |
| | | * @return string URL or false if part does not exist |
| | | */ |
| | | public function get_part_url($mime_id) |
| | | public function get_part_url($mime_id, $embed = false) |
| | | { |
| | | if ($this->mime_parts[$mime_id]) |
| | | return $this->opt['get_url'] . '&_part=' . $mime_id; |
| | | return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1' : ''); |
| | | else |
| | | return false; |
| | | } |
| | |
| | | $structure->type = 'content'; |
| | | $this->parts[] = &$structure; |
| | | } |
| | | // message contains alternative parts |
| | | else if ($mimetype == 'multipart/alternative' && is_array($structure->parts)) { |
| | | // message contains (more than one!) alternative parts |
| | | else if ($mimetype == 'multipart/alternative' |
| | | && is_array($structure->parts) && count($structure->parts) > 1 |
| | | ) { |
| | | // get html/plaintext parts |
| | | $plain_part = $html_part = $print_part = $related_part = null; |
| | | |
| | |
| | | $p->ctype_secondary = 'plain'; |
| | | $p->body = rcube_label('encryptedmessage'); |
| | | $p->size = strlen($p->body); |
| | | |
| | | $this->parts[] = $p; |
| | | } |
| | | // message contains multiple parts |
| | | else if (is_array($structure->parts) && !empty($structure->parts)) { |
| | |
| | | $img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/'; |
| | | |
| | | foreach ($this->inline_parts as $inline_object) { |
| | | $part_url = $this->get_part_url($inline_object->mime_id); |
| | | $part_url = $this->get_part_url($inline_object->mime_id, true); |
| | | if ($inline_object->content_id) |
| | | $a_replaces['cid:'.$inline_object->content_id] = $part_url; |
| | | if ($inline_object->content_location) { |
| | |
| | | |
| | | return $parts; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Interpret a format=flowed message body according to RFC 2646 |
| | | * |
| | | * @param string $text Raw body formatted as flowed text |
| | | * @return string Interpreted text with unwrapped lines and stuffed space removed |
| | | */ |
| | | public static function unfold_flowed($text) |
| | | { |
| | | $text = preg_split('/\r?\n/', $text); |
| | | $last = -1; |
| | | $q_level = 0; |
| | | |
| | | foreach ($text as $idx => $line) { |
| | | if ($line[0] == '>' && preg_match('/^(>+\s*)/', $line, $regs)) { |
| | | $q = strlen(str_replace(' ', '', $regs[0])); |
| | | $line = substr($line, strlen($regs[0])); |
| | | |
| | | if ($q == $q_level && $line |
| | | && isset($text[$last]) |
| | | && $text[$last][strlen($text[$last])-1] == ' ' |
| | | ) { |
| | | $text[$last] .= $line; |
| | | unset($text[$idx]); |
| | | } |
| | | else { |
| | | $last = $idx; |
| | | } |
| | | } |
| | | else { |
| | | $q = 0; |
| | | if ($line == '-- ') { |
| | | $last = $idx; |
| | | } |
| | | else { |
| | | // remove space-stuffing |
| | | $line = preg_replace('/^\s/', '', $line); |
| | | |
| | | if (isset($text[$last]) && $line |
| | | && $text[$last] != '-- ' |
| | | && $text[$last][strlen($text[$last])-1] == ' ' |
| | | ) { |
| | | $text[$last] .= $line; |
| | | unset($text[$idx]); |
| | | } |
| | | else { |
| | | $text[$idx] = $line; |
| | | $last = $idx; |
| | | } |
| | | } |
| | | } |
| | | $q_level = $q; |
| | | } |
| | | |
| | | return implode("\r\n", $text); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Wrap the given text to comply with RFC 2646 |
| | | * |
| | | * @param string $text Text to wrap |
| | | * @param int $length Length |
| | | * @return string Wrapped text |
| | | */ |
| | | public static function format_flowed($text, $length = 72) |
| | | { |
| | | $text = preg_split('/\r?\n/', $text); |
| | | |
| | | foreach ($text as $idx => $line) { |
| | | if ($line != '-- ') { |
| | | if ($line[0] == '>' && preg_match('/^(>+)/', $line, $regs)) { |
| | | $prefix = $regs[0]; |
| | | $level = strlen($prefix); |
| | | $line = rtrim(substr($line, $level)); |
| | | $line = $prefix . rc_wordwrap($line, $length - $level - 2, " \r\n$prefix "); |
| | | } |
| | | else if ($line) { |
| | | $line = rc_wordwrap(rtrim($line), $length - 2, " \r\n"); |
| | | // space-stuffing |
| | | $line = preg_replace('/(^|\r\n)(From| |>)/', '\\1 \\2', $line); |
| | | } |
| | | |
| | | $text[$idx] = $line; |
| | | } |
| | | } |
| | | |
| | | return implode("\r\n", $text); |
| | | } |
| | | |
| | | } |