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