Large code cleanups:
[mailer.git] / inc / libs / admins_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/30/2003 *
4  * ===================                          Last change: 11/27/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admins_functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for the admins extension               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die admins-Erweiterung           *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 }
42
43 // Check ACL for menu combination
44 function isAdminsAllowedByAcl ($action, $what) {
45         // Get admin's id
46         $adminId = getCurrentAdminId();
47
48         if (($action == 'login') || ($action == 'logout')) {
49                 // If action is login or logout allow allways!
50                 return true;
51         } elseif (isset($GLOBALS[__FUNCTION__][$adminId][$action][$what])) {
52                 // If we have cache, use it
53                 return $GLOBALS[__FUNCTION__][$adminId][$action][$what];
54         }
55
56         // But default result is failed
57         $GLOBALS[__FUNCTION__][$action][$what] = false;
58
59         // Get admin's defult access right
60         $default = getAdminDefaultAcl($adminId);
61
62         if (!empty($what)) {
63                 // Check for parent menu:
64                 // First get it's action value
65                 $parent_action = getActionFromModuleWhat('admin', $what);
66
67                 // Check with this function...
68                 $parent = isAdminsAllowedByAcl($parent_action, '');
69         } else {
70                 // Anything else is true!
71                 $parent = false;
72         }
73
74         // Shall I test for a main or sub menu? (action or what?)
75         $aclMode = 'failed';
76         if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['admin_acls'])) && (count($GLOBALS['cache_array']['admin_acls']) > 0)) {
77                 // Lookup in cache
78                 if ((!empty($action)) && (isset($GLOBALS['cache_array']['admin_acls']['action_menu'][$adminId])) & (in_array($action, $GLOBALS['cache_array']['admin_acls']['action_menu'][$adminId]))) {
79                         // Search for it
80                         $key = array_search($action, $GLOBALS['cache_array']['admin_acls']['action_menu'][$adminId]);
81
82                         // Main menu line found
83                         $aclMode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$adminId][$key];
84
85                         // Log debug message
86                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'action=' . $action . ',key=' . $key . ',acl_mode=' . $aclMode);
87
88                         // Count cache hits
89                         incrementStatsEntry('cache_hits');
90                 } elseif ((!empty($what)) && (isset($GLOBALS['cache_array']['admin_acls']['what_menu'][$adminId])) && (in_array($what, $GLOBALS['cache_array']['admin_acls']['what_menu'][$adminId]))) {
91                         // Search for it
92                         $key = array_search($action, $GLOBALS['cache_array']['admin_acls']['what_menu'][$adminId]);
93
94                         // Check sub menu
95                         $aclMode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$adminId][$key];
96
97                         // Log debug message
98                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'what=' . $what . ',key=' . $key . ',acl_mode=' . $aclMode);
99
100                         // Count cache hits
101                         incrementStatsEntry('cache_hits');
102                 }
103         } elseif (!isExtensionActive('cache')) {
104                 // Extension ext-cache is absent, so load it from database
105                 $result = false;
106                 if (!empty($action)) {
107                         // Main menu
108                         $result = SQL_QUERY_ESC("SELECT `access_mode` FROM `{?_MYSQL_PREFIX?}_admins_acls` WHERE `admin_id`=%s AND `action_menu`='%s' LIMIT 1",
109                                 array(bigintval($adminId), $action), __FUNCTION__, __LINE__);
110                 } elseif (!empty($what)) {
111                         // Sub menu
112                         $result = SQL_QUERY_ESC("SELECT `access_mode` FROM `{?_MYSQL_PREFIX?}_admins_acls` WHERE `admin_id`=%s AND `what_menu`='%s' LIMIT 1",
113                                 array(bigintval($adminId), $what), __FUNCTION__, __LINE__);
114                 }
115
116                 // Is an entry found?
117                 if (SQL_NUMROWS($result) == 1) {
118                         // Load ACL
119                         list($aclMode) = SQL_FETCHROW($result);
120                 } // END - if
121
122                 // Free memory
123                 SQL_FREERESULT($result);
124         }
125
126         // Check ACL and (maybe) allow
127         //* DEBUG: */ debugOutput('default='.$default.',acl_mode='.$aclMode.',parent='.intval($parent));
128         if ((($default == 'allow') && ($aclMode != 'deny')) || (($default == 'deny') && ($aclMode == 'allow')) || ($parent === true) || (($default == 'NO-ACL') && ($aclMode == 'failed') && ($parent === false))) {
129                 // Access is granted
130                 $GLOBALS[__FUNCTION__][$adminId][$action][$what] = true;
131         } // END - if
132
133         // Return value
134         //* DEBUG: */ debugOutput(__FUNCTION__.'['.__LINE__.']:act='.$action.',wht='.$what.',default='.$default.',aclMode='.$aclMode);
135         return $GLOBALS[__FUNCTION__][$adminId][$action][$what];
136 }
137
138 // Create email link to admins's account
139 function generateAdminEmailLink ($email, $mod = 'admin') {
140         // Is it an email?
141         if (isInString('@', $email)) {
142                 // Create email link
143                 $result = SQL_QUERY_ESC("SELECT `id`
144 FROM
145         `{?_MYSQL_PREFIX?}_admins`
146 WHERE
147         '%s' REGEXP `email`
148 LIMIT 1",
149                 array($email), __FUNCTION__, __LINE__);
150
151                 // Is there an entry?
152                 if (SQL_NUMROWS($result) == 1) {
153                         // Load userid
154                         list($adminId) = SQL_FETCHROW($result);
155
156                         // Call this function again
157                         $email = generateAdminEmailLink($adminId, $mod);
158                 } // END - if
159
160                 // Free memory
161                 SQL_FREERESULT($result);
162         } elseif (isValidUserId($email)) {
163                 // Direct id given
164                 $email = '{%url=modules.php?module=' . $mod . '&amp;what=admins_contct&amp;id=' . bigintval($email) . '%}';
165         } else {
166                 // This is strange and needs fixing
167                 debug_report_bug(__FUNCTION__, __LINE__, 'email[' . gettype($email) . ']=' . $email . ',mod=' . $mod . ' - This should not happen.');
168         }
169
170         // Return rewritten (?) email address
171         return $email;
172 }
173
174 // Change a lot admin account
175 function adminsChangeAdminAccount ($postData, $element = '', $displayMessage = true) {
176         // Begin the update
177         $cache_update = '0';
178         $message = '';
179
180         foreach ($postData['login'] as $id => $login) {
181                 // Secure id number
182                 $id = bigintval($id);
183                 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',login=' . $login);
184
185                 // When both passwords match update admin account
186                 if ((!empty($element)) && (isset($postData[$element]))) {
187                         // Save this setting
188                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_admins` SET `%s`='%s' WHERE `id`=%s LIMIT 1",
189                                 array($element, $postData[$element][$id], $id), __FUNCTION__, __LINE__);
190
191                         // Admin account saved
192                         $message = '{--ADMIN_ACCOUNT_SAVED--}';
193                 } elseif ((!empty($postData['pass1'])) && (!empty($postData['pass2']))) {
194                         // Update only if both passwords match
195                         if (($postData['pass1'][$id] == $postData['pass2'][$id])) {
196                                 // Save only when both passwords are the same (also when they are empty)
197                                 $add = ''; $cache_update = 1;
198
199                                 // Generate hash
200                                 $hash = generateHash($postData['pass1'][$id]);
201
202                                 // Save password when set
203                                 if (!empty($postData['pass1'][$id])) {
204                                         $add = sprintf(",`password`='%s'", SQL_ESCAPE($hash));
205                                 } // END - if
206
207                                 // Get admin's id
208                                 $adminId = getCurrentAdminId();
209                                 $salt = substr(getAdminHash(getAdminLogin($adminId)), 0, -40);
210
211                                 // Rewrite cookie when it's own account
212                                 if ($adminId == $id) {
213                                         // Set timeout cookie
214                                         setAdminLast(time());
215
216                                         if ($adminId != getCurrentAdminId()) {
217                                                 // Update login cookie
218                                                 setAdminId($adminId);
219
220                                                 // Update password cookie as well?
221                                                 if (!empty($add)) {
222                                                         setAdminMd5($hash);
223                                                 } // END - if
224                                         } elseif (generateHash($postData['pass1'][$id], $salt) != getAdminMd5()) {
225                                                 // Update password cookie
226                                                 setAdminMd5($hash);
227                                         }
228                                 } // END - if
229
230                                 // Get default ACL from admin to check if we can allow him to change the default ACL
231                                 $default = getAdminDefaultAcl(getCurrentAdminId());
232
233                                 // Update admin account
234                                 if ($default == 'allow') {
235                                         // Allow changing default ACL
236                                         SQL_QUERY_ESC("UPDATE
237         `{?_MYSQL_PREFIX?}_admins`
238 SET
239         `login`='%s'" . $add . ",
240         `email`='%s',
241         `default_acl`='%s',
242         `la_mode`='%s'
243 WHERE
244         `id`=%s
245 LIMIT 1",
246                                         array(
247                                                 $login,
248                                                 $postData['email'][$id],
249                                                 $postData['mode'][$id],
250                                                 $postData['la_mode'][$id],
251                                                 $id
252                                         ), __FUNCTION__, __LINE__);
253                                 } else {
254                                         // Do not allow it here
255                                         SQL_QUERY_ESC("UPDATE
256         `{?_MYSQL_PREFIX?}_admins`
257 SET
258         `login`='%s'" . $add . ",
259         `email`='%s',
260         `la_mode`='%s'
261 WHERE
262         `id`=%s
263 LIMIT 1",
264                                         array(
265                                                 $login,
266                                                 $postData['email'][$id],
267                                                 $postData['la_mode'][$id],
268                                                 $id
269                                         ), __FUNCTION__, __LINE__);
270                                 }
271
272                                 // Admin account saved
273                                 $message = '{--ADMIN_ACCOUNT_SAVED--}';
274                         } else {
275                                 // Passwords did not match
276                                 $message = '{--ADMIN_ADMINS_ERROR_PASS_MISMATCH--}';
277                         }
278                 } else {
279                         // Update whole array
280                         $SQL = getUpdateSqlFromArray($postData, 'admins', 'id', '%s', array('login', 'id'), $id);
281
282                         // Run it
283                         SQL_QUERY_ESC($SQL, array(bigintval($id)), __FUNCTION__, __LINE__);
284
285                         // Was it updated?
286                         if (SQL_AFFECTEDROWS() == 1) {
287                                 // Admin account saved
288                                 $message = '{--ADMIN_ACCOUNT_SAVED--}';
289                         } else {
290                                 // Passwords did not match
291                                 $message = '{--ADMIN_ADMINS_ERROR_PASS_MISMATCH--}';
292                         }
293                 }
294         } // END - foreach
295
296         // Display message if not empty and allowed
297         if ((!empty($message)) && ($displayMessage === true)) {
298                 // Display it
299                 displayMessage($message);
300         } // END - if
301
302         // Remove cache file
303         runFilterChain('post_form_submited', postRequestArray());
304
305         // Return message
306         return $message;
307 }
308
309 // Make admin accounts editable
310 function adminsEditAdminAccount ($postData) {
311         // "Resolve" current's admin access mode
312         $currMode = getAdminDefaultAcl(getCurrentAdminId());
313
314         // Begin the edit loop
315         $OUT = '';
316         foreach ($postData['sel'] as $id => $selected) {
317                 // Secure id number
318                 $id = bigintval($id);
319
320                 // Get the admin's data
321                 $result = SQL_QUERY_ESC("SELECT `login`,`email` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
322                         array($id), __FUNCTION__, __LINE__);
323                 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
324                         // Entry found
325                         $content = SQL_FETCHARRAY($result);
326
327                         // Prepare some more data for the template
328                         $content['id'] = $id;
329
330                         // Shall we allow changing default ACL?
331                         if ($currMode == 'allow') {
332                                 // Allow changing it
333                                 $content['mode'] = '{%pipe,generateAdminAccessModeSelectionBox=' . $id . '%}';
334                         } else {
335                                 // Don't allow it
336                                 $content['mode'] = '&nbsp;';
337                         }
338
339                         // Load row template and switch color
340                         $OUT .= loadTemplate('admin_edit_admins_row', true, $content);
341                 } // END - if
342
343                 // Free result
344                 SQL_FREERESULT($result);
345         } // END - foreach
346
347         // Load template
348         loadTemplate('admin_edit_admins', false, $OUT);
349 }
350
351 // Generate access mode selection box for given admin id
352 function generateAdminAccessModeSelectionBox ($adminId = NULL) {
353         // Start the selection box
354         $OUT = '<select name="mode[' . $adminId . ']" size="1" class="form_select">';
355
356         // Add option list
357         $OUT .= generateOptionList('/ARRAY/', array('allow', 'deny'), array('{--ADMIN_ADMINS_ACCESS_MODE_ALLOW--}', '{--ADMIN_ADMINS_ACCESS_MODE_DENY--}'), getAdminDefaultAcl($adminId));
358
359         // Finish it
360         $OUT .= '</select>';
361
362         // Return content
363         return $OUT;
364 }
365
366 // Generate menu mode selection box for given admin it
367 function generateAdminMenuModeSelectionBox ($adminId = NULL) {
368         // Start the selection box
369         $OUT = '<select name="la_mode[{%pipe,makeNullToZero=' . makeZeroToNull($adminId) . '%}]" size="1" class="form_select">';
370
371         // Add option list
372         $OUT .= generateOptionList('/ARRAY/', array('global', 'OLD', 'NEW'), array('{--ADMIN_ADMINS_LA_MODE_GLOBAL--}', '{--ADMIN_ADMINS_LA_MODE_OLD--}', '{--ADMIN_ADMINS_LA_MODE_NEW--}'), getAdminMenuMode($adminId));
373
374         // Finish it
375         $OUT .= '</select>';
376
377         // Return content
378         return $OUT;
379 }
380
381 // Delete given admin accounts
382 function adminsDeleteAdminAccount ($postData) {
383         // Check if this account is the last one which cannot be deleted...
384         if (countSumTotalData('', 'admins', 'id', '', true) > 1) {
385                 // Delete accounts
386                 $OUT = '';
387                 foreach ($postData['sel'] as $id => $selected) {
388                         // Secure id number
389                         $id = bigintval($id);
390
391                         // Get the admin's data
392                         $result = SQL_QUERY_ESC("SELECT `login`,`email`,`default_acl` AS `mode`,`la_mode` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
393                                 array($id), __FUNCTION__, __LINE__);
394
395                         // Do we have an entry?
396                         if (SQL_NUMROWS($result) == 1) {
397                                 // Entry found, so load data
398                                 $content = SQL_FETCHARRAY($result);
399                                 $content['mode']    = '{--ADMIN_ADMINS_ACCESS_MODE_' . strtoupper($content['mode'])    . '--}';
400                                 $content['la_mode'] = '{--ADMIN_ADMINS_LA_MODE_' . strtoupper($content['la_mode']) . '--}';
401
402                                 // Prepare some more data
403                                 $content['id'] = $id;
404
405                                 // Load row template and switch color
406                                 $OUT .= loadTemplate('admin_delete_admins_row', true, $content);
407                         } // END - if
408
409                         // Free result
410                         SQL_FREERESULT($result);
411                 } // END - foreach
412
413                 // Load template
414                 loadTemplate('admin_delete_admins', false, $OUT);
415         } else {
416                 // Cannot delete last account!
417                 displayMessage('{--ADMIN_ADMINS_CANNOT_DELETE_LAST--}');
418         }
419 }
420
421 // Remove the given accounts
422 function adminsRemoveAdminAccount ($postData) {
423         // Begin removal
424         $cache_update = '0';
425         foreach ($postData['sel'] as $id => $del) {
426                 // Secure id number
427                 $id = bigintval($id);
428
429                 // Delete only when it's not your own account!
430                 if (($del == 1) && (getCurrentAdminId() != $id)) {
431                         // Rewrite his tasks to all admins
432                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_task_system` SET `assigned_admin`=NULL WHERE `assigned_admin`=%s",
433                                 array($id), __FUNCTION__, __LINE__);
434
435                         // Remove account
436                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
437                                 array($id), __FUNCTION__, __LINE__);
438                 }
439         }
440
441         // Remove cache if cache system is activated
442         runFilterChain('post_form_deleted', postRequestArray());
443 }
444
445 // List all admin accounts
446 function adminsListAdminAccounts() {
447         // Select all admin accounts
448         $result = SQL_QUERY('SELECT
449         `id`,
450         `login`,
451         `email`,
452         `default_acl` AS `mode`,
453         `la_mode`
454 FROM
455         `{?_MYSQL_PREFIX?}_admins`
456 ORDER BY
457         `login` ASC', __FUNCTION__, __LINE__);
458         $OUT = '';
459         while ($content = SQL_FETCHARRAY($result)) {
460                 // Compile some variables
461                 $content['mode']    = '{--ADMIN_ADMINS_ACCESS_MODE_' . strtoupper($content['mode'])    . '--}';
462                 $content['la_mode'] = '{--ADMIN_ADMINS_LA_MODE_' . strtoupper($content['la_mode']) . '--}';
463
464                 // Load row template and switch color
465                 $OUT .= loadTemplate('admin_list_admins_row', true, $content);
466         } // END - while
467
468         // Free memory
469         SQL_FREERESULT($result);
470
471         // Load template
472         loadTemplate('admin_list_admins', false, $OUT);
473 }
474
475 // Sends out mail to all administrators
476 // IMPORTANT: Please use sendAdminNotification() instead of calling this function directly
477 function sendAdminsEmails ($subj, $template, $content, $userid) {
478         // Trim template name
479         $template = trim($template);
480
481         // Load email template
482         $message = loadEmailTemplate($template, $content, $userid);
483
484         // Check which admin shall receive this mail
485         $result = SQL_QUERY_ESC("SELECT `admin_id` FROM `{?_MYSQL_PREFIX?}_admins_mails` WHERE `mail_template`='%s' ORDER BY `admin_id` ASC",
486                 array($template), __FUNCTION__, __LINE__);
487
488         // No entries found?
489         if (SQL_HASZERONUMS($result)) {
490                 // Create new entry (to all admins)
491                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_admins_mails` (`admin_id`,`mail_template`) VALUES (0, '%s')",
492                         array($template), __FUNCTION__, __LINE__);
493         } else {
494                 // Load admin ids...
495                 // @TODO This can be, somehow, rewritten
496                 $adminIds = array();
497                 while ($content = SQL_FETCHARRAY($result)) {
498                         $adminIds[] = $content['admin_id'];
499                 } // END - while
500
501                 // Free memory
502                 SQL_FREERESULT($result);
503
504                 // Init result
505                 $result = false;
506
507                 // "implode" ids and query string
508                 $adminId = implode(',', $adminIds);
509                 if ($adminId == '-1') {
510                         if (isExtensionActive('events')) {
511                                 // Add line to user events
512                                 EVENTS_ADD_LINE($subj, $message, $userid);
513                         } else {
514                                 // Log error for debug
515                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension 'events' missing: tpl=%s,subj=%s,userid=%s",
516                                         $template,
517                                         $subj,
518                                         $userid
519                                 ));
520                         }
521                 } elseif (($adminId == '0') || (empty($adminId))) {
522                         // Select all email adresses
523                         $result = SQL_QUERY('SELECT `email` FROM `{?_MYSQL_PREFIX?}_admins` ORDER BY `id` ASC',
524                                 __FUNCTION__, __LINE__);
525                 } else {
526                         // If Admin-Id is not "to-all" select
527                         $result = SQL_QUERY_ESC("SELECT `email` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id` IN (%s) ORDER BY `id` ASC",
528                                 array($adminId), __FUNCTION__, __LINE__);
529                 }
530         }
531
532         // Load email addresses and send away
533         while ($content = SQL_FETCHARRAY($result)) {
534                 sendEmail($content['email'], $subj, $message);
535         } // END - while
536
537         // Free memory
538         SQL_FREERESULT($result);
539 }
540
541 // "Getter" for current admin's expert settings
542 function getAminsExpertSettings () {
543         // Default is has not the right
544         $data['expert_settings'] = 'N';
545
546         // Get current admin Id
547         $adminId = getCurrentAdminId();
548
549         // Lookup settings in cache
550         if (isset($GLOBALS['cache_array']['admin']['expert_settings'][$adminId])) {
551                 // Use cache
552                 $data['expert_settings'] = $GLOBALS['cache_array']['admin']['expert_settings'][$adminId];
553
554                 // Update cache hits
555                 incrementStatsEntry('cache_hits');
556         } elseif (!isExtensionInstalled('cache')) {
557                 // Load from database
558                 $result = SQL_QUERY_ESC("SELECT `expert_settings` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
559                         array($adminId), __FUNCTION__, __LINE__);
560
561                 // Entry found?
562                 if (SQL_NUMROWS($result) == 1) {
563                         // Fetch data
564                         $data = SQL_FETCHARRAY($result);
565
566                         // Set cache
567                         $GLOBALS['cache_array']['admin']['expert_settings'][$adminId] = $data['expert_settings'];
568                 } // END - if
569
570                 // Free memory
571                 SQL_FREERESULT($result);
572         }
573
574         // Return the result
575         return $data['expert_settings'];
576 }
577
578 // "Getter" for current admin's expert warning (if he wants to see them or not
579 function getAminsExpertWarning () {
580         // Default is has not the right
581         $data['expert_warning'] = 'N';
582
583         // Get current admin id
584         $adminId = getCurrentAdminId();
585
586         // Lookup warning in cache
587         if (isset($GLOBALS['cache_array']['admin']['expert_warning'][$adminId])) {
588                 // Use cache
589                 $data['expert_warning'] = $GLOBALS['cache_array']['admin']['expert_warning'][$adminId];
590
591                 // Update cache hits
592                 incrementStatsEntry('cache_hits');
593         } elseif (!isExtensionInstalled('cache')) {
594                 // Load from database
595                 $result = SQL_QUERY_ESC("SELECT `expert_warning` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
596                         array($adminId), __FUNCTION__, __LINE__);
597
598                 // Entry found?
599                 if (SQL_NUMROWS($result) == 1) {
600                         // Fetch data
601                         $data = SQL_FETCHARRAY($result);
602
603                         // Set cache
604                         $GLOBALS['cache_array']['admin']['expert_warning'][$adminId] = $data['expert_warning'];
605                 } // END - if
606
607                 // Free memory
608                 SQL_FREERESULT($result);
609         }
610
611         // Return the result
612         return $data['expert_warning'];
613 }
614
615 // Get login_failures number from administrator's login name
616 function getAdminLoginFailures ($adminId) {
617         // Admin login should not be empty
618         if (empty($adminId)) {
619                 debug_report_bug(__FUNCTION__, __LINE__, 'adminId is empty.');
620         } // END - if
621
622         // By default no admin is found
623         $data['login_failures'] = -1;
624
625         // Check cache
626         if (isset($GLOBALS['cache_array']['admin']['login_failures'][$adminId])) {
627                 // Use it if found to save SQL queries
628                 $data['login_failures'] = $GLOBALS['cache_array']['admin']['login_failures'][$adminId];
629
630                 // Update cache hits
631                 incrementStatsEntry('cache_hits');
632         } elseif (!isExtensionActive('cache')) {
633                 // Load from database
634                 $result = SQL_QUERY_ESC("SELECT `login_failures` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
635                         array($adminId), __FUNCTION__, __LINE__);
636
637                 // Do we have an entry?
638                 if (SQL_NUMROWS($result) == 1) {
639                         // Get it
640                         $data = SQL_FETCHARRAY($result);
641                 } // END - if
642
643                 // Free result
644                 SQL_FREERESULT($result);
645         }
646
647         // Return the login_failures
648         return $data['login_failures'];
649 }
650
651 // Get last_failure number from administrator's login name
652 function getAdminLastFailure ($adminId) {
653         // Admin login should not be empty
654         if (empty($adminId)) {
655                 debug_report_bug(__FUNCTION__, __LINE__, 'adminId is empty.');
656         } // END - if
657
658         // By default no admin is found
659         $data['last_failure'] = -1;
660
661         // Check cache
662         if (isset($GLOBALS['cache_array']['admin']['last_failure'][$adminId])) {
663                 // Use it if found to save SQL queries
664                 $data['last_failure'] = $GLOBALS['cache_array']['admin']['last_failure'][$adminId];
665
666                 // Update cache hits
667                 incrementStatsEntry('cache_hits');
668         } elseif (!isExtensionActive('cache')) {
669                 // Load from database
670                 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`last_failure`) AS `last_failure` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
671                         array($adminId), __FUNCTION__, __LINE__);
672
673                 // Do we have an entry?
674                 if (SQL_NUMROWS($result) == 1) {
675                         // Get it
676                         $data = SQL_FETCHARRAY($result);
677                 } // END - if
678
679                 // Free result
680                 SQL_FREERESULT($result);
681         }
682
683         // Return the last_failure
684         return $data['last_failure'];
685 }
686
687 //-----------------------------------------------------------------------------
688 //                             Wrapper functions
689 //-----------------------------------------------------------------------------
690
691 // Wrapper function to check wether expert setting warning is enabled
692 function isAdminsExpertWarningEnabled () {
693         return (getAminsExpertWarning() == 'Y');
694 }
695
696 // [EOF]
697 ?>