Previous admin session data will be destroyed while first admin registration process
[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                         // Try to register the session variables
165                         if ((set_session("admin_md5", generatePassString(generateHash($_POST['pass'], __SALT)))) && (set_session("admin_login", $_POST['login'])) && (set_session("admin_last", time())) && (set_session("admin_to", $_POST['timeout']))) {
166                                 // Construct URL and redirect
167                                 $URL = URL."/modules.php?module=admin&amp;";
168
169                                 // Rewrite overview module
170                                 if ($GLOBALS['what'] == "overview") {
171                                         $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
172                                 }
173
174                                 // Add data to URL
175                                 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
176                                  elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
177                                  elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
178
179                                 // Load URL
180                                 LOAD_URL($URL);
181                         } else {
182                                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
183                                 ADD_FATAL(CANNOT_REGISTER_SESS);
184                         }
185                         break;
186
187                 case "404": // Administrator login not found
188                         $_POST['ok'] = $ret;
189                         $ret = ADMIN_NOT_FOUND;
190                         break;
191
192                 case "pass": // Wrong password
193                         $_POST['ok'] = $ret;
194                         $ret = WRONG_PASS;
195                         break;
196                 }
197         }
198         if ($ret != "done") {
199                 if (!empty($_POST['login'])) {
200                         define('__LOGIN_VALUE', $_POST['login']);
201                 } else {
202                         define('__LOGIN_VALUE', "");
203                 }
204
205                 if (isset($_POST['ok'])) {
206                         // Set messages to zero
207
208                         $MSG1 = ""; $MSG2 = "";
209                         // No login entered?
210                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
211
212                         // An error comes back from login?
213                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
214
215                         // No password entered?
216                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
217
218                         // Or password too short?
219                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
220
221                         // An error comes back from login?
222                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
223
224                         // Load message template
225                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
226                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
227
228                         // Reset variables
229                         $MSG1 = ""; $MSG2 = "";
230                 } else {
231                         // Set constants to empty for hiding them
232                         define('__MSG_LOGIN', "");
233                         define('__MSG_PASS' , "");
234                 }
235
236                 // Load login form
237                 if (!empty($GLOBALS['what'])) {
238                         // Restore old what value
239                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
240                 } elseif (!empty($GLOBALS['action'])) {
241                         if ($GLOBALS['action'] != "logout") {
242                                 // Restore old action value
243                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
244                         } else {
245                                 // Set default values
246                                 $content = array('target' => "action", 'value' => "login");
247                         }
248                 } elseif (!empty($_GET['area'])) {
249                         // Restore old area value
250                         $content = array('target' => "area", 'value' => $_GET['area']);
251                 } else {
252                         // Set default values
253                         $content = array('target' => "action", 'value' => "login");
254                 }
255
256                 // Load login form template
257                 LOAD_TEMPLATE("admin_login_form", false, $content);
258         }
259 } elseif (isset($_GET['logout'])) {
260         // Only try to remove cookies
261         if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
262                 // Also remove array elements
263                 set_session('admin_login'       , "");
264                 set_session('admin_md5'         , "");
265                 set_session('admin_last'        , "");
266                 set_session('admin_to'          , "");
267
268                 // Destroy session
269                 @session_destroy();
270
271                 // Load logout template
272                 if (isset($_GET['sql_patches'])) {
273                         // Special logout redirect for sql_patchrs
274                         LOAD_TEMPLATE("admin_logout_sql_patches");
275                 } else {
276                         // Logged out normally
277                         LOAD_TEMPLATE("admin_logout");
278                 }
279         } else {
280                 // Something went wrong here...
281                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
282
283                 // Add fatal message
284                 ADD_FATAL(CANNOT_UNREG_SESS);
285         }
286 } else {
287         // Maybe an Admin want's to login?
288         $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE(get_session('admin_login')), SQL_ESCAPE(get_session('admin_md5')));
289         switch ($ret)
290         {
291         case "done":
292                 // Cookie-Data accepted
293                 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'))))) {
294                         // Ok, Cookie-Update done
295                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
296                                 // Check if action GET variable was set
297                                 $act = SQL_ESCAPE($GLOBALS['action']);
298                                 if (!empty($GLOBALS['what'])) {
299                                         // Get action value by what-value
300                                         $act = GET_ACTION("admin", $GLOBALS['what']);
301                                 }
302
303                                 // Check for access control line of current menu entry
304                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
305                         } else {
306                                 // Extension not installed so it's always allowed to access everywhere!
307                                 define('__ACL_ALLOW', true);
308                         }
309
310                         // When type of admin menu is not set fallback to old menu system
311                         if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
312
313                         // Check for version and switch between old menu system and new "intelligent menu system"
314                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
315                                 // Default area is the entrance, of course
316                                 $area = "entrance";
317
318                                 // Check for similar URL variable
319                                 if (!empty($_GET['area'])) $area = $_GET['area'];
320
321                                 // Load "logical-area menu-system" file
322                                 require_once(PATH."inc/modules/admin/la_sys-inc.php");
323
324                                 // Create new-style menu system will "logical areas"
325                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
326                         } else {
327                                 // This little call constructs the whole default old and lacky menu system
328                                 // on left side
329                                 ADMIN_DO_ACTION($GLOBALS['what']);
330                         }
331                 } else {
332                         // Login failed (cookies enabled?)
333                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
334                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
335                 }
336                 break;
337
338         case "404": // Administrator login not found
339                 $_POST['ok'] = $ret;
340                 ADD_FATAL(ADMIN_NOT_FOUND);
341                 break;
342
343         case "pass": // Wrong password
344                 $_POST['ok'] = $ret;
345                 ADD_FATAL(WRONG_PASS);
346                 break;
347         }
348 }
349
350 if (isBooleanConstantAndTrue('admin_registered'))
351 {
352         // Check config.php and inc directory for right access rights
353         if (is_INCWritable("config"))     ADD_FATAL(FATAL_CONFIG_WRITABLE);
354         if (is_INCWritable("dummy"))      ADD_FATAL(FATAL_INC_WRITABLE);
355 }
356 //
357 ?>