/** * Creates a new issue from an email if appropriate. Also returns if this message is related * to a previous message. * * @param array $info An array of info about the email account. * @param string $headers The headers of the email. * @param string $message_body The body of the message. * @param string $date The date this message was sent * @param string $from The name and email address of the sender. * @param string $subject The subject of this message. * @param array $to An array of to addresses * @param array $cc An array of cc addresses * @return array An array of information about the message */ public function createIssueFromEmail($info, $headers, $message_body, $date, $from, $subject, $to, $cc) { $should_create_issue = false; $issue_id = ''; $associate_email = ''; $type = 'email'; $parent_id = ''; $customer_id = false; $contact_id = false; $contract_id = false; $severity = false; // we can't trust the in-reply-to from the imap c-client, so let's // try to manually parse that value from the full headers $references = Mail_Helper::getAllReferences($headers); $message_id = Mail_Helper::getMessageID($headers, $message_body); $workflow = Workflow::getIssueIDforNewEmail($info['ema_prj_id'], $info, $headers, $message_body, $date, $from, $subject, $to, $cc); if (is_array($workflow)) { if (isset($workflow['customer_id'])) { $customer_id = $workflow['customer_id']; } if (isset($workflow['contract_id'])) { $contract_id = $workflow['contract_id']; } if (isset($workflow['contact_id'])) { $contact_id = $workflow['contact_id']; } if (isset($workflow['severity'])) { $severity = $workflow['severity']; } if (isset($workflow['should_create_issue'])) { $should_create_issue = $workflow['should_create_issue']; } else { $should_create_issue = true; } } elseif ($workflow == 'new') { $should_create_issue = true; } elseif (is_numeric($workflow)) { $issue_id = $workflow; } else { $setup = Setup::load(); if (@$setup['subject_based_routing']['status'] == 'enabled') { // Look for issue ID in the subject line // look for [#XXXX] in the subject line if (preg_match("/\\[#(\\d+)\\]( Note| BLOCKED)*/", $subject, $matches)) { $should_create_issue = false; $issue_id = $matches[1]; if (!Issue::exists($issue_id, false)) { $issue_id = ''; } elseif (!empty($matches[2])) { $type = 'note'; } } else { $should_create_issue = true; } } else { // - if this email is a reply: if (count($references) > 0) { foreach ($references as $reference_msg_id) { // -> check if the replied email exists in the database: if (Note::exists($reference_msg_id)) { // note exists // get what issue it belongs too. $issue_id = Note::getIssueByMessageID($reference_msg_id); $should_create_issue = false; $type = 'note'; $parent_id = Note::getIDByMessageID($reference_msg_id); break; } elseif (self::exists($reference_msg_id) || Issue::getIssueByRootMessageID($reference_msg_id) != false) { // email or issue exists $issue_id = self::getIssueByMessageID($reference_msg_id); if (empty($issue_id)) { $issue_id = Issue::getIssueByRootMessageID($reference_msg_id); } if (empty($issue_id)) { // parent email isn't associated with issue. // --> create new issue, associate current email and replied email to this issue $should_create_issue = true; $associate_email = $reference_msg_id; } else { // parent email is associated with issue: // --> associate current email with existing issue $should_create_issue = false; } break; } else { // no matching note, email or issue: // => create new issue and associate current email with it $should_create_issue = true; } } } else { // - if this email is not a reply: // -> create new issue and associate current email with it $should_create_issue = true; } } } $sender_email = Mail_Helper::getEmailAddress($from); if (Misc::isError($sender_email)) { $sender_email = 'Error Parsing Email <>'; } // only create a new issue if this email is coming from a known customer if ($should_create_issue && $info['ema_issue_auto_creation_options']['only_known_customers'] == 'yes' && CRM::hasCustomerIntegration($info['ema_prj_id']) && !$customer_id) { try { $crm = CRM::getInstance($info['ema_prj_id']); $should_create_issue = true; } catch (CRMException $e) { $should_create_issue = false; } } // check whether we need to create a new issue or not if ($info['ema_issue_auto_creation'] == 'enabled' && $should_create_issue && !Notification::isBounceMessage($sender_email)) { $options = Email_Account::getIssueAutoCreationOptions($info['ema_id']); Auth::createFakeCookie(APP_SYSTEM_USER_ID, $info['ema_prj_id']); $issue_id = Issue::createFromEmail($info['ema_prj_id'], APP_SYSTEM_USER_ID, $from, Mime_Helper::decodeQuotedPrintable($subject), $message_body, @$options['category'], @$options['priority'], @$options['users'], $date, $message_id, $severity, $customer_id, $contact_id, $contract_id); // add sender to authorized repliers list if they are not a real user $sender_usr_id = User::getUserIDByEmail($sender_email, true); if (empty($sender_usr_id)) { Authorized_Replier::manualInsert($issue_id, $sender_email, false); } // associate any existing replied-to email with this new issue if (!empty($associate_email) && !empty($reference_issue_id)) { $reference_sup_id = self::getIDByMessageID($associate_email); self::associate(APP_SYSTEM_USER_ID, $issue_id, array($reference_sup_id)); } } // need to check crm for customer association if (!empty($from)) { if (CRM::hasCustomerIntegration($info['ema_prj_id']) && !$customer_id) { // check for any customer contact association try { $crm = CRM::getInstance($info['ema_prj_id']); $contact = $crm->getContactByEmail($sender_email); $contact_id = $contact->getContactID(); $contracts = $contact->getContracts(array(CRM_EXCLUDE_EXPIRED)); $contract = $contracts[0]; $customer_id = $contract->getCustomerID(); } catch (CRMException $e) { $customer_id = null; $contact_id = null; } } } return array('should_create_issue' => $should_create_issue, 'associate_email' => $associate_email, 'issue_id' => $issue_id, 'customer_id' => $customer_id, 'contact_id' => $contact_id, 'type' => $type, 'parent_id' => $parent_id); }
/** * 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; }
/** * Creates a new issue from an email if appropriate. Also returns if this message is related * to a previous message. * * @access private * @param array $info An array of info about the email account. * @param string $headers The headers of the email. * @param string $message_body The body of the message. * @param string $date The date this message was sent * @param string $from The name and email address of the sender. * @param string $subject The subject of this message. * @return array An array of information about the message */ function createIssueFromEmail($info, $headers, $message_body, $date, $from, $subject) { $should_create_issue = false; $issue_id = ''; $associate_email = ''; $type = 'email'; $parent_id = ''; // we can't trust the in-reply-to from the imap c-client, so let's // try to manually parse that value from the full headers $references = Mail_API::getAllReferences($headers); $message_id = Mail_API::getMessageID($headers, $message_body); $setup = Setup::load(); if (@$setup['subject_based_routing']['status'] == 'enabled' && preg_match("/\\[#(\\d+)\\]( Note| BLOCKED)*/", $subject, $matches)) { $should_create_issue = false; $issue_id = $matches[1]; if (!Issue::exists($issue_id, false)) { $issue_id = ''; } elseif (!empty($matches[2])) { $type = 'note'; } } else { // - if this email is a reply: if (count($references) > 0) { foreach ($references as $reference_msg_id) { // -> check if the replied email exists in the database: if (Note::exists($reference_msg_id)) { // note exists // get what issue it belongs too. $issue_id = Note::getIssueByMessageID($reference_msg_id); $should_create_issue = false; $type = 'note'; $parent_id = Note::getIDByMessageID($reference_msg_id); break; } elseif (Support::exists($reference_msg_id) || Issue::getIssueByRootMessageID($reference_msg_id) != false) { // email or issue exists $issue_id = Support::getIssueByMessageID($reference_msg_id); if (empty($issue_id)) { $issue_id = Issue::getIssueByRootMessageID($reference_msg_id); } if (empty($issue_id)) { // parent email isn't associated with issue. // --> create new issue, associate current email and replied email to this issue $should_create_issue = true; $associate_email = $reference_msg_id; } else { // parent email is associated with issue: // --> associate current email with existing issue $should_create_issue = false; } break; } else { // no matching note, email or issue: // => create new issue and associate current email with it $should_create_issue = true; } } } else { // - if this email is not a reply: // -> create new issue and associate current email with it $should_create_issue = true; } if (empty($issue_id)) { $issue_id = Issue::getIssueBy($subject, 'iss_summary'); if (!empty($issue_id)) { $should_create_issue = false; } } } $sender_email = Mail_API::getEmailAddress($from); // only create a new issue if this email is coming from a known customer if ($should_create_issue && $info['ema_issue_auto_creation_options']['only_known_customers'] == 'yes' && Customer::hasCustomerIntegration($info['ema_prj_id'])) { list($customer_id, ) = Customer::getCustomerIDByEmails($info['ema_prj_id'], array($sender_email)); if (empty($customer_id)) { $should_create_issue = false; } } // check whether we need to create a new issue or not if ($info['ema_issue_auto_creation'] == 'enabled' && $should_create_issue) { $options = Email_Account::getIssueAutoCreationOptions($info['ema_id']); Auth::createFakeCookie(APP_SYSTEM_USER_ID, $info['ema_prj_id']); $issue_id = Issue::createFromEmail($info['ema_prj_id'], APP_SYSTEM_USER_ID, $from, Mime_Helper::fixEncoding($subject), $message_body, @$options['category'], $options['priority'], @$options['users'], $date, $message_id); // associate any existing replied-to email with this new issue if (!empty($associate_email) && !empty($reference_issue_id)) { $reference_sup_id = Support::getIDByMessageID($associate_email); Support::associate(APP_SYSTEM_USER_ID, $issue_id, array($reference_sup_id)); } } // need to check crm for customer association if (!empty($from)) { $details = Email_Account::getDetails($info['ema_id']); if (Customer::hasCustomerIntegration($info['ema_prj_id'])) { // check for any customer contact association @(list($customer_id, ) = Customer::getCustomerIDByEmails($info['ema_prj_id'], array($sender_email))); } } return array('should_create_issue' => $should_create_issue, 'associate_email' => $associate_email, 'issue_id' => $issue_id, 'customer_id' => @$customer_id, 'type' => $type, 'parent_id' => $parent_id); }
/** * 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; }