Application startup code cleaup.
[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
50 function check_page_security($page_security)
51 {
52         if (!$_SESSION["wa_current_user"]->check_user_access())
53         {
54                 echo "<br><br><br><center>";
55                 echo "<b>" . _("Security settings have not been defined for your user account.");
56                 echo "<br>" . _("Please contact your system administrator.") . "</b>";
57
58                 kill_login();
59                 exit;
60         }
61
62         if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
63         {
64                 page(_("Access denied"));
65                 echo "<center><br><br><br><b>";
66                 echo _("The security settings on your account do not permit you to access this function");
67                 echo "</b>";
68                 echo "<br><br><br><br></center>";
69                 end_page();
70                 //kill_login();
71                 exit;
72         }
73 }
74
75 //-----------------------------------------------------------------------------
76 //      Removing magic quotes from nested arrays/variables
77 //
78 function strip_quotes($data)
79 {
80         if(get_magic_quotes_gpc()) {
81                 if(is_array($data)) {
82                         foreach($data as $k => $v) {
83                                 $data[$k] = strip_quotes($data[$k]);
84                         }
85                 } else
86                         return stripslashes($data);
87         }
88         return $data;
89 }
90
91 //============================================================================
92 if (!isset($path_to_root))
93 {
94         $path_to_root = ".";
95 }
96
97 // Prevent register_globals vulnerability
98 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
99         die("Restricted access");
100
101 include_once($path_to_root . "/frontaccounting.php");
102 include_once($path_to_root . "/includes/current_user.inc");
103 include_once($path_to_root . "/includes/lang/language.php");
104 include_once($path_to_root . "/config_db.php");
105 include_once($path_to_root . "/includes/ajax.inc");
106
107 /*
108         Make sure this directory exists and is writable!
109 //      $session_save_path = dirname(__FILE__).'/../tmp/';
110 */
111
112 session_name('FrontAccounting');
113 session_start();
114 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
115 header("Cache-control: private");
116
117 // Page Initialisation
118 if (!isset($_SESSION['languages'])) 
119 {
120         language::load_languages();
121 }
122
123 $lang = $_SESSION['language'];
124
125 // get_text support
126 get_text::init();
127 get_text::set_language($lang->code, $lang->encoding);
128 //get_text::add_domain("wa", $path_to_root . "/lang");
129 get_text::add_domain($lang->code, $path_to_root . "/lang");
130
131 // Necessary for ajax calls. Due to bug in php 4.3.10 for this 
132 // version set globally in php.ini
133 ini_set('default_charset', $_SESSION['language']->encoding);
134
135 include_once($path_to_root . "/config.php");
136
137 $Ajax =& new Ajax();
138
139 include_once($path_to_root . "/includes/main.inc");
140
141 include_once($path_to_root . "/includes/ui/ui_msgs.inc");
142
143 // intercept all output to destroy it in case of ajax call
144 register_shutdown_function('ob_end_flush');
145 ob_start('output_html',0);
146
147 // colect all error msgs
148 set_error_handler('error_handler' /*, errtypes */);
149
150 if (!isset($_SESSION["wa_current_user"]))
151         $_SESSION["wa_current_user"] = new current_user();
152
153 if (!$_SESSION["wa_current_user"]->logged_in())
154 {
155         // Show login screen
156         if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
157         {
158                 include($path_to_root . "/access/login.php");
159                 $Ajax->redirect($path_to_root . "/access/login.php");
160                 exit;
161         } else {
162                 $succeed = $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
163                         $_POST["user_name_entry_field"],
164                         md5($_POST["password"]));
165                 // select full vs fallback ui mode on login
166                 $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
167                 if (!$succeed)
168                 {
169                         // Incorrect password
170                         login_fail();
171                 }
172                 $lang = $_SESSION['language'];
173                 get_text::set_language($lang->code, $lang->encoding);
174         }
175 }
176
177
178 // Run with debugging messages for the system administrator(s) but not anyone else
179 /*if (in_array(15, $security_groups[$_SESSION["AccessLevel"]])) {
180         $debug = 1;
181 } else {
182         $debug = 0;
183 }*/
184
185 //----------------------------------------------------------------------------------------
186
187 check_page_security($page_security);
188
189 // POST vars cleanup needed for direct reuse.
190 // We quote all values later with db_escape() before db update.
191         $_POST = strip_quotes($_POST);
192
193 ?>