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