#!/usr/local/bin/php
register();
if (extension_loaded('mbstring')) {
define('MB_ENABLED', true);
// This is required for mb_convert_encoding() to strip invalid characters.
// That's utilized by CI_Utf8, but it's also done for consistency with iconv.
mb_substitute_character('none');
} else {
define('MB_ENABLED', false);
}
// There's an ICONV_IMPL constant, but the PHP manual says that using
// iconv's predefined constants is "strongly discouraged".
define('ICONV_ENABLED', extension_loaded('iconv'));
$GLOBALS['CFG'] = & load_class('Config', 'core');
$GLOBALS['UNI'] = & load_class('Utf8', 'core');
if (file_exists(BASEPATH . 'core/Security.php')) {
$GLOBALS['SEC'] = & load_class('Security', 'core');
}
load_class('Router', 'core');
load_class('Input', 'core');
load_class('Lang', 'core');
require(BASEPATH . 'core/Controller.php');
function &get_instance()
{
return CI_Controller::get_instance();
}
$class = 'CI_Controller';
$instance = new $class();
$fd = fopen('php://stdin', 'r');
$input = '';
while (!feof($fd)) {
$input .= fread($fd, 1024);
}
fclose($fd);
require_once(APPPATH . 'hooks/InitHook.php');
_app_init_load();
$instance->load->model('tickets_model');
$instance->load->helper('files');
$instance->load->helper('func');
$instance->load->helper('misc');
$instance->load->helper('database');
$mailParser = new \ZBateson\MailMimeParser\MailMimeParser();
$message = $mailParser->parse($input);
$body = $message->getHtmlContent();
if (! $body) {
$body = $message->getTextContent();
}
$body = trim($body);
if (! $body) {
$body = 'No message found.';
}
$attachments = [];
$mailAttachments = $message->getAllAttachmentParts();
foreach ($mailAttachments as $attachment) {
$filename = $attachment->getFilename();
$content = $attachment->getContent();
if (!$filename || !$content) {
continue;
}
$attachments[] = [
'data' => $content,
'filename' => sanitize_file_name($filename),
];
}
$subject = $message->getHeaderValue('subject');
$fromemail = $message->getHeaderValue('from');
$fromname = '';
if ($fromHeader = $message->getHeader('from')) {
$fromname = $fromHeader->getPersonName();
}
if (empty($fromname)) {
$fromname = $fromemail;
}
if ($reply_to = $message->getHeaderValue('reply-to')) {
$fromemail = $reply_to;
}
$cc = [];
$toemails = [];
foreach (['to', 'cc', 'bcc'] as $checkHeader) {
$addreses = $message->getHeader($checkHeader);
if ($addreses) {
foreach ($addreses->getAddresses() as $addr) {
$toemails[] = $addr->getEmail();
if ($checkHeader === 'cc') {
$cc[] = $addr->getEmail();
}
}
}
}
$to = implode(',', $toemails);
if (class_exists('EmailReplyParser\EmailReplyParser')
&& get_option('ticket_import_reply_only') === '1'
&& (mb_substr_count($subject, 'FWD:') == 0 && mb_substr_count($subject, 'FW:') == 0)) {
$parsedBody = \EmailReplyParser\EmailReplyParser::parseReply($body);
$parsedBody = trim($parsedBody);
// For some emails this is causing an issue and not returning the email, instead is returning empty string
// In this case, only use parsed email reply if not empty
if (!empty($parsedBody)) {
$body = $parsedBody;
}
}
// Trim message
$body = trim($body);
$body = str_replace(' ', ' ', $body);
// Remove html tags - strips inline styles also
$body = trim(strip_html_tags($body, '
,
, '));
// Once again do security
$body = $instance->security->xss_clean($body);
// Remove duplicate new lines
$body = preg_replace("/[\r\n]+/", "\n", $body);
// new lines with
$body = preg_replace('/\n(\s*\n)+/', '
', $body);
$body = preg_replace('/\n/', '
', $body);
$instance->tickets_model->insert_piped_ticket([
'to' => $to,
'cc' => $cc,
'fromname' => $fromname,
'email' => $fromemail,
'subject' => $subject,
'body' => $body,
'attachments' => $attachments,
]);
?>