/** * Method used to associate a support email with an existing * issue. * * @param integer $usr_id The user ID of the person performing this change * @param integer $issue_id The issue ID * @param array $items The list of email IDs to associate * @param boolean $authorize If the senders should be added the authorized repliers list * @return integer 1 if it worked, -1 otherwise */ public static function associate($usr_id, $issue_id, $items, $authorize = false) { $res = self::associateEmail($usr_id, $issue_id, $items); if ($res != 1) { return -1; } $stmt = 'SELECT sup_id, seb_full_email FROM {{%support_email}}, {{%support_email_body}} WHERE sup_id=seb_sup_id AND sup_id IN (' . DB_Helper::buildList($items) . ')'; $res = DB_Helper::getInstance()->getAll($stmt, $items); foreach ($res as $row) { // since downloading email should make the emails 'public', send 'false' below as the 'internal_only' flag $structure = Mime_Helper::decode($row['seb_full_email'], true, false); if (Mime_Helper::hasAttachments($structure)) { $has_attachments = 1; } else { $has_attachments = 0; } $t = array('issue_id' => $issue_id, 'message_id' => @$structure->headers['message-id'], 'from' => @$structure->headers['from'], 'to' => @$structure->headers['to'], 'cc' => @$structure->headers['cc'], 'subject' => @$structure->headers['subject'], 'body' => Mime_Helper::getMessageBody($structure), 'full_email' => $row['seb_full_email'], 'has_attachment' => $has_attachments, 'headers' => @$structure->headers); $prj_id = Issue::getProjectID($t['issue_id']); if (Workflow::shouldAutoAddToNotificationList($prj_id)) { self::addExtraRecipientsToNotificationList($prj_id, $t, false); } Notification::notifyNewEmail($usr_id, $issue_id, $t, false, false, '', $row['sup_id']); if ($authorize) { Authorized_Replier::manualInsert($issue_id, Mail_Helper::getEmailAddress(@$structure->headers['from']), false); } } return 1; }
/** * Method used to get the support email entry details. * * @access public * @param integer $ema_id The support email account ID * @param integer $sup_id The support email ID * @return array The email entry details */ function getEmailDetails($ema_id, $sup_id) { $stmt = "SELECT\n " . APP_TABLE_PREFIX . "support_email.*,\n " . APP_TABLE_PREFIX . "support_email_body.*\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email,\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email_body\n WHERE\n sup_id=seb_sup_id AND\n sup_id=" . Misc::escapeInteger($sup_id) . " AND\n sup_ema_id=" . Misc::escapeInteger($ema_id); $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return ""; } else { // gotta parse MIME based emails now $output = Mime_Helper::decode($res["seb_full_email"], true); $res["message"] = Mime_Helper::getMessageBody($output); // XXX: check which code relies on this var $res["attachments"] = Mime_Helper::getAttachmentCIDs($res["seb_full_email"]); $res["timestamp"] = Date_API::getUnixTimestamp($res['sup_date'], 'GMT'); $res["sup_date"] = Date_API::getFormattedDate($res["sup_date"]); $res["sup_subject"] = Mime_Helper::fixEncoding($res["sup_subject"]); // remove extra 'Re: ' from subject $res['reply_subject'] = Mail_API::removeExcessRe('Re: ' . $res["sup_subject"], true); $res["sup_from"] = Mime_Helper::fixEncoding($res["sup_from"]); $res["sup_to"] = Mime_Helper::fixEncoding($res["sup_to"]); if (!empty($res['sup_iss_id'])) { $res['reply_subject'] = Mail_API::formatSubject($res['sup_iss_id'], $res['reply_subject']); } return $res; } }
<?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";
/** * Converts a note to a draft or an email * * @access public * @param $note_id The id of the note * @param $target What the not should be converted too * @param $authorize_sender If the sender should be added to authorized senders list. */ function convertNote($note_id, $target, $authorize_sender = false) { $note_id = Misc::escapeInteger($note_id); $issue_id = Note::getIssueID($note_id); $email_account_id = Email_Account::getEmailAccount(); $blocked_message = Note::getBlockedMessage($note_id); $unknown_user = Note::getUnknownUser($note_id); $structure = Mime_Helper::decode($blocked_message, true, true); $body = Mime_Helper::getMessageBody($structure); $sender_email = strtolower(Mail_API::getEmailAddress($structure->headers['from'])); if ($target == 'email') { if (Mime_Helper::hasAttachments($blocked_message)) { $has_attachments = 1; } else { $has_attachments = 0; } list($blocked_message, $headers) = Mail_API::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_API::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 (Customer::hasCustomerIntegration($details['ema_prj_id'])) { // check for any customer contact association list($customer_id, ) = Customer::getCustomerIDByEmails($details['ema_prj_id'], array($sender_email)); if (!empty($customer_id)) { $t['customer_id'] = $customer_id; } } } 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, $blocked_message); // 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(Auth::getUserID(), $issue_id, $t, $internal_only, false, '', $sup_id); Issue::markAsUpdated($issue_id, $update_type); Note::remove($note_id, false); History::add($issue_id, Auth::getUserID(), History::getTypeID('note_converted_email'), "Note converted to e-mail (from: " . @$structure->headers['from'] . ") by " . User::getFullName(Auth::getUserID())); // now add sender as an authorized replier if ($authorize_sender) { Authorized_Replier::manualInsert($issue_id, @$structure->headers['from']); } } return $res; } else { // 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) { Note::remove($note_id, false); History::add($issue_id, Auth::getUserID(), History::getTypeID('note_converted_draft'), "Note converted to draft (from: " . @$structure->headers['from'] . ") by " . User::getFullName(Auth::getUserID())); } return $res; } }
/** * 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; }