예제 #1
0
<?php

/**
 * Decode note bodies again which have failed to decode unicode html entities
 */
// notes that need to be decoded
$res = $db->getAll('select not_id, not_iss_id, not_is_blocked, not_created_date, not_note, not_full_message from {{%note}} where not_note like ?', array('%&#x00%'));
$render_diff = function ($old, $new) {
    $diff = new Text_Diff(explode(PHP_EOL, $old), explode(PHP_EOL, $new));
    $renderer = new Text_Diff_Renderer_unified();
    return $renderer->render($diff);
};
$now = Date_Helper::getCurrentDateGMT();
foreach ($res as $i => $row) {
    $email = Mime_Helper::decode($row['not_full_message'], true);
    $note = trim($email->body);
    if ($row['not_is_blocked']) {
        $note = Mail_Helper::getCannedBlockedMsgExplanation() . $note;
    }
    $diff = $render_diff($row['not_note'], $note);
    echo "--- issue #{$row['not_iss_id']} {$row['not_created_date']} GMT\n";
    echo "+++ issue #{$row['not_iss_id']} {$now} GMT\n";
    echo $diff;
    $db->query('UPDATE {{%note}} ' . 'SET not_note=? ' . 'WHERE not_id=?', array($note, $row['not_id']));
}
echo count($res), " notes updated\n";
예제 #2
0
 /**
  * Returns the Message-ID from an email. If no message ID is found (Outlook 2003 doesn't
  * generate them in some cases) a "fake" message-id will be calculated.
  *
  * @param string $headers The message headers
  * @param string $body The message body
  * @return string
  */
 public static function getMessageID($headers, $body)
 {
     $full_email = $headers . "\n\n";
     $structure = Mime_Helper::decode($full_email);
     $has_message_id = isset($structure->headers['message-id']);
     // handle cases when there is duplicate message-id header
     // (presented as Array by PEAR Mail_mimeDecode class)
     if ($has_message_id && is_string($structure->headers['message-id'])) {
         return $structure->headers['message-id'];
     } elseif ($has_message_id && is_array($structure->headers['message-id'])) {
         return current($structure->headers['message-id']);
     }
     return self::generateMessageID($headers, $body);
 }
예제 #3
0
function processResult($res, $date_field, $issue_field)
{
    global $prj_id;
    global $usr_id;
    $data = array();
    for ($i = 0; $i < count($res); $i++) {
        if (!Issue::canAccess($res[$i][$issue_field], $usr_id)) {
            continue;
        }
        if (Customer::hasCustomerIntegration($prj_id)) {
            $details = Customer::getDetails($prj_id, Issue::getCustomerID($res[$i][$issue_field]));
            $res[$i]["customer"] = @$details['customer_name'];
        }
        $res[$i]["date"] = Date_API::getFormattedDate($res[$i][$date_field], Date_API::getPreferredTimezone($usr_id));
        // need to decode From:, To: mail headers
        if (isset($res[$i]["sup_from"])) {
            $res[$i]["sup_from"] = Mime_Helper::fixEncoding($res[$i]["sup_from"]);
        }
        if (isset($res[$i]["sup_to"])) {
            $res[$i]["sup_to"] = Mime_Helper::fixEncoding($res[$i]["sup_to"]);
        }
        $data[] = $res[$i];
    }
    return $data;
}
예제 #4
0
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// | You should have received a copy of the GNU General Public License    |
// | along with this program; if not, write to:                           |
// |                                                                      |
// | Free Software Foundation, Inc.                                       |
// | 51 Franklin Street, Suite 330                                          |
// | Boston, MA 02110-1301, USA.                                          |
// +----------------------------------------------------------------------+
// | Authors: João Prado Maia <*****@*****.**>                             |
// +----------------------------------------------------------------------+
require_once dirname(__FILE__) . '/../init.php';
Auth::checkAuthentication(APP_COOKIE);
if (@$_GET['cat'] == 'blocked_email') {
    $email = Note::getBlockedMessage($_GET['note_id']);
} else {
    $email = Support::getFullEmail($_GET['sup_id']);
}
if (!empty($_GET['raw'])) {
    Attachment::outputDownload($email, 'message.eml', strlen($email), 'message/rfc822');
} else {
    if (!empty($_GET['cid'])) {
        list($mimetype, $data) = Mime_Helper::getAttachment($email, $_GET['filename'], $_GET['cid']);
    } else {
        list($mimetype, $data) = Mime_Helper::getAttachment($email, $_GET['filename']);
    }
    Attachment::outputDownload($data, $_GET['filename'], strlen($data), $mimetype);
}
예제 #5
0
 private function processResult(&$data, $date_field, $issue_field)
 {
     $timezone = Date_Helper::getPreferredTimezone($this->usr_id);
     foreach ($data as &$res) {
         if (!Issue::canAccess($res[$issue_field], $this->usr_id)) {
             continue;
         }
         $res['customer'] = null;
         if ($this->crm) {
             try {
                 $customer = $this->crm->getCustomer(Issue::getCustomerID($res[$issue_field]));
                 $res['customer'] = $customer->getName();
             } catch (CRMException $e) {
             }
         }
         $res['date'] = Date_Helper::getFormattedDate($res[$date_field], $timezone);
         // need to decode From:, To: mail headers
         if (isset($res['sup_from'])) {
             $res['sup_from'] = Mime_Helper::fixEncoding($res['sup_from']);
         }
         if (isset($res['sup_to'])) {
             $res['sup_to'] = Mime_Helper::fixEncoding($res['sup_to']);
         }
     }
 }
예제 #6
0
 /**
  * Moves an email from one account to another.
  *
  * @access  public
  * @param   integer $sup_id The ID of the message.
  * @param   integer $current_ema_id The ID of the account the message is currently in.
  * @param   integer $new_ema_id The ID of the account to move the message too.
  * @return  integer -1 if there was error moving the message, 1 otherwise.
  */
 function moveEmail($sup_id, $current_ema_id, $new_ema_id)
 {
     $usr_id = Auth::getUserID();
     $email = Support::getEmailDetails($current_ema_id, $sup_id);
     if (!empty($email['sup_iss_id'])) {
         return -1;
     }
     $info = Email_Account::getDetails($new_ema_id);
     $full_email = Support::getFullEmail($sup_id);
     $structure = Mime_Helper::decode($full_email, true, true);
     $headers = '';
     foreach ($structure->headers as $key => $value) {
         if (is_array($value)) {
             continue;
         }
         $headers .= "{$key}: {$value}\n";
     }
     // handle auto creating issues (if needed)
     $should_create_array = Support::createIssueFromEmail($info, $headers, $email['seb_body'], $email['timestamp'], $email['sup_from'], $email['sup_subject']);
     $should_create_issue = $should_create_array['should_create_issue'];
     $associate_email = $should_create_array['associate_email'];
     $issue_id = $should_create_array['issue_id'];
     $customer_id = $should_create_array['customer_id'];
     if (empty($issue_id)) {
         $issue_id = 0;
     }
     if (empty($customer_id)) {
         $customer_id = 'NULL';
     }
     $sql = "UPDATE\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email\n                SET\n                    sup_ema_id = " . Misc::escapeInteger($new_ema_id) . ",\n                    sup_iss_id = " . Misc::escapeInteger($issue_id) . ",\n                    sup_customer_id = " . Misc::escapeInteger($customer_id) . "\n                WHERE\n                    sup_id = " . Misc::escapeInteger($sup_id) . " AND\n                    sup_ema_id = " . Misc::escapeInteger($current_ema_id);
     $res = $GLOBALS["db_api"]->dbh->query($sql);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
         return -1;
     }
     $row = array('customer_id' => $customer_id, 'issue_id' => $issue_id, 'ema_id' => $new_ema_id, 'message_id' => $email['sup_message_id'], 'date' => $email['timestamp'], 'from' => $email['sup_from'], 'to' => $email['sup_to'], 'cc' => $email['sup_cc'], 'subject' => $email['sup_subject'], 'body' => $email['seb_body'], 'full_email' => $email['seb_full_email'], 'has_attachment' => $email['sup_has_attachment']);
     Workflow::handleNewEmail(Support::getProjectByEmailAccount($new_ema_id), $issue_id, $structure, $row);
     return 1;
 }
예제 #7
0
 /**
  * Check if this email needs to be blocked and if so, block it.
  *
  *
  */
 public static function blockEmailIfNeeded($email)
 {
     if (empty($email['issue_id'])) {
         return false;
     }
     $issue_id = $email['issue_id'];
     $prj_id = Issue::getProjectID($issue_id);
     $sender_email = strtolower(Mail_Helper::getEmailAddress($email['from']));
     list($text_headers, $body) = Mime_Helper::splitHeaderBody($email['full_email']);
     if (Mail_Helper::isVacationAutoResponder($email['headers']) || Notification::isBounceMessage($sender_email) || !self::isAllowedToEmail($issue_id, $sender_email)) {
         // add the message body as a note
         $_POST = array('full_message' => $email['full_email'], 'title' => @$email['headers']['subject'], 'note' => Mail_Helper::getCannedBlockedMsgExplanation($issue_id) . $email['body'], 'message_id' => Mail_Helper::getMessageID($text_headers, $body));
         // avoid having this type of message re-open the issue
         if (Mail_Helper::isVacationAutoResponder($email['headers'])) {
             $closing = true;
             $notify = false;
         } else {
             $closing = false;
             $notify = true;
         }
         $res = Note::insertFromPost(Auth::getUserID(), $issue_id, $email['headers']['from'], false, $closing, $notify, true);
         // associate the email attachments as internal-only files on this issue
         if ($res != -1) {
             self::extractAttachments($issue_id, $email['full_email'], true, $res);
         }
         $_POST['issue_id'] = $issue_id;
         $_POST['from'] = $sender_email;
         // avoid having this type of message re-open the issue
         if (Mail_Helper::isVacationAutoResponder($email['headers'])) {
             $email_type = 'vacation-autoresponder';
         } else {
             $email_type = 'routed';
         }
         Workflow::handleBlockedEmail($prj_id, $issue_id, $_POST, $email_type);
         // try to get usr_id of sender, if not, use system account
         $usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($email['from']), true);
         if (!$usr_id) {
             $usr_id = APP_SYSTEM_USER_ID;
         }
         History::add($issue_id, $usr_id, 'email_blocked', "Email from '{from}' blocked", array('from' => $email['from']));
         return true;
     }
     return false;
 }
예제 #8
0
 /**
  * Method used to close off an issue.
  *
  * @access  public
  * @param   integer $usr_id The user ID
  * @param   integer $issue_id The issue ID
  * @param   bool $send_notification Whether to send a notification about this action or not
  * @param   integer $resolution_id The resolution ID
  * @param   integer $status_id The status ID
  * @param   string $reason The reason for closing this issue
  * @param   string  $send_notification_to Who this notification should be sent too
  * @return  integer 1 if the update worked, -1 otherwise
  */
 function close($usr_id, $issue_id, $send_notification, $resolution_id, $status_id, $reason, $send_notification_to = 'internal')
 {
     global $HTTP_POST_VARS;
     $usr_id = Misc::escapeInteger($usr_id);
     $issue_id = Misc::escapeInteger($issue_id);
     $resolution_id = Misc::escapeInteger($resolution_id);
     $status_id = Misc::escapeInteger($status_id);
     $stmt = "UPDATE\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n                 SET\n                    iss_updated_date='" . Date_API::getCurrentDateGMT() . "',\n                    iss_last_public_action_date='" . Date_API::getCurrentDateGMT() . "',\n                    iss_last_public_action_type='closed',\n                    iss_closed_date='" . Date_API::getCurrentDateGMT() . "',\n";
     if (!empty($resolution_id)) {
         $stmt .= "iss_res_id={$resolution_id},\n";
     }
     $stmt .= "iss_sta_id={$status_id}\n                 WHERE\n                    iss_id={$issue_id}";
     $res = $GLOBALS["db_api"]->dbh->query($stmt);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
         return -1;
     } else {
         $prj_id = Issue::getProjectID($issue_id);
         // record the change
         History::add($issue_id, $usr_id, History::getTypeID('issue_closed'), "Issue updated to status '" . Status::getStatusTitle($status_id) . "' by " . User::getFullName($usr_id));
         if ($send_notification_to == 'all') {
             $from = User::getFromHeader($usr_id);
             $message_id = User::getFromHeader($usr_id);
             $full_email = Support::buildFullHeaders($issue_id, $message_id, $from, '', '', 'Issue closed comments', $reason, '');
             $structure = Mime_Helper::decode($full_email, true, false);
             $email = array('ema_id' => Email_Account::getEmailAccount(), 'issue_id' => $issue_id, 'message_id' => $message_id, 'date' => Date_API::getCurrentDateGMT(), 'subject' => 'Issue closed comments', 'from' => $from, 'has_attachment' => 0, 'body' => $reason, 'full_email' => $full_email, 'headers' => $structure->headers);
             Support::insertEmail($email, $structure, $sup_id, true);
             $ids = $sup_id;
         } else {
             // add note with the reason to close the issue
             $HTTP_POST_VARS['title'] = 'Issue closed comments';
             $HTTP_POST_VARS["note"] = $reason;
             Note::insert($usr_id, $issue_id, false, true, true);
             $ids = false;
         }
         if ($send_notification) {
             if (Customer::hasCustomerIntegration($prj_id)) {
                 // send a special confirmation email when customer issues are closed
                 $stmt = "SELECT\n                                iss_customer_contact_id\n                             FROM\n                                " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n                             WHERE\n                                iss_id={$issue_id}";
                 $customer_contact_id = $GLOBALS["db_api"]->dbh->getOne($stmt);
                 if (!empty($customer_contact_id)) {
                     Customer::notifyIssueClosed($prj_id, $issue_id, $customer_contact_id);
                 }
             }
             // send notifications for the issue being closed
             Notification::notify($issue_id, 'closed', $ids);
         }
         Workflow::handleIssueClosed($prj_id, $issue_id, $send_notification, $resolution_id, $status_id, $reason);
         return 1;
     }
 }
예제 #9
0
 /**
  * Method used to decode the body of a MIME encoded message.
  *
  * @access  public
  * @param   string $input The full body of the message
  * @param   string $encoding The encoding used in the message
  * @return  string The decoded message body
  */
 function decodeBody($input, $encoding = '7bit')
 {
     switch ($encoding) {
         case '7bit':
             return $input;
             break;
         case 'quoted-printable':
             return Mime_Helper::_quotedPrintableDecode($input);
             break;
         case 'base64':
             return base64_decode($input);
             break;
         default:
             return $input;
     }
 }
예제 #10
0
 /**
  * Method used to send an email notification to the sender of an
  * email message that was automatically converted into an issue.
  *
  * @param   integer $prj_id The project ID
  * @param   integer $issue_id The issue ID
  * @param   string $sender The sender of the email message (and the recipient of this notification)
  * @param   string $date The arrival date of the email message
  * @param   string $subject The subject line of the email message
  * @param bool|string $additional_recipient The user who should receive this email who is not the sender of the original email.
  * @return  void
  */
 public static function notifyAutoCreatedIssue($prj_id, $issue_id, $sender, $date, $subject, $additional_recipient = false)
 {
     if (CRM::hasCustomerIntegration($prj_id)) {
         $crm = CRM::getInstance($prj_id);
         $crm->notifyAutoCreatedIssue($issue_id, $sender, $date, $subject);
         $sent = true;
     } else {
         $sent = false;
     }
     if ($sent === false) {
         if ($additional_recipient != false) {
             $recipient = $additional_recipient;
             $is_message_sender = false;
         } else {
             $recipient = $sender;
             $is_message_sender = true;
         }
         $recipient_usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($recipient));
         if (!Workflow::shouldEmailAddress($prj_id, Mail_Helper::getEmailAddress($recipient), $issue_id, 'auto_created')) {
             return;
         }
         $data = Issue::getDetails($issue_id);
         // open text template
         $tpl = new Template_Helper();
         $tpl->setTemplate('notifications/new_auto_created_issue.tpl.text');
         $tpl->assign(array('app_title' => Misc::getToolCaption(), 'data' => $data, 'sender_name' => Mail_Helper::getName($sender), 'recipient_name' => Mail_Helper::getName($recipient), 'is_message_sender' => $is_message_sender));
         // figure out if sender has a real account or not
         $sender_usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($sender), true);
         if (!empty($sender_usr_id) && Issue::canAccess($issue_id, $sender_usr_id)) {
             $can_access = 1;
         } else {
             $can_access = 0;
         }
         $tpl->assign(array('sender_can_access' => $can_access, 'email' => array('date' => $date, 'from' => Mime_Helper::fixEncoding($sender), 'subject' => $subject)));
         // change the current locale
         if (!empty($recipient_usr_id)) {
             Language::set(User::getLang($recipient_usr_id));
         } else {
             Language::set(APP_DEFAULT_LOCALE);
         }
         $text_message = $tpl->getTemplateContents();
         // send email (use PEAR's classes)
         $mail = new Mail_Helper();
         $mail->setTextBody($text_message);
         $mail->setHeaders(Mail_Helper::getBaseThreadingHeaders($issue_id));
         $setup = $mail->getSMTPSettings();
         $from = self::getFixedFromHeader($issue_id, $setup['from'], 'issue');
         $recipient = Mime_Helper::fixEncoding($recipient);
         // TRANSLATORS: %1: $issue_id, %2 = iss_summary
         $subject = ev_gettext('[#%1$s] Issue Created: %2$s', $issue_id, $data['iss_summary']);
         $mail->send($from, $recipient, $subject, 0, $issue_id, 'auto_created_issue');
         Language::restore();
     }
 }
예제 #11
0
<?php

include_once "../../../config.inc.php";
include_once APP_INC_PATH . "db_access.php";
include_once APP_INC_PATH . "class.mime_helper.php";
ini_set("memory_limit", "512M");
$stmt = "SELECT\n            seb_sup_id,\n            seb_full_email\n         FROM\n            " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email_body\n         ORDER BY\n            seb_sup_id";
$res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
foreach ($res as $sup_id => $full_message) {
    $structure = Mime_Helper::decode($full_message, true, true);
    $body = Mime_Helper::getMessageBody($structure);
    $stmt = "UPDATE\n                " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email_body\n             SET\n                seb_body='" . Misc::escapeString($body) . "'\n             WHERE\n                seb_sup_id={$sup_id}";
    $update = $GLOBALS["db_api"]->dbh->query($stmt);
    if (PEAR::isError($update)) {
        echo "<pre>";
        var_dump($update);
        echo "</pre>";
        exit(1);
    }
    echo "fixed email #{$sup_id}<br />";
    flush();
}
echo "complete";
예제 #12
0
 /**
  * Routes a draft to the correct issue.
  *
  * @param   string $full_message The complete draft.
  * @return  mixed   true or array(ERROR_CODE, ERROR_STRING) in case of failure
  */
 public static function route_drafts($full_message)
 {
     // save the full message for logging purposes
     Draft::saveRoutedMessage($full_message);
     if (preg_match('/^(boundary=).*/m', $full_message)) {
         $pattern = "/(Content-Type: multipart\\/)(.+); ?\r?\n(boundary=)(.*)\$/im";
         $replacement = '$1$2; $3$4';
         $full_message = preg_replace($pattern, $replacement, $full_message);
     }
     // need some validation here
     if (empty($full_message)) {
         return array(self::EX_NOINPUT, ev_gettext('Error: The email message was empty.') . "\n");
     }
     // remove the reply-to: header
     if (preg_match('/^(reply-to:).*/im', $full_message)) {
         $full_message = preg_replace("/^(reply-to:).*\n/im", '', $full_message, 1);
     }
     // check if the draft interface is even supposed to be enabled
     $setup = Setup::get();
     if ($setup['draft_routing']['status'] != 'enabled') {
         return array(self::EX_CONFIG, ev_gettext('Error: The email draft interface is disabled.') . "\n");
     }
     if (empty($setup['draft_routing']['address_prefix'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address prefix.') . "\n");
     }
     if (empty($setup['draft_routing']['address_host'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address domain.') . "\n");
     }
     $structure = Mime_Helper::decode($full_message, true, false);
     // find which issue ID this email refers to
     if (isset($structure->headers['to'])) {
         $issue_id = self::getMatchingIssueIDs($structure->headers['to'], 'draft');
     }
     // validation is always a good idea
     if (empty($issue_id) and isset($structure->headers['cc'])) {
         // we need to try the Cc header as well
         $issue_id = self::getMatchingIssueIDs($structure->headers['cc'], 'draft');
     }
     if (empty($issue_id)) {
         return array(self::EX_DATAERR, ev_gettext('Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.') . "\n");
     }
     $prj_id = Issue::getProjectID($issue_id);
     // check if the sender is allowed in this issue' project and if it is an internal user
     $sender_email = strtolower(Mail_Helper::getEmailAddress($structure->headers['from']));
     $sender_usr_id = User::getUserIDByEmail($sender_email, true);
     if (!empty($sender_usr_id)) {
         $sender_role = User::getRoleByUser($sender_usr_id, $prj_id);
         if ($sender_role < User::ROLE_USER) {
             return array(self::EX_NOPERM, ev_gettext("Error: The sender of this email is not allowed in the project associated with issue #{$issue_id}.") . "\n");
         }
     }
     AuthCookie::setAuthCookie(User::getUserIDByEmail($sender_email));
     AuthCookie::setProjectCookie($prj_id);
     $body = $structure->body;
     Draft::saveEmail($issue_id, @$structure->headers['to'], @$structure->headers['cc'], @$structure->headers['subject'], $body, false, false, false);
     // XXX: need to handle attachments coming from drafts as well?
     $usr_id = Auth::getUserID();
     History::add($issue_id, $usr_id, 'draft_routed', 'Draft routed from {from}', array('from' => $structure->headers['from']));
     return true;
 }
예제 #13
0
 /**
  * Method used to send an email notification to the sender of an
  * email message that was automatically converted into an issue.
  *
  * @access  public
  * @param   integer $prj_id The project ID
  * @param   integer $issue_id The issue ID
  * @param   string $sender The sender of the email message (and the recipient of this notification)
  * @param   string $date The arrival date of the email message
  * @param   string $subject The subject line of the email message
  * @return  void
  */
 function notifyAutoCreatedIssue($prj_id, $issue_id, $sender, $date, $subject)
 {
     if (Customer::hasCustomerIntegration($prj_id)) {
         Customer::notifyAutoCreatedIssue($prj_id, $issue_id, $sender, $date, $subject);
     } else {
         if (!Workflow::shouldEmailAddress($prj_id, Mail_API::getEmailAddress($sender))) {
             return;
         }
         $data = Issue::getDetails($issue_id);
         // open text template
         $tpl = new Template_API();
         $tpl->setTemplate('notifications/new_auto_created_issue.tpl.text');
         $tpl->bulkAssign(array("app_title" => Misc::getToolCaption(), "data" => $data, "sender_name" => Mail_API::getName($sender)));
         // figure out if sender has a real account or not
         $sender_usr_id = User::getUserIDByEmail(Mail_API::getEmailAddress($sender));
         if (!empty($sender_usr_id) && Issue::canAccess($issue_id, $sender_usr_id)) {
             $can_access = 1;
         } else {
             $can_access = 0;
         }
         $tpl->assign(array('sender_can_access' => $can_access, 'email' => array('date' => $date, 'from' => Mime_Helper::fixEncoding($sender), 'subject' => $subject)));
         $text_message = $tpl->getTemplateContents();
         // send email (use PEAR's classes)
         $mail = new Mail_API();
         $mail->setTextBody($text_message);
         $mail->setHeaders(Mail_API::getBaseThreadingHeaders($issue_id));
         $setup = $mail->getSMTPSettings();
         $from = Notification::getFixedFromHeader($issue_id, $setup["from"], 'issue');
         $sender = Mime_Helper::fixEncoding($sender);
         $mail->send($from, $sender, "[#{$issue_id}] Issue Created: " . $data['iss_summary'], 0, $issue_id, 'auto_created_issue');
     }
 }
예제 #14
0
include_once APP_INC_PATH . "class.support.php";
include_once APP_INC_PATH . "db_access.php";
$tpl = new Template_API();
$tpl->setTemplate("view_email.tpl.html");
Auth::checkAuthentication(APP_COOKIE, 'index.php?err=5', true);
$email = Support::getEmailDetails($HTTP_GET_VARS["ema_id"], $HTTP_GET_VARS["id"]);
$email["message"] = str_replace("&amp;nbsp;", "&nbsp;", $email["message"]);
$issue_id = Support::getIssueFromEmail($HTTP_GET_VARS["id"]);
if (!Issue::canAccess($issue_id, Auth::getUserID())) {
    $tpl->setTemplate("permission_denied.tpl.html");
    $tpl->displayTemplate();
    exit;
}
$tpl->bulkAssign(array("email" => $email, "issue_id" => $issue_id, 'extra_title' => "Email #" . $HTTP_GET_VARS['id'] . ": " . $email['sup_subject'], 'email_accounts' => Email_Account::getAssocList(array_keys(Project::getAssocList(Auth::getUserID())), true)));
if (@$HTTP_GET_VARS['cat'] == 'list_emails') {
    $sides = Support::getListingSides($HTTP_GET_VARS["id"]);
    $tpl->assign(array('previous' => $sides['previous'], 'next' => $sides['next']));
} elseif (@$HTTP_GET_VARS['cat'] == 'move_email' && Auth::getCurrentRole() >= User::getRoleID("Standard User")) {
    $res = Support::moveEmail(@$HTTP_GET_VARS['id'], @$HTTP_GET_VARS['ema_id'], @$HTTP_GET_VARS['new_ema_id']);
    $tpl->assign("move_email_result", $res);
    $tpl->assign("current_user_prefs", Prefs::get(Auth::getUserID()));
} else {
    $sides = Support::getIssueSides($issue_id, $HTTP_GET_VARS["id"]);
    $tpl->assign(array('previous' => $sides['previous'], 'next' => $sides['next']));
}
// set the page charset to whatever is set on this email
$charset = Mime_Helper::getCharacterSet($email['seb_full_email']);
if (!empty($charset)) {
    header("Content-Type: text/html; charset=" . $charset);
}
$tpl->displayTemplate();
예제 #15
0
 public static function getMessageRecipients($types, $type_id)
 {
     if (!is_array($types)) {
         $types = array($types);
     }
     $types_list = DB_Helper::buildList($types);
     $sql = "SELECT\n                    maq_recipient\n                FROM\n                    {{%mail_queue}}\n                WHERE\n                    maq_type IN ({$types_list}) AND\n                    maq_type_id = ?";
     $params = $types;
     $params[] = $type_id;
     try {
         $res = DB_Helper::getInstance()->getColumn($sql, $params);
     } catch (DbException $e) {
         return false;
     }
     foreach ($res as &$row) {
         // FIXME: what does quote stripping fix here
         $row = Mime_Helper::decodeAddress(str_replace('"', '', $row));
     }
     return $res;
 }
예제 #16
0
 /**
  * Returns the Message-ID from an email. If no message ID is found (Outlook 2003 doesn't
  * generate them in some cases) a "fake" message-id will be calculated.
  *
  * @param   string $headers The message headers
  * @param   string $body The message body
  */
 public static function getMessageID($headers, $body)
 {
     $full_email = $headers . "\n\n";
     $structure = Mime_Helper::decode($full_email);
     $has_message_id = isset($structure->headers['message-id']);
     // handle cases when there is duplicate message-id header
     // (presented as Array by PEAR Mail_mimeDecode class)
     if ($has_message_id && is_string($structure->headers['message-id'])) {
         return $structure->headers['message-id'];
     } elseif ($has_message_id && is_array($structure->headers['message-id'])) {
         return current($structure->headers['message-id']);
     }
     // no match, calculate hash to make fake message ID
     $first = base_convert(md5($headers), 10, 36);
     $second = base_convert(md5($body), 10, 36);
     return '<eventum.md5.' . $first . '.' . $second . '@' . APP_HOSTNAME . '>';
 }
예제 #17
0
 *
 * @copyright (c) Eventum Team
 * @license GNU General Public License, version 2 or later (GPL-2+)
 *
 * For the full copyright and license information,
 * please see the COPYING and AUTHORS files
 * that were distributed with this source code.
 */
/*
 * Update database fields with fixEncoding instead doing that runtime
 */
/** @var DbInterface $db */
$logger = Logger::getInstance('db');
$res = $db->getAll("SELECT sup_id,sup_subject,sup_from,sup_to,sup_cc FROM {{%support_email}} WHERE concat(sup_subject,sup_from,sup_to,sup_cc) LIKE '%=?%'");
$changed = 0;
foreach ($res as $idx => $row) {
    $params = array();
    foreach ($row as $k => $v) {
        $params[$k] = Mime_Helper::fixEncoding($v);
    }
    if ($row == $params) {
        $logger->warning("sup_id={$row['sup_id']} no changes", array('sup_id' => $row['sup_id'], 'old' => $row));
        continue;
    }
    $logger->info("updated sup_id={$row['sup_id']}", array('sup_id' => $row['sup_id'], 'old' => $row, 'new' => $params));
    $params[] = $row['sup_id'];
    $db->query('UPDATE {{%support_email}} SET ' . DB_Helper::buildSet($row) . ' WHERE sup_id=?', $params);
    $changed++;
}
$count = count($res);
$logger->info("Updated {$changed} out of {$count} entries");
예제 #18
0
 function getMessageRecipients($type, $type_id)
 {
     $sql = "SELECT\n                    maq_recipient\n                FROM\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "mail_queue\n                WHERE\n                    maq_type = '" . Misc::escapeString($type) . "' AND\n                    maq_type_id = " . Misc::escapeInteger($type_id);
     $res = $GLOBALS["db_api"]->dbh->getCol($sql);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
         return false;
     } else {
         for ($i = 0; $i < count($res); $i++) {
             $res[$i] = Mime_Helper::decodeAddress(str_replace('"', '', $res[$i]));
         }
         return $res;
     }
 }
예제 #19
0
// | GNU General Public License for more details.                         |
// |                                                                      |
// | You should have received a copy of the GNU General Public License    |
// | along with this program; if not, write to:                           |
// |                                                                      |
// | Free Software Foundation, Inc.                                       |
// | 59 Temple Place - Suite 330                                          |
// | Boston, MA 02111-1307, USA.                                          |
// +----------------------------------------------------------------------+
// | Authors: João Prado Maia <*****@*****.**>                             |
// +----------------------------------------------------------------------+
//
// @(#) $Id: s.get_attachment.php 1.5 03/09/30 18:07:03-00:00 jpradomaia $
//
include_once "config.inc.php";
include_once APP_INC_PATH . "class.auth.php";
include_once APP_INC_PATH . "class.support.php";
include_once APP_INC_PATH . "class.mime_helper.php";
include_once APP_INC_PATH . "db_access.php";
Auth::checkAuthentication(APP_COOKIE);
if (@$HTTP_GET_VARS['cat'] == 'blocked_email') {
    $email = Note::getBlockedMessage($HTTP_GET_VARS["note_id"]);
} else {
    $email = Support::getFullEmail($HTTP_GET_VARS["sup_id"]);
}
if (!empty($HTTP_GET_VARS['cid'])) {
    list($mimetype, $data) = Mime_Helper::getAttachment($email, $HTTP_GET_VARS["filename"], $HTTP_GET_VARS["cid"]);
} else {
    list($mimetype, $data) = Mime_Helper::getAttachment($email, $HTTP_GET_VARS["filename"]);
}
Attachment::outputDownload($data, $HTTP_GET_VARS["filename"], strlen($data), $mimetype);
예제 #20
0
 /**
  * Returns the text message body.
  *
  * @return string The message body
  * @see Mime_Helper::getMessageBody()
  */
 public function getMessageBody()
 {
     $parts = array();
     foreach ($this as $part) {
         $ctype = $part->getHeaderField('Content-Type');
         $disposition = $part->getHeaderField('Content-Disposition');
         $filename = $part->getHeaderField('Content-Disposition', 'filename');
         $is_attachment = $disposition == 'attachment' || $filename;
         $charset = $part->getHeaderField('Content-Type', 'charset');
         switch ($ctype) {
             case 'text/plain':
                 if (!$is_attachment) {
                     $format = $part->getHeaderField('Content-Type', 'format');
                     $delsp = $part->getHeaderField('Content-Type', 'delsp');
                     $text = Mime_Helper::convertString($part->getContent(), $charset);
                     if ($format == 'flowed') {
                         $text = Mime_Helper::decodeFlowedBodies($text, $delsp);
                     }
                     $parts['text'][] = $text;
                 }
                 break;
             case 'text/html':
                 if (!$is_attachment) {
                     $parts['html'][] = Mime_Helper::convertString($part->getContent(), $charset);
                 }
                 break;
                 // special case for Apple Mail
             // special case for Apple Mail
             case 'text/enriched':
                 if (!$is_attachment) {
                     $parts['html'][] = Mime_Helper::convertString($part->getContent(), $charset);
                 }
                 break;
             default:
                 // avoid treating forwarded messages as attachments
                 $is_attachment |= $disposition == 'inline' && $ctype != 'message/rfc822';
                 // handle inline images
                 $type = current(explode('/', $ctype));
                 $is_attachment |= $type == 'image';
                 if (!$is_attachment) {
                     $parts['text'][] = $part->getContent();
                 }
         }
     }
     // now we have $parts with type 'text' and type 'html'
     if (isset($parts['text'])) {
         return implode("\n\n", $parts['text']);
     }
     if (isset($parts['html'])) {
         $str = implode("\n\n", $parts['html']);
         // hack for inotes to prevent content from being displayed all on one line.
         $str = str_replace('</DIV><DIV>', "\n", $str);
         $str = str_replace(array('<br>', '<br />', '<BR>', '<BR />'), "\n", $str);
         // XXX: do we also need to do something here about base64 encoding?
         $str = strip_tags($str);
         // convert html entities. this should be done after strip tags
         $str = html_entity_decode($str, ENT_QUOTES, APP_CHARSET);
         return $str;
     }
     return null;
 }
<?php

/*
 * This file is part of the Eventum (Issue Tracking System) package.
 *
 * @copyright (c) Eventum Team
 * @license GNU General Public License, version 2 or later (GPL-2+)
 *
 * For the full copyright and license information,
 * please see the COPYING and AUTHORS files
 * that were distributed with this source code.
 */
/**
 * Decode attachment filenames from QuotedPrintable MIME encoding.
 * Also set Untitled.jpg to unnamed attachments (Usually inline).
 */
/** @var DbInterface $db */
// Attachments that need to be decoded
$res = $db->getAll('SELECT iaf_id, iaf_filename FROM {{%issue_attachment_file}} WHERE iaf_filename LIKE ?', array('%=?%'));
foreach ($res as $idx => $row) {
    $iaf_filename = Mime_Helper::decodeQuotedPrintable($row['iaf_filename']);
    $db->query('UPDATE {{%issue_attachment_file}} ' . 'SET iaf_filename=? ' . 'WHERE iaf_id=?', array($iaf_filename, $row['iaf_id']));
}
// Unnamed attachments
$res = $db->getAll("SELECT iaf_id, iaf_filetype FROM {{%issue_attachment_file}} WHERE iaf_filename=''");
foreach ($res as $idx => $row) {
    list($type, $ext) = explode('/', $row['iaf_filetype']);
    $iaf_filename = ev_gettext('Untitled.%s', $ext);
    $db->query('UPDATE {{%issue_attachment_file}} ' . 'SET iaf_filename=? ' . 'WHERE iaf_id=?', array($iaf_filename, $row['iaf_id']));
}
예제 #22
0
 /**
  * Checks to make sure In-Reply-To and References headers are correct.
  *
  */
 function rewriteThreadingHeaders($issue_id, $full_email, $headers, $type = 'email')
 {
     list($text_headers, $body) = Mime_Helper::splitHeaderBody($full_email);
     if ($type == 'note') {
         $class = 'Note';
     } else {
         $class = 'Support';
     }
     $msg_id = Mail_API::getMessageID($text_headers, $body);
     // check if the In-Reply-To header exists and if so, does it relate to a message stored in Eventum
     // if it does not, set new In-Reply-To header
     $reference_msg_id = Mail_API::getReferenceMessageID($text_headers);
     $reference_issue_id = false;
     if (!empty($reference_msg_id)) {
         // check if referenced msg id is associated with this issue
         $reference_issue_id = call_user_func(array($class, 'getIssueByMessageID'), $reference_msg_id);
     }
     if (empty($reference_msg_id) || $reference_issue_id != $issue_id) {
         $reference_msg_id = Issue::getRootMessageID($issue_id);
     }
     $references = Mail_API::getReferences($issue_id, $reference_msg_id, $type);
     // now the fun part, re-writing the email headers
     if (empty($headers['message-id'])) {
         // add Message-ID since it doesn't exist (curses on Outlook 2003)
         $text_headers .= "\r\nMessage-ID: {$msg_id}";
         $headers['message-id'] = $msg_id;
     }
     if (preg_match('/^In-Reply-To: (.*)/mi', $text_headers) > 0) {
         // replace existing header
         $text_headers = preg_replace('/^In-Reply-To: (.*)/mi', 'In-Reply-To: ' . $reference_msg_id, $text_headers, 1);
     } else {
         // add new header after message ID
         $text_headers = preg_replace('/^Message-ID: (.*)$/mi', "Message-ID: \$1\r\nIn-Reply-To: {$reference_msg_id}", $text_headers, 1);
     }
     $headers['in-reply-to'] = $reference_msg_id;
     if (preg_match('/^References: (.*)/mi', $text_headers) > 0) {
         // replace existing header
         $text_headers = preg_replace('/^References: (.*)/mi', 'References: ' . Mail_API::fold(join(' ', $references)), $text_headers, 1);
     } else {
         // add new header after In-Reply-To
         $text_headers = preg_replace('/^In-Reply-To: (.*)$/mi', "In-Reply-To: \$1\r\nReferences: " . Mail_API::fold(join(' ', $references)), $text_headers, 1);
     }
     $headers['references'] = Mail_API::fold(join(' ', $references));
     return array($text_headers . "\r\n\r\n" . $body, $headers);
 }
예제 #23
0
 /**
  * Method used to close off an issue.
  *
  * @param   integer $usr_id The user ID
  * @param   integer $issue_id The issue ID
  * @param   bool $send_notification Whether to send a notification about this action or not
  * @param   integer $resolution_id The resolution ID
  * @param   integer $status_id The status ID
  * @param   string $reason The reason for closing this issue
  * @param   string  $send_notification_to Who this notification should be sent too
  * @return  integer 1 if the update worked, -1 otherwise
  */
 public static function close($usr_id, $issue_id, $send_notification, $resolution_id, $status_id, $reason, $send_notification_to = 'internal')
 {
     $usr_id = (int) $usr_id;
     $issue_id = (int) $issue_id;
     $resolution_id = (int) $resolution_id;
     $status_id = (int) $status_id;
     $params = array('iss_updated_date' => Date_Helper::getCurrentDateGMT(), 'iss_last_public_action_date' => Date_Helper::getCurrentDateGMT(), 'iss_last_public_action_type' => 'closed', 'iss_closed_date' => Date_Helper::getCurrentDateGMT(), 'iss_sta_id' => $status_id);
     if (!empty($resolution_id)) {
         $params['iss_res_id'] = $resolution_id;
     }
     $stmt = 'UPDATE {{%issue}} SET ' . DB_Helper::buildSet($params) . ' WHERE iss_id=?';
     $params[] = $issue_id;
     try {
         DB_Helper::getInstance()->query($stmt, $params);
     } catch (DbException $e) {
         return -1;
     }
     $prj_id = self::getProjectID($issue_id);
     // record the change
     History::add($issue_id, $usr_id, 'issue_closed', "Issue updated to status '{status}' by {user}", array('status' => Status::getStatusTitle($status_id), 'user' => User::getFullName($usr_id)));
     if ($send_notification_to == 'all') {
         $from = User::getFromHeader($usr_id);
         $message_id = User::getFromHeader($usr_id);
         $full_email = Support::buildFullHeaders($issue_id, $message_id, $from, '', '', 'Issue closed comments', $reason, '');
         $structure = Mime_Helper::decode($full_email, true, false);
         $email = array('ema_id' => Email_Account::getEmailAccount(self::getProjectID($issue_id)), 'issue_id' => $issue_id, 'message_id' => $message_id, 'date' => Date_Helper::getCurrentDateGMT(), 'subject' => 'Issue closed comments', 'from' => $from, 'has_attachment' => 0, 'body' => $reason, 'full_email' => $full_email, 'headers' => $structure->headers);
         $sup_id = null;
         Support::insertEmail($email, $structure, $sup_id, true);
         $ids = $sup_id;
     } else {
         // add note with the reason to close the issue
         $_POST['title'] = 'Issue closed comments';
         $_POST['note'] = $reason;
         Note::insertFromPost($usr_id, $issue_id, false, true, true, $send_notification);
         $ids = false;
     }
     if ($send_notification) {
         if (CRM::hasCustomerIntegration($prj_id)) {
             $crm = CRM::getInstance($prj_id);
             // send a special confirmation email when customer issues are closed
             $stmt = 'SELECT
                         iss_customer_contact_id
                      FROM
                         {{%issue}}
                      WHERE
                         iss_id=?';
             $customer_contact_id = DB_Helper::getInstance()->getOne($stmt, array($issue_id));
             if (!empty($customer_contact_id)) {
                 try {
                     $contact = $crm->getContact($customer_contact_id);
                     $contact->notifyIssueClosed($issue_id, $reason);
                 } catch (CRMException $e) {
                 }
             }
         }
         // send notifications for the issue being closed
         Notification::notify($issue_id, 'closed', $ids);
     }
     Workflow::handleIssueClosed($prj_id, $issue_id, $send_notification, $resolution_id, $status_id, $reason, $usr_id);
     return 1;
 }
예제 #24
0
 /**
  * Method used to get the list of changes made against a specific issue.
  *
  * @access  public
  * @param   integer $iss_id The issue ID
  * @param   string $order_by The order to sort the history
  * @return  array The list of changes
  */
 function getListing($iss_id, $order_by = 'DESC')
 {
     $stmt = "SELECT\n                    *\n                 FROM\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_history,\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "history_type\n                 WHERE\n                    htt_id = his_htt_id AND\n                    his_is_hidden != 1 AND\n                    his_iss_id=" . Misc::escapeInteger($iss_id) . " AND\n                    htt_role <= " . Auth::getCurrentRole() . "\n                 ORDER BY\n                    his_id {$order_by}";
     $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
         return "";
     } else {
         for ($i = 0; $i < count($res); $i++) {
             $res[$i]["his_created_date"] = Date_API::getFormattedDate($res[$i]["his_created_date"]);
             $res[$i]["his_summary"] = Mime_Helper::fixEncoding($res[$i]["his_summary"]);
         }
         return $res;
     }
 }
예제 #25
0
 /**
  * Converts a note to a draft or an email
  *
  * @param int $note_id The id of the note
  * @param string $target What the note should be converted too (email, etc)
  * @param bool $authorize_sender If $authorize_sender If the sender should be added to authorized senders list.
  * @return int
  */
 public static function convertNote($note_id, $target, $authorize_sender = false)
 {
     $issue_id = self::getIssueID($note_id);
     $email_account_id = Email_Account::getEmailAccount();
     $blocked_message = self::getBlockedMessage($note_id);
     $unknown_user = self::getUnknownUser($note_id);
     $structure = Mime_Helper::decode($blocked_message, true, true);
     $body = $structure->body;
     $sender_email = strtolower(Mail_Helper::getEmailAddress($structure->headers['from']));
     $current_usr_id = Auth::getUserID();
     if ($target == 'email') {
         if (Mime_Helper::hasAttachments($structure)) {
             $has_attachments = 1;
         } else {
             $has_attachments = 0;
         }
         list($blocked_message, $headers) = Mail_Helper::rewriteThreadingHeaders($issue_id, $blocked_message, @$structure->headers);
         $t = array('issue_id' => $issue_id, 'ema_id' => $email_account_id, 'message_id' => @$structure->headers['message-id'], 'date' => Date_Helper::getCurrentDateGMT(), 'from' => @$structure->headers['from'], 'to' => @$structure->headers['to'], 'cc' => @$structure->headers['cc'], 'subject' => @$structure->headers['subject'], 'body' => @$body, 'full_email' => @$blocked_message, 'has_attachment' => $has_attachments, 'headers' => $headers);
         // need to check for a possible customer association
         if (!empty($structure->headers['from'])) {
             $details = Email_Account::getDetails($email_account_id);
             // check from the associated project if we need to lookup any customers by this email address
             if (CRM::hasCustomerIntegration($details['ema_prj_id'])) {
                 $crm = CRM::getInstance($details['ema_prj_id']);
                 // check for any customer contact association
                 try {
                     $contact = $crm->getContactByEmail($sender_email);
                     $issue_contract = $crm->getContract(Issue::getContractID($issue_id));
                     if ($contact->canAccessContract($issue_contract)) {
                         $t['customer_id'] = $issue_contract->getCustomerID();
                     }
                 } catch (CRMException $e) {
                 }
             }
         }
         if (empty($t['customer_id'])) {
             $update_type = 'staff response';
             $t['customer_id'] = null;
         } else {
             $update_type = 'customer action';
         }
         $res = Support::insertEmail($t, $structure, $sup_id);
         if ($res != -1) {
             Support::extractAttachments($issue_id, $structure);
             // notifications about new emails are always external
             $internal_only = false;
             // special case when emails are bounced back, so we don't want to notify the customer about those
             if (Notification::isBounceMessage($sender_email)) {
                 $internal_only = true;
             }
             Notification::notifyNewEmail($current_usr_id, $issue_id, $t, $internal_only, false, '', $sup_id);
             Issue::markAsUpdated($issue_id, $update_type);
             self::remove($note_id, false);
             History::add($issue_id, $current_usr_id, 'note_converted_email', 'Note converted to e-mail (from: {from}) by {user}', array('from' => @$structure->headers['from'], 'user' => User::getFullName($current_usr_id)));
             // now add sender as an authorized replier
             if ($authorize_sender) {
                 Authorized_Replier::manualInsert($issue_id, @$structure->headers['from']);
             }
         }
         return $res;
     }
     // save message as a draft
     $res = Draft::saveEmail($issue_id, $structure->headers['to'], $structure->headers['cc'], $structure->headers['subject'], $body, false, $unknown_user);
     // remove the note, if the draft was created successfully
     if ($res) {
         self::remove($note_id, false);
         $usr_id = $current_usr_id;
         History::add($issue_id, $usr_id, 'note_converted_draft', 'Note converted to draft (from: {from}) by {user}', array('from' => @$structure->headers['from'], 'user' => User::getFullName($current_usr_id)));
     }
     return $res;
 }
예제 #26
0
 /**
  * Routes a draft to the correct issue.
  *
  * @param   string $full_message The complete draft.
  */
 function route_drafts($full_message)
 {
     global $HTTP_POST_VARS;
     // save the full message for logging purposes
     Draft::saveRoutedMessage($full_message);
     if (preg_match("/^(boundary=).*/m", $full_message)) {
         $pattern = "/(Content-Type: multipart\\/)(.+); ?\r?\n(boundary=)(.*)\$/im";
         $replacement = '$1$2; $3$4';
         $full_message = preg_replace($pattern, $replacement, $full_message);
     }
     // need some validation here
     if (empty($full_message)) {
         return array(66, "Error: The email message was empty.\n");
     }
     //
     // DON'T EDIT ANYTHING BELOW THIS LINE
     //
     // remove the reply-to: header
     if (preg_match("/^(reply-to:).*/im", $full_message)) {
         $full_message = preg_replace("/^(reply-to:).*\n/im", '', $full_message, 1);
     }
     // check if the draft interface is even supposed to be enabled
     $setup = Setup::load();
     if (@$setup['draft_routing']['status'] != 'enabled') {
         return array(78, "Error: The email draft interface is disabled.\n");
     }
     $prefix = $setup['draft_routing']['address_prefix'];
     // escape plus signs so '*****@*****.**' becomes a valid address
     $prefix = str_replace('+', '\\+', $prefix);
     $mail_domain = quotemeta($setup['draft_routing']['address_host']);
     if (empty($prefix)) {
         return array(78, "Error: Please configure the email address prefix.\n");
     }
     if (empty($mail_domain)) {
         return array(78, "Error: Please configure the email address domain.\n");
     }
     $structure = Mime_Helper::decode($full_message, true, false);
     // find which issue ID this email refers to
     @preg_match("/{$prefix}(\\d*)@{$mail_domain}/i", $structure->headers['to'], $matches);
     @($issue_id = $matches[1]);
     // validation is always a good idea
     if (empty($issue_id)) {
         // we need to try the Cc header as well
         @preg_match("/{$prefix}(\\d*)@{$mail_domain}/i", $structure->headers['cc'], $matches);
         if (!empty($matches[1])) {
             $issue_id = $matches[1];
         } else {
             return array(65, "Error: The routed draft had no associated Eventum issue ID or had an invalid recipient address.\n");
         }
     }
     $prj_id = Issue::getProjectID($issue_id);
     // check if the sender is allowed in this issue' project and if it is an internal user
     $users = Project::getUserEmailAssocList($prj_id, 'active', User::getRoleID('Customer'));
     $sender_email = strtolower(Mail_API::getEmailAddress($structure->headers['from']));
     $user_emails = array_map('strtolower', array_values($users));
     if (!in_array($sender_email, $user_emails)) {
         return array(77, "Error: The sender of this email is not allowed in the project associated with issue #{$issue_id}.\n");
     }
     Auth::createFakeCookie(User::getUserIDByEmail($sender_email), $prj_id);
     $body = Mime_Helper::getMessageBody($structure);
     Draft::saveEmail($issue_id, @$structure->headers['to'], @$structure->headers['cc'], @$structure->headers['subject'], $body, false, false, false);
     // XXX: need to handle attachments coming from drafts as well?
     History::add($issue_id, Auth::getUserID(), History::getTypeID('draft_routed'), "Draft routed from " . $structure->headers['from']);
     return true;
 }