function __construct($section, $info, $mime_filename) { $this->mime_filename = $mime_filename; $this->section = $section; $this->info = $info; $this->setTempFile(ParserFile::makeTempFilename(), @$info['content-type']); $this->fp = fopen($this->getTempFile(), 'wb'); if ($this->fp && !empty($this->section) && !empty($this->mime_filename)) { mailparse_msg_extract_part_file($this->section, $this->mime_filename, array($this, "writeCallback")); } @fclose($this->fp); }
/** * Enter description here... * * @param object $mime * @return CerberusParserMessage */ public static function parseMime($mime, $full_filename) { $struct = mailparse_msg_get_structure($mime); $msginfo = mailparse_msg_get_part_data($mime); $message = new CerberusParserMessage(); @($message->encoding = $msginfo['content-charset']); @($message->body_encoding = $message->encoding); // default // Decode headers @($message->headers = $msginfo['headers']); if (is_array($message->headers)) { foreach ($message->headers as $header_name => $header_val) { if (is_array($header_val)) { foreach ($header_val as $idx => $val) { $message->headers[$header_name][$idx] = self::fixQuotePrintableString($val); } } else { $message->headers[$header_name] = self::fixQuotePrintableString($header_val); } } } $settings = DevblocksPlatform::getPluginSettingsService(); $is_attachments_enabled = $settings->get('cerberusweb.core', CerberusSettings::ATTACHMENTS_ENABLED, 1); $attachments_max_size = $settings->get('cerberusweb.core', CerberusSettings::ATTACHMENTS_MAX_SIZE, 10); foreach ($struct as $st) { // echo "PART $st...<br>\r\n"; $section = mailparse_msg_get_part($mime, $st); $info = mailparse_msg_get_part_data($section); // handle parts that shouldn't have a content-name, don't handle twice $handled = 0; if (empty($info['content-name'])) { if ($info['content-type'] == 'text/plain') { $text = mailparse_msg_extract_part_file($section, $full_filename, NULL); if (isset($info['content-charset']) && !empty($info['content-charset'])) { $message->body_encoding = $info['content-charset']; if (@mb_check_encoding($text, $info['content-charset'])) { $text = mb_convert_encoding($text, LANG_CHARSET_CODE, $info['content-charset']); } else { $text = mb_convert_encoding($text, LANG_CHARSET_CODE); } } @($message->body .= $text); unset($text); $handled = 1; } elseif ($info['content-type'] == 'text/html') { @($text = mailparse_msg_extract_part_file($section, $full_filename, NULL)); if (isset($info['content-charset']) && !empty($info['content-charset'])) { if (@mb_check_encoding($text, $info['content-charset'])) { $text = mb_convert_encoding($text, LANG_CHARSET_CODE, $info['content-charset']); } else { $text = mb_convert_encoding($text, LANG_CHARSET_CODE); } } $message->htmlbody .= $text; unset($text); // Add the html part as an attachment // [TODO] Make attaching the HTML part an optional config option (off by default) $tmpname = ParserFile::makeTempFilename(); $html_attach = new ParserFile(); $html_attach->setTempFile($tmpname, 'text/html'); @file_put_contents($tmpname, $message->htmlbody); $html_attach->file_size = filesize($tmpname); $message->files["original_message.html"] = $html_attach; unset($html_attach); $handled = 1; } elseif ($info['content-type'] == 'message/rfc822') { @($message_content = mailparse_msg_extract_part_file($section, $full_filename, NULL)); $message_counter = empty($message_counter) ? 1 : $message_counter + 1; $tmpname = ParserFile::makeTempFilename(); $html_attach = new ParserFile(); $html_attach->setTempFile($tmpname, 'message/rfc822'); @file_put_contents($tmpname, $message_content); $html_attach->file_size = filesize($tmpname); $message->files['inline' . $message_counter . '.msg'] = $html_attach; unset($html_attach); $handled = 1; } } // whether or not it has a content-name, we need to add it as an attachment (if not already handled) if ($handled == 0) { if (false === strpos(strtolower($info['content-type']), 'multipart')) { if (!$is_attachments_enabled) { break; // skip attachment } $attach = new ParseCronFileBuffer($section, $info, $full_filename); // [TODO] This could be more efficient by not even saving in the first place above: // Make sure our attachment is under the max preferred size if (filesize($attach->tmpname) > $attachments_max_size * 1024000) { @unlink($attach->tmpname); break; } // if un-named, call it "unnamed message part" if (!isset($info['content-name']) || isset($info['content-name']) && empty($info['content-name'])) { // or blank $info['content-name'] = 'unnamed_message_part'; } // filenames can be quoted-printable strings, too... $info['content-name'] = self::fixQuotePrintableString($info['content-name']); // content-name is not necessarily unique... if (isset($message->files[$info['content-name']])) { $j = 1; while (isset($message->files[$info['content-name'] . '(' . $j . ')'])) { $j++; } $info['content-name'] = $info['content-name'] . '(' . $j . ')'; } $message->files[$info['content-name']] = $attach; } } } // generate the plaintext part (if necessary) if (empty($message->body) && !empty($message->htmlbody)) { $message->body = CerberusApplication::stripHTML($message->htmlbody); } return $message; }