/** * Method used to get the information about a specific message * from a given mailbox. * * XXX this function does more than that. * * @param resource $mbox The mailbox * @param array $info The support email account information * @param integer $num The index of the message * @return void */ public static function getEmailInfo($mbox, $info, $num) { Auth::createFakeCookie(APP_SYSTEM_USER_ID); // check if the current message was already seen if ($info['ema_get_only_new']) { list($overview) = @imap_fetch_overview($mbox, $num); if ($overview->seen || $overview->deleted || $overview->answered) { return; } } $email = @imap_headerinfo($mbox, $num); $headers = imap_fetchheader($mbox, $num); $body = imap_body($mbox, $num); // check for mysterious blank messages if (empty($body) and empty($headers)) { // XXX do some error reporting? return; } $message_id = Mail_Helper::getMessageID($headers, $body); $message = $headers . $body; // we don't need $body anymore -- free memory unset($body); // if message_id already exists, return immediately -- nothing to do if (self::exists($message_id) || Note::exists($message_id)) { return; } $structure = Mime_Helper::decode($message, true, true); $message_body = $structure->body; if (Mime_Helper::hasAttachments($structure)) { $has_attachments = 1; } else { $has_attachments = 0; } // pass in $email by reference so it can be modified $workflow = Workflow::preEmailDownload($info['ema_prj_id'], $info, $mbox, $num, $message, $email, $structure); if ($workflow === -1) { return; } // route emails if necessary if ($info['ema_use_routing'] == 1) { $setup = Setup::load(); // we create addresses array so it can be reused $addresses = array(); if (isset($email->to)) { foreach ($email->to as $address) { $addresses[] = $address->mailbox . '@' . $address->host; } } if (isset($email->cc)) { foreach ($email->cc as $address) { $addresses[] = $address->mailbox . '@' . $address->host; } } if (@$setup['email_routing']['status'] == 'enabled') { $res = Routing::getMatchingIssueIDs($addresses, 'email'); if ($res != false) { $return = Routing::route_emails($message); if ($return === true) { self::deleteMessage($info, $mbox, $num); return; } // TODO: handle errors? return; } } if (@$setup['note_routing']['status'] == 'enabled') { $res = Routing::getMatchingIssueIDs($addresses, 'note'); if ($res != false) { $return = Routing::route_notes($message); // if leave copy of emails on IMAP server is off we can // bounce on note that user had no permission to write // here. // otherwise proper would be to create table - // eventum_bounce: bon_id, bon_message_id, bon_error if ($info['ema_leave_copy']) { if ($return === true) { self::deleteMessage($info, $mbox, $num); } } else { if ($return !== true) { // in case of error, create bounce, but still // delete email not to send bounce in next process :) self::bounceMessage($email, $return); } self::deleteMessage($info, $mbox, $num); } return; } } if (@$setup['draft_routing']['status'] == 'enabled') { $res = Routing::getMatchingIssueIDs($addresses, 'draft'); if ($res != false) { $return = Routing::route_drafts($message); // if leave copy of emails on IMAP server is off we can // bounce on note that user had no permission to write // here. // otherwise proper would be to create table - // eventum_bounce: bon_id, bon_message_id, bon_error if ($info['ema_leave_copy']) { if ($return === true) { self::deleteMessage($info, $mbox, $num); } } else { if ($return !== true) { // in case of error, create bounce, but still // delete email not to send bounce in next process :) self::bounceMessage($email, $return); } self::deleteMessage($info, $mbox, $num); } return; } } // TODO: // disabling return here allows routing and issue auto creating from same account // but it will download email store it in database and do nothing // with it if it does not match support@ address. //return; } $sender_email = Mail_Helper::getEmailAddress($email->fromaddress); if (Misc::isError($sender_email)) { $sender_email = 'Error Parsing Email <>'; } $t = array('ema_id' => $info['ema_id'], 'message_id' => $message_id, 'date' => Date_Helper::convertDateGMTByTS($email->udate), 'from' => $sender_email, 'to' => @$email->toaddress, 'cc' => @$email->ccaddress, 'subject' => @$structure->headers['subject'], 'body' => @$message_body, 'full_email' => @$message, 'has_attachment' => $has_attachments, 'headers' => @$structure->headers); $subject = Mime_Helper::decodeQuotedPrintable(@$structure->headers['subject']); $should_create_array = self::createIssueFromEmail($info, $headers, $message_body, $t['date'], $sender_email, $subject, $t['to'], $t['cc']); $should_create_issue = $should_create_array['should_create_issue']; if (!empty($should_create_array['issue_id'])) { $t['issue_id'] = $should_create_array['issue_id']; // figure out if we should change to a different email account $iss_prj_id = Issue::getProjectID($t['issue_id']); if ($info['ema_prj_id'] != $iss_prj_id) { $new_ema_id = Email_Account::getEmailAccount($iss_prj_id); if (!empty($new_ema_id)) { $t['ema_id'] = $new_ema_id; } } } if (!empty($should_create_array['customer_id'])) { $t['customer_id'] = $should_create_array['customer_id']; } if (empty($t['issue_id'])) { $t['issue_id'] = 0; } else { $prj_id = Issue::getProjectID($t['issue_id']); Auth::createFakeCookie(APP_SYSTEM_USER_ID, $prj_id); } if ($should_create_array['type'] == 'note') { // assume that this is not a valid note $res = -1; if ($t['issue_id'] != 0) { // check if this is valid user $usr_id = User::getUserIDByEmail($sender_email); if (!empty($usr_id)) { $role_id = User::getRoleByUser($usr_id, $prj_id); if ($role_id > User::getRoleID('Customer')) { // actually a valid user so insert the note Auth::createFakeCookie($usr_id, $prj_id); $users = Project::getUserEmailAssocList($prj_id, 'active', User::getRoleID('Customer')); $user_emails = array_map(function ($s) { return strtolower($s); }, array_values($users)); $users = array_flip($users); $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) { if (in_array(strtolower($email), $user_emails)) { $cc_users[] = $users[strtolower($email)]; } } // XXX FIXME, this is not nice thing to do $_POST = array('title' => Mail_Helper::removeExcessRe($t['subject']), 'note' => $t['body'], 'note_cc' => $cc_users, 'add_extra_recipients' => 'yes', 'message_id' => $t['message_id'], 'parent_id' => $should_create_array['parent_id']); $res = Note::insertFromPost($usr_id, $t['issue_id']); // need to handle attachments coming from notes as well if ($res != -1) { Support::extractAttachments($t['issue_id'], $structure, true, $res); } } } } } else { // check if we need to block this email if ($should_create_issue == true || !self::blockEmailIfNeeded($t)) { if (!empty($t['issue_id'])) { list($t['full_email'], $t['headers']) = Mail_Helper::rewriteThreadingHeaders($t['issue_id'], $t['full_email'], $t['headers'], 'email'); } // make variable available for workflow to be able to detect whether this email created new issue $t['should_create_issue'] = $should_create_array['should_create_issue']; $res = self::insertEmail($t, $structure, $sup_id); if ($res != -1) { // only extract the attachments from the email if we are associating the email to an issue if (!empty($t['issue_id'])) { self::extractAttachments($t['issue_id'], $structure); // notifications about new emails are always external $internal_only = false; $assignee_only = false; // special case when emails are bounced back, so we don't want a notification to customers about those if (Notification::isBounceMessage($sender_email)) { // broadcast this email only to the assignees for this issue $internal_only = true; $assignee_only = true; } elseif ($should_create_issue == true) { // if a new issue was created, only send a copy of the email to the assignee (if any), don't resend to the original TO/CC list $assignee_only = true; $internal_only = true; } if (Workflow::shouldAutoAddToNotificationList($info['ema_prj_id'])) { self::addExtraRecipientsToNotificationList($info['ema_prj_id'], $t, $should_create_issue); } if (self::isAllowedToEmail($t['issue_id'], $sender_email)) { Notification::notifyNewEmail(Auth::getUserID(), $t['issue_id'], $t, $internal_only, $assignee_only, '', $sup_id); } // try to get usr_id of sender, if not, use system account $addr = Mail_Helper::getEmailAddress($structure->headers['from']); if (Misc::isError($addr)) { // XXX should we log or is this expected? Error_Handler::logError(array($addr->getMessage() . " addr: {$addr}", $addr->getDebugInfo()), __FILE__, __LINE__); $usr_id = APP_SYSTEM_USER_ID; } else { $usr_id = User::getUserIDByEmail($addr); if (!$usr_id) { $usr_id = APP_SYSTEM_USER_ID; } } // mark this issue as updated if (!empty($t['customer_id']) && $t['customer_id'] != 'NULL' && (empty($usr_id) || User::getRoleByUser($usr_id, $prj_id) == User::getRoleID('Customer'))) { Issue::markAsUpdated($t['issue_id'], 'customer action'); } else { if (!empty($usr_id) && User::getRoleByUser($usr_id, $prj_id) > User::getRoleID('Customer')) { Issue::markAsUpdated($t['issue_id'], 'staff response'); } else { Issue::markAsUpdated($t['issue_id'], 'user response'); } } // log routed email History::add($t['issue_id'], $usr_id, 'email_routed', 'Email routed from {from}', array('from' => $structure->headers['from'])); } } } else { $res = 1; } } if ($res > 0) { // need to delete the message from the server? if (!$info['ema_leave_copy']) { @imap_delete($mbox, $num); } else { // mark the message as already read @imap_setflag_full($mbox, $num, '\\Seen'); } } }
/** * Method used to get the information about a specific message * from a given mailbox. * * @access public * @param resource $mbox The mailbox * @param array $info The support email account information * @param integer $num The index of the message * @return array The message information */ function getEmailInfo($mbox, $info, $num) { Auth::createFakeCookie(APP_SYSTEM_USER_ID); // check if the current message was already seen if ($info['ema_get_only_new']) { list($overview) = @imap_fetch_overview($mbox, $num); if ($overview->seen || $overview->deleted || $overview->answered) { return false; } } $email = @imap_headerinfo($mbox, $num); $body = imap_body($mbox, $num); $headers = imap_fetchheader($mbox, $num); $message = $headers . $body; // check for mysterious blank messages if (empty($message)) { return ''; } $message_id = Mail_API::getMessageID($headers, $body); if (!Support::exists($message_id) && !Note::exists($message_id)) { $structure = Mime_Helper::decode($message, true, true); $message_body = Mime_Helper::getMessageBody(&$structure); if (Mime_Helper::hasAttachments($message)) { $has_attachments = 1; } else { $has_attachments = 0; } // 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 $reference_msg_id = Mail_API::getReferenceMessageID($headers); // route emails if neccassary if ($info['ema_use_routing'] == 1) { $setup = Setup::load(); if (@$setup['email_routing']['status'] == 'enabled') { $prefix = $setup['email_routing']['address_prefix']; // escape plus signs so '*****@*****.**' becomes a valid routing address $prefix = str_replace('+', '\\+', $prefix); $mail_domain = $setup['email_routing']['address_host']; $mail_domain_alias = @$setup['email_routing']['host_alias']; if (!empty($mail_domain_alias)) { $mail_domain = "[" . $mail_domain . "|" . $mail_domain_alias . "]"; } if (empty($prefix)) { return false; } if (empty($mail_domain)) { return false; } if (preg_match("/{$prefix}(\\d*)@{$mail_domain}/i", $email->toaddress, $matches)) { $return = Routing::route_emails($message); if ($return == true) { Support::deleteMessage($info, $mbox, $num); } return $return; } } if (@$setup['note_routing']['status'] == 'enabled') { $prefix = $setup['note_routing']['address_prefix']; // escape plus signs so '*****@*****.**' becomes a valid routing address $prefix = str_replace('+', '\\+', $prefix); $mail_domain = $setup['note_routing']['address_host']; if (empty($prefix)) { return false; } if (empty($mail_domain)) { return false; } if (preg_match("/{$prefix}(\\d*)@{$mail_domain}/i", $email->toaddress, $matches)) { $return = Routing::route_notes($message); if ($return == true) { Support::deleteMessage($info, $mbox, $num); } return $return; } } if (@$setup['draft_routing']['status'] == 'enabled') { $prefix = $setup['draft_routing']['address_prefix']; // escape plus signs so '*****@*****.**' becomes a valid routing address $prefix = str_replace('+', '\\+', $prefix); $mail_domain = $setup['draft_routing']['address_host']; if (empty($prefix)) { return false; } if (empty($mail_domain)) { return false; } if (preg_match("/{$prefix}(\\d*)@{$mail_domain}/i", $email->toaddress, $matches)) { $return = Routing::route_drafts($message); if ($return == true) { Support::deleteMessage($info, $mbox, $num); } return $return; } } return false; } $sender_email = Mail_API::getEmailAddress($email->fromaddress); $t = array('ema_id' => $info['ema_id'], 'message_id' => $message_id, 'date' => @Date_API::getDateGMTByTS($email->udate), 'from' => @$email->fromaddress, 'to' => @$email->toaddress, 'cc' => @$email->ccaddress, 'subject' => @$email->subject, 'body' => @$message_body, 'full_email' => @$message, 'has_attachment' => $has_attachments, 'headers' => @$structure->headers); $should_create_array = Support::createIssueFromEmail($info, $headers, $message_body, $t['date'], @$email->fromaddress, @$email->subject); $should_create_issue = $should_create_array['should_create_issue']; $associate_email = $should_create_array['associate_email']; if (!empty($should_create_array['issue_id'])) { $t['issue_id'] = $should_create_array['issue_id']; // figure out if we should change to a different email account $iss_prj_id = Issue::getProjectID($t['issue_id']); if ($info['ema_prj_id'] != $iss_prj_id) { $new_ema_id = Email_Account::getEmailAccount($iss_prj_id); if (!empty($new_ema_id)) { $t['ema_id'] = $new_ema_id; } } } if (!empty($should_create_array['customer_id'])) { $t['customer_id'] = $should_create_array['customer_id']; } if (empty($t['issue_id'])) { $t['issue_id'] = 0; } else { $prj_id = Issue::getProjectID($t['issue_id']); Auth::createFakeCookie(APP_SYSTEM_USER_ID, $prj_id); } if ($should_create_array['type'] == 'note') { // assume that this is not a valid note $res = -1; if ($t['issue_id'] != 0) { // check if this is valid user $usr_id = User::getUserIDByEmail($sender_email); if (!empty($usr_id)) { $role_id = User::getRoleByUser($usr_id, $prj_id); if ($role_id > User::getRoleID("Customer")) { // actually a valid user so insert the note Auth::createFakeCookie($usr_id, $prj_id); $users = Project::getUserEmailAssocList($prj_id, 'active', User::getRoleID('Customer')); $user_emails = array_map('strtolower', array_values($users)); $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]; } } $GLOBALS['HTTP_POST_VARS'] = array('title' => Mail_API::removeExcessRe($t['subject']), 'note' => $t['body'], 'note_cc' => $cc_users, 'add_extra_recipients' => 'yes', 'message_id' => $t['message_id'], 'parent_id' => $should_create_array['parent_id']); $res = Note::insert($usr_id, $t['issue_id']); } } } } else { // check if we need to block this email if ($should_create_issue == true || !Support::blockEmailIfNeeded($t)) { if (!empty($t['issue_id'])) { list($t['full_email'], $t['headers']) = Mail_API::rewriteThreadingHeaders($t['issue_id'], $t['full_email'], $t['headers'], 'email'); } $res = Support::insertEmail($t, $structure, $sup_id); if ($res != -1) { // only extract the attachments from the email if we are associating the email to an issue if (!empty($t['issue_id'])) { Support::extractAttachments($t['issue_id'], $t['full_email']); // notifications about new emails are always external $internal_only = false; $assignee_only = false; // special case when emails are bounced back, so we don't want a notification to customers about those if (Notification::isBounceMessage($sender_email)) { // broadcast this email only to the assignees for this issue $internal_only = true; $assignee_only = true; } Notification::notifyNewEmail(Auth::getUserID(), $t['issue_id'], $t, $internal_only, $assignee_only, '', $sup_id); // try to get usr_id of sender, if not, use system account $usr_id = User::getUserIDByEmail(Mail_API::getEmailAddress($structure->headers['from'])); if (!$usr_id) { $usr_id = APP_SYSTEM_USER_ID; } // mark this issue as updated if (!empty($t['customer_id']) && $t['customer_id'] != 'NULL') { Issue::markAsUpdated($t['issue_id'], 'customer action'); } else { if (!empty($usr_id) && User::getRoleByUser($usr_id, $prj_id) > User::getRoleID('Customer')) { Issue::markAsUpdated($t['issue_id'], 'staff response'); } else { Issue::markAsUpdated($t['issue_id'], 'user response'); } } // log routed email History::add($t['issue_id'], $usr_id, History::getTypeID('email_routed'), "Email routed from " . $structure->headers['from']); } } } else { $res = 1; } } if ($res > 0) { // need to delete the message from the server? if (!$info['ema_leave_copy']) { @imap_delete($mbox, $num); } else { // mark the message as already read @imap_setflag_full($mbox, $num, "\\Seen"); } } return true; } else { return false; } }
/** * Method used to send email notifications for a given issue. * * @access public * @param integer $issue_id The issue ID * @param string $type The notification type * @param array $ids The list of entries that were changed * @param integer $internal_only Whether the notification should only be sent to internal users or not * @return void */ function notify($issue_id, $type, $ids = FALSE, $internal_only = FALSE, $extra_recipients = FALSE) { if ($extra_recipients) { $extra = array(); for ($i = 0; $i < count($extra_recipients); $i++) { $extra[] = array('sub_usr_id' => $extra_recipients[$i], 'sub_email' => ''); } } $emails = array(); $users = Notification::getUsersByIssue($issue_id, $type); if ($extra_recipients && count($extra) > 0) { $users = array_merge($users, $extra); } $user_emails = Project::getUserEmailAssocList(Issue::getProjectID($issue_id), 'active', User::getRoleID('Customer')); $user_emails = array_map('strtolower', $user_emails); for ($i = 0; $i < count($users); $i++) { if (empty($users[$i]["sub_usr_id"])) { if ($internal_only == false || in_array(strtolower($users[$i]["sub_email"]), array_values($user_emails))) { $email = $users[$i]["sub_email"]; } } else { // don't send the notification email to the person who performed the action if (Auth::getUserID() == $users[$i]["sub_usr_id"]) { continue; } // if we are only supposed to send email to internal users, check if the role is lower than standard user if ($internal_only == true && User::getRoleByUser($users[$i]["sub_usr_id"], Issue::getProjectID($issue_id)) < User::getRoleID('standard user')) { continue; } $email = User::getFromHeader($users[$i]["sub_usr_id"]); } // now add it to the list of emails if (!empty($email) && !in_array($email, $emails)) { $emails[] = $email; } } // prevent the primary customer contact from receiving two emails about the issue being closed if ($type == 'closed') { $stmt = "SELECT\r\n iss_customer_contact_id\r\n FROM\r\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\r\n WHERE\r\n iss_id=" . Misc::escapeInteger($issue_id); $customer_contact_id = $GLOBALS["db_api"]->dbh->getOne($stmt); if (!empty($customer_contact_id)) { list($contact_email, , ) = Customer::getContactLoginDetails(Issue::getProjectID($issue_id), $customer_contact_id); for ($i = 0; $i < count($emails); $i++) { $email = Mail_API::getEmailAddress($emails[$i]); if ($email == $contact_email) { unset($emails[$i]); $emails = array_values($emails); break; } } } } if (count($emails) > 0) { $headers = false; switch ($type) { case 'closed': $data = Notification::getIssueDetails($issue_id); $data["closer_name"] = User::getFullName(History::getIssueCloser($issue_id)); $subject = 'Closed'; if ($ids != false) { $data['reason'] = Support::getEmail($ids); } break; case 'updated': // this should not be used anymore return false; break; case 'notes': $data = Notification::getNote($issue_id, $ids); $headers = array('Message-ID' => $data['note']['not_message_id']); if (@$data['note']['reference_msg_id'] != false) { $headers['In-Reply-To'] = $data['note']['reference_msg_id']; } else { $headers['In-Reply-To'] = Issue::getRootMessageID($issue_id); } $headers['References'] = Mail_API::fold(join(' ', Mail_API::getReferences($issue_id, @$data['note']['reference_msg_id'], 'note'))); $subject = 'Note'; break; case 'emails': // this should not be used anymore return false; break; case 'files': $data = Notification::getAttachment($issue_id, $ids); $subject = 'File Attached'; break; } Notification::notifySubscribers($issue_id, $emails, $type, $data, $subject, $internal_only, $ids, $headers); } }
/** * Method used to send email notifications for a given issue. * * @param integer $issue_id The issue ID * @param string $type The notification type * @param int $entry_id The entries id that was changed * @param bool $internal_only Whether the notification should only be sent to internal users or not * @param array $extra_recipients * @return bool */ public static function notify($issue_id, $type, $entry_id = null, $internal_only = false, $extra_recipients = null) { $prj_id = Issue::getProjectID($issue_id); $extra = array(); if ($extra_recipients) { foreach ($extra_recipients as $user) { $extra[] = array('sub_usr_id' => $user, 'sub_email' => ''); } } $emails = array(); $users = self::getUsersByIssue($issue_id, $type); if ($extra_recipients && count($extra) > 0) { $users = array_merge($users, $extra); } $user_emails = Project::getUserEmailAssocList(Issue::getProjectID($issue_id), 'active', User::ROLE_CUSTOMER); $user_emails = Misc::lowercase($user_emails); foreach ($users as $user) { if (empty($user['sub_usr_id'])) { if ($internal_only == false || in_array(strtolower($user['sub_email']), array_values($user_emails))) { $email = $user['sub_email']; } } else { $prefs = Prefs::get($user['sub_usr_id']); if (Auth::getUserID() == $user['sub_usr_id'] && (empty($prefs['receive_copy_of_own_action'][$prj_id]) || $prefs['receive_copy_of_own_action'][$prj_id] == false)) { continue; } // if we are only supposed to send email to internal users, check if the role is lower than standard user if ($internal_only == true && User::getRoleByUser($user['sub_usr_id'], Issue::getProjectID($issue_id)) < User::ROLE_USER) { continue; } if ($type == 'notes' && User::isPartner($user['sub_usr_id']) && !Partner::canUserAccessIssueSection($user['sub_usr_id'], 'notes')) { continue; } $email = User::getFromHeader($user['sub_usr_id']); } // now add it to the list of emails if (!empty($email) && !in_array($email, $emails)) { $emails[] = $email; } } // prevent the primary customer contact from receiving two emails about the issue being closed if ($type == 'closed') { if (CRM::hasCustomerIntegration($prj_id)) { $crm = CRM::getInstance($prj_id); $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_email = $contact->getEmail(); } catch (CRMException $e) { $contact_email = ''; } foreach ($emails as $i => $email) { $email = Mail_Helper::getEmailAddress($email); if ($email == $contact_email) { unset($emails[$i]); $emails = array_values($emails); break; } } } } } if (!$emails) { return null; } $headers = false; switch ($type) { case 'closed': $data = Issue::getDetails($issue_id); $data['closer_name'] = User::getFullName(History::getIssueCloser($issue_id)); $subject = ev_gettext('Closed'); if ($entry_id) { $data['reason'] = Support::getEmail($entry_id); } break; case 'updated': // this should not be used anymore return false; case 'notes': $data = self::getNote($issue_id, $entry_id); $headers = array('Message-ID' => $data['note']['not_message_id']); if (@$data['note']['reference_msg_id'] != false) { $headers['In-Reply-To'] = $data['note']['reference_msg_id']; } else { $headers['In-Reply-To'] = Issue::getRootMessageID($issue_id); } $headers['References'] = Mail_Helper::fold(implode(' ', Mail_Helper::getReferences($issue_id, @$data['note']['reference_msg_id'], 'note'))); $subject = 'Note'; break; case 'emails': // this should not be used anymore return false; case 'files': $data = self::getAttachment($issue_id, $entry_id); $subject = 'File Attached'; break; } // FIXME: $data and $subject might be used uninitialized self::notifySubscribers($issue_id, $emails, $type, $data, $subject, $internal_only, $entry_id, $headers); }
/** * 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; }