+$Refs = new references();
+
+// intercept all output to destroy it in case of ajax call
+register_shutdown_function('end_flush');
+ob_start('output_html',0);
+
+if (!isset($_SESSION["wa_current_user"]))
+ $_SESSION["wa_current_user"] = new current_user();
+
+html_cleanup($_GET);
+html_cleanup($_POST);
+html_cleanup($_REQUEST);
+html_cleanup($_SERVER);
+
+// logout.php is the only page we should have always
+// accessable regardless of access level and current login status.
+if (strstr($_SERVER['PHP_SELF'], 'logout.php') == false){
+
+ login_timeout();
+
+ if (!$_SESSION["wa_current_user"]->old_db)
+ include_once($path_to_root . '/company/'.user_company().'/installed_extensions.php');
+
+ install_hooks();
+
+ if (!$_SESSION["wa_current_user"]->logged_in())
+ {
+ // Show login screen
+ if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
+ {
+ // strip ajax marker from uri, to force synchronous page reload
+ $_SESSION['timeout'] = array( 'uri'=>preg_replace('/JsHttpRequest=(?:(\d+)-)?([^&]+)/s',
+ '', @htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, $_SESSION['language']->encoding)),
+ 'post' => $_POST);
+
+ include($path_to_root . "/access/login.php");
+ if (in_ajax())
+ $Ajax->activate('_page_body');
+ exit;
+ } else {
+
+ $succeed = isset($db_connections[$_POST["company_login_name"]]) &&
+ $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
+ $_POST["user_name_entry_field"], $_POST["password"]);
+ // select full vs fallback ui mode on login
+ $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
+ if (!$succeed)
+ {
+ // Incorrect password
+ login_fail();
+ }
+ $lang = &$_SESSION['language'];
+ $lang->set_language($_SESSION['language']->code);
+ }
+ } else
+ set_global_connection();
+
+ if (!isset($_SESSION["App"])) {
+ $_SESSION["App"] = new front_accounting();
+ $_SESSION["App"]->init();
+ }
+}