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