62de1cab3763327df4434fd8e8dca67068d63e0d
[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://www.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 (strpos($email, '@') !== false) {
142                 // Create email link
143                 $result = SQL_QUERY_ESC("SELECT `id`
144 FROM
145         `{?_MYSQL_PREFIX?}_admins`
146 WHERE
147         `email`='%s'
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                         // Rewrite email address to contact link
157                         $email = '{%url=modules.php?module=' . $mod . '&amp;what=admins_contct&amp;admin=' . bigintval($adminId) . '%}';
158                 } // END - if
159
160                 // Free memory
161                 SQL_FREERESULT($result);
162         } elseif ((is_int($email)) && ($email > 0)) {
163                 // Direct id given
164                 $email = '{%url=modules.php?module=' . $mod . '&amp;what=admins_contct&amp;admin=' . bigintval($email) . '%}';
165         }
166
167         // Return rewritten (?) email address
168         return $email;
169 }
170
171 // Change a lot admin account
172 function adminsChangeAdminAccount ($postData, $element = '') {
173         // Begin the update
174         $cache_update = '0';
175         foreach ($postData['login'] as $id => $login) {
176                 // Secure id number
177                 $id = bigintval($id);
178
179                 // When both passwords match update admin account
180                 if ((!empty($element)) && (isset($postData[$element]))) {
181                         // Save this setting
182                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_admins` SET `%s`='%s' WHERE `id`=%s LIMIT 1",
183                                 array($element, $postData[$element][$id], $id), __FUNCTION__, __LINE__);
184
185                         // Admin account saved
186                         $message = '{--ADMIN_ACCOUNT_SAVED--}';
187                 } elseif ((isset($postData['pass1'])) && (isset($postData['pass2']))) {
188                         // Update only if both passwords match
189                         if (($postData['pass1'][$id] == $postData['pass2'][$id])) {
190                                 // Save only when both passwords are the same (also when they are empty)
191                                 $add = ''; $cache_update = 1;
192
193                                 // Generate hash
194                                 $hash = generateHash($postData['pass1'][$id]);
195
196                                 // Save password when set
197                                 if (!empty($postData['pass1'][$id])) $add = sprintf(", `password`='%s'", SQL_ESCAPE($hash));
198
199                                 // Get admin's id
200                                 $adminId = getCurrentAdminId();
201                                 $salt = substr(getAdminHash(getAdminLogin($adminId)), 0, -40);
202
203                                 // Rewrite cookie when it's own account
204                                 if ($adminId == $id) {
205                                         // Set timeout cookie
206                                         setAdminLast(time());
207
208                                         if ($adminId != getCurrentAdminId()) {
209                                                 // Update login cookie
210                                                 setAdminId($adminId);
211
212                                                 // Update password cookie as well?
213                                                 if (!empty($add)) {
214                                                         setAdminMd5($hash);
215                                                 } // END - if
216                                         } elseif (generateHash($postData['pass1'][$id], $salt) != getAdminMd5()) {
217                                                 // Update password cookie
218                                                 setAdminMd5($hash);
219                                         }
220                                 } // END - if
221
222                                 // Get default ACL from admin to check if we can allow him to change the default ACL
223                                 $default = getAdminDefaultAcl(getCurrentAdminId());
224
225                                 // Update admin account
226                                 if ($default == 'allow') {
227                                         // Allow changing default ACL
228                                         SQL_QUERY_ESC("UPDATE
229         `{?_MYSQL_PREFIX?}_admins`
230 SET
231         `login`='%s'" . $add . ",
232         `email`='%s',
233         `default_acl`='%s',
234         `la_mode`='%s'
235 WHERE
236         `id`=%s
237 LIMIT 1",
238                                         array(
239                                                 $login,
240                                                 $postData['email'][$id],
241                                                 $postData['mode'][$id],
242                                                 $postData['la_mode'][$id],
243                                                 $id
244                                         ), __FUNCTION__, __LINE__);
245                                 } else {
246                                         // Do not allow it here
247                                         SQL_QUERY_ESC("UPDATE
248         `{?_MYSQL_PREFIX?}_admins`
249 SET
250         `login`='%s'" . $add . ",
251         `email`='%s',
252         `la_mode`='%s'
253 WHERE
254         `id`=%s
255 LIMIT 1",
256                                         array(
257                                                 $login,
258                                                 $postData['email'][$id],
259                                                 $postData['la_mode'][$id],
260                                                 $id
261                                         ), __FUNCTION__, __LINE__);
262                                 }
263
264                                 // Admin account saved
265                                 $message = '{--ADMIN_ACCOUNT_SAVED--}';
266                         } else {
267                                 // Passwords did not match
268                                 $message = '{--ADMIN_ADMINS_ERROR_PASS_MISMATCH--}';
269                         }
270                 } else {
271                         // Update whole array
272                         $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_admins` SET ';
273                         foreach ($postData as $entry => $value) {
274                                 // Skip login/id entry
275                                 if (in_array($entry, array('login', 'id'))) continue;
276
277                                 // Do we have a non-string (e.g. number, NULL, NOW() or back-tick at the beginning?
278                                 if (is_null($value[$id])) {
279                                         // NULL detected
280                                         $SQL .= '`' . $entry . '`=NULL, ';
281                                 } elseif ((bigintval($value[$id], true, false) === $value[$id]) || ($value[$id] == 'NOW()') || (substr($value[$id], 0, 1) == '`'))  {
282                                         // No need for ticks (')
283                                         $SQL .= '`' . $entry . '`=' . $value[$id] . ', ';
284                                 } else {
285                                         // Strings need ticks (') around them
286                                         $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$id]) . "', ";
287                                 }
288                         } // END - foreach
289
290                         // Remove last 2 chars and finish query
291                         $SQL = substr($SQL, 0, -2) . ' WHERE `id`=%s LIMIT 1';
292
293                         // Run it
294                         SQL_QUERY_ESC($SQL, array(bigintval($id)), __FUNCTION__, __LINE__);
295                 }
296         } // END - foreach
297
298         // Display message
299         if (!empty($message)) {
300                 displayMessage($message);
301         } // END - if
302
303         // Remove cache file
304         runFilterChain('post_form_submited', postRequestArray());
305 }
306
307 // Make admin accounts editable
308 function adminsEditAdminAccount ($postData) {
309         // "Resolve" current's admin access mode
310         $currMode = getAdminDefaultAcl(getCurrentAdminId());
311
312         // Begin the edit loop
313         $OUT = '';
314         foreach ($postData['sel'] as $id => $selected) {
315                 // Secure id number
316                 $id = bigintval($id);
317
318                 // Get the admin's data
319                 $result = SQL_QUERY_ESC("SELECT `login`, `email`, `default_acl` AS mode, `la_mode` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
320                         array($id), __FUNCTION__, __LINE__);
321                 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
322                         // Entry found
323                         $content = SQL_FETCHARRAY($result);
324                         SQL_FREERESULT($result);
325
326                         // Prepare some more data for the template
327                         $content['id'] = $id;
328
329                         // Shall we allow changing default ACL?
330                         if ($currMode == 'allow') {
331                                 // Allow chaning it
332                                 $content['mode']    = generateOptionList('/ARRAY/', array('allow', 'deny'), array('{--ADMIN_ADMINS_ACCESS_MODE_ALLOW--}', '{--ADMIN_ADMINS_ACCESS_MODE_DENY--}'), $content['mode']);
333                         } else {
334                                 // Don't allow it
335                                 $content['mode'] = '&nbsp;';
336                         }
337                         $content['la_mode'] = generateOptionList('/ARRAY/', array('global', 'OLD', 'NEW'), array('{--ADMIN_ADMINS_LA_MODE_GLOBAL--}', '{--ADMIN_ADMINS_LA_MODE_OLD--}', '{--ADMIN_ADMINS_LA_MODE_NEW--}'), $content['la_mode']);
338
339                         // Load row template and switch color
340                         $OUT .= loadTemplate('admin_edit_admins_row', true, $content);
341                 } // END - if
342         } // END - foreach
343
344         // Load template
345         loadTemplate('admin_edit_admins', false, $OUT);
346 }
347
348 // Delete given admin accounts
349 function adminsDeleteAdminAccount ($postData) {
350         // Check if this account is the last one which cannot be deleted...
351         if (countSumTotalData('', 'admins', 'id', '', true) > 1) {
352                 // Delete accounts
353                 $OUT = '';
354                 foreach ($postData['sel'] as $id => $selected) {
355                         // Secure id number
356                         $id = bigintval($id);
357
358                         // Get the admin's data
359                         $result = SQL_QUERY_ESC("SELECT `login`, `email`, `default_acl` AS `mode`, `la_mode` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
360                                 array($id), __FUNCTION__, __LINE__);
361
362                         // Do we have an entry?
363                         if (SQL_NUMROWS($result) == 1) {
364                                 // Entry found, so load data
365                                 $content = SQL_FETCHARRAY($result);
366                                 $content['mode']    = '{--ADMIN_ADMINS_ACCESS_MODE_' . strtoupper($content['mode'])    . '--}';
367                                 $content['la_mode'] = '{--ADMIN_ADMINS_LA_MODE_' . strtoupper($content['la_mode']) . '--}';
368
369                                 // Prepare some more data
370                                 $content['id'] = $id;
371
372                                 // Load row template and switch color
373                                 $OUT .= loadTemplate('admin_delete_admins_row', true, $content);
374                         } // END - if
375
376                         // Free result
377                         SQL_FREERESULT($result);
378                 } // END - foreach
379
380                 // Load template
381                 loadTemplate('admin_delete_admins', false, $OUT);
382         } else {
383                 // Cannot delete last account!
384                 displayMessage('{--ADMIN_ADMINS_CANNOT_DELETE_LAST--}');
385         }
386 }
387
388 // Remove the given accounts
389 function adminsRemoveAdminAccount ($postData) {
390         // Begin removal
391         $cache_update = '0';
392         foreach ($postData['sel'] as $id => $del) {
393                 // Secure id number
394                 $id = bigintval($id);
395
396                 // Delete only when it's not your own account!
397                 if (($del == 1) && (getCurrentAdminId() != $id)) {
398                         // Rewrite his tasks to all admins
399                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_task_system` SET `assigned_admin`=NULL WHERE `assigned_admin`=%s",
400                                 array($id), __FUNCTION__, __LINE__);
401
402                         // Remove account
403                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
404                                 array($id), __FUNCTION__, __LINE__);
405                 }
406         }
407
408         // Remove cache if cache system is activated
409         runFilterChain('post_form_deleted', postRequestArray());
410 }
411
412 // List all admin accounts
413 function adminsListAdminAccounts() {
414         // Select all admin accounts
415         $result = SQL_QUERY('SELECT `id`, `login`, `email`, `default_acl` AS mode, `la_mode` FROM `{?_MYSQL_PREFIX?}_admins` ORDER BY `login` ASC', __FUNCTION__, __LINE__);
416         $OUT = '';
417         while ($content = SQL_FETCHARRAY($result)) {
418                 // Compile some variables
419                 $content['mode']    = '{--ADMIN_ADMINS_ACCESS_MODE_' . strtoupper($content['mode'])    . '--}';
420                 $content['la_mode'] = '{--ADMIN_ADMINS_LA_MODE_' . strtoupper($content['la_mode']) . '--}';
421
422                 // Load row template and switch color
423                 $OUT .= loadTemplate('admin_list_admins_row', true, $content);
424         } // END - while
425
426         // Free memory
427         SQL_FREERESULT($result);
428
429         // Load template
430         loadTemplate('admin_list_admins', false, $OUT);
431 }
432
433 // Sends out mail to all administrators
434 // IMPORTANT: Please use sendAdminNotification() instead of calling this function directly
435 function sendAdminsEmails ($subj, $template, $content, $userid) {
436         // Trim template name
437         $template = trim($template);
438
439         // Load email template
440         $message = loadEmailTemplate($template, $content, $userid);
441
442         // Check which admin shall receive this mail
443         $result = SQL_QUERY_ESC("SELECT `admin_id` FROM `{?_MYSQL_PREFIX?}_admins_mails` WHERE `mail_template`='%s' ORDER BY `admin_id` ASC",
444                 array($template), __FUNCTION__, __LINE__);
445         if (SQL_HASZERONUMS($result)) {
446                 // Create new entry (to all admins)
447                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_admins_mails` (`admin_id`, `mail_template`) VALUES (0, '%s')",
448                         array($template), __FUNCTION__, __LINE__);
449         } else {
450                 // Load admin ids...
451                 // @TODO This can be, somehow, rewritten
452                 $adminIds = array();
453                 while ($content = SQL_FETCHARRAY($result)) {
454                         $adminIds[] = $content['admin_id'];
455                 } // END - while
456
457                 // Free memory
458                 SQL_FREERESULT($result);
459
460                 // Init result
461                 $result = false;
462
463                 // "implode" ids and query string
464                 $adminId = implode(',', $adminIds);
465                 if ($adminId == '-1') {
466                         if (isExtensionActive('events')) {
467                                 // Add line to user events
468                                 EVENTS_ADD_LINE($subj, $message, $userid);
469                         } else {
470                                 // Log error for debug
471                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension 'events' missing: tpl=%s,subj=%s,userid=%s",
472                                         $template,
473                                         $subj,
474                                         $userid
475                                 ));
476                         }
477                 } elseif (($adminId == '0') || (empty($adminId))) {
478                         // Select all email adresses
479                         $result = SQL_QUERY('SELECT `email` FROM `{?_MYSQL_PREFIX?}_admins` ORDER BY `id` ASC',
480                                 __FUNCTION__, __LINE__);
481                 } else {
482                         // If Admin-Id is not "to-all" select
483                         $result = SQL_QUERY_ESC("SELECT `email` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id` IN (%s) ORDER BY `id` ASC",
484                                 array($adminId), __FUNCTION__, __LINE__);
485                 }
486         }
487
488         // Load email addresses and send away
489         while ($content = SQL_FETCHARRAY($result)) {
490                 sendEmail($content['email'], $subj, $message);
491         } // END - while
492
493         // Free memory
494         SQL_FREERESULT($result);
495 }
496
497 // "Getter" for current admin's expert settings
498 function getAminsExpertSettings () {
499         // Default is has not the right
500         $data['expert_settings'] = 'N';
501
502         // Get current admin Id
503         $adminId = getCurrentAdminId();
504
505         // Lookup settings in cache
506         if (isset($GLOBALS['cache_array']['admin']['expert_settings'][$adminId])) {
507                 // Use cache
508                 $data['expert_settings'] = $GLOBALS['cache_array']['admin']['expert_settings'][$adminId];
509
510                 // Update cache hits
511                 incrementStatsEntry('cache_hits');
512         } elseif (!isExtensionInstalled('cache')) {
513                 // Load from database
514                 $result = SQL_QUERY_ESC("SELECT `expert_settings` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
515                         array($adminId), __FUNCTION__, __LINE__);
516
517                 // Entry found?
518                 if (SQL_NUMROWS($result) == 1) {
519                         // Fetch data
520                         $data = SQL_FETCHARRAY($result);
521
522                         // Set cache
523                         $GLOBALS['cache_array']['admin']['expert_settings'][$adminId] = $data['expert_settings'];
524                 } // END - if
525
526                 // Free memory
527                 SQL_FREERESULT($result);
528         }
529
530         // Return the result
531         return $data['expert_settings'];
532 }
533
534 // "Getter" for current admin's expert warning (if he wants to see them or not
535 function getAminsExpertWarning () {
536         // Default is has not the right
537         $data['expert_warning'] = 'N';
538
539         // Get current admin id
540         $adminId = getCurrentAdminId();
541
542         // Lookup warning in cache
543         if (isset($GLOBALS['cache_array']['admin']['expert_warning'][$adminId])) {
544                 // Use cache
545                 $data['expert_warning'] = $GLOBALS['cache_array']['admin']['expert_warning'][$adminId];
546
547                 // Update cache hits
548                 incrementStatsEntry('cache_hits');
549         } elseif (!isExtensionInstalled('cache')) {
550                 // Load from database
551                 $result = SQL_QUERY_ESC("SELECT `expert_warning` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
552                         array($adminId), __FUNCTION__, __LINE__);
553
554                 // Entry found?
555                 if (SQL_NUMROWS($result) == 1) {
556                         // Fetch data
557                         $data = SQL_FETCHARRAY($result);
558
559                         // Set cache
560                         $GLOBALS['cache_array']['admin']['expert_warning'][$adminId] = $data['expert_warning'];
561                 } // END - if
562
563                 // Free memory
564                 SQL_FREERESULT($result);
565         }
566
567         // Return the result
568         return $data['expert_warning'];
569 }
570
571 // Get login_failures number from administrator's login name
572 function getAdminLoginFailures ($adminId) {
573         // Admin login should not be empty
574         if (empty($adminId)) {
575                 debug_report_bug(__FUNCTION__, __LINE__, 'adminId is empty.');
576         } // END - if
577
578         // By default no admin is found
579         $data['login_failures'] = -1;
580
581         // Check cache
582         if (isset($GLOBALS['cache_array']['admin']['login_failures'][$adminId])) {
583                 // Use it if found to save SQL queries
584                 $data['login_failures'] = $GLOBALS['cache_array']['admin']['login_failures'][$adminId];
585
586                 // Update cache hits
587                 incrementStatsEntry('cache_hits');
588         } elseif (!isExtensionActive('cache')) {
589                 // Load from database
590                 $result = SQL_QUERY_ESC("SELECT `login_failures` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
591                         array($adminId), __FUNCTION__, __LINE__);
592
593                 // Do we have an entry?
594                 if (SQL_NUMROWS($result) == 1) {
595                         // Get it
596                         $data = SQL_FETCHARRAY($result);
597                 } // END - if
598
599                 // Free result
600                 SQL_FREERESULT($result);
601         }
602
603         // Return the login_failures
604         return $data['login_failures'];
605 }
606
607 // Get last_failure number from administrator's login name
608 function getAdminLastFailure ($adminId) {
609         // Admin login should not be empty
610         if (empty($adminId)) {
611                 debug_report_bug(__FUNCTION__, __LINE__, 'adminId is empty.');
612         } // END - if
613
614         // By default no admin is found
615         $data['last_failure'] = -1;
616
617         // Check cache
618         if (isset($GLOBALS['cache_array']['admin']['last_failure'][$adminId])) {
619                 // Use it if found to save SQL queries
620                 $data['last_failure'] = $GLOBALS['cache_array']['admin']['last_failure'][$adminId];
621
622                 // Update cache hits
623                 incrementStatsEntry('cache_hits');
624         } elseif (!isExtensionActive('cache')) {
625                 // Load from database
626                 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`last_failure`) AS `last_failure` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `id`=%s LIMIT 1",
627                         array($adminId), __FUNCTION__, __LINE__);
628
629                 // Do we have an entry?
630                 if (SQL_NUMROWS($result) == 1) {
631                         // Get it
632                         $data = SQL_FETCHARRAY($result);
633                 } // END - if
634
635                 // Free result
636                 SQL_FREERESULT($result);
637         }
638
639         // Return the last_failure
640         return $data['last_failure'];
641 }
642
643 //-----------------------------------------------------------------------------
644 //                             Wrapper functions
645 //-----------------------------------------------------------------------------
646
647 // Wrapper function to check wether expert setting warning is enabled
648 function isAdminsExpertWarningEnabled () {
649         return (getAminsExpertWarning() == 'Y');
650 }
651
652 // [EOF]
653 ?>