cb73fecea022525744d1f1f26211dd5dd1e4eb67
[mailer.git] / inc / modules / admin.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/31/2003 *
4  * ===============                              Last change: 07/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admin.php                                        *
8  * -------------------------------------------------------------------- *
9  * Short description : Administration module                            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Administrationsmodul                             *
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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Login is default
41 if ((empty($GLOBALS['action'])) && ($check == "admin_only")) {
42         // Redirect to right URL
43         LOAD_URL("modules.php?module=admin&amp;action=login");
44 }
45
46 // Load include file
47 require_once(PATH."inc/modules/admin/admin-inc.php");
48
49 // Fix "deleted" cookies in PHP4 (PHP5 does remove them, PHP4 sets them to deleted!)
50 FIX_DELETED_COOKIES(array('admin_login', 'admin_md5', 'admin_last', 'admin_to'));
51
52 if (!isBooleanConstantAndTrue('admin_registered')) {
53         // Admin is not registered so we have to inform the user
54         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
55         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
56                 // Hash the password with the old function because we are here in install mode
57                 $hashedPass = md5($_POST['pass']);
58
59                 // Kill maybe existing session variables
60                 set_session('admin_login'       , "");
61                 set_session('admin_md5'         , "");
62                 set_session('admin_last'        , "");
63                 set_session('admin_to'          , "");
64
65                 // Do registration
66                 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
67                 switch ($ret)
68                 {
69                 case "done":
70                         admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
71                         if (!_FATAL) {
72                                 // Registering is done
73                                 LOAD_URL(URL."/modules.php?module=admin&amp;action=login&register=done");
74                         } else {
75                                 $ret = ADMIN_CANNOT_COMPLETE;
76                         }
77                         break;
78
79                 case "failed":
80                         $ret = ADMIN_REGISTER_FAILED;
81                         break;
82
83                 case "already":
84                 default:
85                         if ($ret == "already") {
86                                 // Admin does already exists!
87                                 $ret = ADMIN_LOGIN_ALREADY_REG;
88                         } else {
89                                 // Any other kind
90                                 $ret = "done";
91                         }
92
93                         if (!isBooleanConstantAndTrue('admin_registered')) {
94                                 // Write to config that registration is done
95                                 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
96
97                                 // Load URL for login
98                                 $URL = URL."/modules.php?module=admin&amp;action=login";
99                                 LOAD_URL($URL);
100                         }
101                         break;
102                 }
103         }
104
105         // Whas that action okay?
106         if ($ret != "done") {
107                 // Fixes another "Notice"
108                 if (!empty($_POST['login'])) {
109                         define('__LOGIN_VALUE', $_POST['login']);
110                 } else {
111                         define('__LOGIN_VALUE', "");
112                 }
113
114                 // Yet-another "Notice" fix
115                 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
116                         // No login entered?
117                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
118
119                         // An error comes back from registration?
120                         if (!empty($ret)) $MSG1 = $ret;
121
122                         // No password entered?
123                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
124
125                         // Or password too short?
126                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
127
128                         // Output error messages
129                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
130                         define('__MSG_PASS',  LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
131
132                         // Reset variables
133                         $MSG1 = ""; $MSG2 = "";
134                 } else {
135                         // Reset values to nothing
136                         define('__MSG_LOGIN', "");
137                         define('__MSG_PASS' , "");
138                 }
139
140                 // Load register template
141                 LOAD_TEMPLATE("admin_reg_form");
142         }
143 } elseif ((!isSessionVariableSet('admin_login')) || (!isSessionVariableSet('admin_md5')) || (!isSessionVariableSet('admin_last')) || (!isSessionVariableSet('admin_to')) || ((get_session('admin_last') + bigintval(get_session('admin_to')) * 3600 * 24) < time())) {
144         // At leat one administrator account was created
145         if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
146                 // Timeout for last login, we have to logout first!
147                 $URL = URL."/modules.php?module=admin&amp;action=login&amp;logout=1";
148                 LOAD_URL($URL);
149         }
150         if (!empty($_GET['register'])) {
151                 // Registration of first admin is done
152                 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
153         }
154
155         // Check if the admin has submitted data or not
156         $ret = "";
157         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
158         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
159                 // All required data was entered so we check his account
160                 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
161                 switch ($ret)
162                 {
163                 case "done": // Admin and password are okay, so we log in now
164                         // Construct URL and redirect
165                         $URL = URL."/modules.php?module=admin&amp;";
166
167                         // Rewrite overview module
168                         if ($GLOBALS['what'] == "overview") {
169                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
170                         }
171
172                         // Add data to URL
173                         if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
174                          elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
175                          elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
176
177                         // Load URL
178                         LOAD_URL($URL);
179                         break;
180
181                 case "404": // Administrator login not found
182                         $_POST['ok'] = $ret;
183                         $ret = ADMIN_NOT_FOUND;
184                         break;
185
186                 case "pass": // Wrong password
187                         $_POST['ok'] = $ret;
188                         $ret = WRONG_PASS;
189                         break;
190                 }
191         }
192         if ($ret != "done") {
193                 if (!empty($_POST['login'])) {
194                         define('__LOGIN_VALUE', $_POST['login']);
195                 } else {
196                         define('__LOGIN_VALUE', "");
197                 }
198
199                 if (isset($_POST['ok'])) {
200                         // Set messages to zero
201
202                         $MSG1 = ""; $MSG2 = "";
203                         // No login entered?
204                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
205
206                         // An error comes back from login?
207                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
208
209                         // No password entered?
210                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
211
212                         // Or password too short?
213                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
214
215                         // An error comes back from login?
216                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
217
218                         // Load message template
219                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
220                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
221
222                         // Reset variables
223                         $MSG1 = ""; $MSG2 = "";
224                 } else {
225                         // Set constants to empty for hiding them
226                         define('__MSG_LOGIN', "");
227                         define('__MSG_PASS' , "");
228                 }
229
230                 // Load login form
231                 if (!empty($GLOBALS['what'])) {
232                         // Restore old what value
233                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
234                 } elseif (!empty($GLOBALS['action'])) {
235                         if ($GLOBALS['action'] != "logout") {
236                                 // Restore old action value
237                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
238                         } else {
239                                 // Set default values
240                                 $content = array('target' => "action", 'value' => "login");
241                         }
242                 } elseif (!empty($_GET['area'])) {
243                         // Restore old area value
244                         $content = array('target' => "area", 'value' => $_GET['area']);
245                 } else {
246                         // Set default values
247                         $content = array('target' => "action", 'value' => "login");
248                 }
249
250                 // Load login form template
251                 LOAD_TEMPLATE("admin_login_form", false, $content);
252         }
253 } elseif (isset($_GET['logout'])) {
254         // Only try to remove cookies
255         if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
256                 // Also remove array elements
257                 set_session('admin_login'       , "");
258                 set_session('admin_md5'         , "");
259                 set_session('admin_last'        , "");
260                 set_session('admin_to'          , "");
261
262                 // Destroy session
263                 @session_destroy();
264
265                 // Load logout template
266                 if (isset($_GET['sql_patches'])) {
267                         // Special logout redirect for sql_patchrs
268                         LOAD_TEMPLATE("admin_logout_sql_patches");
269                 } else {
270                         // Logged out normally
271                         LOAD_TEMPLATE("admin_logout");
272                 }
273         } else {
274                 // Something went wrong here...
275                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
276
277                 // Add fatal message
278                 ADD_FATAL(CANNOT_UNREG_SESS);
279         }
280 } else {
281         // Maybe an Admin want's to login?
282         $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE(get_session('admin_login')), SQL_ESCAPE(get_session('admin_md5')));
283         switch ($ret)
284         {
285         case "done":
286                 // Cookie-Data accepted
287                 if ((set_session("admin_md5", SQL_ESCAPE(get_session('admin_md5')))) && (set_session("admin_login", SQL_ESCAPE(get_session('admin_login')))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
288                         // Ok, Cookie-Update done
289                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
290                                 // Check if action GET variable was set
291                                 $act = SQL_ESCAPE($GLOBALS['action']);
292                                 if (!empty($GLOBALS['what'])) {
293                                         // Get action value by what-value
294                                         $act = GET_ACTION("admin", $GLOBALS['what']);
295                                 }
296
297                                 // Check for access control line of current menu entry
298                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
299                         } else {
300                                 // Extension not installed so it's always allowed to access everywhere!
301                                 define('__ACL_ALLOW', true);
302                         }
303
304                         // When type of admin menu is not set fallback to old menu system
305                         if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
306
307                         // Check for version and switch between old menu system and new "intelligent menu system"
308                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
309                                 // Default area is the entrance, of course
310                                 $area = "entrance";
311
312                                 // Check for similar URL variable
313                                 if (!empty($_GET['area'])) $area = $_GET['area'];
314
315                                 // Load "logical-area menu-system" file
316                                 require_once(PATH."inc/modules/admin/la_sys-inc.php");
317
318                                 // Create new-style menu system will "logical areas"
319                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
320                         } else {
321                                 // This little call constructs the whole default old and lacky menu system
322                                 // on left side
323                                 ADMIN_DO_ACTION($GLOBALS['what']);
324                         }
325                 } else {
326                         // Login failed (cookies enabled?)
327                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
328                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
329                 }
330                 break;
331
332         case "404": // Administrator login not found
333                 $_POST['ok'] = $ret;
334                 ADD_FATAL(ADMIN_NOT_FOUND);
335                 break;
336
337         case "pass": // Wrong password
338                 $_POST['ok'] = $ret;
339                 ADD_FATAL(WRONG_PASS);
340                 break;
341         }
342 }
343
344 if (isBooleanConstantAndTrue('admin_registered'))
345 {
346         // Check config.php and inc directory for right access rights
347         if (is_INCWritable("config"))     ADD_FATAL(FATAL_CONFIG_WRITABLE);
348         if (is_INCWritable("dummy"))      ADD_FATAL(FATAL_INC_WRITABLE);
349 }
350 //
351 ?>