550b8a69bc539399caf339908aafe5b8a0092070
[mailer.git] / inc / libs / admins_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Check ACL for menu combination
41 function ADMINS_CHECK_ACL($act, $wht) {
42         global $cacheArray, $_CONFIG, $cacheInstance;
43         // If action is login or logout allow allways!
44         $default = "allow";
45         if (($act == "login") || ($act == "logout")) return true;
46
47         // Default is deny
48         $ret = false;
49
50         // Get admin's defult access right
51         if (!empty($cacheArray['admins']['def_acl'][get_session('admin_login')])) {
52                 // Load from cache
53                 $default = $cacheArray['admins']['def_acl'][get_session('admin_login')];
54
55                 // Count cache hits
56                 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
57         } elseif (!is_object($cacheInstance)) {
58                 // Load from database
59                 $default = GET_ADMIN_DEFAULT_ACL(get_session('admin_login'));
60         }
61
62         // Get admin's ID
63         $aid = GET_ADMIN_ID(get_session('admin_login'));
64
65         if (!empty($wht)) {
66                 // Check for parent menu:
67                 // First get it's action value
68                 $parent_action = GET_ACTION("admin", $wht);
69
70                 // Check with this function...
71                 $parent = ADMINS_CHECK_ACL($parent_action, "");
72         } else {
73                 // Anything else is true!
74                 $parent = false;
75         }
76
77         // Shall I test for a main or sub menu? (action or what?)
78         $lines = 0; $acl_mode = "failed";
79         if (GET_EXT_VERSION("cache") >= "0.1.2") {
80                 // Load only from array when there are lines!
81                 if ((isset($cacheArray['admin_acls'])) && (is_array($cacheArray['admin_acls'])) && (count($cacheArray['admin_acls']) > 0)) {
82                         // Load ACL from array
83                         foreach ($cacheArray['admin_acls']['admin_id'] as $id => $aid_acls) {
84                                 if ($aid == $aid_acls) {
85                                         // Okay, one line was found!
86                                         if ((!empty($act)) && ($cacheArray['admin_acls']['action_menu'][$id] == $act)) {
87                                                 // Main menu line found
88                                                 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
89                                                 $lines = 1;
90                                         }
91                                          elseif ((!empty($wht)) && ($cacheArray['admin_acls']['what_menu'][$id] == $wht)) {
92                                                 // Check sub menu
93                                                 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
94                                                 $lines = 1;
95                                         }
96                                         if ($lines == 1) {
97                                                 // Count cache hits
98                                                 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
99                                                 break;
100                                         }
101                                 }
102                         }
103
104                         // No ACL found?
105                         if ($acl_mode == "failed") {
106                                 $acl_mode = "";
107                                 $lines = 0;
108                         }
109                 } else {
110                         // No lines here
111                         $lines = 0;
112                 }
113         } else {
114                 // Old version, so load it from database
115                 if (!empty($act))
116                 {
117                         // Main menu
118                         $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
119                          array(bigintval($aid), $act), __FILE__, __LINE__);
120                 } elseif (!empty($wht)) {
121                         // Sub menu
122                         $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
123                          array(bigintval($aid), $wht), __FILE__, __LINE__);
124                 }
125
126                 // Get number of lines
127                 $lines = SQL_NUMROWS($result);
128
129                 // Load ACL
130                 list($acl_mode) = SQL_FETCHROW($result);
131                 SQL_FREERESULT($result);
132         }
133
134         // Check ACL and (maybe) allow
135         if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
136
137         // Return value
138         return $ret;
139 }
140
141 // Create email link to admins's account
142 function ADMINS_CREATE_EMAIL_LINK($email, $mod="admin") {
143         $locked = " AND status='CONFIRMED'";
144         if (IS_ADMIN()) $locked = "";
145         if (strpos("@", $email) > 0) {
146                 // Create email link
147                 $result = SQL_QUERY_ESC("SELECT id
148 FROM "._MYSQL_PREFIX."_admins
149 WHERE email='%s'".$locked." LIMIT 1",
150  array($email), __FILE__, __LINE__);
151
152                 // Is there an entry?
153                 if (SQL_NUMROWS($result) == 1) {
154                         // Load userid
155                         list($uid) = SQL_FETCHROW($result);
156
157                         // Rewrite email address to contact link
158                         $email = URL."/modules.php?module=".$mod."&amp;what=user_contct&amp;u_id=".bigintval($uid);
159                 }
160
161                 // Free memory
162                 SQL_FREERESULT($result);
163         } elseif (bigintval($email) > 0) {
164                 // Direct ID given
165                 $email = URL."/modules.php?module=".$mod."&amp;what=admins_contct&amp;admin=".bigintval($email);
166         }
167
168         // Return rewritten (?) email address
169         return $email;
170 }
171
172 // Change a lot admin account
173 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
174         global $cacheInstance;
175
176         // Begin the update
177         $cache_update = 0;
178         foreach ($POST['login'] as $id => $login) {
179                 // Secure ID number
180                 $id = bigintval($id);
181
182                 // When both passwords match update admin account
183                 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
184                         // Save only when both passwords are the same (also when they are empty)
185                         $ADD = ""; $cache_update = "1";
186
187                         // Generate hash
188                         $hash = generateHash($POST['pass1'][$id]);
189
190                         // Save password when set
191                         if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
192
193                         // Get admin's ID
194                         $salt = substr(GET_ADMIN_HASH(get_session('admin_login')), 0, -40);
195                         $aid = GET_ADMIN_ID(get_session('admin_login'));
196
197                         // Rewrite cookie when it's own account
198                         if ($aid == $id) {
199                                 // Set timeout cookie
200                                 set_session("admin_last", time());
201
202                                 if ($login != get_session('admin_login')) {
203                                         // Update login cookie
204                                         set_session("admin_login", $login);
205
206                                         // Update password cookie as well?
207                                         if (!empty($ADD)) set_session("admin_md5", $hash);
208                                 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
209                                         // Update password cookie
210                                         set_session("admin_md5", $hash);
211                                 }
212                         } // END - if
213
214                         // Get default ACL from admin to check if we can allow him to change the default ACL
215                         $default = GET_ADMIN_DEFAULT_ACL(get_session('admin_login'));
216
217                         // Update admin account
218                         if ($default == "allow") {
219                                 // Allow changing default ACL
220                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
221 login='%s'".$ADD.",
222 email='%s',
223 default_acl='%s',
224 la_mode='%s'
225 WHERE id=%s LIMIT 1",
226  array(
227         $login,
228         $POST['email'][$id],
229         $POST['mode'][$id],
230         $POST['la_mode'][$id],
231         $id
232 ), __FILE__, __LINE__);
233                         } else {
234                                 // Do not allow it here
235                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
236 login='%s'".$ADD.",
237 email='%s',
238 la_mode='%s'
239 WHERE id=%s LIMIT 1",
240  array(
241         $login,
242         $POST['email'][$id],
243         $POST['la_mode'][$id],
244         $id
245 ), __FILE__, __LINE__);
246                         }
247
248                         // Purge cache
249                         CACHE_PURGE_ADMIN_MENU($id);
250
251                         // Admin account saved
252                         $MSG = ADMIN_ACCOUNT_SAVED;
253                 } else {
254                         // Passwords did not match
255                         $MSG = ADMINS_ERROR_PASS_MISMATCH;
256                 }
257
258                 // Display message
259                 if (!empty($MSG)) {
260                         LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"admin_done\">".$MSG."</SPAN>");
261                 }
262         }
263
264         // Remove cache file
265         if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
266                 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
267         }
268 }
269
270 // Make admin accounts editable
271 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
272         // Begin the edit loop
273         $SW = 2; $OUT = "";
274         foreach ($POST['sel'] as $id => $sel) {
275                 // Secure ID number
276                 $id = bigintval($id);
277
278                 // Get the admin's data
279                 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
280                  array($id), __FILE__, __LINE__);
281                 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
282                         // Entry found
283                         $content = SQL_FETCHARRAY($result);
284                         SQL_FREERESULT($result);
285
286                         // Prepare some more data for the template
287                         $content['sw'] = $SW;
288                         $content['id'] = $id;
289
290                         // Shall we allow changing default ACL?
291                         if ($content['mode'] == "allow") {
292                                 // Allow chaning it
293                                 $content['mode']    = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(ADMINS_ALLOW_MODE, ADMINS_DENY_MODE), $content['mode']);
294                         } else {
295                                 // Don't allow it
296                                 $content['mode'] = "&nbsp;";
297                         }
298                         $content['la_mode'] = ADD_OPTION_LINES("/ARRAY/", array("global", "OLD", "NEW"), array(ADMINS_GLOBAL_LA_SETTING, ADMINS_OLD_LA_SETTING, ADMINS_NEW_LA_SETTING), $content['la_mode']);
299
300                         // Load row template and switch color
301                         $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
302                         $SW = 3 - $SW;
303                 }
304         }
305         define('__ADMINS_ROWS', $OUT);
306
307         // Load template
308         LOAD_TEMPLATE("admin_edit_admins");
309 }
310
311 // Delete given admin accounts
312 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
313         // Check if this account is the last one which cannot be deleted...
314         $result_main = SQL_QUERY("SELECT id FROM "._MYSQL_PREFIX."_admins", __FILE__, __LINE__);
315         $accounts = SQL_NUMROWS($result_main);
316         SQL_FREERESULT($result_main);
317         if ($accounts > 1) {
318                 // Delete accounts
319                 $SW = 2; $OUT = "";
320                 foreach ($POST['sel'] as $id => $sel) {
321                         // Secure ID number
322                         $id = bigintval($id);
323
324                         // Get the admin's data
325                         $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
326                          array($id), __FILE__, __LINE__);
327                         if (SQL_NUMROWS($result) == 1) {
328                                 // Entry found
329                                 $content = SQL_FETCHARRAY($result);
330                                 SQL_FREERESULT($result);
331                                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
332                                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
333
334                                 // Prepare some more data
335                                 $content['sw'] = $SW;
336                                 $content['id'] = $id;
337
338                                 // Load row template and switch color
339                                 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
340                                 $SW = 3 - $SW;
341                         }
342                 }
343                 define('__ADMINS_ROWS', $OUT);
344
345                 // Load template
346                 LOAD_TEMPLATE("admin_del_admins");
347         } else {
348                 // Cannot delete last account!
349                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ADMINS_CANNOT_DELETE_LAST);
350         }
351 }
352
353 // Remove the given accounts
354 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
355         // Begin removal
356         $cache_update = 0;
357         foreach ($POST['sel'] as $id => $del) {
358                 // Secure ID number
359                 $id = bigintval($id);
360
361                 // Delete only when it's not your own account!
362                 if (($del == 1) && (GET_ADMIN_ID(get_session('admin_login')) != $id)) {
363                         // Rewrite his tasks to all admins
364                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin=0 WHERE assigned_admin=%s",
365                          array($id), __FILE__, __LINE__);
366
367                         // Remove account
368                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
369                          array($id), __FILE__, __LINE__);
370
371                         // Purge cache
372                         CACHE_PURGE_ADMIN_MENU($id);
373                         $cache_update = "1";
374                 }
375         }
376
377         // Remove cache if cache system is activated
378         if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
379                 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
380         }
381 }
382
383 // List all admin accounts
384 function ADMINS_LIST_ADMIN_ACCOUNTS() {
385         // Select all admin accounts
386         $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins ORDER BY login ASC", __FILE__, __LINE__);
387         $SW = 2; $OUT = "";
388         while ($content = SQL_FETCHARRAY($result)) {
389                 // Compile some variables
390                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
391                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
392
393                 // Prepare some more data
394                 $content['sw']         = $SW;
395                 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
396
397                 // Load row template and switch color
398                 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
399                 $SW = 3 - $SW;
400         }
401
402         // Free memory
403         SQL_FREERESULT($result);
404         define('__ADMINS_ROWS', $OUT);
405
406         // Load template
407         LOAD_TEMPLATE("admin_list_admins");
408 }
409
410 //
411 ?>