public function getIssueIDs($options)
 {
     // Build the Sphinx client
     $this->sphinx->SetSortMode(SPH_SORT_RELEVANCE);
     //        $this->sphinx->SetWeights(array(1, 1));
     $this->sphinx->SetLimits(0, 500, 100000);
     $this->sphinx->SetArrayResult(true);
     if (empty($options['match_mode'])) {
         $options['match_mode'] = SPH_MATCH_ALL;
     }
     $this->sphinx->SetMatchMode($options['match_mode']);
     $this->sphinx->SetFilter('prj_id', array(Auth::getCurrentProject()));
     // TODO: Add support for selecting indexes to search
     $indexes = implode('; ', $this->getIndexes(Auth::getCurrentRole() > User::ROLE_CUSTOMER));
     if (isset($options['customer_id']) && !empty($options['customer_id'])) {
         $this->sphinx->SetFilter('customer_id', array($options['customer_id']));
     }
     $this->keywords = $options['keywords'];
     $this->match_mode = $options['match_mode'];
     $res = $this->sphinx->Query($options['keywords'], $indexes);
     // TODO: report these somehow back to the UI
     // probably easy to do with Logger framework (add new handler?)
     if (method_exists($this->sphinx, 'IsConnectError') && $this->sphinx->IsConnectError()) {
         Logger::app()->error('sphinx_fulltext_search: Network Error');
     }
     if ($this->sphinx->GetLastWarning()) {
         Logger::app()->warning('sphinx_fulltext_search: ' . $this->sphinx->GetLastWarning());
     }
     if ($this->sphinx->GetLastError()) {
         Logger::app()->error('sphinx_fulltext_search: ' . $this->sphinx->GetLastError());
     }
     $issue_ids = array();
     if (isset($res['matches'])) {
         foreach ($res['matches'] as $match_details) {
             // Variable translation
             $match_id = $match_details['id'];
             $issue_id = $match_details['attrs']['issue_id'];
             $weight = $match_details['weight'];
             $index_id = $match_details['attrs']['index_id'];
             // if sphinx returns 0 as a weight, make it one because it
             // did find a match in the result set
             if ($weight <= 0) {
                 $weight = 1;
             }
             $index_name = $this->getIndexNameByID($index_id);
             $this->matches[$issue_id][] = array('weight' => $weight, 'index' => $index_name, 'match_id' => $match_id);
             $issue_ids[] = $issue_id;
         }
     }
     return $issue_ids;
 }
Example #2
0
 /**
  * Method used to update the account details for a specific user.
  *
  * @param $usr_id
  * @param $data
  * @param bool $notify
  * @return  integer 1 if the update worked, -1 otherwise
  */
 public static function update($usr_id, $data, $notify = true)
 {
     // system account should not be updateable
     if ($usr_id == APP_SYSTEM_USER_ID) {
         return 1;
     }
     $params = array('usr_email' => $data['email']);
     if (isset($data['full_name'])) {
         $params['usr_full_name'] = $data['full_name'];
     }
     if (isset($data['grp_id'])) {
         $params['usr_grp_id'] = !empty($data['grp_id']) ? $data['grp_id'] : null;
     }
     if (isset($data['external_id'])) {
         $params['usr_external_id'] = $data['external_id'];
     }
     if (isset($data['par_code'])) {
         $params['usr_par_code'] = $data['par_code'];
     }
     $stmt = 'UPDATE
                 {{%user}}
              SET ' . DB_Helper::buildSet($params) . ' WHERE usr_id=?';
     $params[] = $usr_id;
     try {
         DB_Helper::getInstance()->query($stmt, $params);
     } catch (DbException $e) {
         return -1;
     }
     if (!empty($data['password'])) {
         try {
             self::updatePassword($usr_id, $data['password']);
         } catch (Exception $e) {
             Logger::app()->error($e);
             return -1;
         }
     }
     if (isset($data['role'])) {
         // update the project associations now
         $stmt = 'DELETE FROM
                     {{%project_user}}
                  WHERE
                     pru_usr_id=?';
         try {
             DB_Helper::getInstance()->query($stmt, array($usr_id));
         } catch (DbException $e) {
             return -1;
         }
         foreach ($data['role'] as $prj_id => $role) {
             if ($role < 1) {
                 continue;
             }
             $stmt = 'INSERT INTO
                         {{%project_user}}
                      (
                         pru_prj_id,
                         pru_usr_id,
                         pru_role
                      ) VALUES (
                         ?, ?, ?
                      )';
             try {
                 DB_Helper::getInstance()->query($stmt, array($prj_id, $usr_id, $role));
             } catch (DbException $e) {
                 return -1;
             }
         }
     }
     if ($notify == true) {
         if (!empty($data['password'])) {
             Notification::notifyUserPassword($usr_id, $data['password']);
         } else {
             Notification::notifyUserAccount($usr_id);
         }
     }
     return 1;
 }
 /**
  * Connects to the SMTP server and sends the queued message.
  *
  * @param   string $recipient The recipient of this message
  * @param   string $text_headers The full headers of this message
  * @param   string $body The full body of this message
  * @param   string $status The status of this message
  * @return  true, or a PEAR_Error object
  */
 private function _sendEmail($recipient, $text_headers, &$body, $status)
 {
     $header_names = Mime_Helper::getHeaderNames($text_headers);
     $_headers = self::_getHeaders($text_headers, $body);
     $headers = array();
     foreach ($_headers as $lowercase_name => $value) {
         // need to remove the quotes to avoid a parsing problem
         // on senders that have extended characters in the first
         // or last words in their sender name
         if ($lowercase_name == 'from') {
             $value = Mime_Helper::removeQuotes($value);
         }
         $value = Mime_Helper::encode($value);
         // add the quotes back
         if ($lowercase_name == 'from') {
             $value = Mime_Helper::quoteSender($value);
         }
         $headers[$header_names[$lowercase_name]] = $value;
     }
     // remove any Reply-To:/Return-Path: values from outgoing messages
     unset($headers['Reply-To']);
     unset($headers['Return-Path']);
     // mutt sucks, so let's remove the broken Mime-Version header and add the proper one
     if (in_array('Mime-Version', array_keys($headers))) {
         unset($headers['Mime-Version']);
         $headers['MIME-Version'] = '1.0';
     }
     $mail = Mail::factory('smtp', Mail_Helper::getSMTPSettings());
     $res = $mail->send($recipient, $headers, $body);
     if (Misc::isError($res)) {
         Logger::app()->error($res->getMessage(), array('debug' => $res->getDebugInfo()));
         return $res;
     }
     return true;
 }
Example #4
0
 * @license GNU General Public License, version 2 or later (GPL-2+)
 *
 * For the full copyright and license information,
 * please see the COPYING and AUTHORS files
 * that were distributed with this source code.
 */
require_once __DIR__ . '/../../init.php';
// handle ajax upload
// FIXME: no identity logged who added the file.
try {
    // check if logged in. if not, just give error
    if (!AuthCookie::hasAuthCookie()) {
        throw new BadFunctionCallException(ev_gettext('Must be logged in'));
    }
    if (!isset($_GET['file'])) {
        // TRANSLATORS: this is technical error and should not be displayed to end users
        throw new InvalidArgumentException(ev_gettext('No file argument'));
    }
    $file = (string) $_GET['file'];
    if (!isset($_FILES[$file])) {
        throw new InvalidArgumentException(ev_gettext('No files uploaded'));
    }
    $iaf_id = Attachment::addFiles($_FILES[$file]);
    $res = array('error' => 0, 'iaf_id' => $iaf_id);
} catch (Exception $e) {
    $code = $e->getCode();
    $res = array('error' => $code ? $code : -1, 'message' => $e->getMessage());
    Logger::app()->error($e);
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($res);
Example #5
0
 /**
  * @return Auth_Backend_Interface
  */
 public static function getAuthBackend()
 {
     /** @var Auth_Backend_Interface $instance */
     static $instance = false;
     if ($instance == false) {
         $class = APP_AUTH_BACKEND;
         // legacy: allow lowercase variants
         if (strtolower($class) == 'mysql_auth_backend') {
             $class = 'Mysql_Auth_Backend';
         } elseif (strtolower($class) == 'ldap_auth_backend') {
             $class = 'LDAP_Auth_Backend';
         }
         try {
             $instance = new $class();
         } catch (AuthException $e) {
             $message = "Unable to use auth backend '{$class}'";
             Logger::app()->critical($message, array('exception' => $e));
             if (APP_AUTH_BACKEND_ALLOW_FALLBACK != true) {
                 $tpl = new Template_Helper();
                 $tpl->setTemplate('authentication_error.tpl.html');
                 $tpl->assign('error_message', $e->getMessage());
                 $tpl->displayTemplate();
                 exit;
             }
             $instance = self::getFallBackAuthBackend();
         }
     }
     return $instance;
 }
Example #6
0
 /**
  * Method used to save the setup options for the application.
  * The $options are merged with existing config and then saved.
  *
  * @param array $options Options to modify (does not need to be full setup)
  * @return integer 1 if the update worked, -1 or -2 otherwise
  */
 public static function save($options = array())
 {
     $config = self::set($options);
     try {
         $clone = clone $config;
         // save ldap config to separate file
         $ldap = $clone->ldap;
         unset($clone->ldap);
         self::saveConfig(APP_SETUP_FILE, $clone);
         if ($ldap) {
             self::saveConfig(APP_CONFIG_PATH . '/ldap.php', $ldap);
         }
     } catch (Exception $e) {
         $code = $e->getCode();
         Logger::app()->error($e);
         return $code ?: -1;
     }
     return 1;
 }
Example #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;
 }
 /**
  * Method used to save a copy of the given email to a configurable address.
  *
  * @param   array $email The email to save.
  * @return bool
  */
 public static function saveOutgoingEmailCopy(&$email)
 {
     // check early: do we really want to save every outgoing email?
     $setup = Setup::get();
     $save_outgoing_email = !empty($setup['smtp']['save_outgoing_email']) && $setup['smtp']['save_outgoing_email'] == 'yes';
     if (!$save_outgoing_email || empty($setup['smtp']['save_address'])) {
         return false;
     }
     static $subjects = array();
     $hdrs =& $email['headers'];
     $body =& $email['body'];
     $issue_id = $email['maq_iss_id'];
     $sender_usr_id = $email['maq_usr_id'];
     // ok, now parse the headers text and build the assoc array
     $full_email = $hdrs . "\n\n" . $body;
     $structure = Mime_Helper::decode($full_email, false, false);
     $_headers =& $structure->headers;
     $header_names = Mime_Helper::getHeaderNames($hdrs);
     $headers = array();
     foreach ($_headers as $lowercase_name => $value) {
         // need to remove the quotes to avoid a parsing problem
         // on senders that have extended characters in the first
         // or last words in their sender name
         if ($lowercase_name == 'from') {
             $value = Mime_Helper::removeQuotes($value);
         }
         $value = Mime_Helper::encode($value);
         // add the quotes back
         if ($lowercase_name == 'from') {
             $value = Mime_Helper::quoteSender($value);
         }
         $headers[$header_names[$lowercase_name]] = $value;
     }
     // remove any Reply-To:/Return-Path: values from outgoing messages
     unset($headers['Reply-To']);
     unset($headers['Return-Path']);
     // prevent duplicate emails from being sent out...
     $subject = @$headers['Subject'];
     if (@in_array($subject, $subjects)) {
         return false;
     }
     // replace the To: header with the requested address
     $address = $setup['smtp']['save_address'];
     $headers['To'] = $address;
     // add specialized headers if they are not already added
     if (empty($headers['X-Eventum-Type'])) {
         $headers += self::getSpecializedHeaders($issue_id, $email['maq_type'], $sender_usr_id);
     }
     $params = self::getSMTPSettings();
     $mail = Mail::factory('smtp', $params);
     $res = $mail->send($address, $headers, $body);
     if (Misc::isError($res)) {
         Logger::app()->error($res->getMessage(), array('debug' => $res->getDebugInfo()));
     }
     $subjects[] = $subject;
 }