2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
15 function sessionStart($name, $limit = 0, $path = '/', $domain = null, $secure = null)
17 // Set the cookie name
21 $https = isset($secure) ? $secure : (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
23 // Set session cookie options
24 if (version_compare(PHP_VERSION, '5.2', '<')) // avoid failure on older php versions
25 session_set_cookie_params($limit, $path, $domain, $https);
27 session_set_cookie_params($limit, $path, $domain, $https, true);
31 // Make sure the session hasn't expired, and destroy it if it has
32 if ($this->validateSession())
34 // Check to see if the session is new or a hijacking attempt
35 if(!$this->preventHijacking())
37 // Reset session data and regenerate id
39 $_SESSION['IPaddress'] = $_SERVER['REMOTE_ADDR'];
40 $_SESSION['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
41 $this->regenerateSession();
43 // Give a 5% chance of the session id changing on any request
45 elseif (rand(1, 100) <= 5)
47 $this->regenerateSession();
58 function preventHijacking()
60 if (!isset($_SESSION['IPaddress']) || !isset($_SESSION['userAgent']))
63 if ($_SESSION['IPaddress'] != $_SERVER['REMOTE_ADDR'])
66 if ( $_SESSION['userAgent'] != $_SERVER['HTTP_USER_AGENT'])
72 function regenerateSession()
74 // If this session is obsolete it means there already is a new id
75 if (isset($_SESSION['OBSOLETE']) && ($_SESSION['OBSOLETE'] == true))
78 // Set current session to expire in 10 seconds
79 $_SESSION['OBSOLETE'] = true;
80 $_SESSION['EXPIRES'] = time() + 10;
82 // Create new session without destroying the old one
83 session_regenerate_id();
84 // Grab current session ID and close both sessions to allow other scripts to use them
85 $newSession = session_id();
86 session_write_close();
87 // Set session ID to the new one, and start it back up again
89 session_id($newSession);
92 // Now we unset the obsolete and expiration values for the session we want to keep
93 unset($_SESSION['OBSOLETE']);
94 unset($_SESSION['EXPIRES']);
97 function validateSession()
99 if (isset($_SESSION['OBSOLETE']) && !isset($_SESSION['EXPIRES']) )
102 if (isset($_SESSION['EXPIRES']) && $_SESSION['EXPIRES'] < time())
109 function output_html($text)
111 global $before_box, $Ajax, $messages;
112 // Fatal errors are not send to error_handler,
113 // so we must check the output
114 if ($text && preg_match('/\bFatal error(<.*?>)?:(.*)/i', $text, $m)) {
115 $Ajax->aCommands = array(); // Don't update page via ajax on errors
116 $text = preg_replace('/\bFatal error(<.*?>)?:(.*)/i','', $text);
117 $messages[] = array(E_ERROR, $m[2], null, null);
120 return in_ajax() ? fmt_errors() : ($before_box.fmt_errors().$text);
122 //----------------------------------------------------------------------------------------
124 function kill_login()
129 //----------------------------------------------------------------------------------------
131 function login_fail()
133 global $path_to_root;
135 header("HTTP/1.1 401 Authorization Required");
136 echo "<center><br><br><font size='5' color='red'><b>" . _("Incorrect Password") . "<b></font><br><br>";
137 echo "<b>" . _("The user and password combination is not valid for the system.") . "<b><br><br>";
139 echo _("If you are not an authorized user, please contact your system administrator to obtain an account to enable you to use the system.");
140 echo "<br><a href='$path_to_root/index.php'>" . _("Try again") . "</a>";
147 function password_reset_fail()
149 global $path_to_root;
151 echo "<center><br><br><font size='5' color='red'><b>" . _("Incorrect Email") . "<b></font><br><br>";
152 echo "<b>" . _("The email address does not exist in the system.") . "<b><br><br>";
154 echo _("If you are not an authorized user, please contact your system administrator to obtain an account to enable you to use the system.");
155 echo "<br><a href='$path_to_root/index.php?reset=1'>" . _("Try again") . "</a>";
162 function password_reset_success()
164 global $path_to_root;
166 echo "<center><br><br><font size='5' color='green'><b>" . _("New password sent") . "<b></font><br><br>";
167 echo "<b>" . _("A new password has been sent to your mailbox.") . "<b><br><br>";
169 echo "<br><a href='$path_to_root/index.php'>" . _("Login here") . "</a>";
176 function check_faillog()
178 global $login_delay, $login_faillog, $login_max_attempts;
180 $user = $_SESSION["wa_current_user"]->user;
182 if (@$login_delay && (@$login_faillog[$user][$_SERVER['REMOTE_ADDR']] >= @$login_max_attempts) && (time() < $login_faillog[$user]['last'] + $login_delay))
188 Simple brute force attack detection is performed before connection to company database is open. Therefore access counters have to be stored in file.
189 Login attempts counter is created for every new user IP, which partialy prevent DOS attacks.
191 function write_login_filelog($login, $result)
193 global $login_faillog, $login_max_attempts, $path_to_root;
195 $user = $_SESSION["wa_current_user"]->user;
197 $ip = $_SERVER['REMOTE_ADDR'];
199 if (!isset($login_faillog[$user][$ip]) || $result) // init or reset on successfull login
200 $login_faillog[$user] = array($ip => 0, 'last' => '');
204 if ($login_faillog[$user][$ip] < @$login_max_attempts) {
206 $login_faillog[$user][$ip]++;
208 $login_faillog[$user][$ip] = 0; // comment out to restart counter only after successfull login.
209 error_log(sprintf(_("Brute force attack on account '%s' detected. Access for non-logged users temporarily blocked." ), $login));
211 $login_faillog[$user]['last'] = time();
216 $msg .= "Login attempts info.\n";
218 $msg .= "\$login_faillog = " .var_export($login_faillog, true). ";\n";
220 $filename = $path_to_root."/tmp/faillog.php";
222 if ((!file_exists($filename) && is_writable($path_to_root)) || is_writable($filename))
224 file_put_contents($filename, $msg);
228 //----------------------------------------------------------------------------------------
230 function check_page_security($page_security)
236 if (!$_SESSION["wa_current_user"]->check_user_access())
238 // notification after upgrade from pre-2.2 version
239 $msg = $_SESSION["wa_current_user"]->old_db ?
240 _("Security settings have not been defined for your user account.")
241 . "<br>" . _("Please contact your system administrator.")
242 : _("Please remove \$security_groups and \$security_headings arrays from config.php file!");
243 } elseif (!$_SESSION['SysPrefs']->db_ok && !$_SESSION["wa_current_user"]->can_access('SA_SOFTWAREUPGRADE')) {
244 $msg = _('Access to application has been blocked until database upgrade is completed by system administrator.');
249 end_page(@$_REQUEST['popup']);
254 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
257 echo "<center><br><br><br><b>";
258 echo _("The security settings on your account do not permit you to access this function");
260 echo "<br><br><br><br></center>";
261 end_page(@$_REQUEST['popup']);
264 if (!$_SESSION['SysPrefs']->db_ok
265 && !in_array($page_security, array('SA_SOFTWAREUPGRADE', 'SA_OPEN', 'SA_BACKUP')))
267 display_error(_('System is blocked after source upgrade until database is updated on System/Software Upgrade page'));
274 Helper function for setting page security level depeding on
275 GET start variable and/or some value stored in session variable.
276 Before the call $page_security should be set to default page_security value.
278 function set_page_security($value=null, $trans = array(), $gtrans = array())
280 global $page_security;
282 // first check is this is not start page call
283 foreach($gtrans as $key => $area)
284 if (isset($_GET[$key])) {
285 $page_security = $area;
289 // then check session value
290 if (isset($trans[$value])) {
291 $page_security = $trans[$value];
296 //-----------------------------------------------------------------------------
297 // Removing magic quotes from nested arrays/variables
299 function strip_quotes($data)
301 if(get_magic_quotes_gpc()) {
302 if(is_array($data)) {
303 foreach($data as $k => $v) {
304 $data[$k] = strip_quotes($data[$k]);
307 return stripslashes($data);
312 function html_cleanup(&$parms)
314 foreach($parms as $name => $value) {
315 // $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
316 if (is_array($value))
317 html_cleanup($parms[$name]);
319 $parms[$name] = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
321 reset($parms); // needed for direct key() usage later throughout the sources
324 //============================================================================
327 function login_timeout()
329 // skip timeout on logout page
330 if ($_SESSION["wa_current_user"]->logged) {
331 $tout = $_SESSION["wa_current_user"]->timeout;
332 if ($tout && (time() > $_SESSION["wa_current_user"]->last_act + $tout))
334 $_SESSION["wa_current_user"]->logged = false;
336 $_SESSION["wa_current_user"]->last_act = time();
339 //============================================================================
340 if (!isset($path_to_root))
345 //----------------------------------------------------------------------------------------
346 // set to reasonable values if not set in config file (pre-2.3.12 installations)
348 if ((!isset($login_delay)) || ($login_delay < 0))
351 if ((!isset($login_max_attempts)) || ($login_max_attempts < 0))
352 $login_max_attempts = 3;
355 // Prevent register_globals vulnerability
356 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
357 die("Restricted access");
359 include_once($path_to_root . "/includes/errors.inc");
360 // colect all error msgs
361 set_error_handler('error_handler' /*, errtypes */);
363 include_once($path_to_root . "/includes/current_user.inc");
364 include_once($path_to_root . "/frontaccounting.php");
365 include_once($path_to_root . "/admin/db/security_db.inc");
366 include_once($path_to_root . "/includes/lang/language.php");
367 include_once($path_to_root . "/config_db.php");
368 include_once($path_to_root . "/includes/ajax.inc");
369 include_once($path_to_root . "/includes/ui/ui_msgs.inc");
370 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
372 include_once($path_to_root . "/includes/hooks.inc");
374 // include all extensions hook files.
376 foreach ($installed_extensions as $ext)
378 if (file_exists($path_to_root.'/'.$ext['path'].'/hooks.php'))
379 include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
383 Uncomment the setting below when using FA on shared hosting
384 to avoid unexpeced session timeouts.
385 Make sure this directory exists and is writable!
387 // ini_set('session.save_path', dirname(__FILE__).'/../tmp/');
389 ini_set('session.gc_maxlifetime', 36000); // 10hrs
391 hook_session_start(@$_POST["company_login_name"]);
393 $Session_manager = new SessionManager();
394 $Session_manager->sessionStart('FA'.md5(dirname(__FILE__)));
396 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
397 header("Cache-control: private");
399 include_once($path_to_root . "/config.php");
402 if ($login_delay > 0)
403 @include_once($path_to_root . "/tmp/faillog.php");
405 // Page Initialisation
406 if (!isset($_SESSION['wa_current_user']) || !$_SESSION['wa_current_user']->logged_in()
407 || !isset($_SESSION['language']) || !method_exists($_SESSION['language'], 'set_language'))
409 $l = array_search_value($dflt_lang, $installed_languages, 'code');
410 $_SESSION['language'] = new language($l['name'], $l['code'], $l['encoding'],
411 (isset($l['rtl']) && $l['rtl'] === true) ? 'rtl' : 'ltr');
414 $_SESSION['language']->set_language($_SESSION['language']->code);
417 include_once($path_to_root . "/includes/access_levels.inc");
418 include_once($path_to_root . "/version.php");
419 include_once($path_to_root . "/includes/main.inc");
420 include_once($path_to_root . "/includes/app_entries.inc");
422 // Ajax communication object
425 // js/php validation rules container
427 // bindings for editors
429 // page help. Currently help for function keys.
432 $Refs = new references();
434 // intercept all output to destroy it in case of ajax call
435 register_shutdown_function('end_flush');
436 ob_start('output_html',0);
438 if (!isset($_SESSION["wa_current_user"]))
439 $_SESSION["wa_current_user"] = new current_user();
442 html_cleanup($_POST);
443 html_cleanup($_REQUEST);
444 html_cleanup($_SERVER);
446 // logout.php is the only page we should have always
447 // accessable regardless of access level and current login status.
448 if (!defined('FA_LOGOUT_PHP_FILE')){
452 if (!$_SESSION["wa_current_user"]->old_db)
453 include_once($path_to_root . '/company/'.user_company().'/installed_extensions.php');
457 if (!$_SESSION["wa_current_user"]->logged_in())
459 if (@$allow_password_reset && !$allow_demo_mode
460 && (isset($_GET['reset']) || isset($_POST['email_entry_field']))) {
461 if (!isset($_POST["email_entry_field"])) {
462 include($path_to_root . "/access/password_reset.php");
466 if (isset($_POST["company_login_nickname"]) && !isset($_POST["company_login_name"])) {
467 for ($i = 0; $i < count($db_connections); $i++) {
468 if ($db_connections[$i]["name"] == $_POST["company_login_nickname"]) {
469 $_POST["company_login_name"] = $i;
470 unset($_POST["company_login_nickname"]);
471 break 1; // cannot pass variables to break from PHP v5.4 onwards
475 $_succeed = isset($db_connections[$_POST["company_login_name"]]) &&
476 $_SESSION["wa_current_user"]->reset_password($_POST["company_login_name"],
477 $_POST["email_entry_field"]);
480 password_reset_success();
483 password_reset_fail();
487 if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
489 // strip ajax marker from uri, to force synchronous page reload
490 $_SESSION['timeout'] = array( 'uri'=>preg_replace('/JsHttpRequest=(?:(\d+)-)?([^&]+)/s',
491 '', @htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, $_SESSION['language']->encoding)),
494 include($path_to_root . "/access/login.php");
496 $Ajax->activate('_page_body');
499 if (isset($_POST["company_login_nickname"]) && !isset($_POST["company_login_name"])) {
500 for ($i = 0; $i < count($db_connections); $i++) {
501 if ($db_connections[$i]["name"] == $_POST["company_login_nickname"]) {
502 $_POST["company_login_name"] = $i;
503 unset($_POST["company_login_nickname"]);
504 break 1; // cannot pass variables to break from PHP v5.4 onwards
508 $succeed = isset($db_connections[$_POST["company_login_name"]]) &&
509 $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
510 $_POST["user_name_entry_field"], $_POST["password"]);
511 // select full vs fallback ui mode on login
512 $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
515 // Incorrect password
518 elseif(isset($_SESSION['timeout']) && !$_SESSION['timeout']['post'])
520 // in case of GET request redirect to avoid confirmation dialog
521 // after return from menu option
522 header("HTTP/1.1 303 See Other");
523 header("Location: ".$_SESSION['timeout']['uri']);
526 $lang = &$_SESSION['language'];
527 $lang->set_language($_SESSION['language']->code);
530 { set_global_connection();
532 db_set_encoding($_SESSION['language']->encoding);
535 if (!isset($_SESSION["App"])) {
536 $_SESSION["App"] = new front_accounting();
537 $_SESSION["App"]->init();
541 $SysPrefs = &$_SESSION['SysPrefs'];
543 // POST vars cleanup needed for direct reuse.
544 // We quote all values later with db_escape() before db update.
545 $_POST = strip_quotes($_POST);