[ Index ] |
PHP Cross Reference of Eventum |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */ 3 // +----------------------------------------------------------------------+ 4 // | Eventum - Issue Tracking System | 5 // +----------------------------------------------------------------------+ 6 // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB | 7 // | | 8 // | This program is free software; you can redistribute it and/or modify | 9 // | it under the terms of the GNU General Public License as published by | 10 // | the Free Software Foundation; either version 2 of the License, or | 11 // | (at your option) any later version. | 12 // | | 13 // | This program is distributed in the hope that it will be useful, | 14 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 // | GNU General Public License for more details. | 17 // | | 18 // | You should have received a copy of the GNU General Public License | 19 // | along with this program; if not, write to: | 20 // | | 21 // | Free Software Foundation, Inc. | 22 // | 59 Temple Place - Suite 330 | 23 // | Boston, MA 02111-1307, USA. | 24 // +----------------------------------------------------------------------+ 25 // | Authors: Bryan Alsdorf <bryan@mysql.com> | 26 // +----------------------------------------------------------------------+ 27 // 28 // @(#) $Id: class.authorized_replier.php 3246 2007-02-09 09:10:12Z glen $ 29 // 30 31 require_once (APP_INC_PATH . "class.user.php"); 32 33 /** 34 * Class designed to handle adding, removing and viewing authorized repliers for an issue. 35 * 36 * @author Bryan Alsdorf <bryan@mysql.com> 37 */ 38 class Authorized_Replier 39 { 40 /** 41 * Method used to get the full list of users (the full names) authorized to 42 * reply to emails in a given issue. 43 * 44 * @access public 45 * @param integer $issue_id The issue ID 46 * @return array The list of users 47 */ 48 function getAuthorizedRepliers($issue_id) 49 { 50 $issue_id = Misc::escapeInteger($issue_id); 51 // split into users and others (those with email address but no real user accounts) 52 $repliers = array( 53 "users" => array(), 54 "other" => array() 55 ); 56 57 $stmt = "SELECT 58 iur_id, 59 iur_usr_id, 60 usr_email, 61 if (iur_usr_id = '" . APP_SYSTEM_USER_ID . "', iur_email, usr_full_name) replier, 62 if (iur_usr_id = '" . APP_SYSTEM_USER_ID . "', 'other', 'user') replier_type 63 FROM 64 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier, 65 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user 66 WHERE 67 iur_iss_id=" . Misc::escapeInteger($issue_id) . " AND 68 iur_usr_id=usr_id"; 69 $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC); 70 if (PEAR::isError($res)) { 71 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 72 return array( 73 array(), 74 $repliers 75 ); 76 } else { 77 // split into users and others (those with email address but no real user accounts) 78 $names = array(); 79 if (count($res) > 0) { 80 foreach ($res as $row) { 81 if ($row["iur_usr_id"] == APP_SYSTEM_USER_ID) { 82 $repliers["other"][] = $row; 83 } else { 84 $repliers["users"][] = $row; 85 } 86 $names[] = $row['replier']; 87 } 88 } 89 $repliers["all"] = array_merge($repliers["users"], $repliers["other"]); 90 return array( 91 $names, 92 $repliers 93 ); 94 } 95 } 96 97 98 /** 99 * Removes the specified authorized replier 100 * 101 * @access public 102 * @param integer $iur_id The id of the authorized replier 103 */ 104 function removeRepliers($iur_ids) 105 { 106 $iur_ids = Misc::escapeInteger($iur_ids); 107 108 // get issue_id for logging 109 $stmt = "SELECT 110 iur_iss_id 111 FROM 112 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 113 WHERE 114 iur_id IN(" . join(",", $iur_ids) . ")"; 115 $issue_id = $GLOBALS["db_api"]->dbh->getOne($stmt); 116 if (PEAR::isError($issue_id)) { 117 Error_Handler::logError(array($issue_id->getMessage(), $issue_id->getDebugInfo()), __FILE__, __LINE__); 118 } 119 120 foreach ($iur_ids as $id) { 121 $replier = Authorized_Replier::getReplier($id); 122 $stmt = "DELETE FROM 123 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 124 WHERE 125 iur_id IN(" . join(",", $iur_ids) . ")"; 126 $res = $GLOBALS["db_api"]->dbh->query($stmt); 127 if (PEAR::isError($res)) { 128 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 129 return ""; 130 } else { 131 History::add($issue_id, Auth::getUserID(), History::getTypeID('replier_removed'), 132 "Authorized replier $replier removed by " . User::getFullName(Auth::getUserID())); 133 } 134 } 135 } 136 137 138 /** 139 * Adds the specified email address to the list of authorized users. 140 * 141 * @access public 142 * @param integer $issue_id The id of the issue. 143 * @param string $email The email of the user. 144 * @param boolean $add_history If this should be logged. 145 */ 146 function manualInsert($issue_id, $email, $add_history = true) 147 { 148 if (Authorized_Replier::isAuthorizedReplier($issue_id, $email)) { 149 return -1; 150 } else { 151 $email = strtolower(Mail_API::getEmailAddress($email)); 152 153 $workflow = Workflow::handleAuthorizedReplierAdded(Issue::getProjectID($issue_id), $issue_id, $email); 154 if ($workflow === false) { 155 // cancel subscribing the user 156 return -1; 157 } 158 159 // first check if this is an actual user or just an email address 160 $user_emails = User::getAssocEmailList(); 161 $user_emails = array_map('strtolower', $user_emails); 162 if (in_array($email, array_keys($user_emails))) { 163 return Authorized_Replier::addUser($issue_id, $user_emails[$email], $add_history); 164 } 165 166 $stmt = "INSERT INTO 167 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 168 ( 169 iur_iss_id, 170 iur_usr_id, 171 iur_email 172 ) VALUES ( 173 " . Misc::escapeInteger($issue_id) . ", 174 " . APP_SYSTEM_USER_ID . ", 175 '" . Misc::escapeString($email) . "' 176 )"; 177 $res = $GLOBALS["db_api"]->dbh->query($stmt); 178 if (PEAR::isError($res)) { 179 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 180 return -1; 181 } else { 182 if ($add_history) { 183 // add the change to the history of the issue 184 $summary = $email . ' added to the authorized repliers list by ' . User::getFullName(Auth::getUserID()); 185 History::add($issue_id, Auth::getUserID(), History::getTypeID('replier_other_added'), $summary); 186 } 187 } 188 return 1; 189 } 190 } 191 192 193 /** 194 * Adds a real user to the authorized repliers list. 195 * 196 * @access public 197 * @param integer $issue_id The id of the issue. 198 * @param integer $usr_id The id of the user. 199 * @param boolean $add_history If this should be logged. 200 */ 201 function addUser($issue_id, $usr_id, $add_history = true) 202 { 203 // don't add customers to this list. They should already be able to send 204 if (User::getRoleByUser($usr_id, Issue::getProjectID($issue_id)) == User::getRoleID("Customer")) { 205 return -2; 206 } 207 208 $stmt = "INSERT INTO 209 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 210 ( 211 iur_iss_id, 212 iur_usr_id 213 ) VALUES ( 214 " . Misc::escapeInteger($issue_id) . ", 215 " . Misc::escapeInteger($usr_id) . " 216 )"; 217 $res = $GLOBALS["db_api"]->dbh->query($stmt); 218 if (PEAR::isError($res)) { 219 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 220 return -1; 221 } else { 222 if ($add_history) { 223 // add the change to the history of the issue 224 $summary = User::getFullName($usr_id) . ' added to the authorized repliers list by ' . User::getFullName(Auth::getUserID()); 225 History::add($issue_id, Auth::getUserID(), History::getTypeID('replier_added'), $summary); 226 } 227 } 228 return 1; 229 } 230 231 232 /** 233 * Returns if the specified user is authorized to reply to this issue. 234 * 235 * @access public 236 * @param integer $issue_id The id of the issue. 237 * @param string $email The email address to check. 238 * @return boolean If the specified user is allowed to reply to the issue. 239 */ 240 function isAuthorizedReplier($issue_id, $email) 241 { 242 $email = strtolower(Mail_API::getEmailAddress($email)); 243 // first check if this is an actual user or just an email address 244 $user_emails = User::getAssocEmailList(); 245 if (in_array($email, array_keys($user_emails))) { 246 // real user, get id 247 $usr_id = User::getUserIDByEmail($email); 248 return Authorized_Replier::isUserAuthorizedReplier($issue_id, $usr_id); 249 } else { 250 // not a real user 251 $stmt = "SELECT 252 COUNT(*) AS total 253 FROM 254 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 255 WHERE 256 iur_iss_id=" . Misc::escapeInteger($issue_id) . " AND 257 iur_email='" . Misc::escapeString($email) . "'"; 258 $res = $GLOBALS["db_api"]->dbh->getOne($stmt); 259 if (PEAR::isError($res)) { 260 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 261 return false; 262 } else { 263 if ($res > 0) { 264 return true; 265 } else { 266 return false; 267 } 268 } 269 } 270 } 271 272 273 /** 274 * Returns if the specified usr_id is authorized to reply. 275 * 276 * @access public 277 * @param integer $issue_id The id of the issue 278 * @param integer $usr_id The id of the user. 279 * @return boolean If the user is authorized to reply. 280 */ 281 function isUserAuthorizedReplier($issue_id, $usr_id) 282 { 283 $stmt = "SELECT 284 count(iur_id) 285 FROM 286 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 287 WHERE 288 iur_iss_id = " . Misc::escapeInteger($issue_id) . " AND 289 iur_usr_id = " . Misc::escapeInteger($usr_id); 290 $res = $GLOBALS["db_api"]->dbh->getOne($stmt); 291 if (PEAR::isError($res)) { 292 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 293 return ""; 294 } else { 295 if ($res > 0) { 296 return true; 297 } else { 298 return false; 299 } 300 } 301 } 302 303 304 /** 305 * Returns the replier based on the iur_id 306 * 307 * @access public 308 * @param integer iur_id The id of the authorized replier 309 * @return string The name/email of the replier 310 */ 311 function getReplier($iur_id) 312 { 313 $stmt = "SELECT 314 if (iur_usr_id = '" . APP_SYSTEM_USER_ID . "', iur_email, usr_full_name) replier 315 FROM 316 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier, 317 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user 318 WHERE 319 iur_usr_id = usr_id AND 320 iur_id = " . Misc::escapeInteger($iur_id); 321 $res = $GLOBALS["db_api"]->dbh->getOne($stmt); 322 if (PEAR::isError($res)) { 323 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 324 return ""; 325 } 326 return $res; 327 } 328 329 330 /** 331 * Returns the replier based on the given issue and email address combo. 332 * 333 * @access public 334 * @param integer $issue_id The id of the issue. 335 * @param string $email The email address of the user 336 * @return integer The id of the replier 337 */ 338 function getReplierIDByEmail($issue_id, $email) 339 { 340 $stmt = "SELECT 341 iur_id 342 FROM 343 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_user_replier 344 LEFT JOIN 345 " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user 346 ON 347 iur_usr_id = usr_id 348 WHERE 349 iur_iss_id = " . Misc::escapeInteger($issue_id) . " AND 350 (iur_email = '" . Misc::escapeString($email) . "' OR usr_email = '" . Misc::escapeString($email) . "')"; 351 $res = $GLOBALS["db_api"]->dbh->getOne($stmt); 352 if (PEAR::isError($res)) { 353 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); 354 return 0; 355 } 356 return $res; 357 } 358 359 360 /** 361 * Method used to remotely add an authorized replier to a given issue. 362 * 363 * @access public 364 * @param integer $issue_id The issue ID 365 * @param integer $usr_id The user ID of the person performing the change 366 * @param boolean $replier The user ID of the authorized replier 367 * @return integer The status ID 368 */ 369 function remoteAddAuthorizedReplier($issue_id, $usr_id, $replier) 370 { 371 $res = Authorized_Replier::manualInsert($issue_id, $replier, false); 372 if ($res != -1) { 373 // save a history entry about this... 374 History::add($issue_id, $usr_id, History::getTypeID('remote_replier_added'), 375 $replier . " remotely added to authorized repliers by " . User::getFullName($usr_id)); 376 } 377 return $res; 378 } 379 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 19 21:21:33 2007 | Cross-referenced by PHPXref 0.7 |