*/ include_once "../../../config.inc.php"; include_once APP_INC_PATH . "db_access.php"; include_once APP_INC_PATH . "class.issue.php"; $stmt = "SELECT\n iss_id\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n WHERE\n iss_root_message_id IS NULL"; $issues = $GLOBALS["db_api"]->dbh->getCol($stmt); foreach ($issues as $issue_id) { $sql = "SELECT\n sup_message_id\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email\n WHERE\n sup_iss_id = {$issue_id}\n ORDER BY\n sup_date ASC\n LIMIT 1"; $res = $GLOBALS["db_api"]->dbh->getOne($sql); if (PEAR::isError($res)) { echo "<pre>"; print_r($res); echo "</pre>"; exit; } if (empty($res)) { $msg_id = Mail_API::generateMessageID(); } else { $msg_id = $res; } $sql = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n SET\n iss_root_message_id = '" . Misc::escapeString($msg_id) . "'\n WHERE\n iss_id = {$issue_id}"; $res = $GLOBALS["db_api"]->dbh->query($sql); if (PEAR::isError($res)) { echo "<pre>"; print_r($res); echo "</pre>"; exit; } } ?> done
/** * 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; }
function getBaseThreadingHeaders($issue_id) { $root_msg_id = Issue::getRootMessageID($issue_id); return array("Message-ID" => Mail_API::generateMessageID(), "In-Reply-To" => $root_msg_id, "References" => $root_msg_id); }
/** * Method used to add a note using the user interface form * available in the application. * * @param integer $usr_id The user ID * @param integer $issue_id The issue ID * @param string $unknown_user The email address of a user that sent the blocked email that was turned into this note. Default is false. * @param boolean $log If adding this note should be logged. Default true. * @param boolean $closing If The issue is being closed. Default false * @param boolean $send_notification Whether to send a notification about this note or not * @access public * @return integer the new note id if the insert worked, -1 or -2 otherwise */ function insert($usr_id, $issue_id, $unknown_user = FALSE, $log = true, $closing = false, $send_notification = true) { global $HTTP_POST_VARS; $issue_id = Misc::escapeInteger($issue_id); if (@$HTTP_POST_VARS['add_extra_recipients'] != 'yes') { $note_cc = array(); } else { $note_cc = $HTTP_POST_VARS['note_cc']; } // add the poster to the list of people to be subscribed to the notification list // only if there is no 'unknown user'. $note_cc[] = $usr_id; if ($unknown_user == false) { for ($i = 0; $i < count($note_cc); $i++) { Notification::subscribeUser($usr_id, $issue_id, $note_cc[$i], Notification::getDefaultActions()); } } if (Validation::isWhitespace($HTTP_POST_VARS["note"])) { return -2; } if (empty($HTTP_POST_VARS['message_id'])) { $HTTP_POST_VARS['message_id'] = Mail_API::generateMessageID(); } $stmt = "INSERT INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "note\n (\n not_iss_id,\n not_usr_id,\n not_created_date,\n not_note,\n not_title"; if (!@empty($HTTP_POST_VARS['blocked_msg'])) { $stmt .= ", not_blocked_message"; } $stmt .= ", not_message_id"; if (!@empty($HTTP_POST_VARS['parent_id'])) { $stmt .= ", not_parent_id"; } if ($unknown_user != false) { $stmt .= ", not_unknown_user"; } $stmt .= "\n ) VALUES (\n {$issue_id},\n {$usr_id},\n '" . Date_API::getCurrentDateGMT() . "',\n '" . Misc::escapeString($HTTP_POST_VARS["note"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["title"]) . "'"; if (!@empty($HTTP_POST_VARS['blocked_msg'])) { $stmt .= ", '" . Misc::escapeString($HTTP_POST_VARS['blocked_msg']) . "'"; } $stmt .= ", '" . Misc::escapeString($HTTP_POST_VARS['message_id']) . "'"; if (!@empty($HTTP_POST_VARS['parent_id'])) { $stmt .= ", " . Misc::escapeInteger($HTTP_POST_VARS['parent_id']) . ""; } if ($unknown_user != false) { $stmt .= ", '" . Misc::escapeString($unknown_user) . "'"; } $stmt .= "\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 { $new_note_id = $GLOBALS["db_api"]->get_last_insert_id(); Issue::markAsUpdated($issue_id, 'note'); if ($log) { // need to save a history entry for this History::add($issue_id, $usr_id, History::getTypeID('note_added'), 'Note added by ' . User::getFullName($usr_id)); } // send notifications for the issue being updated if ($send_notification) { $internal_only = true; if (@$HTTP_POST_VARS['add_extra_recipients'] != 'yes' && @count($HTTP_POST_VARS['note_cc']) > 0) { Notification::notify($issue_id, 'notes', $new_note_id, $internal_only, $HTTP_POST_VARS['note_cc']); } else { Notification::notify($issue_id, 'notes', $new_note_id, $internal_only); } Workflow::handleNewNote(Issue::getProjectID($issue_id), $issue_id, $usr_id, $closing); } // need to return the new note id here so it can // be re-used to associate internal-only attachments return $new_note_id; } }
/** * Method used to add a new issue using the normal report form. * * @access public * @return integer The new issue ID */ function insert() { global $HTTP_POST_VARS, $HTTP_POST_FILES, $insert_errors; $usr_id = Auth::getUserID(); $prj_id = Auth::getCurrentProject(); $initial_status = Project::getInitialStatus($prj_id); $insert_errors = array(); $missing_fields = array(); if ($HTTP_POST_VARS["category"] == '-1') { $missing_fields[] = "Category"; } if ($HTTP_POST_VARS["priority"] == '-1') { $missing_fields[] = "Priority"; } if ($HTTP_POST_VARS["estimated_dev_time"] == '') { $HTTP_POST_VARS["estimated_dev_time"] = 0; } // add new issue $stmt = "INSERT INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n (\n iss_prj_id,\n"; if (!empty($HTTP_POST_VARS["group"])) { $stmt .= "iss_grp_id,\n"; } if (!empty($HTTP_POST_VARS["category"])) { $stmt .= "iss_prc_id,\n"; } if (!empty($HTTP_POST_VARS["release"])) { $stmt .= "iss_pre_id,\n"; } if (!empty($HTTP_POST_VARS["priority"])) { $stmt .= "iss_pri_id,\n"; } $stmt .= "iss_usr_id,"; if (!empty($initial_status)) { $stmt .= "iss_sta_id,"; } if (Customer::hasCustomerIntegration($prj_id)) { $stmt .= "\n iss_customer_id,\n iss_customer_contact_id,\n iss_contact_person_lname,\n iss_contact_person_fname,\n iss_contact_email,\n iss_contact_phone,\n iss_contact_timezone,"; } $stmt .= "\n iss_created_date,\n iss_last_public_action_date,\n iss_last_public_action_type,\n iss_summary,\n iss_description,\n iss_dev_time,\n iss_private,\n iss_root_message_id\n ) VALUES (\n " . $prj_id . ",\n"; if (!empty($HTTP_POST_VARS["group"])) { $stmt .= Misc::escapeInteger($HTTP_POST_VARS["group"]) . ",\n"; } if (!empty($HTTP_POST_VARS["category"])) { $stmt .= Misc::escapeInteger($HTTP_POST_VARS["category"]) . ",\n"; } if (!empty($HTTP_POST_VARS["release"])) { $stmt .= Misc::escapeInteger($HTTP_POST_VARS["release"]) . ",\n"; } if (!empty($HTTP_POST_VARS["priority"])) { $stmt .= Misc::escapeInteger($HTTP_POST_VARS["priority"]) . ","; } // if we are creating an issue for a customer, put the // main customer contact as the reporter for it if (Customer::hasCustomerIntegration($prj_id)) { $contact_usr_id = User::getUserIDByContactID($HTTP_POST_VARS['contact']); if (empty($contact_usr_id)) { $contact_usr_id = $usr_id; } $stmt .= Misc::escapeInteger($contact_usr_id) . ","; } else { $stmt .= $usr_id . ","; } if (!empty($initial_status)) { $stmt .= Misc::escapeInteger($initial_status) . ","; } if (Customer::hasCustomerIntegration($prj_id)) { $stmt .= "\n " . Misc::escapeInteger($HTTP_POST_VARS['customer']) . ",\n " . Misc::escapeInteger($HTTP_POST_VARS['contact']) . ",\n '" . Misc::escapeString($HTTP_POST_VARS["contact_person_lname"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["contact_person_fname"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["contact_email"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["contact_phone"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["contact_timezone"]) . "',"; } $stmt .= "\n '" . Date_API::getCurrentDateGMT() . "',\n '" . Date_API::getCurrentDateGMT() . "',\n 'created',\n '" . Misc::escapeString($HTTP_POST_VARS["summary"]) . "',\n '" . Misc::escapeString($HTTP_POST_VARS["description"]) . "',\n " . Misc::escapeString($HTTP_POST_VARS["estimated_dev_time"]) . ",\n " . Misc::escapeInteger($HTTP_POST_VARS["private"]) . " ,\n '" . Misc::escapeString(Mail_API::generateMessageID()) . "'\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 { $new_issue_id = $GLOBALS["db_api"]->get_last_insert_id(); $has_TAM = false; $has_RR = false; $info = User::getNameEmail($usr_id); // log the creation of the issue History::add($new_issue_id, Auth::getUserID(), History::getTypeID('issue_opened'), 'Issue opened by ' . User::getFullName(Auth::getUserID())); $emails = array(); if (Customer::hasCustomerIntegration($prj_id)) { if (@count($HTTP_POST_VARS['contact_extra_emails']) > 0) { $emails = $HTTP_POST_VARS['contact_extra_emails']; } // add the primary contact to the notification list if ($HTTP_POST_VARS['add_primary_contact'] == 'yes') { $contact_email = User::getEmailByContactID($HTTP_POST_VARS['contact']); if (!empty($contact_email)) { $emails[] = $contact_email; } } // if there are any technical account managers associated with this customer, add these users to the notification list $managers = Customer::getAccountManagers($prj_id, $HTTP_POST_VARS['customer']); $manager_usr_ids = array_keys($managers); $manager_emails = array_values($managers); $emails = array_merge($emails, $manager_emails); } // add the reporter to the notification list $emails[] = $info['usr_email']; $emails = array_unique($emails); // COMPAT: version >= 4.0.1 $actions = Notification::getDefaultActions(); foreach ($emails as $address) { Notification::subscribeEmail($usr_id, $new_issue_id, $address, $actions); } // only assign the issue to an user if the associated customer has any technical account managers $users = array(); $has_TAM = false; if (Customer::hasCustomerIntegration($prj_id) && count($manager_usr_ids) > 0) { foreach ($manager_usr_ids as $manager_usr_id) { $users[] = $manager_usr_id; Issue::addUserAssociation($usr_id, $new_issue_id, $manager_usr_id, false); History::add($new_issue_id, $usr_id, History::getTypeID('issue_auto_assigned'), 'Issue auto-assigned to ' . User::getFullName($manager_usr_id) . ' (TAM)'); } $has_TAM = true; } // now add the user/issue association (aka assignments) if (@count($HTTP_POST_VARS["users"]) > 0) { for ($i = 0; $i < count($HTTP_POST_VARS["users"]); $i++) { Notification::subscribeUser($usr_id, $new_issue_id, $HTTP_POST_VARS["users"][$i], $actions); Issue::addUserAssociation($usr_id, $new_issue_id, $HTTP_POST_VARS["users"][$i]); if ($HTTP_POST_VARS["users"][$i] != $usr_id) { $users[] = $HTTP_POST_VARS["users"][$i]; } } } else { // only use the round-robin feature if this new issue was not // already assigned to a customer account manager if (@count($manager_usr_ids) < 1) { $assignee = Round_Robin::getNextAssignee($prj_id); // assign the issue to the round robin person if (!empty($assignee)) { $users[] = $assignee; Issue::addUserAssociation($usr_id, $new_issue_id, $assignee, false); History::add($new_issue_id, APP_SYSTEM_USER_ID, History::getTypeID('rr_issue_assigned'), 'Issue auto-assigned to ' . User::getFullName($assignee) . ' (RR)'); $has_RR = true; } } } // now process any files being uploaded $found = 0; for ($i = 0; $i < count(@$HTTP_POST_FILES["file"]["name"]); $i++) { if (!@empty($HTTP_POST_FILES["file"]["name"][$i])) { $found = 1; break; } } if ($found) { $files = array(); for ($i = 0; $i < count($HTTP_POST_FILES["file"]["name"]); $i++) { $filename = @$HTTP_POST_FILES["file"]["name"][$i]; if (empty($filename)) { continue; } $blob = Misc::getFileContents($HTTP_POST_FILES["file"]["tmp_name"][$i]); if (empty($blob)) { // error reading a file $insert_errors["file[{$i}]"] = "There was an error uploading the file '{$filename}'."; continue; } $files[] = array("filename" => $filename, "type" => $HTTP_POST_FILES['file']['type'][$i], "blob" => $blob); } if (count($files) > 0) { $attachment_id = Attachment::add($new_issue_id, $usr_id, 'Files uploaded at issue creation time'); foreach ($files as $file) { Attachment::addFile($attachment_id, $new_issue_id, $file["filename"], $file["type"], $file["blob"]); } } } // need to associate any emails ? if (!empty($HTTP_POST_VARS["attached_emails"])) { $items = explode(",", $HTTP_POST_VARS["attached_emails"]); Support::associate($usr_id, $new_issue_id, $items, true); } // need to notify any emails being converted into issues ? if (@count($HTTP_POST_VARS["notify_senders"]) > 0) { $recipients = Notification::notifyEmailConvertedIntoIssue($prj_id, $new_issue_id, $HTTP_POST_VARS["notify_senders"], $customer_id); } else { $recipients = array(); } // need to process any custom fields ? if (@count($HTTP_POST_VARS["custom_fields"]) > 0) { foreach ($HTTP_POST_VARS["custom_fields"] as $fld_id => $value) { Custom_Field::associateIssue($new_issue_id, $fld_id, $value); } } // also send a special confirmation email to the customer contact if (@$HTTP_POST_VARS['notify_customer'] == 'yes' && !empty($HTTP_POST_VARS['contact'])) { // also need to pass the list of sender emails already notified, // so we can avoid notifying the same person again $contact_email = User::getEmailByContactID($HTTP_POST_VARS['contact']); if (@(!in_array($contact_email, $recipients))) { Customer::notifyCustomerIssue($prj_id, $new_issue_id, $HTTP_POST_VARS['contact']); } } Workflow::handleNewIssue($prj_id, $new_issue_id, $has_TAM, $has_RR); // also notify any users that want to receive emails anytime a new issue is created Notification::notifyNewIssue($prj_id, $new_issue_id); return $new_issue_id; } }