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