/** * Method used to remove an existing set of requirements. * * @return integer -1 if an error occurred or 1 otherwise */ public static function remove() { $items = $_POST['item']; $itemlist = DB_Helper::buildList($items); $stmt = "SELECT\n isr_iss_id\n FROM\n {{%issue_requirement}}\n WHERE\n isr_id IN ({$itemlist})"; $issue_id = DB_Helper::getInstance()->getOne($stmt, $items); $stmt = "DELETE FROM\n {{%issue_requirement}}\n WHERE\n isr_id IN ({$itemlist})"; try { DB_Helper::getInstance()->query($stmt, $items); } catch (DbException $e) { return -1; } Issue::markAsUpdated($issue_id); // need to save a history entry for this $usr_id = Auth::getUserID(); History::add($issue_id, $usr_id, 'impact_analysis_removed', 'Impact analysis removed by {user}', array('user' => User::getFullName($usr_id))); return 1; }
/** * Method used to send an email from the user interface. * * @access public * @return integer 1 if it worked, -1 otherwise */ function sendEmail($parent_sup_id = FALSE) { global $HTTP_POST_VARS, $HTTP_SERVER_VARS; // if we are replying to an existing email, set the In-Reply-To: header accordingly if ($parent_sup_id) { $in_reply_to = Support::getMessageIDByID($parent_sup_id); } else { $in_reply_to = false; } // get ID of whoever is sending this. $sender_usr_id = User::getUserIDByEmail(Mail_API::getEmailAddress($HTTP_POST_VARS["from"])); if (empty($sender_usr_id)) { $sender_usr_id = false; } // get type of email this is if (!empty($HTTP_POST_VARS['type'])) { $type = $HTTP_POST_VARS['type']; } else { $type = ''; } // remove extra 'Re: ' from subject $HTTP_POST_VARS['subject'] = Mail_API::removeExcessRe($HTTP_POST_VARS['subject'], true); $internal_only = false; $message_id = Mail_API::generateMessageID(); // hack needed to get the full headers of this web-based email $full_email = Support::buildFullHeaders($HTTP_POST_VARS["issue_id"], $message_id, $HTTP_POST_VARS["from"], $HTTP_POST_VARS["to"], $HTTP_POST_VARS["cc"], $HTTP_POST_VARS["subject"], $HTTP_POST_VARS["message"], $in_reply_to); // email blocking should only be done if this is an email about an associated issue if (!empty($HTTP_POST_VARS['issue_id'])) { $user_info = User::getNameEmail(Auth::getUserID()); // check whether the current user is allowed to send this email to customers or not if (!Support::isAllowedToEmail($HTTP_POST_VARS["issue_id"], $user_info['usr_email'])) { // add the message body as a note $HTTP_POST_VARS['blocked_msg'] = $full_email; $HTTP_POST_VARS['title'] = $HTTP_POST_VARS["subject"]; $HTTP_POST_VARS['note'] = Mail_API::getCannedBlockedMsgExplanation() . $HTTP_POST_VARS["message"]; Note::insert(Auth::getUserID(), $HTTP_POST_VARS["issue_id"]); Workflow::handleBlockedEmail(Issue::getProjectID($HTTP_POST_VARS['issue_id']), $HTTP_POST_VARS['issue_id'], $HTTP_POST_VARS, '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 (@$HTTP_POST_VARS['add_unknown'] == 'yes') { if (!empty($HTTP_POST_VARS['issue_id'])) { // add the recipients to the notification list of the associated issue $recipients = array($HTTP_POST_VARS['to']); $recipients = array_merge($recipients, Support::getRecipientsCC($HTTP_POST_VARS['cc'])); for ($i = 0; $i < count($recipients); $i++) { if (!empty($recipients[$i]) && !Notification::isIssueRoutingSender($HTTP_POST_VARS["issue_id"], $recipients[$i])) { Notification::subscribeEmail(Auth::getUserID(), $HTTP_POST_VARS["issue_id"], Mail_API::getEmailAddress($recipients[$i]), array('emails')); } } } } 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 (!empty($HTTP_POST_VARS['issue_id'])) { // send direct emails only to the unknown addresses, and leave the rest to be // catched by the notification list $from = Notification::getFixedFromHeader($HTTP_POST_VARS['issue_id'], $HTTP_POST_VARS['from'], 'issue'); // build the list of unknown recipients if (!empty($HTTP_POST_VARS['to'])) { $recipients = array($HTTP_POST_VARS['to']); $recipients = array_merge($recipients, Support::getRecipientsCC($HTTP_POST_VARS['cc'])); } else { $recipients = Support::getRecipientsCC($HTTP_POST_VARS['cc']); } $unknowns = array(); for ($i = 0; $i < count($recipients); $i++) { if (!Notification::isSubscribedToEmails($HTTP_POST_VARS['issue_id'], $recipients[$i])) { $unknowns[] = $recipients[$i]; } } if (count($unknowns) > 0) { $to = array_shift($unknowns); $cc = implode('; ', $unknowns); // send direct emails Support::sendDirectEmail($HTTP_POST_VARS['issue_id'], $from, $to, $cc, $HTTP_POST_VARS['subject'], $HTTP_POST_VARS['message'], $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_API::getFormattedName(User::getFullName(Auth::getUserID()), $project_info['email']); } else { // otherwise, use the real email address for the current user $from = User::getFromHeader(Auth::getUserID()); } // send direct emails Support::sendDirectEmail($HTTP_POST_VARS['issue_id'], $from, $HTTP_POST_VARS['to'], $HTTP_POST_VARS['cc'], $HTTP_POST_VARS['subject'], $HTTP_POST_VARS['message'], $message_id); } } $t = array('customer_id' => 'NULL', 'issue_id' => $HTTP_POST_VARS["issue_id"] ? $HTTP_POST_VARS["issue_id"] : 0, 'ema_id' => $HTTP_POST_VARS['ema_id'], 'message_id' => $message_id, 'date' => Date_API::getCurrentDateGMT(), 'from' => $HTTP_POST_VARS['from'], 'to' => $HTTP_POST_VARS['to'], 'cc' => @$HTTP_POST_VARS['cc'], 'subject' => @$HTTP_POST_VARS['subject'], 'body' => $HTTP_POST_VARS['message'], 'full_email' => $full_email, 'has_attachment' => 0); // associate this new email with a customer, if appropriate if (Auth::getCurrentRole() == User::getRoleID('Customer')) { $customer_id = User::getCustomerID(Auth::getUserID()); if (!empty($customer_id) && $customer_id != -1) { $t['customer_id'] = $customer_id; } } $structure = Mime_Helper::decode($full_email, true, false); $t['headers'] = $structure->headers; $res = Support::insertEmail($t, $structure, $sup_id); if (!empty($HTTP_POST_VARS["issue_id"])) { // need to send a notification Notification::notifyNewEmail(Auth::getUserID(), $HTTP_POST_VARS["issue_id"], $t, $internal_only, false, $type, $sup_id); // mark this issue as updated if (!empty($t['customer_id']) && $t['customer_id'] != 'NULL') { Issue::markAsUpdated($HTTP_POST_VARS["issue_id"], 'customer action'); } else { if (!empty($sender_usr_id) && User::getRoleByUser($sender_usr_id, Issue::getProjectID($HTTP_POST_VARS['issue_id'])) > User::getRoleID('Customer')) { Issue::markAsUpdated($HTTP_POST_VARS["issue_id"], 'staff response'); } else { Issue::markAsUpdated($HTTP_POST_VARS["issue_id"], 'user response'); } } // save a history entry for this History::add($HTTP_POST_VARS["issue_id"], Auth::getUserID(), History::getTypeID('email_sent'), 'Outgoing email sent by ' . User::getFullName(Auth::getUserID())); // also update the last_response_date field for the associated issue if (Auth::getCurrentRole() > User::getRoleID('Customer')) { $stmt = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n SET\n iss_last_response_date='" . Date_API::getCurrentDateGMT() . "'\n WHERE\n iss_id=" . Misc::escapeInteger($HTTP_POST_VARS["issue_id"]); $GLOBALS["db_api"]->dbh->query($stmt); $stmt = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n SET\n iss_first_response_date='" . Date_API::getCurrentDateGMT() . "'\n WHERE\n iss_first_response_date IS NULL AND\n iss_id=" . Misc::escapeInteger($HTTP_POST_VARS["issue_id"]); $GLOBALS["db_api"]->dbh->query($stmt); } } return 1; }
/** * Method used to update an existing draft response. * * @access public * @param integer $issue_id The issue ID * @param integer $emd_id The email draft ID * @param string $to The primary recipient of the draft * @param string $cc The secondary recipients of the draft * @param string $subject The subject of the draft * @param string $message The draft body * @param integer $parent_id The ID of the email that this draft is replying to, if any * @return integer 1 if the update worked, -1 otherwise */ function update($issue_id, $emd_id, $to, $cc, $subject, $message, $parent_id = FALSE) { $issue_id = Misc::escapeInteger($issue_id); $emd_id = Misc::escapeInteger($emd_id); $parent_id = Misc::escapeInteger($issue_id); if (empty($parent_id)) { $parent_id = 'NULL'; } $usr_id = Auth::getUserID(); // update previous draft and insert new record $stmt = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft\n SET\n emd_updated_date='" . Date_API::getCurrentDateGMT() . "',\n emd_status = 'edited'\n WHERE\n emd_id={$emd_id}"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { Issue::markAsUpdated($issue_id, "draft saved"); History::add($issue_id, $usr_id, History::getTypeID('draft_updated'), 'Email message draft updated by ' . User::getFullName($usr_id)); Draft::saveEmail($issue_id, $to, $cc, $subject, $message, $parent_id, false, false); return 1; } }
/** * Method used to update the values stored in the database. * * @access public * @return integer 1 if the update worked properly, any other value otherwise */ function updateValues() { global $HTTP_POST_VARS; $prj_id = Auth::getCurrentProject(); $issue_id = Misc::escapeInteger($HTTP_POST_VARS["issue_id"]); $old_values = Custom_Field::getValuesByIssue($prj_id, $issue_id); // get the types for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_type\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id IN (" . implode(", ", Misc::escapeInteger(@array_keys($HTTP_POST_VARS['custom_fields']))) . ")"; $field_types = $GLOBALS["db_api"]->dbh->getAssoc($stmt); // get the titles for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_title\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id IN (" . implode(", ", Misc::escapeInteger(@array_keys($HTTP_POST_VARS['custom_fields']))) . ")"; $field_titles = $GLOBALS["db_api"]->dbh->getAssoc($stmt); $updated_fields = array(); foreach ($HTTP_POST_VARS["custom_fields"] as $fld_id => $value) { $fld_id = Misc::escapeInteger($fld_id); // security check $sql = "SELECT\n fld_min_role\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id = {$fld_id}"; $min_role = $GLOBALS["db_api"]->dbh->getOne($sql); if ($min_role > Auth::getCurrentRole()) { continue; } $option_types = array('multiple', 'combo'); if (!in_array($field_types[$fld_id], $option_types)) { // check if this is a date field if ($field_types[$fld_id] == 'date') { $value = $value['Year'] . "-" . $value['Month'] . "-" . $value['Day']; if ($value == '--') { $value = ''; } } // first check if there is actually a record for this field for the issue $stmt = "SELECT\n icf_id,\n icf_value\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n WHERE\n icf_iss_id=" . $issue_id . " AND\n icf_fld_id={$fld_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 -1; } $icf_id = $res['icf_id']; $icf_value = $res['icf_value']; if ($icf_value == $value) { continue; } if (empty($icf_id)) { // record doesn't exist, insert new record $stmt = "INSERT IGNORE INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n (\n icf_iss_id,\n icf_fld_id,\n icf_value\n ) VALUES (\n " . $issue_id . ",\n {$fld_id},\n '" . Misc::escapeString($value) . "'\n )"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } } else { // record exists, update it $stmt = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n SET\n icf_value='" . Misc::escapeString($value) . "'\n WHERE\n icf_id={$icf_id}"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } } if ($field_types[$fld_id] == 'textarea') { $updated_fields[$field_titles[$fld_id]] = ''; } else { $updated_fields[$field_titles[$fld_id]] = History::formatChanges($icf_value, $value); } } else { $old_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id, true); if (!is_array($old_value)) { $old_value = array($old_value); } if (!is_array($value)) { $value = array($value); } if (count(array_diff($old_value, $value)) > 0 || count(array_diff($value, $old_value)) > 0) { $old_display_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id); // need to remove all associated options from issue_custom_field and then // add the selected options coming from the form Custom_Field::removeIssueAssociation($fld_id, $HTTP_POST_VARS["issue_id"]); if (@count($value) > 0) { Custom_Field::associateIssue($HTTP_POST_VARS["issue_id"], $fld_id, $value); } $new_display_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id); $updated_fields[$field_titles[$fld_id]] = History::formatChanges($old_display_value, $new_display_value); } } } Workflow::handleCustomFieldsUpdated($prj_id, $issue_id, $old_values, Custom_Field::getValuesByIssue($prj_id, $issue_id)); Issue::markAsUpdated($HTTP_POST_VARS["issue_id"]); // need to save a history entry for this if (count($updated_fields) > 0) { // log the changes $changes = ''; $i = 0; foreach ($updated_fields as $key => $value) { if ($i > 0) { $changes .= "; "; } if (!empty($value)) { $changes .= "{$key}: {$value}"; } else { $changes .= "{$key}"; } $i++; } History::add($HTTP_POST_VARS["issue_id"], Auth::getUserID(), History::getTypeID('custom_field_updated'), "Custom field updated ({$changes}) by " . User::getFullName(Auth::getUserID())); } return 1; }
/** * Method used to update the values stored in the database. * * @return integer 1 if the update worked properly, any other value otherwise */ public static function updateValues() { $prj_id = Auth::getCurrentProject(); $issue_id = $_POST['issue_id']; $old_values = self::getValuesByIssue($prj_id, $issue_id); if (isset($_POST['custom_fields']) && count($_POST['custom_fields']) > 0) { $custom_fields = $_POST['custom_fields']; // get the types for all of the custom fields being submitted $cf = array_keys($custom_fields); $cf_list = DB_Helper::buildList($cf); $stmt = "SELECT\n fld_id,\n fld_type\n FROM\n {{%custom_field}}\n WHERE\n fld_id IN ({$cf_list})"; $field_types = DB_Helper::getInstance()->getPair($stmt, $cf); // get the titles for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_title\n FROM\n {{%custom_field}}\n WHERE\n fld_id IN ({$cf_list})"; $field_titles = DB_Helper::getInstance()->getPair($stmt, $cf); $updated_fields = array(); foreach ($custom_fields as $fld_id => $value) { // security check $sql = 'SELECT fld_min_role FROM {{%custom_field}} WHERE fld_id = ?'; $min_role = DB_Helper::getInstance()->getOne($sql, array($fld_id)); if ($min_role > Auth::getCurrentRole()) { continue; } $option_types = array('multiple', 'combo', 'checkbox'); if (!in_array($field_types[$fld_id], $option_types)) { // check if this is a date field $fld_db_name = self::getDBValueFieldNameByType($field_types[$fld_id]); // first check if there is actually a record for this field for the issue $stmt = "SELECT\n icf_id,\n {$fld_db_name} as value\n FROM\n {{%issue_custom_field}}\n WHERE\n icf_iss_id=? AND\n icf_fld_id=?"; try { $res = DB_Helper::getInstance()->getRow($stmt, array($issue_id, $fld_id)); } catch (DbException $e) { return -1; } $icf_id = $res['icf_id']; $old_value = $res['value']; if ($old_value == $value) { continue; } if (empty($icf_id)) { // record doesn't exist, insert new record $stmt = "INSERT INTO\n {{%issue_custom_field}}\n (\n icf_iss_id,\n icf_fld_id,\n {$fld_db_name}\n ) VALUES (\n ?, ?, ?\n )"; $params = array($issue_id, $fld_id, $value); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } } else { // record exists, update it $stmt = "UPDATE\n {{%issue_custom_field}}\n SET\n {$fld_db_name}=?\n WHERE\n icf_id=?"; $params = array($value, $icf_id); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } } if ($field_types[$fld_id] == 'textarea') { $updated_fields[$field_titles[$fld_id]] = ''; } else { $updated_fields[$field_titles[$fld_id]] = History::formatChanges($old_value, $value); } } else { $old_value = self::getDisplayValue($issue_id, $fld_id, true); if (!is_array($old_value)) { $old_value = array($old_value); } if (!is_array($value)) { $value = array($value); } if (count(array_diff($old_value, $value)) > 0 || count(array_diff($value, $old_value)) > 0) { $old_display_value = self::getDisplayValue($issue_id, $fld_id); // need to remove all associated options from issue_custom_field and then // add the selected options coming from the form self::removeIssueAssociation($fld_id, $issue_id); if (@count($value) > 0) { self::associateIssue($issue_id, $fld_id, $value); } $new_display_value = self::getDisplayValue($issue_id, $fld_id); $updated_fields[$field_titles[$fld_id]] = History::formatChanges($old_display_value, $new_display_value); } } } Workflow::handleCustomFieldsUpdated($prj_id, $issue_id, $old_values, self::getValuesByIssue($prj_id, $issue_id)); Issue::markAsUpdated($issue_id); // need to save a history entry for this if (count($updated_fields) > 0) { // log the changes $changes = ''; $i = 0; foreach ($updated_fields as $key => $value) { if ($i > 0) { $changes .= '; '; } if (!empty($value)) { $changes .= "{$key}: {$value}"; } else { $changes .= "{$key}"; } $i++; } $usr_id = Auth::getUserID(); History::add($issue_id, $usr_id, 'custom_field_updated', 'Custom field updated ({changes}) by {user}', array('changes' => $changes, 'user' => User::getFullName($usr_id))); } } return 1; }
/** * Method used to remove a specific phone support entry from the * application. * * @access public * @param integer $phone_id The phone support entry ID * @return integer 1 if the removal worked, -1 or -2 otherwise */ function remove($phone_id) { $phone_id = Misc::escapeInteger($phone_id); $stmt = "SELECT\n phs_iss_id,\n phs_ttr_id,\n phs_usr_id\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "phone_support\n WHERE\n phs_id={$phone_id}"; $details = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC); if ($details['phs_usr_id'] != Auth::getUserID()) { return -2; } $stmt = "DELETE FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "phone_support\n WHERE\n phs_id={$phone_id}"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { Issue::markAsUpdated($details["phs_iss_id"]); // need to save a history entry for this History::add($details["phs_iss_id"], Auth::getUserID(), History::getTypeID('phone_entry_removed'), 'Phone Support entry removed by ' . User::getFullName(Auth::getUserID())); if (!empty($details["phs_ttr_id"])) { $time_result = Time_Tracking::removeEntry($details["phs_ttr_id"], $details['phs_usr_id']); if ($time_result == 1) { return 2; } else { return $time_result; } } else { return 1; } } }
/** * Method used to remotely record a time tracking entry. * * @access public * @param integer $issue_id The issue ID * @param integer $usr_id The user ID * @param integer $cat_id The time tracking category ID * @param string $summary The summary of the work entry * @param integer $time_spent The time spent in minutes * @return integer 1 if the insert worked, -1 otherwise */ function recordRemoteEntry($issue_id, $usr_id, $cat_id, $summary, $time_spent) { $stmt = "INSERT INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "time_tracking\n (\n ttr_ttc_id,\n ttr_iss_id,\n ttr_usr_id,\n ttr_created_date,\n ttr_time_spent,\n ttr_summary\n ) VALUES (\n " . Misc::escapeInteger($cat_id) . ",\n " . Misc::escapeInteger($issue_id) . ",\n " . Misc::escapeInteger($usr_id) . ",\n '" . Date_API::getCurrentDateGMT() . "',\n " . Misc::escapeInteger($time_spent) . ",\n '" . Misc::escapeString($summary) . "'\n )"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { Issue::markAsUpdated($issue_id); // need to save a history entry for this History::add($issue_id, $usr_id, History::getTypeID('remote_time_added'), 'Time tracking entry submitted remotely by ' . User::getFullName($usr_id)); return 1; } }
/** * Method used to remotely record a time tracking entry. * * @param integer $issue_id The issue ID * @param integer $usr_id The user ID * @param integer $cat_id The time tracking category ID * @param string $summary The summary of the work entry * @param integer $time_spent The time spent in minutes * @return integer 1 if the insert worked, -1 otherwise */ public static function recordRemoteTimeEntry($issue_id, $usr_id, $cat_id, $summary, $time_spent) { $stmt = 'INSERT INTO {{%time_tracking}} ( ttr_ttc_id, ttr_iss_id, ttr_usr_id, ttr_created_date, ttr_time_spent, ttr_summary ) VALUES ( ?, ?, ?, ?, ?, ? )'; $params = array($cat_id, $issue_id, $usr_id, Date_Helper::getCurrentDateGMT(), $time_spent, $summary); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } Issue::markAsUpdated($issue_id); History::add($issue_id, $usr_id, 'remote_time_added', 'Time tracking entry submitted remotely by {user}', array('user' => User::getFullName($usr_id))); return 1; }
/** * Method used to update the details of a given subscription. * * @param $issue_id * @param integer $sub_id The subscription ID * @param $email * @return integer 1 if the update worked, -1 otherwise */ public static function update($issue_id, $sub_id, $email) { $usr_id = User::getUserIDByEmail(strtolower(Mail_Helper::getEmailAddress($email)), true); if (!empty($usr_id)) { $email = ''; } else { $usr_id = 0; } $prj_id = Issue::getProjectID($issue_id); // call workflow to modify actions or cancel adding this user. $actions = array(); $subscriber_usr_id = false; $workflow = Workflow::handleSubscription($prj_id, $issue_id, $subscriber_usr_id, $email, $actions); if ($workflow === false) { // cancel subscribing the user return -2; } // always set the type of notification to issue-level $stmt = "UPDATE\n {{%subscription}}\n SET\n sub_level='issue',\n sub_email=?,\n sub_usr_id=?\n WHERE\n sub_id=?"; try { DB_Helper::getInstance()->query($stmt, array($email, $usr_id, $sub_id)); } catch (DbException $e) { return -1; } $stmt = 'DELETE FROM {{%subscription_type}} WHERE sbt_sub_id=?'; DB_Helper::getInstance()->query($stmt, array($sub_id)); // now add them all again foreach ($_POST['actions'] as $sbt_type) { // FIXME: $sbt_type not validated for sane values self::addType($sub_id, $sbt_type); } // need to mark the issue as updated Issue::markAsUpdated($issue_id); $current_usr_id = Auth::getUserID(); History::add($issue_id, $current_usr_id, 'notification_updated', "Notification list entry ('{subscriber}') updated by {user}", array('subscriber' => self::getSubscriber($sub_id), 'user' => User::getFullName($current_usr_id))); return 1; }
/** * Method used to remove an existing set of requirements. * * @access public * @return integer -1 if an error occurred or 1 otherwise */ function remove() { global $HTTP_POST_VARS; $items = implode(", ", Misc::escapeInteger($HTTP_POST_VARS["item"])); $stmt = "SELECT\n isr_iss_id\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_requirement\n WHERE\n isr_id IN ({$items})"; $issue_id = $GLOBALS["db_api"]->dbh->getOne($stmt); $stmt = "DELETE FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_requirement\n WHERE\n isr_id IN ({$items})"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { Issue::markAsUpdated($issue_id); // need to save a history entry for this History::add($issue_id, Auth::getUserID(), History::getTypeID('impact_analysis_removed'), 'Impact analysis removed by ' . User::getFullName(Auth::getUserID())); return 1; } }
/** * Method used to associate a new checkin with an existing issue * * @access public * @param integer $issue_id The issue ID * @param integer $i The offset of the file that was changed * @return integer 1 if the update worked, -1 otherwise */ function logCheckin($issue_id, $i) { global $HTTP_GET_VARS; $stmt = "INSERT INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_checkin\n (\n isc_iss_id,\n isc_module,\n isc_filename,\n isc_old_version,\n isc_new_version,\n isc_created_date,\n isc_username,\n isc_commit_msg\n ) VALUES (\n {$issue_id},\n '" . Misc::escapeString($HTTP_GET_VARS["module"]) . "',\n '" . Misc::escapeString($HTTP_GET_VARS["files"][$i]) . "',\n '" . Misc::escapeString($HTTP_GET_VARS["old_versions"][$i]) . "',\n '" . Misc::escapeString($HTTP_GET_VARS["new_versions"][$i]) . "',\n '" . Date_API::getCurrentDateGMT() . "',\n '" . Misc::escapeString($HTTP_GET_VARS["username"]) . "',\n '" . Misc::escapeString($HTTP_GET_VARS["commit_msg"]) . "'\n )"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { // need to mark this issue as updated Issue::markAsUpdated($issue_id, 'scm checkin'); // need to save a history entry for this History::add($issue_id, APP_SYSTEM_USER_ID, History::getTypeID('scm_checkin_associated'), 'SCM Checkins associated by SCM user \'' . $HTTP_GET_VARS["username"] . '\'.'); return 1; } }
/** * Routes an email to the correct issue. * * @param string $full_message The full email message, including headers * @return mixed true or array(ERROR_CODE, ERROR_STRING) in case of failure */ public static function route_emails($full_message) { // need some validation here if (empty($full_message)) { return array(self::EX_NOINPUT, ev_gettext('Error: The email message was empty') . ".\n"); } // save the full message for logging purposes Support::saveRoutedEmail($full_message); // check if the email routing interface is even supposed to be enabled $setup = Setup::get(); if ($setup['email_routing']['status'] != 'enabled') { return array(self::EX_CONFIG, ev_gettext('Error: The email routing interface is disabled.') . "\n"); } if (empty($setup['email_routing']['address_prefix'])) { return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address prefix.') . "\n"); } if (empty($setup['email_routing']['address_host'])) { return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address domain.') . "\n"); } // associate routed emails to the internal system account $sys_account = User::getNameEmail(APP_SYSTEM_USER_ID); if (empty($sys_account['usr_email'])) { return array(self::EX_CONFIG, ev_gettext('Error: The associated user for the email routing interface needs to be set.') . "\n"); } unset($sys_account); // 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); } // remove the reply-to: header if (preg_match('/^reply-to:.*/im', $full_message)) { $full_message = preg_replace("/^(reply-to:).*\n/im", '', $full_message, 1); } AuthCookie::setAuthCookie(APP_SYSTEM_USER_ID); $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'], 'email'); } // we need to try the Cc header as well if (empty($issue_id) and isset($structure->headers['cc'])) { $issue_id = self::getMatchingIssueIDs($structure->headers['cc'], 'email'); } if (empty($issue_id)) { return array(self::EX_DATAERR, ev_gettext('Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.') . "\n"); } $issue_prj_id = Issue::getProjectID($issue_id); if (empty($issue_prj_id)) { return array(self::EX_DATAERR, ev_gettext('Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.') . "\n"); } $email_account_id = Email_Account::getEmailAccount($issue_prj_id); if (empty($email_account_id)) { return array(self::EX_CONFIG, ev_gettext('Error: Please provide the email account ID.') . "\n"); } $body = $structure->body; // hack for clients that set more then one from header if (is_array($structure->headers['from'])) { $structure->headers['from'] = $structure->headers['from'][0]; } // associate the email to the issue $parts = array(); Mime_Helper::parse_output($structure, $parts); // get the sender's email address $sender_email = strtolower(Mail_Helper::getEmailAddress($structure->headers['from'])); // strip out the warning message sent to staff users if ($setup['email_routing']['status'] == 'enabled' && $setup['email_routing']['warning']['status'] == 'enabled') { $full_message = Mail_Helper::stripWarningMessage($full_message); $body = Mail_Helper::stripWarningMessage($body); } $prj_id = Issue::getProjectID($issue_id); AuthCookie::setAuthCookie(APP_SYSTEM_USER_ID); AuthCookie::setProjectCookie($prj_id); if (Mime_Helper::hasAttachments($structure)) { $has_attachments = 1; } else { $has_attachments = 0; } // remove certain CC addresses if (!empty($structure->headers['cc']) && $setup['smtp']['save_outgoing_email'] == 'yes') { $ccs = explode(',', @$structure->headers['cc']); foreach ($ccs as $i => $address) { if (Mail_Helper::getEmailAddress($address) == $setup['smtp']['save_address']) { unset($ccs[$i]); } } $structure->headers['cc'] = implode(', ', $ccs); } // Remove excess Re's $structure->headers['subject'] = Mail_Helper::removeExcessRe(@$structure->headers['subject'], true); $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' => @$full_message, 'has_attachment' => $has_attachments, 'headers' => @$structure->headers); // automatically associate this incoming email with a customer if (CRM::hasCustomerIntegration($prj_id)) { $crm = CRM::getInstance($prj_id); if (!empty($structure->headers['from'])) { 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'])) { $t['customer_id'] = null; } if (Support::blockEmailIfNeeded($t)) { return true; } // re-write Threading headers if needed list($t['full_email'], $t['headers']) = Mail_Helper::rewriteThreadingHeaders($t['issue_id'], $t['full_email'], $t['headers'], 'email'); $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; $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(), $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_Helper::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($issue_id, 'customer action'); } else { if (!empty($usr_id) && $usr_id != APP_SYSTEM_USER_ID && User::getRoleByUser($usr_id, $prj_id) > User::ROLE_CUSTOMER) { Issue::markAsUpdated($issue_id, 'staff response'); } else { Issue::markAsUpdated($issue_id, 'user response'); } } // log routed email History::add($issue_id, $usr_id, 'email_routed', 'Email routed from {from}', array('from' => $structure->headers['from'])); } return true; }
/** * Method used to update the details of a given subscription. * * @access public * @param integer $sub_id The subscription ID * @return integer 1 if the update worked, -1 otherwise */ function update($sub_id) { global $HTTP_POST_VARS; $sub_id = Misc::escapeInteger($sub_id); $stmt = "SELECT\r\n sub_iss_id,\r\n sub_usr_id\r\n FROM\r\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "subscription\r\n WHERE\r\n sub_id={$sub_id}"; list($issue_id, $usr_id) = $GLOBALS["db_api"]->dbh->getRow($stmt); $email = strtolower(Mail_API::getEmailAddress($HTTP_POST_VARS["email"])); $usr_id = User::getUserIDByEmail($email); if (!empty($usr_id)) { $email = ''; } else { $usr_id = 0; $email = Misc::escapeString($HTTP_POST_VARS["email"]); } $prj_id = Issue::getProjectID($issue_id); // call workflow to modify actions or cancel adding this user. $actions = array(); $subscriber_usr_id = false; $workflow = Workflow::handleSubscription($prj_id, $issue_id, $subscriber_usr_id, $email, $actions); if ($workflow === false) { // cancel subscribing the user return -2; } // always set the type of notification to issue-level $stmt = "UPDATE\r\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "subscription\r\n SET\r\n sub_level='issue',\r\n sub_email='" . Misc::escapeString($email) . "',\r\n sub_usr_id={$usr_id}\r\n WHERE\r\n sub_id={$sub_id}"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } else { $stmt = "DELETE FROM\r\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "subscription_type\r\n WHERE\r\n sbt_sub_id={$sub_id}"; $GLOBALS["db_api"]->dbh->query($stmt); // now add them all again for ($i = 0; $i < count($HTTP_POST_VARS["actions"]); $i++) { Notification::addType($sub_id, $HTTP_POST_VARS["actions"][$i]); } // need to mark the issue as updated Issue::markAsUpdated($issue_id); // need to save a history entry for this History::add($issue_id, Auth::getUserID(), History::getTypeID('notification_updated'), "Notification list entry ('" . Notification::getSubscriber($sub_id) . "') updated by " . User::getFullName(Auth::getUserID())); return 1; } }
/** * Method used to update an existing draft response. * * @param integer $issue_id The issue ID * @param integer $emd_id The email draft ID * @param string $to The primary recipient of the draft * @param string $cc The secondary recipients of the draft * @param string $subject The subject of the draft * @param string $message The draft body * @param integer $parent_id The ID of the email that this draft is replying to, if any * @return integer 1 if the update worked, -1 otherwise */ public static function update($issue_id, $emd_id, $to, $cc, $subject, $message, $parent_id = null) { if (!$parent_id) { $parent_id = null; } $usr_id = Auth::getUserID(); // update previous draft and insert new record $stmt = "UPDATE\n {{%email_draft}}\n SET\n emd_updated_date=?,\n emd_status = 'edited'\n WHERE\n emd_id=?"; $params = array(Date_Helper::getCurrentDateGMT(), $emd_id); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } Issue::markAsUpdated($issue_id, 'draft saved'); History::add($issue_id, $usr_id, 'draft_updated', 'Email message draft updated by {user}', array('user' => User::getFullName($usr_id))); self::saveEmail($issue_id, $to, $cc, $subject, $message, $parent_id, false, false); return 1; }
/** * Method used to associate checkins to an existing issue * * @param integer $issue_id The ID of the issue. * @param string $commit_time Time when commit occurred (in UTC) * @param string $scm_name SCM definition name in Eventum * @param string $username SCM user doing the checkin. * @param string $commit_msg Message associated with the SCM commit. * @param array $files Files info with their version numbers changes made on. * @return integer 1 if the update worked, -1 otherwise */ public static function addCheckins($issue_id, $commit_time, $scm_name, $username, $commit_msg, $files) { // validate that $scm_name is valid // this will throw if invalid self::getScmCheckinByName($scm_name); // TODO: add workflow pre method first, so it may setup username, etc $usr_id = APP_SYSTEM_USER_ID; // workflow needs to know project_id to find out which workflow class to use. $prj_id = Issue::getProjectID($issue_id); foreach ($files as $file) { self::insertCheckin($issue_id, $commit_time, $scm_name, $file, $username, $commit_msg); } // need to mark this issue as updated Issue::markAsUpdated($issue_id, 'scm checkin'); // need to save a history entry for this // TRANSLATORS: %1: scm username History::add($issue_id, $usr_id, 'scm_checkin_associated', "SCM Checkins associated by SCM user '{user}'", array('user' => $username)); Workflow::handleSCMCheckins($prj_id, $issue_id, $files, $username, $commit_msg); return 1; }
/** * Attach uploaded files to an issue * It also notifies any subscribers of this new attachment. * * @param int $issue_id The issue ID * @param int $usr_id The user ID * @param int[] $iaf_ids attachment file id-s to attach * @param boolean $internal_only Whether this attachment is supposed to be internal only or not * @param string $file_description File description text * @param string $unknown_user The email of the user who originally sent this email, who doesn't have an account. * @param integer $associated_note_id The note ID that these attachments should be associated with */ public static function attachFiles($issue_id, $usr_id, $iaf_ids, $internal_only, $file_description, $unknown_user = null, $associated_note_id = null) { if (!$iaf_ids) { throw new LogicException('No attachment ids'); } $attachment_id = self::add($issue_id, $usr_id, $file_description, $internal_only, $unknown_user, $associated_note_id); self::associateFiles($attachment_id, $iaf_ids); Issue::markAsUpdated($issue_id, 'file uploaded'); History::add($issue_id, $usr_id, 'attachment_added', 'Attachment uploaded by {user}', array('user' => User::getFullName($usr_id))); // if there is customer integration, mark last customer action $prj_id = Issue::getProjectID($issue_id); $has_crm = CRM::hasCustomerIntegration($prj_id); $is_customer = User::getRoleByUser($usr_id, $prj_id) == User::ROLE_CUSTOMER; if ($has_crm && $is_customer) { Issue::recordLastCustomerAction($issue_id); } Workflow::handleAttachment($prj_id, $issue_id, $usr_id); Notification::notify($issue_id, 'files', $attachment_id, $internal_only); }
/** * Method used to remove a specific phone support entry from the * application. * * @param integer $phone_id The phone support entry ID * @return integer 1 if the removal worked, -1 or -2 otherwise */ public static function remove($phone_id) { $stmt = 'SELECT phs_iss_id, phs_ttr_id, phs_usr_id FROM {{%phone_support}} WHERE phs_id=?'; $details = DB_Helper::getInstance()->getRow($stmt, array($phone_id)); if ($details['phs_usr_id'] != Auth::getUserID()) { return -2; } $stmt = 'DELETE FROM {{%phone_support}} WHERE phs_id=?'; try { DB_Helper::getInstance()->query($stmt, array($phone_id)); } catch (DbException $e) { return -1; } Issue::markAsUpdated($details['phs_iss_id']); $usr_id = Auth::getUserID(); History::add($details['phs_iss_id'], $usr_id, 'phone_entry_removed', 'Phone Support entry removed by {user}', array('user' => User::getFullName($usr_id))); if (!empty($details['phs_ttr_id'])) { $time_result = Time_Tracking::removeTimeEntry($details['phs_ttr_id'], $details['phs_usr_id']); if ($time_result == 1) { return 2; } return $time_result; } return 1; }
/** * 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 * - (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()) { $parent_sup_id = $options['parent_sup_id']; $iaf_ids = $options['iaf_ids']; $add_unknown = $options['add_unknown']; $ema_id = $options['ema_id']; $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); } } $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::getRoleID('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::getRoleID('Customer'))) { Issue::markAsUpdated($issue_id, 'customer action'); } else { if ($sender_usr_id && User::getRoleByUser($sender_usr_id, $prj_id) > User::getRoleID('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; }
/** * Method used to update the values stored in the database. * * @param $issue_id * @param $custom_fields * @return array|int -1 if there is an error, otherwise an array of the updated fields */ public static function updateValues($issue_id, $custom_fields) { $prj_id = Auth::getCurrentProject(); $old_values = self::getValuesByIssue($prj_id, $issue_id); if (count($custom_fields) > 0) { // get the types for all of the custom fields being submitted $cf = array_keys($custom_fields); $cf_list = DB_Helper::buildList($cf); $stmt = "SELECT\n fld_id,\n fld_type\n FROM\n {{%custom_field}}\n WHERE\n fld_id IN ({$cf_list})"; $field_types = DB_Helper::getInstance()->getPair($stmt, $cf); // get the titles for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_title\n FROM\n {{%custom_field}}\n WHERE\n fld_id IN ({$cf_list})"; $field_titles = DB_Helper::getInstance()->getPair($stmt, $cf); $updated_fields = array(); foreach ($custom_fields as $fld_id => $value) { // security check $sql = 'SELECT fld_min_role FROM {{%custom_field}} WHERE fld_id = ?'; $min_role = DB_Helper::getInstance()->getOne($sql, array($fld_id)); if ($min_role > Auth::getCurrentRole()) { continue; } $updated_fields[$fld_id] = array('title' => $field_titles[$fld_id], 'type' => $field_types[$fld_id], 'min_role' => $min_role, 'changes' => '', 'old_display' => '', 'new_display' => ''); if (!in_array($field_types[$fld_id], self::$option_types)) { // check if this is a date field $fld_db_name = self::getDBValueFieldNameByType($field_types[$fld_id]); // first check if there is actually a record for this field for the issue $stmt = "SELECT\n icf_id,\n {$fld_db_name} as value\n FROM\n {{%issue_custom_field}}\n WHERE\n icf_iss_id=? AND\n icf_fld_id=?"; try { $res = DB_Helper::getInstance()->getRow($stmt, array($issue_id, $fld_id)); } catch (DbException $e) { return -1; } $icf_id = $res['icf_id']; $old_value = $res['value']; if ($old_value == $value) { unset($updated_fields[$fld_id]); continue; } if (empty($icf_id)) { // record doesn't exist, insert new record $stmt = "INSERT INTO\n {{%issue_custom_field}}\n (\n icf_iss_id,\n icf_fld_id,\n {$fld_db_name}\n ) VALUES (\n ?, ?, ?\n )"; $params = array($issue_id, $fld_id, $value); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } } else { // record exists, update it $stmt = "UPDATE\n {{%issue_custom_field}}\n SET\n {$fld_db_name}=?\n WHERE\n icf_id=?"; $params = array($value, $icf_id); try { DB_Helper::getInstance()->query($stmt, $params); } catch (DbException $e) { return -1; } } $updated_fields[$fld_id]['old_display'] = $old_value; $updated_fields[$fld_id]['new_display'] = $value; if ($field_types[$fld_id] == 'textarea') { $updated_fields[$fld_id]['changes'] = ''; } else { $updated_fields[$fld_id]['changes'] = History::formatChanges($old_value, $value); } } else { $old_value = self::getDisplayValue($issue_id, $fld_id, true); // remove dummy value from checkboxes. This dummy value is required so all real values can be unchecked. if ($field_types[$fld_id] == 'checkbox') { $value = array_filter($value); } if (!is_array($old_value)) { $old_value = array($old_value); } if (!is_array($value)) { $value = array($value); } if (count(array_diff($old_value, $value)) > 0 || count(array_diff($value, $old_value)) > 0) { $old_display_value = self::getDisplayValue($issue_id, $fld_id); // need to remove all associated options from issue_custom_field and then // add the selected options coming from the form self::removeIssueAssociation($fld_id, $issue_id); if (@count($value) > 0) { self::associateIssue($issue_id, $fld_id, $value); } $new_display_value = self::getDisplayValue($issue_id, $fld_id); $updated_fields[$fld_id]['changes'] = History::formatChanges($old_display_value, $new_display_value); $updated_fields[$fld_id]['old_display'] = $old_display_value; $updated_fields[$fld_id]['new_display'] = $new_display_value; } else { unset($updated_fields[$fld_id]); } } } Workflow::handleCustomFieldsUpdated($prj_id, $issue_id, $old_values, self::getValuesByIssue($prj_id, $issue_id), $updated_fields); Issue::markAsUpdated($issue_id); if (count($updated_fields) > 0) { // log the changes $changes = array(); foreach ($updated_fields as $fld_id => $updated_field) { if (!isset($changes[$updated_field['min_role']])) { $changes[$updated_field['min_role']] = array(); } $title = $updated_field['title']; $value = $updated_field['changes']; if (!empty($value)) { $changes[$updated_field['min_role']][] = "{$title}: {$value}"; } else { $changes[$updated_field['min_role']][] = $title; } } $usr_id = Auth::getUserID(); $usr_full_name = User::getFullName($usr_id); foreach ($changes as $min_role => $role_changes) { History::add($issue_id, $usr_id, 'custom_field_updated', 'Custom field updated ({changes}) by {user}', array('changes' => implode('; ', $role_changes), 'user' => $usr_full_name), $min_role); } } return $updated_fields; } return array(); }
/** * 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; }
/** * Routes an email to the correct issue. * * @param string $full_message The full email message, including headers * @param integer $email_account_id The ID of the email account this email should be routed too. If empty this method will try to figure it out */ function route_emails($full_message, $email_account_id = 0) { global $HTTP_POST_VARS; // save the full message for logging purposes Support::saveRoutedEmail($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); } // associate routed emails to the internal system account $sys_account = User::getNameEmail(APP_SYSTEM_USER_ID); $associated_user = $sys_account['usr_email']; // need some validation here if (empty($full_message)) { return array(66, "Error: The email message was empty.\n"); } if (empty($associated_user)) { return array(78, "Error: The associated user for the email routing interface needs to be set.\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 for magic cookie if (Mail_API::hasMagicCookie($full_message)) { // strip the magic cookie $full_message = Mail_API::stripMagicCookie($full_message); $has_magic_cookie = true; } else { $has_magic_cookie = false; } Auth::createFakeCookie(APP_SYSTEM_USER_ID); // check if the email routing interface is even supposed to be enabled $setup = Setup::load(); if ($setup['email_routing']['status'] != 'enabled') { return array(78, "Error: The email routing interface is disabled.\n"); } $prefix = $setup['email_routing']['address_prefix']; // escape plus signs so '*****@*****.**' becomes a valid routing address $prefix = str_replace('+', '\\+', $prefix); $mail_domain = quotemeta($setup['email_routing']['address_host']); $mail_domain_alias = quotemeta(@$setup['email_routing']['host_alias']); if (!empty($mail_domain_alias)) { $mail_domain = "(?:" . $mail_domain . "|" . $mail_domain_alias . ")"; } 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 email had no associated Eventum issue ID or had an invalid recipient address.\n"); } } if (empty($email_account_id)) { $issue_prj_id = Issue::getProjectID($issue_id); if (empty($issue_prj_id)) { return array(65, "Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.\n"); } $email_account_id = Email_Account::getEmailAccount($issue_prj_id); } if (empty($email_account_id)) { return array(78, "Error: Please provide the email account ID.\n"); } $body = Mime_Helper::getMessageBody($structure); // hack for clients that set more then one from header if (is_array($structure->headers['from'])) { $structure->headers['from'] = $structure->headers['from'][0]; } // associate the email to the issue $parts = array(); Mime_Helper::parse_output($structure, $parts); // get the sender's email address $sender_email = strtolower(Mail_API::getEmailAddress($structure->headers['from'])); // strip out the warning message sent to staff users if ($setup['email_routing']['status'] == 'enabled' && $setup['email_routing']['warning']['status'] == 'enabled') { $full_message = Mail_API::stripWarningMessage($full_message); $body = Mail_API::stripWarningMessage($body); } $prj_id = Issue::getProjectID($issue_id); Auth::createFakeCookie(APP_SYSTEM_USER_ID, $prj_id); $staff_emails = Project::getUserEmailAssocList($prj_id, 'active', User::getRoleID('Customer')); $staff_emails = array_map('strtolower', $staff_emails); // only allow staff users to use the magic cookie if (!in_array($sender_email, array_values($staff_emails))) { $has_magic_cookie = false; } if (Mime_Helper::hasAttachments($full_message)) { $has_attachments = 1; } else { $has_attachments = 0; } // remove certain CC addresses if (!empty($structure->headers['cc']) && @$setup['smtp']['save_outgoing_email'] == 'yes') { $ccs = explode(",", @$structure->headers['cc']); for ($i = 0; $i < count($ccs); $i++) { if (Mail_API::getEmailAddress($ccs[$i]) == $setup['smtp']['save_address']) { unset($ccs[$i]); } } @($structure->headers['cc'] = join(', ', $ccs)); } // Remove excess Re's @($structure->headers['subject'] = Mail_API::removeExcessRe(@$structure->headers['subject'], true)); $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' => @$full_message, 'has_attachment' => $has_attachments, 'headers' => @$structure->headers); // automatically associate this incoming email with a customer if (Customer::hasCustomerIntegration($prj_id)) { if (!empty($structure->headers['from'])) { list($customer_id, ) = Customer::getCustomerIDByEmails($prj_id, array($sender_email)); if (!empty($customer_id)) { $t['customer_id'] = $customer_id; } } } if (empty($t['customer_id'])) { $t['customer_id'] = "NULL"; } if (!$has_magic_cookie && Support::blockEmailIfNeeded($t)) { return true; } // re-write Threading headers if needed 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) { Support::extractAttachments($issue_id, $full_message); // 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(), $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($issue_id, 'customer action'); } else { if (!empty($usr_id) && User::getRoleByUser($usr_id, $prj_id) > User::getRoleID('Customer')) { Issue::markAsUpdated($issue_id, 'staff response'); } else { Issue::markAsUpdated($issue_id, 'user response'); } } // log routed email History::add($issue_id, $usr_id, History::getTypeID('email_routed'), "Email routed from " . $structure->headers['from']); } return true; }