Update from usntable branch.
[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         global $path_to_root;
37         
38         header("HTTP/1.1 401 Authorization Required");
39         echo "<center><br><br><font size='5' color='red'><b>" . _("Incorrect Password") . "<b></font><br><br>";
40         echo "<b>" . _("The user and password combination is not valid for the system.") . "<b><br><br>";
41
42         echo _("If you are not an authorized user, please contact your system administrator to obtain an account to enable you to use the system.");
43         echo "<br><a href='$path_to_root/index.php'>" . _("Try again") . "</a>";
44         echo "</center>";
45
46         kill_login();
47         die();
48 }
49
50 //----------------------------------------------------------------------------------------
51
52 function check_page_security($page_security)
53 {
54         if (!$_SESSION["wa_current_user"]->check_user_access())
55         {
56                 // notification after upgrade from pre-2.2 version
57                 $msg = $_SESSION["wa_current_user"]->old_db ?
58                          _("Security settings have not been defined for your user account.")
59                                 . "<br>" . _("Please contact your system administrator.")       
60                         : _("Please remove \$security_groups and \$security_headings arrays from config.php file!");
61
62                 display_error($msg);
63                 end_page();
64                 kill_login();
65                 exit;
66         }
67
68         if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
69         {
70
71                 echo "<center><br><br><br><b>";
72                 echo _("The security settings on your account do not permit you to access this function");
73                 echo "</b>";
74                 echo "<br><br><br><br></center>";
75                 end_page();
76                 exit;
77         }
78 }
79 /*
80         Helper function for setting page security level depeding on 
81         GET start variable and/or some value stored in session variable.
82         Before the call $page_security should be set to default page_security value.
83 */
84 function set_page_security($value=null, $trans = array(), $gtrans = array())
85 {
86         global $page_security;
87
88         // first check is this is not start page call
89         foreach($gtrans as $key => $area)
90                 if (isset($_GET[$key])) {
91                         $page_security = $area;
92                         return;
93                 }
94
95         // then check session value
96         if (isset($trans[$value])) {
97                 $page_security = $trans[$value];
98                 return;
99         }
100 }
101
102 //-----------------------------------------------------------------------------
103 //      Removing magic quotes from nested arrays/variables
104 //
105 function strip_quotes($data)
106 {
107         if(get_magic_quotes_gpc()) {
108                 if(is_array($data)) {
109                         foreach($data as $k => $v) {
110                                 $data[$k] = strip_quotes($data[$k]);
111                         }
112                 } else
113                         return stripslashes($data);
114         }
115         return $data;
116 }
117
118 //============================================================================
119 //
120 //
121 function login_timeout()
122 {
123         // skip timeout on logout page
124         if ($_SESSION["wa_current_user"]->logged) {
125                 $tout = $_SESSION["wa_current_user"]->timeout;
126                 if ($tout && (time() > $_SESSION["wa_current_user"]->last_act + $tout))
127                 {
128                         $_SESSION["wa_current_user"]->logged = false;
129                 }
130                 $_SESSION["wa_current_user"]->last_act = time();
131         }
132 }
133 //============================================================================
134 if (!isset($path_to_root))
135 {
136         $path_to_root = ".";
137 }
138
139 // Prevent register_globals vulnerability
140 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
141         die("Restricted access");
142
143 include_once($path_to_root . "/includes/current_user.inc");
144 include_once($path_to_root . "/frontaccounting.php");
145 include_once($path_to_root . "/admin/db/security_db.inc");
146 include_once($path_to_root . "/includes/lang/language.php");
147 include_once($path_to_root . "/config_db.php");
148 include_once($path_to_root . "/includes/ajax.inc");
149 include_once($path_to_root . "/includes/ui/ui_msgs.inc");
150
151 /*
152         Uncomment the setting below when using FA on shared hosting
153         to avoid unexpeced session timeouts.
154         Make sure this directory exists and is writable!
155 */
156 //ini_set('session.save_path', dirname(__FILE__).'/../tmp/');
157
158 ini_set('session.gc_maxlifetime', 36000); // 10hrs
159
160 session_name('FrontAccounting');
161 session_start();
162 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
163 header("Cache-control: private");
164
165 get_text_init();
166
167 // Page Initialisation
168 if (!isset($_SESSION['language'])) 
169 {
170         load_languages(); // sets also default $_SESSION['language']
171 }
172
173 $_SESSION['language']->set_language($_SESSION['language']->code);
174
175 // include $Hooks object if locale file exists
176 if(@include_once($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc")) 
177 {
178         $Hooks = new Hooks();
179 }
180
181 include_once($path_to_root . "/includes/access_levels.inc");
182 include_once($path_to_root . "/config.php");
183 include_once($path_to_root . "/includes/main.inc");
184
185 // Ajax communication object
186 $Ajax =& new Ajax();
187
188 // js/php validation rules container
189 $Validate = array();
190 // bindings for editors
191 $Editors = array();
192 // page help. Currently help for function keys.
193 $Pagehelp = array();
194
195 $SysPrefs = new sys_prefs();
196
197 $Refs = new references();
198
199 // intercept all output to destroy it in case of ajax call
200 register_shutdown_function('end_flush');
201 ob_start('output_html',0);
202
203 // colect all error msgs
204 set_error_handler('error_handler' /*, errtypes */);
205
206 if (!isset($_SESSION["wa_current_user"]))
207         $_SESSION["wa_current_user"] = new current_user();
208
209 // logout.php is the only page we should have always 
210 // accessable regardless of access level and current login status.
211 if (strstr($_SERVER['PHP_SELF'], 'logout.php') == false){
212
213         login_timeout();
214
215         if (!$_SESSION["wa_current_user"]->logged_in())
216         {
217                 // Show login screen
218                 if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
219                 {
220                         $_SESSION['timeout'] = array( 'uri'=> $_SERVER['REQUEST_URI'],
221                                 'post' => $_POST);
222
223                         if (!in_ajax()) {
224                                 include($path_to_root . "/access/login.php");
225                         } else {
226                                 // ajax update of current page elements - open login window in popup
227                                 // to not interfere with ajaxified page.
228                                 $Ajax->popup($path_to_root . "/access/timeout.php");
229                         }
230                         exit;
231                 } else {
232                         $succeed = $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
233                                 $_POST["user_name_entry_field"], md5($_POST["password"]));
234                         // select full vs fallback ui mode on login
235                         $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
236                         if (!$succeed)
237                         {
238                         // Incorrect password
239                                 login_fail();
240                         }
241                         $lang = &$_SESSION['language'];
242                         $lang->set_language($_SESSION['language']->code);
243                 }
244         } else
245                 set_global_connection();
246
247         if (!$_SESSION["wa_current_user"]->old_db)
248                 include_once($path_to_root . '/company/'.user_company().'/installed_extensions.php');
249
250         if (!isset($_SESSION["App"])) {
251                 $_SESSION["App"] = new front_accounting();
252                 $_SESSION["App"]->init();
253         }
254 }
255
256
257 // POST vars cleanup needed for direct reuse.
258 // We quote all values later with db_escape() before db update.
259         $_POST = strip_quotes($_POST);
260
261 ?>