/**
  * Returns if the specified user is authorized to reply to this issue.
  *
  * @access  public
  * @param   integer $issue_id The id of the issue.
  * @param   string  $email The email address to check.
  * @return  boolean If the specified user is allowed to reply to the issue.
  */
 function isAuthorizedReplier($issue_id, $email)
 {
     $email = strtolower(Mail_API::getEmailAddress($email));
     // first check if this is an actual user or just an email address
     $user_emails = User::getAssocEmailList();
     if (in_array($email, array_keys($user_emails))) {
         // real user, get id
         $usr_id = User::getUserIDByEmail($email);
         return Authorized_Replier::isUserAuthorizedReplier($issue_id, $usr_id);
     } else {
         // not a real user
         $stmt = "SELECT\n                        COUNT(*) AS total\n                     FROM\n                        " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier\n                     WHERE\n                        iur_iss_id=" . Misc::escapeInteger($issue_id) . " AND\n                        iur_email='" . Misc::escapeString($email) . "'";
         $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
         if (PEAR::isError($res)) {
             Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
             return false;
         } else {
             if ($res > 0) {
                 return true;
             } else {
                 return false;
             }
         }
     }
 }
 /**
  * Method used to add a new subscriber manually, by using the
  * email notification interface.
  *
  * @access  public
  * @param   integer $usr_id The user ID of the person performing this change
  * @param   integer $issue_id The issue ID
  * @param   string $form_email The email address to subscribe
  * @param   array $actions The actions to subcribe to
  * @return  integer 1 if the update worked, -1 otherwise
  */
 function subscribeEmail($usr_id, $issue_id, $form_email, $actions)
 {
     $form_email = strtolower(Mail_API::getEmailAddress($form_email));
     // first check if this is an actual user or just an email address
     $user_emails = User::getAssocEmailList();
     if (in_array($form_email, array_keys($user_emails))) {
         return Notification::subscribeUser($usr_id, $issue_id, $user_emails[$form_email], $actions);
     }
     $issue_id = Misc::escapeInteger($issue_id);
     $email = Misc::escapeString($form_email);
     $prj_id = Issue::getProjectID($issue_id);
     // call workflow to modify actions or cancel adding this user.
     $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;
     }
     // manual check to prevent duplicates
     if (!empty($email)) {
         $stmt = "SELECT\r\n                        COUNT(sub_id)\r\n                     FROM\r\n                        " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "subscription\r\n                     WHERE\r\n                        sub_iss_id={$issue_id} AND\r\n                        sub_email='{$email}'";
         $total = $GLOBALS["db_api"]->dbh->getOne($stmt);
         if ($total > 0) {
             return -1;
         }
     }
     $stmt = "INSERT INTO\r\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "subscription\r\n                 (\r\n                    sub_iss_id,\r\n                    sub_usr_id,\r\n                    sub_created_date,\r\n                    sub_level,\r\n                    sub_email\r\n                 ) VALUES (\r\n                    {$issue_id},\r\n                    0,\r\n                    '" . Date_API::getCurrentDateGMT() . "',\r\n                    'issue',\r\n                    '{$email}'\r\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 {
         $sub_id = $GLOBALS["db_api"]->get_last_insert_id();
         for ($i = 0; $i < count($actions); $i++) {
             Notification::addType($sub_id, $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, $usr_id, History::getTypeID('notification_added'), "Notification list entry ('{$email}') added by " . User::getFullName($usr_id));
         return 1;
     }
 }