diff --git a/webui/model/search/message.php b/webui/model/search/message.php index 38bd2b7d..43045115 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -1,7 +1,7 @@ get_raw_message($id); - Zend_Mime_Decode::splitMessageRaw($msg, $headers, $body); + Piler_Mime_Decode::splitMessageRaw($msg, $headers, $body); $has_journal = $this->remove_journal($headers); - $headers = $this->escape_lt_gt_symbols($headers); + $headers = Piler_Mime_Decode::escape_lt_gt_symbols($headers); return array('headers' => $headers, 'has_journal' => $has_journal); } @@ -170,7 +152,7 @@ class ModelSearchMessage extends Model { $p = ''; - $data = $this->escape_lt_gt_symbols($data); + $data = Piler_Mime_Decode::escape_lt_gt_symbols($data); } return $data; @@ -228,54 +210,23 @@ class ModelSearchMessage extends Model { $has_journal = $this->remove_journal($msg); - Zend_Mime_Decode::splitMessage($msg, $headers, $body); - $boundary = $this->get_boundary($headers['content-type']); + Piler_Mime_Decode::splitMessage($msg, $headers, $body); - if(is_array($headers['from'])) { $headers['from'] = $headers['from'][0]; } - if(is_array($headers['to'])) { $headers['to'] = $headers['to'][0]; } - if(is_array($headers['cc'])) { $headers['cc'] = $headers['cc'][0]; } - if(is_array($headers['subject'])) { $headers['subject'] = $headers['subject'][0]; } - if(is_array($headers['date'])) { $headers['date'] = $headers['date'][0]; } - - if(isset($headers['from'])) $from .= $this->escape_lt_gt_symbols($headers['from']); - if(isset($headers['to'])) $to .= $this->escape_lt_gt_symbols($headers['to']); - if(isset($headers['cc'])) $cc .= $this->escape_lt_gt_symbols($headers['cc']); - if(isset($headers['subject'])) $subject .= $this->escape_lt_gt_symbols($headers['subject']); - if(isset($headers['date'])) $date .= $headers['date']; - - $this->message = array( - 'text/plain' => '', - 'text/html' => '' - ); - - $this->extract_textuals_from_mime_parts($headers, $body, $boundary); - - return array('from' => $from, - 'to' => $to, - 'cc' => $cc, - 'subject' => $this->highlight_search_terms($subject, $terms), - 'date' => $date, - 'message' => $this->message['text/html'] ? $this->message['text/html'] : $this->message['text/plain'], - 'has_journal' => $has_journal, - 'verification' => $this->verification - ); - } - - - private function extract_textuals_from_mime_parts($headers = array(), $body = '', $boundary = '') { - $mime_parts = array(); - - if($boundary) { - try { - $mime_parts = Zend_Mime_Decode::splitMessageStruct($body, $boundary); + for($i=0; $igetMessage()); + + if(Piler_Mime_Decode::HEADER_FIELDS[$i] == 'date') { + ${Piler_Mime_Decode::HEADER_FIELDS[$i]} .= $headers[Piler_Mime_Decode::HEADER_FIELDS[$i]]; + } else { + ${Piler_Mime_Decode::HEADER_FIELDS[$i]} .= Piler_Mime_Decode::escape_lt_gt_symbols($headers[Piler_Mime_Decode::HEADER_FIELDS[$i]]); } - } else { - $mime_parts[] = array('header' => $headers, 'body' => $body); } + + Piler_Mime_Decode::parseMessage($msg, $parts); + require_once DIR_SYSTEM . 'helper/HTMLPurifier.standalone.php'; $config = HTMLPurifier_Config::createDefault(); @@ -285,69 +236,33 @@ class ModelSearchMessage extends Model { $purifier = new HTMLPurifier($config); - for($i=0; $i '', - 'encoding' => '' - ); + $this->message = array( + 'text/plain' => '', + 'text/html' => '' + ); - if(isset($mime_parts[$i]['header']['content-type'])) { - $mime['content-type'] = Zend_Mime_Decode::splitContentType($mime_parts[$i]['header']['content-type']); + for($i=0; $imessage['text/html'] = $purifier->purify($body); } - /* - Fix the mime type for some emails having a single textual body part - without the Content-type header. - */ - else if (count($mime_parts) == 1) { - $mime['content-type']['type'] = 'text/plain'; + else { + $this->message['text/plain'] = $body; } - $mime['content-type']['type'] = strtolower($mime['content-type']['type']); - - if(in_array($mime['content-type']['type'], array('multipart/mixed', 'multipart/related', 'multipart/alternative'))) - $this->extract_textuals_from_mime_parts($mime_parts[$i]['header'], $mime_parts[$i]['body'], $mime['content-type']['boundary']); - - if(isset($mime_parts[$i]['header']['content-transfer-encoding'])) - $mime['encoding'] = $mime_parts[$i]['header']['content-transfer-encoding']; - - if(in_array($mime['content-type']['type'], array('text/plain', 'text/html'))) - $this->message[$mime['content-type']['type']] .= $this->fix_mime_body_part($purifier, $mime, $mime_parts[$i]['body']); - } - } - - - private function fix_mime_body_part($purifier, $mime = array(), $body = '') { - if($mime['encoding'] == 'quoted-printable') - $body = Zend_Mime_Decode::decodeQuotedPrintable($body); - - if($mime['encoding'] == 'base64') - $body = base64_decode($body); - - if(strtolower($mime['content-type']['charset']) != 'utf-8') - $body = iconv($mime['content-type']['charset'], 'utf-8' . '//IGNORE', $body); - - - if(strtolower($mime['content-type']['type']) == 'text/plain') { - - $body = $this->escape_lt_gt_symbols($body); - - $body = preg_replace("/\n/", "
\n", $body); - $body = "\n" . $this->print_nicely($body); } - if(strtolower($mime['content-type']['type']) == 'text/html') { - $body = $purifier->purify($body); - } - - return $body; - } - - - private function escape_lt_gt_symbols($s = '') { - $s = preg_replace("//", ">", $s); - - return $s; + return array('from' => $from, + 'to' => $to, + 'cc' => $cc, + 'subject' => $this->highlight_search_terms($subject, $terms), + 'date' => $date, + 'message' => $this->message['text/html'] ? $this->message['text/html'] : $this->message['text/plain'], + 'has_journal' => $has_journal, + 'verification' => $this->verification + ); } @@ -405,25 +320,6 @@ class ModelSearchMessage extends Model { } - private function print_nicely($chunk) { - $k = 0; - $nice_chunk = ""; - - $x = explode(" ", $chunk); - - for($i=0; $i 70){ $nice_chunk .= "\n"; $k = 0; } - } - - return $nice_chunk; - } - - public function NiceSize($size) { if($size < 1000) return "1k"; if($size < 100000) return round($size/1000) . "k"; diff --git a/webui/system/helper/mime.php b/webui/system/helper/mime.php new file mode 100644 index 00000000..4eeb1f29 --- /dev/null +++ b/webui/system/helper/mime.php @@ -0,0 +1,303 @@ + $headers, + 'body' => $body + ); + } + + return; + } + + $parts = self::splitMime($body, $boundary); + + for($i=0; $i $headers, 'body' => $body); + } + else if($headers['content-type']['type'] == "message/rfc822") { + self::parseMessage($body, $result); + } + } + } + } + + + public static function splitMime($body, $boundary) { + $start = 0; + $res = array(); + + $body = self::remove_LF($body); + + // Extract the mime parts excluding the boundary itself + + $p = strpos($body, '--' . $boundary . "\n", $start); + if($p === false) { + // no parts found! + return array(); + } + + // Position after first boundary line + + $start = $p + 3 + strlen($boundary); + + while(($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) { + $res[] = substr($body, $start, $p-$start); + $start = $p + 3 + strlen($boundary); + } + + // No more parts, find end boundary + + $p = strpos($body, '--' . $boundary . '--', $start); + if($p === false) { + return array(); + } + + // The remaining part also needs to be parsed: + $res[] = substr($body, $start, $p - $start); + + return $res; + } + + + public static function splitMessage($message, &$headers, &$body, $EOL = "\n") { + self::splitMessageRaw($message, $headers, $body); + $headers = self::splitHeaders($headers); + } + + + public static function splitMessageRaw($message, &$headers, &$body, $EOL = "\n") { + $headers = []; + $body = ''; + + $message = self::remove_LF($message); + + // Find an empty line between headers and body, otherwise we got a header-only message + + if(strpos($message, $EOL . $EOL)) { + list($headers, $body) = explode($EOL . $EOL, $message, 2); + } + else { + $headers = $message; + } + } + + + public static function splitHeaders($headers) { + $headers = self::headersToArray($headers); + + // normalize header names + foreach ($headers as $name => $header) { + $lower = strtolower($name); + if($lower == $name) { + continue; + } + + unset($headers[$name]); + + if(!isset($headers[$lower])) { + $headers[$lower] = $header; + continue; + } + + if(is_array($headers[$lower])) { + $headers[$lower][] = $header; + continue; + } + + $headers[$lower] = array($headers[$lower], $header); + } + + // Add some default values, if they are missing + + if(!isset($headers['content-type'])) { $headers['content-type'] = 'text/plain'; } + + for($i=0; $i $name) { + $name = strtolower($name); + if($matches[2][$key][0] == '"') { + $split[$name] = substr($matches[2][$key], 1, -1); + } else { + $split[$name] = $matches[2][$key]; + } + } + + return $split; + } + + + public static function remove_LF($message = '') { + return str_replace("\r", "", $message); + //return preg_replace("/\r/", "", $message); + } + + + public static function getBoundary($headers = array()) { + if(isset($headers['content-type']['boundary'])) { + return $headers['content-type']['boundary']; + } + + return ''; + } + + + public static function fixMimeBodyPart($headers = array(), $body = '') { + + if(isset($headers['content-transfer-encoding'])) { + if($headers['content-transfer-encoding'] == 'quoted-printable') { + $body = quoted_printable_decode($body); + } + + if($headers['content-transfer-encoding'] == 'base64') { + $body = base64_decode($body); + } + } + + if(isset($headers['content-type']['charset'])) { + $body = iconv($headers['content-type']['charset'], 'utf-8' . '//IGNORE', $body); + } + + if(strtolower($headers['content-type']['type']) == 'text/plain') { + $body = self::escape_lt_gt_symbols($body); + $body = preg_replace("/\n/", "
\n", $body); + $body = "\n" . self::printNicely($body); + } + + return $body; + } + + + public static function escape_lt_gt_symbols($s = '') { + $s = preg_replace("//", ">", $s); + + return $s; + } + + + public static function printNicely($s = '') { + $k = 0; + $nice = ""; + + $x = explode(" ", $s); + + for($i=0; $i 70){ $nice .= "\n"; $k = 0; } + } + + return $nice; + } + +}