e8a83a6dad9f6d67d6b313e4aa40a23194336482
[fa-stable.git] / includes / session.inc
1 <?php
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 ***********************************************************************/
12 function output_html($text)
13 {
14         global $before_box, $Ajax, $messages;
15         // Fatal errors are not send to error_handler,
16         // so we must check the output
17         if ($text && preg_match('/\bFatal error(<.*?>)?:(.*)/i', $text, $m)) {
18                 $Ajax->aCommands = array();  // Don't update page via ajax on errors
19                 $text = preg_replace('/\bFatal error(<.*?>)?:(.*)/i','', $text);
20                 $messages[] = array(E_ERROR, $m[2], null, null);
21         }
22         $Ajax->run();
23         return  in_ajax() ? fmt_errors() : ($before_box.fmt_errors().$text);
24 }
25 //----------------------------------------------------------------------------------------
26
27 function kill_login()
28 {
29         session_unset();
30         session_destroy();
31 }
32 //----------------------------------------------------------------------------------------
33
34 function login_fail()
35 {
36         header("HTTP/1.1 401 Authorization Required");
37         echo "<center><br><br><font size='5' color='red'><b>" . _("Incorrect Password") . "<b></font><br><br>";
38         echo "<b>" . _("The user and password combination is not valid for the system.") . "<b><br><br>";
39
40         echo _("If you are not an authorized user, please contact your system administrator to obtain an account to enable you to use the system.");
41         echo "<br><a href='javascript:history.go(-1)'>" . _("Back") . "</a>";
42         echo "</center>";
43
44         kill_login();
45         die();
46 }
47
48 //-----------------------------------------------------------------------------
49 //      Removing magic quotes from nested arrays/variables
50 //
51 function strip_quotes($data)
52 {
53         if(get_magic_quotes_gpc()) {
54                 if(is_array($data)) {
55                         foreach($data as $k => $v) {
56                                 $data[$k] = strip_quotes($data[$k]);
57                         }
58                 } else
59                         return stripslashes($data);
60         }
61         return $data;
62 }
63
64 //============================================================================
65 if (!isset($path_to_root))
66 {
67         $path_to_root = ".";
68 }
69
70 // Prevent register_globals vulnerability
71 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
72         die("Restricted access");
73
74 include_once($path_to_root . "/frontaccounting.php");
75 include_once($path_to_root . "/includes/current_user.inc");
76 include_once($path_to_root . "/includes/lang/language.php");
77 include_once($path_to_root . "/config_db.php");
78 include_once($path_to_root . "/includes/ajax.inc");
79 include_once($path_to_root . "/includes/ui/ui_msgs.inc");
80
81 /*
82         Make sure this directory exists and is writable!
83 //      $session_save_path = dirname(__FILE__).'/../tmp/';
84 */
85
86 session_name('FrontAccounting');
87 session_start();
88 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
89 header("Cache-control: private");
90
91 get_text::init();
92
93 // Page Initialisation
94 if (!isset($_SESSION['languages'])) 
95 {
96         language::load_languages(); // sets also default $_SESSION['language']
97 }
98
99 language::set_language($_SESSION['language']->code);
100
101 include_once($path_to_root . "/config.php");
102 include_once($path_to_root . "/includes/main.inc");
103
104 $Ajax =& new Ajax();
105
106 // intercept all output to destroy it in case of ajax call
107 register_shutdown_function('end_flush');
108 ob_start('output_html',0);
109
110 // colect all error msgs
111 set_error_handler('error_handler' /*, errtypes */);
112
113 if (!isset($_SESSION["wa_current_user"]))
114         $_SESSION["wa_current_user"] = new current_user();
115 set_global_connection();
116
117 if (!$_SESSION["wa_current_user"]->logged_in())
118 {
119         // Show login screen
120         if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
121         {
122                 include($path_to_root . "/access/login.php");
123                 $Ajax->redirect($path_to_root . "/access/login.php");
124                 exit;
125         } else {
126                 $succeed = $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
127                         $_POST["user_name_entry_field"],
128                         md5($_POST["password"]));
129                 // select full vs fallback ui mode on login
130                 $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
131                 if (!$succeed)
132                 {
133                         // Incorrect password
134                         login_fail();
135                 }
136                 $lang = $_SESSION['language'];
137                 language::set_language($_SESSION['language']->code);
138         }
139 }
140
141 if (!isset($_SESSION["App"])) {
142         $_SESSION["App"] = new front_accounting();
143         $_SESSION["App"]->init();
144 }
145
146 // Run with debugging messages for the system administrator(s) but not anyone else
147 /*if (in_array(15, $security_groups[$_SESSION["AccessLevel"]])) {
148         $debug = 1;
149 } else {
150         $debug = 0;
151 }*/
152
153 //----------------------------------------------------------------------------------------
154
155 // POST vars cleanup needed for direct reuse.
156 // We quote all values later with db_escape() before db update.
157         $_POST = strip_quotes($_POST);
158
159 ?>