Exemple #1
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;
 }
 /**
  * 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);
 }
 /**
  * Checks to make sure In-Reply-To and References headers are correct.
  *
  */
 public static function rewriteThreadingHeaders($issue_id, $full_email, $headers, $type = 'email')
 {
     list($text_headers, $body) = Mime_Helper::splitHeaderBody($full_email);
     $msg_id = self::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 = self::getReferenceMessageID($text_headers);
     $reference_issue_id = false;
     if (!empty($reference_msg_id)) {
         // check if referenced msg id is associated with this issue
         if ($type == 'note') {
             $reference_issue_id = Note::getIssueByMessageID($reference_msg_id);
         } else {
             $reference_issue_id = Support::getIssueByMessageID($reference_msg_id);
         }
     }
     if (empty($reference_msg_id) || $reference_issue_id != $issue_id) {
         $reference_msg_id = Issue::getRootMessageID($issue_id);
     }
     $references = self::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;
     }
     /**
      * Make sure that In-Reply-To and References headers are set and reference a message in this issue.
      * If not, set to be the root message ID of the issue. This is to ensure messages are threaded by
      * issue in mail clients.
      */
     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: ' . self::fold(implode(' ', $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: " . self::fold(implode(' ', $references)), $text_headers, 1);
     }
     $headers['references'] = self::fold(implode(' ', $references));
     return array($text_headers . "\r\n\r\n" . $body, $headers);
 }
 /**
  * Routes a note to the correct issue
  *
  * @param   string $full_message The full note
  * @return  mixed   true or array(ERROR_CODE, ERROR_STRING) in case of failure
  */
 public static function route_notes($full_message)
 {
     // save the full message for logging purposes
     Note::saveRoutedNote($full_message);
     // join the Content-Type line (for easier parsing?)
     if (preg_match('/^boundary=/m', $full_message)) {
         $pattern = "#(Content-Type: multipart/.+); ?\r?\n(boundary=.*)\$#im";
         $replacement = '$1; $2';
         $full_message = preg_replace($pattern, $replacement, $full_message);
     }
     list($headers) = Mime_Helper::splitHeaderBody($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 email routing interface is even supposed to be enabled
     $setup = Setup::get();
     if ($setup['note_routing']['status'] != 'enabled') {
         return array(self::EX_CONFIG, ev_gettext('Error: The internal note routing interface is disabled.') . "\n");
     }
     if (empty($setup['note_routing']['address_prefix'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address prefix.') . "\n");
     }
     if (empty($setup['note_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, true);
     // find which issue ID this email refers to
     if (isset($structure->headers['to'])) {
         $issue_id = self::getMatchingIssueIDs($structure->headers['to'], 'note');
     }
     // 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'], 'note');
     }
     if (empty($issue_id)) {
         return array(self::EX_DATAERR, ev_gettext('Error: The routed note 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) || User::getRoleByUser($sender_usr_id, $prj_id) < User::ROLE_USER || User::isPartner($sender_usr_id) && !Access::canViewInternalNotes($issue_id, $sender_usr_id)) && !Workflow::canSendNote($prj_id, $issue_id, $sender_email, $structure)) {
         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");
     }
     if (empty($sender_usr_id)) {
         $sender_usr_id = APP_SYSTEM_USER_ID;
         $unknown_user = $structure->headers['from'];
     } else {
         $unknown_user = false;
     }
     AuthCookie::setAuthCookie($sender_usr_id);
     AuthCookie::setProjectCookie($prj_id);
     // parse the Cc: list, if any, and add these internal users to the issue notification list
     $addresses = array();
     $to_addresses = Mail_Helper::getEmailAddresses(@$structure->headers['to']);
     if (count($to_addresses)) {
         $addresses = $to_addresses;
     }
     $cc_addresses = Mail_Helper::getEmailAddresses(@$structure->headers['cc']);
     if (count($cc_addresses)) {
         $addresses = array_merge($addresses, $cc_addresses);
     }
     $cc_users = array();
     foreach ($addresses as $email) {
         $cc_usr_id = User::getUserIDByEmail(strtolower($email), true);
         if (!empty($cc_usr_id) && User::getRoleByUser($cc_usr_id, $prj_id) >= User::ROLE_USER) {
             $cc_users[] = $cc_usr_id;
         }
     }
     $body = $structure->body;
     $reference_msg_id = Mail_Helper::getReferenceMessageID($headers);
     if (!empty($reference_msg_id)) {
         $parent_id = Note::getIDByMessageID($reference_msg_id);
     } else {
         $parent_id = false;
     }
     // insert the new note and send notification about it
     $_POST = array('title' => @$structure->headers['subject'], 'note' => $body, 'note_cc' => $cc_users, 'add_extra_recipients' => 'yes', 'message_id' => @$structure->headers['message-id'], 'parent_id' => $parent_id);
     // add the full email to the note if there are any attachments
     // this is needed because the front end code will display attachment links
     if (Mime_Helper::hasAttachments($structure)) {
         $_POST['full_message'] = $full_message;
     }
     $usr_id = Auth::getUserID();
     $res = Note::insertFromPost($usr_id, $issue_id, $unknown_user, false);
     // need to handle attachments coming from notes as well
     if ($res != -1) {
         Support::extractAttachments($issue_id, $structure, true, $res);
     }
     // FIXME! $res == -2 is not handled
     History::add($issue_id, $usr_id, 'note_routed', 'Note routed from {user}', array('user' => $structure->headers['from']));
     return true;
 }
 /**
  * Routes a note to the correct issue
  *
  * @param   string $full_message The full note
  */
 function route_notes($full_message)
 {
     global $HTTP_POST_VARS;
     // save the full message for logging purposes
     Note::saveRoutedNote($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);
     }
     list($headers, ) = Mime_Helper::splitHeaderBody($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 email routing interface is even supposed to be enabled
     $setup = Setup::load();
     if (@$setup['note_routing']['status'] != 'enabled') {
         return array(78, "Error: The internal note routing interface is disabled.\n");
     }
     $prefix = $setup['note_routing']['address_prefix'];
     // escape plus signs so '*****@*****.**' becomes a valid routing address
     $prefix = str_replace('+', '\\+', $prefix);
     $mail_domain = quotemeta($setup['note_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, true);
     // 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 note 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);
     // parse the Cc: list, if any, and add these internal users to the issue notification list
     $users = array_flip($users);
     $addresses = array();
     $to_addresses = Mail_API::getEmailAddresses(@$structure->headers['to']);
     if (count($to_addresses)) {
         $addresses = $to_addresses;
     }
     $cc_addresses = Mail_API::getEmailAddresses(@$structure->headers['cc']);
     if (count($cc_addresses)) {
         $addresses = array_merge($addresses, $cc_addresses);
     }
     $cc_users = array();
     foreach ($addresses as $email) {
         if (in_array(strtolower($email), $user_emails)) {
             $cc_users[] = $users[$email];
         }
     }
     $body = Mime_Helper::getMessageBody($structure);
     $reference_msg_id = Mail_API::getReferenceMessageID($headers);
     if (!empty($reference_msg_id)) {
         $parent_id = Note::getIDByMessageID($reference_msg_id);
     } else {
         $parent_id = false;
     }
     // insert the new note and send notification about it
     $HTTP_POST_VARS = array('title' => @$structure->headers['subject'], 'note' => $body, 'note_cc' => $cc_users, 'add_extra_recipients' => 'yes', 'message_id' => @$structure->headers['message-id'], 'parent_id' => $parent_id);
     // add the full email to the note if there are any attachments
     // this is needed because the front end code will display attachment links
     if (Mime_Helper::hasAttachments($full_message)) {
         $HTTP_POST_VARS['blocked_msg'] = $full_message;
     }
     $res = Note::insert(Auth::getUserID(), $issue_id, false, false);
     // need to handle attachments coming from notes as well
     if ($res != -1) {
         Support::extractAttachments($issue_id, $full_message, true, $res);
     }
     History::add($issue_id, Auth::getUserID(), History::getTypeID('note_routed'), "Note routed from " . $structure->headers['from']);
     return true;
 }