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