[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[441] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[441] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[441] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[42] | 24 | * admins.php |
---|
[1] | 25 | */ |
---|
| 26 | |
---|
| 27 | // require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 28 | |
---|
[457] | 29 | $app->sslOn(); |
---|
[1] | 30 | $auth->requireLogin(); |
---|
| 31 | |
---|
| 32 | require_once 'codebase/lib/PageNumbers.inc.php'; |
---|
[136] | 33 | require_once 'codebase/lib/Cache.inc.php'; |
---|
[1] | 34 | require_once 'codebase/lib/FormValidator.inc.php'; |
---|
| 35 | require_once 'codebase/lib/SortOrder.inc.php'; |
---|
[497] | 36 | require_once 'codebase/lib/HTML.inc.php'; |
---|
[1] | 37 | require_once 'codebase/lib/Prefs.inc.php'; |
---|
[137] | 38 | require_once 'codebase/lib/Lock.inc.php'; |
---|
| 39 | require_once 'codebase/lib/Version.inc.php'; |
---|
[1] | 40 | |
---|
| 41 | |
---|
[143] | 42 | /******************************************************************** |
---|
| 43 | * CONFIG |
---|
| 44 | ********************************************************************/ |
---|
[42] | 45 | |
---|
[1] | 46 | // Titles and navigation header. |
---|
[202] | 47 | $nav->add(_("Administrators"), null); |
---|
[535] | 48 | $nav->set('id', 'admins'); |
---|
[42] | 49 | |
---|
[1] | 50 | // The object to validate form input. |
---|
| 51 | $fv = new FormValidator(); |
---|
| 52 | |
---|
[152] | 53 | // Configure the prefs object. |
---|
[153] | 54 | $tmp_prefs = new Prefs('admins'); |
---|
| 55 | $tmp_prefs->setParam(array('persistent' => false)); |
---|
[136] | 56 | |
---|
[152] | 57 | // Configure the cache object. |
---|
[468] | 58 | $cache =& Cache::getInstance('admins'); |
---|
[405] | 59 | $cache->setParam(array('enabled' => false)); // Better leave disabled; the list gets out of sync with the db otherwise, somehow. |
---|
[152] | 60 | |
---|
[1] | 61 | // Instantiate a sorting object with the default sort and order. Add SQL for each column. |
---|
[295] | 62 | $so = new SortOrder('admin_id', 'DESC'); |
---|
| 63 | $so->setColumn('admin_id', $auth->getParam('db_primary_key') . ' ASC', $auth->getParam('db_primary_key') . ' DESC'); |
---|
| 64 | $so->setColumn('username', $auth->getParam('db_username_column') . ' ASC', $auth->getParam('db_username_column') . ' DESC'); |
---|
| 65 | $so->setColumn('userpass', $auth->getParam('db_table') . '.userpass ASC', $auth->getParam('db_table') . '.userpass DESC'); |
---|
| 66 | $so->setColumn('first_name', $auth->getParam('db_table') . '.first_name ASC', $auth->getParam('db_table') . '.first_name DESC'); |
---|
| 67 | $so->setColumn('last_name', $auth->getParam('db_table') . '.last_name ASC', $auth->getParam('db_table') . '.last_name DESC'); |
---|
| 68 | $so->setColumn('email', $auth->getParam('db_table') . '.email ASC', $auth->getParam('db_table') . '.email DESC'); |
---|
| 69 | $so->setColumn('seconds_online', $auth->getParam('db_table') . '.seconds_online ASC', $auth->getParam('db_table') . '.seconds_online DESC'); |
---|
| 70 | $so->setColumn('last_login_datetime', $auth->getParam('db_table') . '.last_login_datetime ASC', $auth->getParam('db_table') . '.last_login_datetime DESC'); |
---|
| 71 | $so->setColumn('last_access_datetime', $auth->getParam('db_table') . '.last_access_datetime ASC', $auth->getParam('db_table') . '.last_access_datetime DESC'); |
---|
| 72 | $so->setColumn('last_login_ip', $auth->getParam('db_table') . '.last_login_ip ASC', $auth->getParam('db_table') . '.last_login_ip DESC'); |
---|
| 73 | $so->setColumn('added_by_user_id', $auth->getParam('db_table') . '.added_by_user_id ASC', $auth->getParam('db_table') . '.added_by_user_id DESC'); |
---|
| 74 | $so->setColumn('modified_by_user_id', $auth->getParam('db_table') . '.modified_by_user_id ASC', $auth->getParam('db_table') . '.modified_by_user_id DESC'); |
---|
| 75 | $so->setColumn('added_datetime', $auth->getParam('db_table') . '.added_datetime ASC', $auth->getParam('db_table') . '.added_datetime DESC'); |
---|
| 76 | $so->setColumn('modified_datetime', $auth->getParam('db_table') . '.modified_datetime ASC', $auth->getParam('db_table') . '.modified_datetime DESC'); |
---|
[1] | 77 | |
---|
| 78 | // Instantiate page numbers. Total items are set and calculation is done in the getRecordList function. |
---|
| 79 | $page = new PageNumbers(); |
---|
| 80 | $page->setPerPage(getFormData('per_page'), 50); |
---|
| 81 | $page->setPageNumber(getFormData('page_number')); |
---|
| 82 | |
---|
[408] | 83 | // Query parameters to retain only locally. |
---|
| 84 | $locally_carried_queries = array( |
---|
| 85 | 'search_query', |
---|
| 86 | ); |
---|
[1] | 87 | |
---|
[143] | 88 | /******************************************************************** |
---|
| 89 | * MAIN |
---|
| 90 | ********************************************************************/ |
---|
[42] | 91 | |
---|
[1] | 92 | // We may want to use the add/edit interface from another script, so this |
---|
| 93 | // allows us to remember which page we came from so we can go back there. |
---|
[20] | 94 | if (getFormData('boomerang', false) && isset($_SERVER['HTTP_REFERER'])) { |
---|
[136] | 95 | $app->setBoomerangURL($_SERVER['HTTP_REFERER'], 'admins'); |
---|
[1] | 96 | } |
---|
| 97 | |
---|
| 98 | if (getFormData('break_list_cache', false)) { |
---|
[152] | 99 | // Remove any stale cached list data. |
---|
| 100 | $cache->delete('list'); |
---|
[1] | 101 | } |
---|
| 102 | |
---|
| 103 | // What action to take. |
---|
| 104 | switch (getFormData('op')) { |
---|
| 105 | |
---|
| 106 | case 'add' : |
---|
| 107 | // Initialize variables for the form template. |
---|
| 108 | $frm =& addRecordForm(); |
---|
[266] | 109 | $nav->add(_("Add Administrator")); |
---|
[1] | 110 | $main_template = 'admin_form.ihtml'; |
---|
| 111 | break; |
---|
| 112 | |
---|
| 113 | case 'edit' : |
---|
| 114 | // Initialize variables for the form template. |
---|
| 115 | $frm =& editRecordForm(getFormData('admin_id')); |
---|
[266] | 116 | $nav->add(_("Edit Administrator")); |
---|
[1] | 117 | $main_template = 'admin_form.ihtml'; |
---|
| 118 | break; |
---|
| 119 | |
---|
| 120 | case 'del' : |
---|
| 121 | deleteRecord(getFormData('admin_id')); |
---|
[136] | 122 | if ($app->validBoomerangURL('admins')) { |
---|
[1] | 123 | // Display boomerang page. |
---|
[408] | 124 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[1] | 125 | } |
---|
| 126 | // Display default page. |
---|
[408] | 127 | $app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries); |
---|
[1] | 128 | break; |
---|
| 129 | |
---|
| 130 | case 'insert' : |
---|
| 131 | if (getFormdata('cancel', false)) { |
---|
[136] | 132 | if ($app->validBoomerangURL('admins')) { |
---|
[22] | 133 | // Display boomerang page. |
---|
[408] | 134 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[22] | 135 | } |
---|
| 136 | // Display default page. |
---|
[408] | 137 | $app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries); |
---|
[1] | 138 | } |
---|
| 139 | validateInput(); |
---|
| 140 | if ($fv->anyErrors()) { |
---|
| 141 | $frm =& addRecordForm(); |
---|
| 142 | $frm = array_merge($frm, getFormData()); |
---|
[266] | 143 | $nav->add(_("Add Administrator")); |
---|
[1] | 144 | $main_template = 'admin_form.ihtml'; |
---|
| 145 | } else { |
---|
| 146 | $admin_id = insertRecord(getFormData()); |
---|
| 147 | if (getFormdata('repeat', false)) { |
---|
| 148 | // Display function again. |
---|
[408] | 149 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=add', $locally_carried_queries); |
---|
[136] | 150 | } else if ($app->validBoomerangURL('admins')) { |
---|
[1] | 151 | // Display boomerang page. |
---|
[408] | 152 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[1] | 153 | } |
---|
| 154 | // Display default page. |
---|
[408] | 155 | $app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries); |
---|
[1] | 156 | } |
---|
| 157 | break; |
---|
| 158 | |
---|
| 159 | case 'update' : |
---|
| 160 | if (getFormdata('reset', false)) { |
---|
[136] | 161 | $app->raiseMsg(_("Saved values have been reloaded."), MSG_NOTICE, __FILE__, __LINE__); |
---|
[408] | 162 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&admin_id=' . getFormData('admin_id'), $locally_carried_queries); |
---|
[1] | 163 | } |
---|
| 164 | if (getFormdata('cancel', false)) { |
---|
| 165 | // Remove lock |
---|
[295] | 166 | $lock->select($auth->getParam('db_table'), $auth->getParam('db_primary_key'), getFormData('admin_id')); |
---|
[1] | 167 | $lock->remove(); |
---|
[136] | 168 | if ($app->validBoomerangURL('admins')) { |
---|
[1] | 169 | // Display boomerang page. |
---|
[408] | 170 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[1] | 171 | } |
---|
| 172 | // Display default page. |
---|
[408] | 173 | $app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries); |
---|
[1] | 174 | } |
---|
| 175 | validateInput(); |
---|
| 176 | if ($fv->anyErrors()) { |
---|
| 177 | $frm =& editRecordForm(getFormData('admin_id')); |
---|
| 178 | $frm = array_merge($frm, getFormData()); |
---|
[266] | 179 | $nav->add(_("Edit Administrator")); |
---|
[1] | 180 | $main_template = 'admin_form.ihtml'; |
---|
| 181 | } else { |
---|
| 182 | updateRecord(getFormData()); |
---|
| 183 | if (getFormdata('repeat', false)) { |
---|
| 184 | // Display edit function with next available ID. |
---|
[295] | 185 | $qid = $db->query("SELECT " . $auth->getParam('db_primary_key') . " FROM " . $auth->getParam('db_table') . " WHERE " . $auth->getParam('db_primary_key') . " > '" . $db->escapeString(getFormData('admin_id')) . "' ORDER BY " . $auth->getParam('db_primary_key') . " ASC LIMIT 1"); |
---|
[1] | 186 | if (list($next_id) = mysql_fetch_row($qid)) { |
---|
[408] | 187 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&admin_id=' . $next_id, $locally_carried_queries); |
---|
[1] | 188 | } else { |
---|
[136] | 189 | $app->raiseMsg(_("Cannot edit next, the end of the list was reached"), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 190 | } |
---|
[136] | 191 | } else if ($app->validBoomerangURL('admins')) { |
---|
[1] | 192 | // Display boomerang page. |
---|
[408] | 193 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[1] | 194 | } |
---|
| 195 | // Display default page. |
---|
[408] | 196 | $app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries); |
---|
[1] | 197 | } |
---|
| 198 | break; |
---|
| 199 | |
---|
| 200 | default : |
---|
| 201 | $list =& getRecordList(); |
---|
| 202 | $main_template = 'admin_list.ihtml'; |
---|
| 203 | break; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /****************************************************************************** |
---|
| 207 | * TEMPLATE INITIALIZATION |
---|
| 208 | *****************************************************************************/ |
---|
| 209 | |
---|
| 210 | include 'header.ihtml'; |
---|
[408] | 211 | $app->carryQuery($locally_carried_queries); |
---|
[1] | 212 | include 'codebase/services/templates/' . $main_template; |
---|
| 213 | include 'footer.ihtml'; |
---|
| 214 | |
---|
[143] | 215 | /******************************************************************** |
---|
| 216 | * FUNCTIONS |
---|
| 217 | ********************************************************************/ |
---|
[1] | 218 | |
---|
| 219 | |
---|
| 220 | function validateInput() |
---|
| 221 | { |
---|
| 222 | global $fv, $auth; |
---|
| 223 | |
---|
| 224 | // If the username was changed during edit, verify. |
---|
| 225 | if (((getFormData('username') != getFormData('old_username')) && 'update' == getFormData('op')) |
---|
| 226 | || 'insert' == getFormData('op')) { |
---|
| 227 | if ($auth->usernameExists(getFormData('username'))) { |
---|
[141] | 228 | $fv->addError('username', sprintf(_("The username %s already exists. Please choose another."), getFormData('username'))); |
---|
[1] | 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
[497] | 232 | $fv->numericRange('admin_id', 0, 32767, _("<strong>Admin id</strong> must be a valid number between 0 and 32767.")); |
---|
[1] | 233 | |
---|
| 234 | $fv->isEmpty('username', _("<strong>Username</strong> cannot be blank.")); |
---|
| 235 | $fv->stringLength('username', 0, 255, _("<strong>Username</strong> must contain less than 256 characters.")); |
---|
[42] | 236 | |
---|
[1] | 237 | $fv->isEmpty('userpass', _("<strong>Passwords</strong> cannot be blank.")); |
---|
[497] | 238 | $fv->stringLength('userpass', 8, 36, _("<strong>Passwords</strong> must be between 8 and 36 characters long.")); |
---|
[42] | 239 | |
---|
[1] | 240 | $fv->stringLength('first_name', 0, 255, _("<strong>First name</strong> must contain less than 256 characters.")); |
---|
[42] | 241 | |
---|
[1] | 242 | $fv->stringLength('last_name', 0, 255, _("<strong>Last name</strong> must contain less than 256 characters.")); |
---|
[19] | 243 | |
---|
| 244 | $fv->isEmpty('email', _("<strong>Email</strong> cannot be blank.")); |
---|
[1] | 245 | $fv->validateEmail('email'); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | function &addRecordForm() |
---|
| 249 | { |
---|
[19] | 250 | // Set default values for the reset of the fields. |
---|
| 251 | $frm = array( |
---|
| 252 | 'admin_id' => '', |
---|
[72] | 253 | 'old_username' => '', |
---|
[19] | 254 | 'username' => '', |
---|
| 255 | 'userpass' => '', |
---|
| 256 | 'first_name' => '', |
---|
| 257 | 'last_name' => '', |
---|
| 258 | 'email' => '', |
---|
| 259 | 'seconds_online' => '0', |
---|
[601] | 260 | 'last_login_datetime' => '', |
---|
| 261 | 'last_access_datetime' => '', |
---|
[19] | 262 | 'last_login_ip' => '0.0.0.0', |
---|
| 263 | 'added_by_user_id' => '', |
---|
| 264 | 'modified_by_user_id' => '', |
---|
[601] | 265 | 'added_datetime' => '', |
---|
| 266 | 'modified_datetime' => '', |
---|
[19] | 267 | 'new_op' => 'insert', |
---|
| 268 | 'submit_buttons' => array( |
---|
[497] | 269 | array('name' => 'submit', 'value' => _("Add Administrator"), 'class' => 'small button', 'accesskey' => 's'), |
---|
| 270 | array('name' => 'repeat', 'value' => _("Add & repeat"), 'class' => 'small button secondary', 'accesskey' => 'r'), |
---|
| 271 | array('name' => 'cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'), |
---|
[19] | 272 | ), |
---|
| 273 | ); |
---|
[1] | 274 | |
---|
| 275 | return $frm; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | function &editRecordForm($id) |
---|
| 279 | { |
---|
[295] | 280 | global $auth; |
---|
[22] | 281 | global $lock; |
---|
[497] | 282 | global $locally_carried_queries; |
---|
[479] | 283 | $app =& App::getInstance(); |
---|
| 284 | $db =& DB::getInstance(); |
---|
[441] | 285 | |
---|
[295] | 286 | $lock->select($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $id); |
---|
[1] | 287 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 288 | $lock->dieErrorPage(); |
---|
[19] | 289 | } |
---|
| 290 | |
---|
| 291 | // Get the information for the form. |
---|
[136] | 292 | $qid = $db->query(" |
---|
[295] | 293 | SELECT *, |
---|
| 294 | " . $auth->getParam('db_primary_key') . " AS admin_id |
---|
| 295 | FROM " . $auth->getParam('db_table') . " |
---|
| 296 | WHERE " . $auth->getParam('db_primary_key') . " = '" . $db->escapeString($id) . "' |
---|
[19] | 297 | "); |
---|
| 298 | if (!$frm = mysql_fetch_assoc($qid)) { |
---|
[136] | 299 | $app->logMsg('Could not find record with admin_id: ' . $id, LOG_WARNING, __FILE__, __LINE__); |
---|
| 300 | $app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__); |
---|
[408] | 301 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[19] | 302 | } |
---|
[42] | 303 | |
---|
[19] | 304 | // Lock this record. |
---|
[295] | 305 | $lock->set($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $id, $frm['username']); |
---|
[42] | 306 | |
---|
[19] | 307 | // Set misc values for the form. |
---|
| 308 | $frm = array_merge(array( |
---|
| 309 | 'admin_id' => '', |
---|
| 310 | 'old_username' => $frm['username'], |
---|
| 311 | 'username' => '', |
---|
| 312 | // 'userpass' => '****************', |
---|
| 313 | 'first_name' => '', |
---|
| 314 | 'last_name' => '', |
---|
| 315 | 'email' => '', |
---|
| 316 | 'seconds_online' => '0', |
---|
[601] | 317 | 'last_login_datetime' => '', |
---|
| 318 | 'last_access_datetime' => '', |
---|
[19] | 319 | 'last_login_ip' => '0.0.0.0', |
---|
| 320 | 'added_by_user_id' => '', |
---|
| 321 | 'modified_by_user_id' => '', |
---|
[601] | 322 | 'added_datetime' => '', |
---|
| 323 | 'modified_datetime' => '', |
---|
[19] | 324 | 'new_op' => 'update', |
---|
[25] | 325 | 'old_username' => $frm['username'], |
---|
[19] | 326 | 'submit_buttons' => array( |
---|
[497] | 327 | array('name' => 'submit', 'value' => _("Save changes"), 'class' => 'small button', 'accesskey' => 's'), |
---|
| 328 | array('name' => 'repeat', 'value' => _("Save & edit next"), 'class' => 'small button secondary', 'accesskey' => 'e'), |
---|
| 329 | array('name' => 'reset', 'value' => _("Reset"), 'class' => 'small button secondary', 'accesskey' => 'r'), |
---|
| 330 | array('name' => 'cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'), |
---|
[19] | 331 | ), |
---|
[497] | 332 | ), $frm, array('userpass' => 'â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢')); |
---|
[19] | 333 | |
---|
| 334 | return $frm; |
---|
[1] | 335 | } |
---|
| 336 | |
---|
| 337 | function deleteRecord($id) |
---|
| 338 | { |
---|
[153] | 339 | global $auth; |
---|
| 340 | global $lock; |
---|
| 341 | global $cache; |
---|
[497] | 342 | global $locally_carried_queries; |
---|
[479] | 343 | $app =& App::getInstance(); |
---|
| 344 | $db =& DB::getInstance(); |
---|
[441] | 345 | |
---|
[295] | 346 | $lock->select($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $id); |
---|
[1] | 347 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 348 | $lock->dieErrorPage(); |
---|
[21] | 349 | } |
---|
| 350 | |
---|
[152] | 351 | // Remove any stale cached list data. |
---|
| 352 | $cache->delete('list'); |
---|
[42] | 353 | |
---|
[21] | 354 | // Get the information for this object. |
---|
[136] | 355 | $qid = $db->query(" |
---|
[432] | 356 | SELECT " . $auth->getParam('db_username_column') . " |
---|
[458] | 357 | FROM " . $auth->getParam('db_table') . " |
---|
[295] | 358 | WHERE " . $auth->getParam('db_primary_key') . " = '" . $db->escapeString($id) . "' |
---|
[21] | 359 | "); |
---|
[432] | 360 | if (! list($name) = mysql_fetch_row($qid)) { |
---|
[136] | 361 | $app->logMsg('Could not find record with admin_id: ' . $id, LOG_WARNING, __FILE__, __LINE__); |
---|
| 362 | $app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__); |
---|
[408] | 363 | $app->dieBoomerangURL('admins', $locally_carried_queries); |
---|
[21] | 364 | } |
---|
[42] | 365 | |
---|
[21] | 366 | // Get the information for this object. |
---|
[295] | 367 | $qid = $db->query("SELECT COUNT(*) from " . $auth->getParam('db_table') . ""); |
---|
[21] | 368 | list($num_admins) = mysql_fetch_row($qid); |
---|
[432] | 369 | if ($num_admins <= 1) { |
---|
[21] | 370 | // There must always be at least one admnistrator! |
---|
[497] | 371 | $app->raiseMsg(_("You cannot delete the only user in the database. There must be at least one to log in and create other users."), MSG_NOTICE, __FILE__, __LINE__); |
---|
[147] | 372 | } else if ($auth->get('user_id') == $id) { |
---|
[21] | 373 | // Do not delete yourself! |
---|
[136] | 374 | $app->raiseMsg(_("You cannot delete yourself."), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 375 | } else { |
---|
[21] | 376 | // Delete the record. |
---|
[295] | 377 | $db->query("DELETE FROM " . $auth->getParam('db_table') . " WHERE " . $auth->getParam('db_primary_key') . " = '" . $db->escapeString($id) . "'"); |
---|
[141] | 378 | $app->raiseMsg(sprintf(_("The admin <em>%s</em> has been deleted."), $name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[21] | 379 | } |
---|
[1] | 380 | |
---|
[21] | 381 | // Unlock record. |
---|
| 382 | $lock->remove(); |
---|
[1] | 383 | } |
---|
| 384 | |
---|
| 385 | function insertRecord($frm) |
---|
| 386 | { |
---|
| 387 | global $auth; |
---|
[153] | 388 | global $cache; |
---|
[479] | 389 | $app =& App::getInstance(); |
---|
| 390 | $db =& DB::getInstance(); |
---|
[441] | 391 | |
---|
[152] | 392 | // Remove any stale cached list data. |
---|
| 393 | $cache->delete('list'); |
---|
[42] | 394 | |
---|
[1] | 395 | // Insert record data. |
---|
[136] | 396 | $db->query(" |
---|
[295] | 397 | INSERT INTO " . $auth->getParam('db_table') . " ( |
---|
| 398 | " . $auth->getParam('db_username_column') . ", |
---|
[1] | 399 | first_name, |
---|
| 400 | last_name, |
---|
| 401 | email, |
---|
[19] | 402 | added_by_user_id, |
---|
[1] | 403 | added_datetime |
---|
| 404 | ) VALUES ( |
---|
[136] | 405 | '" . $db->escapeString($frm['username']) . "', |
---|
| 406 | '" . $db->escapeString($frm['first_name']) . "', |
---|
| 407 | '" . $db->escapeString($frm['last_name']) . "', |
---|
| 408 | '" . $db->escapeString($frm['email']) . "', |
---|
[147] | 409 | '" . $db->escapeString($auth->get('user_id')) . "', |
---|
[1] | 410 | NOW() |
---|
| 411 | ) |
---|
| 412 | "); |
---|
[136] | 413 | $last_insert_id = mysql_insert_id($db->getDBH()); |
---|
[42] | 414 | |
---|
[1] | 415 | // Set admin password. |
---|
| 416 | $auth->setPassword($last_insert_id, $frm['userpass']); |
---|
[42] | 417 | |
---|
[19] | 418 | // Create version. |
---|
[159] | 419 | $version = Version::getInstance($auth); |
---|
[295] | 420 | $version->create($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $last_insert_id, $frm['username']); |
---|
[42] | 421 | |
---|
[497] | 422 | $app->raiseMsg(sprintf(_("The user <em>%s</em> has been added."), $frm['username']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[42] | 423 | |
---|
[1] | 424 | return $last_insert_id; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | function updateRecord($frm) |
---|
| 428 | { |
---|
[153] | 429 | global $auth; |
---|
| 430 | global $lock; |
---|
| 431 | global $cache; |
---|
[479] | 432 | $app =& App::getInstance(); |
---|
| 433 | $db =& DB::getInstance(); |
---|
[441] | 434 | |
---|
[295] | 435 | $lock->select($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $frm['admin_id']); |
---|
[1] | 436 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 437 | $lock->dieErrorPage(); |
---|
[21] | 438 | } |
---|
[1] | 439 | |
---|
[152] | 440 | // Remove any stale cached list data. |
---|
| 441 | $cache->delete('list'); |
---|
[42] | 442 | |
---|
[21] | 443 | // If the userpass is left blank or with the filler **** characters, we don't want to update it. |
---|
| 444 | if (!empty($frm['userpass']) && !preg_match('/[\*]{4,}/', $frm['userpass'])) { |
---|
| 445 | // Set user password. |
---|
| 446 | $auth->setPassword($frm['admin_id'], $frm['userpass']); |
---|
| 447 | } |
---|
[42] | 448 | |
---|
[21] | 449 | // Update record data. |
---|
[136] | 450 | $db->query(" |
---|
[295] | 451 | UPDATE " . $auth->getParam('db_table') . " SET |
---|
| 452 | " . $auth->getParam('db_username_column') . " = '" . $db->escapeString($frm['username']) . "', |
---|
[136] | 453 | first_name = '" . $db->escapeString($frm['first_name']) . "', |
---|
| 454 | last_name = '" . $db->escapeString($frm['last_name']) . "', |
---|
| 455 | email = '" . $db->escapeString($frm['email']) . "', |
---|
[147] | 456 | modified_by_user_id = '" . $db->escapeString($auth->get('user_id')) . "', |
---|
[21] | 457 | modified_datetime = NOW() |
---|
[295] | 458 | WHERE " . $auth->getParam('db_primary_key') . " = '" . $db->escapeString($frm['admin_id']) . "' |
---|
[21] | 459 | "); |
---|
[1] | 460 | |
---|
[21] | 461 | // Create version. |
---|
[159] | 462 | $version = Version::getInstance($auth); |
---|
[295] | 463 | $version->create($auth->getParam('db_table'), $auth->getParam('db_primary_key'), $frm['admin_id'], $frm['username']); |
---|
[21] | 464 | |
---|
[497] | 465 | $app->raiseMsg(sprintf(_("The user <em>%s</em> has been updated."), $frm['username']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[21] | 466 | |
---|
| 467 | // Unlock record. |
---|
| 468 | $lock->remove(); |
---|
[1] | 469 | } |
---|
| 470 | |
---|
| 471 | function &getRecordList() |
---|
| 472 | { |
---|
| 473 | global $page; |
---|
| 474 | global $so; |
---|
[153] | 475 | global $tmp_prefs; |
---|
| 476 | global $cache; |
---|
[295] | 477 | global $auth; |
---|
[479] | 478 | $db =& DB::getInstance(); |
---|
[441] | 479 | |
---|
[19] | 480 | $where_clause = ''; |
---|
[42] | 481 | |
---|
[19] | 482 | // Build search query if available. |
---|
| 483 | if (getFormData('search_query', false)) { |
---|
| 484 | $qry_words = preg_split('/[^\w]/', getFormData('search_query')); |
---|
[1] | 485 | for ($i=0; $i<sizeof($qry_words); $i++) { |
---|
[19] | 486 | $where_clause .= (empty($where_clause) ? 'WHERE' : 'AND') . " |
---|
| 487 | ( |
---|
[295] | 488 | " . $auth->getParam('db_table') . "." . $auth->getParam('db_username_column') . " LIKE '%" . $db->escapeString($qry_words[$i]) . "%' |
---|
| 489 | OR " . $auth->getParam('db_table') . ".first_name LIKE '%" . $db->escapeString($qry_words[$i]) . "%' |
---|
| 490 | OR " . $auth->getParam('db_table') . ".last_name LIKE '%" . $db->escapeString($qry_words[$i]) . "%' |
---|
| 491 | OR " . $auth->getParam('db_table') . ".email LIKE '%" . $db->escapeString($qry_words[$i]) . "%' |
---|
[1] | 492 | ) |
---|
| 493 | "; |
---|
| 494 | } |
---|
| 495 | } |
---|
[19] | 496 | |
---|
[1] | 497 | // Count the total number of records so we can do something about the page numbers. |
---|
[136] | 498 | $qid = $db->query(" |
---|
[42] | 499 | SELECT COUNT(*) |
---|
[295] | 500 | FROM " . $auth->getParam('db_table') . " |
---|
[19] | 501 | $where_clause |
---|
| 502 | "); |
---|
[1] | 503 | list($num_results) = mysql_fetch_row($qid); |
---|
[42] | 504 | |
---|
[1] | 505 | // Set page numbers now we know (needed for next step). |
---|
| 506 | $page->setTotalItems($num_results); |
---|
| 507 | $page->calculate(); |
---|
[42] | 508 | |
---|
[1] | 509 | // Final SQL, with sort and page limiters. |
---|
| 510 | $sql = " |
---|
[42] | 511 | SELECT |
---|
[295] | 512 | " . $auth->getParam('db_table') . ".*, |
---|
[441] | 513 | " . $auth->getParam('db_table') . "." . $auth->getParam('db_primary_key') . " AS admin_id, |
---|
[295] | 514 | a1." . $auth->getParam('db_username_column') . " AS added_admin_username, |
---|
| 515 | a2." . $auth->getParam('db_username_column') . " AS modified_admin_username |
---|
| 516 | FROM " . $auth->getParam('db_table') . " |
---|
| 517 | LEFT JOIN " . $auth->getParam('db_table') . " a1 ON (" . $auth->getParam('db_table') . ".added_by_user_id = a1." . $auth->getParam('db_primary_key') . ") |
---|
| 518 | LEFT JOIN " . $auth->getParam('db_table') . " a2 ON (" . $auth->getParam('db_table') . ".modified_by_user_id = a2." . $auth->getParam('db_primary_key') . ") |
---|
[1] | 519 | $where_clause |
---|
| 520 | " . $so->getSortOrderSQL() . " |
---|
| 521 | " . $page->getLimitSQL() . " |
---|
| 522 | "; |
---|
[42] | 523 | |
---|
[152] | 524 | // Use a cash hash to determine if the result-set has changed. |
---|
[1] | 525 | // A unique key for this query, with the total_items in case db records |
---|
| 526 | // were added since the last cache. This identifies a unique set of |
---|
| 527 | // cached data, but we must refer to the list that is cached by a more |
---|
| 528 | // generic name. so that we can flush the cache (if records updated) |
---|
| 529 | // without knowing the hash. |
---|
| 530 | $cache_hash = md5($sql . '|' . $page->total_items); |
---|
[153] | 531 | if ($tmp_prefs->get('cache_hash') != $cache_hash) { |
---|
[152] | 532 | $cache->delete('list'); |
---|
[153] | 533 | $tmp_prefs->set('cache_hash', $cache_hash); |
---|
[1] | 534 | } |
---|
[42] | 535 | |
---|
[152] | 536 | // First try to return from the cache. |
---|
| 537 | if ($cache->exists('list')) { |
---|
[266] | 538 | $list = $cache->get('list'); |
---|
| 539 | return $list; |
---|
[152] | 540 | } |
---|
[441] | 541 | |
---|
[152] | 542 | // The list was not cached, so issue the real query. |
---|
| 543 | $qid = $db->query($sql); |
---|
| 544 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 545 | $list[] = $row; |
---|
| 546 | } |
---|
[42] | 547 | |
---|
[152] | 548 | // Save this list into the cache. |
---|
| 549 | if (isset($list) && !empty($list)) { |
---|
| 550 | $cache->set('list', $list); |
---|
[1] | 551 | } |
---|
| 552 | |
---|
| 553 | return $list; |
---|
| 554 | } |
---|
| 555 | |
---|