Пример #1
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;
 }
Пример #2
0
 /**
  * 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;
 }
 /**
  * Called when a new message is recieved.
  *
  * @param   integer $prj_id The projectID
  * @param   integer $issue_id The ID of the issue.
  * @param   object $message An object containing the new email
  * @param   array $row The array of data that was inserted into the database.
  * @param   boolean $closing If we are closing the issue.
  */
 function handleNewEmail($prj_id, $issue_id, $message, $row = false, $closing = false)
 {
     $subject = $row['subject'];
     $body = $row['body'];
     preg_match('/(H|C)[A-Z0-9]{12,14}/', $subject, $header_matches);
     preg_match_all('/(H|C)[A-Z0-9]{12,14}/', $body, $body_matches);
     if ($header_matches[0]) {
         $refs[] = $header_matches[0];
     }
     foreach ($body_matches[0] as $body_match) {
         if ($body_match) {
             $refs[] = $body_match;
         }
     }
     $refs = @array_unique($refs);
     $stmt = "Select reference_number,ss_subscription_id from " . ETEL_TRANS_SUBS_TABLE_NOSUB . "\n\t\tWHERE\n\t\t\treference_number in ('" . @implode("','", $refs) . "')";
     $res = $GLOBALS["db_api"]->dbh->getRow($stmt);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
     } else {
         if ($res[0]) {
             Custom_Field::associateIssue($issue_id, 1, $res[0]);
         }
         if ($res[1]) {
             Custom_Field::associateIssue($issue_id, 4, $res[1]);
         }
     }
     $res = Authorized_Replier::manualInsert($issue_id, $row['from']);
     Issue::updateControlStatus($issue_id);
 }
Пример #4
0
 /**
  * Method used to associate a support email with an existing
  * issue.
  *
  * @access  public
  * @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
  */
 function associate($usr_id, $issue_id, $items, $authorize = false)
 {
     $res = Support::associateEmail($usr_id, $issue_id, $items);
     if ($res == 1) {
         $stmt = "SELECT\n                        sup_id,\n                        seb_full_email\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 IN (" . @implode(", ", Misc::escapeInteger($items)) . ")";
         $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
         for ($i = 0; $i < count($res); $i++) {
             // since downloading email should make the emails 'public', send 'false' below as the 'internal_only' flag
             $structure = Mime_Helper::decode($res[$i]['seb_full_email'], true, false);
             if (Mime_Helper::hasAttachments($res[$i]['seb_full_email'])) {
                 $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' => $res[$i]['seb_full_email'], 'has_attachment' => $has_attachments, 'headers' => @$structure->headers);
             Notification::notifyNewEmail($usr_id, $issue_id, $t, false, false, '', $res[$i]['sup_id']);
             if ($authorize) {
                 Authorized_Replier::manualInsert($issue_id, Mail_API::getEmailAddress(@$structure->headers['from']), false);
             }
         }
         return 1;
     } else {
         return -1;
     }
 }
Пример #5
0
// +----------------------------------------------------------------------+
// | Authors: Bryan Alsdorf <*****@*****.**>                             |
// +----------------------------------------------------------------------+
//
// @(#) $Id$
//
include_once "config.inc.php";
include_once APP_INC_PATH . "class.template.php";
include_once APP_INC_PATH . "class.auth.php";
include_once APP_INC_PATH . "class.project.php";
include_once APP_INC_PATH . "class.authorized_replier.php";
include_once APP_INC_PATH . "class.prefs.php";
include_once APP_INC_PATH . "db_access.php";
$tpl = new Template_API();
$tpl->setTemplate("authorized_replier.tpl.html");
Auth::checkAuthentication(APP_COOKIE, 'index.php?err=5', true);
$prj_id = Auth::getCurrentProject();
$issue_id = @$HTTP_POST_VARS["issue_id"] ? $HTTP_POST_VARS["issue_id"] : $HTTP_GET_VARS["iss_id"];
$tpl->assign("issue_id", $issue_id);
if (@$HTTP_POST_VARS["cat"] == "insert") {
    $res = Authorized_Replier::manualInsert($issue_id, $HTTP_POST_VARS['email']);
    $tpl->assign("insert_result", $res);
} elseif (@$HTTP_POST_VARS["cat"] == "delete") {
    $res = Authorized_Replier::removeRepliers($HTTP_POST_VARS["items"]);
    $tpl->assign("delete_result", $res);
}
list(, $repliers) = Authorized_Replier::getAuthorizedRepliers($issue_id);
$tpl->assign("list", $repliers);
$t = Project::getAddressBook($prj_id, $issue_id);
$tpl->assign("assoc_users", $t);
$tpl->displayTemplate();
 /**
  * Method used to remotely add an authorized replier to a given issue.
  *
  * @access  public
  * @param   integer $issue_id The issue ID
  * @param   integer $usr_id The user ID of the person performing the change
  * @param   boolean $replier The user ID of the authorized replier
  * @return  integer The status ID
  */
 function remoteAddAuthorizedReplier($issue_id, $usr_id, $replier)
 {
     $res = Authorized_Replier::manualInsert($issue_id, $replier, false);
     if ($res != -1) {
         // save a history entry about this...
         History::add($issue_id, $usr_id, History::getTypeID('remote_replier_added'), $replier . " remotely added to authorized repliers by " . User::getFullName($usr_id));
     }
     return $res;
 }
Пример #7
0
 /**
  * TODO: merge use of $options and $email arrays to just $email
  *
  * @param int $issue_id
  * @param string $type type of email
  * @param string $from
  * @param string $to
  * @param string $cc
  * @param string $subject
  * @param string $body
  * @param array $options optional parameters
  * - (int) parent_sup_id
  * - (array) iaf_ids attachment file ids
  * - (bool) add_unknown
  * - (bool) add_cc_to_ar
  * - (int) ema_id
  * @return int 1 if it worked, -1 otherwise
  */
 public static function sendEmail($issue_id, $type, $from, $to, $cc, $subject, $body, $options = array())
 {
     if ($to === null) {
         // BTW, $to = '' is ok
         Logger::app()->error('"To:" can not be NULL');
         return -1;
     }
     $parent_sup_id = isset($options['parent_sup_id']) ? $options['parent_sup_id'] : null;
     $iaf_ids = isset($options['iaf_ids']) ? (array) $options['iaf_ids'] : null;
     $add_unknown = isset($options['add_unknown']) ? (bool) $options['add_unknown'] : false;
     $add_cc_to_ar = isset($options['add_cc_to_ar']) ? (bool) $options['add_cc_to_ar'] : false;
     $ema_id = isset($options['ema_id']) ? (int) $options['ema_id'] : null;
     $current_usr_id = Auth::getUserID();
     $prj_id = Issue::getProjectID($issue_id);
     // if we are replying to an existing email, set the In-Reply-To: header accordingly
     $in_reply_to = $parent_sup_id ? self::getMessageIDByID($parent_sup_id) : false;
     // get ID of whoever is sending this.
     $sender_usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($from)) ?: false;
     // remove extra 'Re: ' from subject
     $subject = Mail_Helper::removeExcessRe($subject, true);
     $internal_only = false;
     $message_id = Mail_Helper::generateMessageID();
     // process any files being uploaded
     // from ajax upload, attachment file ids
     if ($iaf_ids) {
         // FIXME: is it correct to use sender from post data?
         $attach_usr_id = $sender_usr_id ?: $current_usr_id;
         Attachment::attachFiles($issue_id, $attach_usr_id, $iaf_ids, false, 'Attachment originated from outgoing email');
     }
     // hack needed to get the full headers of this web-based email
     $full_email = self::buildFullHeaders($issue_id, $message_id, $from, $to, $cc, $subject, $body, $in_reply_to, $iaf_ids);
     // email blocking should only be done if this is an email about an associated issue
     if ($issue_id) {
         $user_info = User::getNameEmail($current_usr_id);
         // check whether the current user is allowed to send this email to customers or not
         if (!self::isAllowedToEmail($issue_id, $user_info['usr_email'])) {
             // add the message body as a note
             $note = Mail_Helper::getCannedBlockedMsgExplanation() . $body;
             $note_options = array('full_message' => $full_email, 'is_blocked' => true);
             Note::insertNote($current_usr_id, $issue_id, $subject, $note, $note_options);
             $email_details = array('from' => $from, 'to' => $to, 'cc' => $cc, 'subject' => $subject, 'body' => &$body, 'message' => &$body, 'title' => $subject);
             Workflow::handleBlockedEmail($prj_id, $issue_id, $email_details, 'web');
             return 1;
         }
     }
     // only send a direct email if the user doesn't want to add the Cc'ed people to the notification list
     if (($add_unknown || Workflow::shouldAutoAddToNotificationList($prj_id)) && $issue_id) {
         // add the recipients to the notification list of the associated issue
         $recipients = array($to);
         $recipients = array_merge($recipients, self::getRecipientsCC($cc));
         foreach ($recipients as $address) {
             if ($address && !Notification::isIssueRoutingSender($issue_id, $address)) {
                 $actions = Notification::getDefaultActions($issue_id, $address, 'add_unknown_user');
                 Notification::subscribeEmail($current_usr_id, $issue_id, Mail_Helper::getEmailAddress($address), $actions);
             }
         }
     } else {
         // Usually when sending out emails associated to an issue, we would
         // simply insert the email in the table and call the Notification::notifyNewEmail() method,
         // but on this case we need to actually send the email to the recipients that are not
         // already in the notification list for the associated issue, if any.
         // In the case of replying to an email that is not yet associated with an issue, then
         // we are always directly sending the email, without using any notification list
         // functionality.
         if ($issue_id) {
             // send direct emails only to the unknown addresses, and leave the rest to be
             // catched by the notification list
             $from = Notification::getFixedFromHeader($issue_id, $from, 'issue');
             // build the list of unknown recipients
             if ($to) {
                 $recipients = array($to);
                 $recipients = array_merge($recipients, self::getRecipientsCC($cc));
             } else {
                 $recipients = self::getRecipientsCC($cc);
             }
             $unknowns = array();
             foreach ($recipients as $address) {
                 if (!Notification::isSubscribedToEmails($issue_id, $address)) {
                     $unknowns[] = $address;
                 }
             }
             if ($unknowns) {
                 $to2 = array_shift($unknowns);
                 $cc2 = implode('; ', $unknowns);
                 // send direct emails
                 self::sendDirectEmail($issue_id, $from, $to2, $cc2, $subject, $body, $_FILES['attachment'], $message_id, $sender_usr_id);
             }
         } else {
             // send direct emails to all recipients, since we don't have an associated issue
             $project_info = Project::getOutgoingSenderAddress(Auth::getCurrentProject());
             // use the project-related outgoing email address, if there is one
             if (!empty($project_info['email'])) {
                 $from = Mail_Helper::getFormattedName(User::getFullName($current_usr_id), $project_info['email']);
             } else {
                 // otherwise, use the real email address for the current user
                 $from = User::getFromHeader($current_usr_id);
             }
             // send direct emails
             self::sendDirectEmail($issue_id, $from, $to, $cc, $subject, $body, $_FILES['attachment'], $message_id);
         }
     }
     if ($add_cc_to_ar) {
         foreach (self::getRecipientsCC($cc) as $recipient) {
             Authorized_Replier::manualInsert($issue_id, $recipient);
         }
     }
     $email = array('customer_id' => 'NULL', 'issue_id' => $issue_id, 'ema_id' => $ema_id, 'message_id' => $message_id, 'date' => Date_Helper::getCurrentDateGMT(), 'from' => $from, 'to' => $to, 'cc' => $cc, 'subject' => $subject, 'body' => $body, 'full_email' => $full_email);
     // associate this new email with a customer, if appropriate
     if (Auth::getCurrentRole() == User::ROLE_CUSTOMER) {
         if ($issue_id) {
             $crm = CRM::getInstance($prj_id);
             try {
                 $contact = $crm->getContact(User::getCustomerContactID($current_usr_id));
                 $issue_contract = $crm->getContract(Issue::getContractID($issue_id));
                 if ($contact->canAccessContract($issue_contract)) {
                     $email['customer_id'] = $issue_contract->getCustomerID();
                 }
             } catch (CRMException $e) {
             }
         } else {
             $customer_id = User::getCustomerID($current_usr_id);
             if ($customer_id && $customer_id != -1) {
                 $email['customer_id'] = $customer_id;
             }
         }
     }
     $email['has_attachment'] = $iaf_ids ? 1 : 0;
     $structure = Mime_Helper::decode($full_email, true, false);
     $email['headers'] = $structure->headers;
     self::insertEmail($email, $structure, $sup_id);
     if ($issue_id) {
         // need to send a notification
         Notification::notifyNewEmail($current_usr_id, $issue_id, $email, $internal_only, false, $type, $sup_id);
         // mark this issue as updated
         $has_customer = $email['customer_id'] && $email['customer_id'] != 'NULL';
         if ($has_customer && (!$current_usr_id || User::getRoleByUser($current_usr_id, $prj_id) == User::ROLE_CUSTOMER)) {
             Issue::markAsUpdated($issue_id, 'customer action');
         } else {
             if ($sender_usr_id && User::getRoleByUser($sender_usr_id, $prj_id) > User::ROLE_CUSTOMER) {
                 Issue::markAsUpdated($issue_id, 'staff response');
             } else {
                 Issue::markAsUpdated($issue_id, 'user response');
             }
         }
         History::add($issue_id, $current_usr_id, 'email_sent', 'Outgoing email sent by {user}', array('user' => User::getFullName($current_usr_id)));
     }
     return 1;
 }
Пример #8
0
// | Authors: Bryan Alsdorf <*****@*****.**>                             |
// +----------------------------------------------------------------------+
require_once dirname(__FILE__) . '/../init.php';
$tpl = new Template_Helper();
$tpl->setTemplate('authorized_replier.tpl.html');
Auth::checkAuthentication(APP_COOKIE, 'index.php?err=5', true);
$prj_id = Auth::getCurrentProject();
$issue_id = @$_POST['issue_id'] ? $_POST['issue_id'] : $_GET['iss_id'];
$tpl->assign('issue_id', $issue_id);
if (!Access::canViewAuthorizedRepliers($issue_id, Auth::getUserID())) {
    $tpl->setTemplate('permission_denied.tpl.html');
    $tpl->displayTemplate();
    exit;
}
if (@$_POST['cat'] == 'insert') {
    $res = Authorized_Replier::manualInsert($issue_id, $_POST['email']);
    if ($res == 1) {
        Misc::setMessage(ev_gettext('Thank you, the authorized replier was inserted successfully.'));
    } elseif ($res == -1) {
        Misc::setMessage(ev_gettext('An error occurred while trying to insert the authorized replier.'), Misc::MSG_ERROR);
    } elseif ($res == -2) {
        Misc::setMessage(ev_gettext("Users with a role of 'customer' or below are not allowed to be added to the authorized repliers list."), Misc::MSG_ERROR);
    }
} elseif (@$_POST['cat'] == 'delete') {
    $res = Authorized_Replier::removeRepliers($_POST['items']);
    if ($res == 1) {
        Misc::setMessage(ev_gettext('Thank you, the authorized replier was deleted successfully.'));
    } elseif ($res == -1) {
        Misc::setMessage(ev_gettext('An error occurred while trying to delete the authorized replier.'), Misc::MSG_ERROR);
    }
}