/** * 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; }
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); }
function doContactSendAction() { @($sFrom = DevblocksPlatform::importGPC($_POST['from'], 'string', '')); @($sSubject = DevblocksPlatform::importGPC($_POST['subject'], 'string', '')); @($sContent = DevblocksPlatform::importGPC($_POST['content'], 'string', '')); @($sCaptcha = DevblocksPlatform::importGPC($_POST['captcha'], 'string', '')); @($aFieldIds = DevblocksPlatform::importGPC($_POST['field_ids'], 'array', array())); @($aFollowUpQ = DevblocksPlatform::importGPC($_POST['followup_q'], 'array', array())); $fields = DAO_CustomField::getBySource('cerberusweb.fields.source.ticket'); // Load the answers to any situational questions $aFollowUpA = array(); if (is_array($aFollowUpQ)) { foreach ($aFollowUpQ as $idx => $q) { // Only form values we were passed if (!isset($_POST['followup_a_' . $idx])) { continue; } if (is_array($_POST['followup_a_' . $idx])) { @($answer = DevblocksPlatform::importGPC($_POST['followup_a_' . $idx], 'array', array())); $aFollowUpA[$idx] = implode(', ', $answer); } else { @($answer = DevblocksPlatform::importGPC($_POST['followup_a_' . $idx], 'string', '')); $aFollowUpA[$idx] = $answer; } // Translate field values into something human-readable (if needed) if (isset($aFieldIds[$idx]) && !empty($aFieldIds[$idx])) { // Were we given a legit field id? if (null != @($field = $fields[$aFieldIds[$idx]])) { switch ($field->type) { // Translate 'worker' fields into worker name (not ID) case Model_CustomField::TYPE_WORKER: if (null != ($worker = DAO_Worker::getAgent($answer))) { $aFollowUpA[$idx] = $worker->getName(); } break; } // switch } // if } // if } } $umsession = UmPortalHelper::getSession(); $active_user = $umsession->getProperty('sc_login', null); $fingerprint = UmPortalHelper::getFingerprint(); $settings = CerberusSettings::getInstance(); $default_from = $settings->get(CerberusSettings::DEFAULT_REPLY_FROM); $umsession->setProperty('support.write.last_from', $sFrom); $umsession->setProperty('support.write.last_subject', $sSubject); $umsession->setProperty('support.write.last_content', $sContent); $umsession->setProperty('support.write.last_followup_a', $aFollowUpA); $sNature = $umsession->getProperty('support.write.last_nature', ''); $captcha_enabled = DAO_CommunityToolProperty::get(UmPortalHelper::getCode(), self::PARAM_CAPTCHA_ENABLED, 1); $captcha_session = $umsession->getProperty(UmScApp::SESSION_CAPTCHA, '***'); // Subject is required if the field is on the form if (isset($_POST['subject']) && empty($sSubject)) { $umsession->setProperty('support.write.last_error', 'A subject is required.'); DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('portal', UmPortalHelper::getCode(), 'contact', 'step2'))); return; } // Sender and CAPTCHA required if (empty($sFrom) || $captcha_enabled && 0 != strcasecmp($sCaptcha, $captcha_session)) { if (empty($sFrom)) { $umsession->setProperty('support.write.last_error', 'Invalid e-mail address.'); } else { $umsession->setProperty('support.write.last_error', 'What you typed did not match the image.'); } // Need to report the captcha didn't match and redraw the form DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('portal', UmPortalHelper::getCode(), 'contact', 'step2'))); return; } // Dispatch $to = $default_from; $subject = 'Contact me: Other'; $sDispatch = DAO_CommunityToolProperty::get(UmPortalHelper::getCode(), self::PARAM_SITUATIONS, ''); $dispatch = !empty($sDispatch) ? unserialize($sDispatch) : array(); foreach ($dispatch as $k => $v) { if (md5($k) == $sNature) { $to = $v['to']; $subject = 'Contact me: ' . strip_tags($k); break; } } if (!empty($sSubject)) { $subject = $sSubject; } $fieldContent = ''; if (!empty($aFollowUpQ)) { $fieldContent = "\r\n\r\n"; $fieldContent .= "--------------------------------------------\r\n"; if (!empty($sNature)) { $fieldContent .= $subject . "\r\n"; $fieldContent .= "--------------------------------------------\r\n"; } foreach ($aFollowUpQ as $idx => $q) { $answer = isset($aFollowUpA[$idx]) ? $aFollowUpA[$idx] : ''; $fieldContent .= "Q) " . $q . "\r\n" . "A) " . $answer . "\r\n"; if ($idx + 1 < count($aFollowUpQ)) { $fieldContent .= "\r\n"; } } $fieldContent .= "--------------------------------------------\r\n"; "\r\n"; } $message = new CerberusParserMessage(); $message->headers['date'] = date('r'); $message->headers['to'] = $to; $message->headers['subject'] = $subject; $message->headers['message-id'] = CerberusApplication::generateMessageId(); $message->headers['x-cerberus-portal'] = 1; // Sender $fromList = imap_rfc822_parse_adrlist($sFrom, ''); if (empty($fromList) || !is_array($fromList)) { return; // abort with message } $from = array_shift($fromList); $message->headers['from'] = $from->mailbox . '@' . $from->host; $message->body = 'IP: ' . $fingerprint['ip'] . "\r\n\r\n" . $sContent . $fieldContent; // Attachments $attachments_mode = DAO_CommunityToolProperty::get(UmPortalHelper::getCode(), self::PARAM_ATTACHMENTS_MODE, 0); if (0 == $attachments_mode || 1 == $attachments_mode && !empty($active_user)) { if (is_array($_FILES) && !empty($_FILES)) { foreach ($_FILES as $name => $files) { // field[] if (is_array($files['name'])) { foreach ($files['name'] as $idx => $name) { $attach = new ParserFile(); $attach->setTempFile($files['tmp_name'][$idx], 'application/octet-stream'); $attach->file_size = filesize($files['tmp_name'][$idx]); $message->files[$name] = $attach; } } else { $attach = new ParserFile(); $attach->setTempFile($files['tmp_name'], 'application/octet-stream'); $attach->file_size = filesize($files['tmp_name']); $message->files[$files['name']] = $attach; } } } } // Custom Fields if (!empty($aFieldIds)) { foreach ($aFieldIds as $iIdx => $iFieldId) { if (!empty($iFieldId)) { $field =& $fields[$iFieldId]; /* @var $field Model_CustomField */ $value = ""; switch ($field->type) { case Model_CustomField::TYPE_SINGLE_LINE: case Model_CustomField::TYPE_MULTI_LINE: case Model_CustomField::TYPE_URL: @($value = trim($aFollowUpA[$iIdx])); break; case Model_CustomField::TYPE_NUMBER: @($value = $aFollowUpA[$iIdx]); if (!is_numeric($value) || 0 == strlen($value)) { $value = null; } break; case Model_CustomField::TYPE_DATE: if (false !== ($time = strtotime($aFollowUpA[$iIdx]))) { @($value = intval($time)); } break; case Model_CustomField::TYPE_DROPDOWN: @($value = $aFollowUpA[$iIdx]); break; case Model_CustomField::TYPE_MULTI_PICKLIST: @($value = DevblocksPlatform::importGPC($_POST['followup_a_' . $iIdx], 'array', array())); break; case Model_CustomField::TYPE_CHECKBOX: @($value = isset($aFollowUpA[$iIdx]) && !empty($aFollowUpA[$iIdx]) ? 1 : 0); break; case Model_CustomField::TYPE_MULTI_CHECKBOX: @($value = DevblocksPlatform::importGPC($_POST['followup_a_' . $iIdx], 'array', array())); break; case Model_CustomField::TYPE_WORKER: @($value = DevblocksPlatform::importGPC($_POST['followup_a_' . $iIdx], 'integer', 0)); break; } if (is_array($value) && !empty($value) || !is_array($value) && 0 != strlen($value)) { $message->custom_fields[$iFieldId] = $value; } } } } // Parse $ticket_id = CerberusParser::parseMessage($message); // It's possible for the parser to reject the message using pre-filters if (!empty($ticket_id) && null != ($ticket = DAO_Ticket::getTicket($ticket_id))) { $umsession->setProperty('support.write.last_opened', $ticket->mask); } else { $umsession->setProperty('support.write.last_opened', null); } // Clear any errors $umsession->setProperty('support.write.last_nature', null); $umsession->setProperty('support.write.last_nature_string', null); $umsession->setProperty('support.write.last_content', null); $umsession->setProperty('support.write.last_error', null); DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('portal', UmPortalHelper::getCode(), 'contact', 'confirm'))); }