Fixed modules/plugins security extensions inclusion.
[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                 page(_("Access denied"), false);
63                         display_error($msg);
64                 end_page();
65                 kill_login();
66                 exit;
67         }
68
69         if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
70         {
71                 // no_menu parameter guess here is ugly hack, but works for now.
72                 // Better solution is to use global switch for menu, set before 
73                 // session.inc inclusion.
74                 page(_("Access denied"), strpos($_SERVER['PHP_SELF'], '/view/'));
75
76                 echo "<center><br><br><br><b>";
77                 echo _("The security settings on your account do not permit you to access this function");
78                 echo "</b>";
79                 echo "<br><br><br><br></center>";
80                 end_page();
81                 //kill_login();
82                 exit;
83         }
84 }
85
86 //-----------------------------------------------------------------------------
87 //      Removing magic quotes from nested arrays/variables
88 //
89 function strip_quotes($data)
90 {
91         if(get_magic_quotes_gpc()) {
92                 if(is_array($data)) {
93                         foreach($data as $k => $v) {
94                                 $data[$k] = strip_quotes($data[$k]);
95                         }
96                 } else
97                         return stripslashes($data);
98         }
99         return $data;
100 }
101
102 //============================================================================
103 //
104 //
105 function login_timeout()
106 {
107         // skip timeout on logout page
108         if ($_SESSION["wa_current_user"]->logged) {
109                 $tout = $_SESSION["wa_current_user"]->timeout;
110                 if ($tout && (time() > $_SESSION["wa_current_user"]->last_act + $tout))
111                 {
112                         $_SESSION["wa_current_user"]->logged = false;
113                 }
114                 $_SESSION["wa_current_user"]->last_act = time();
115         }
116 }
117 //============================================================================
118 if (!isset($path_to_root))
119 {
120         $path_to_root = ".";
121 }
122
123 // Prevent register_globals vulnerability
124 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
125         die("Restricted access");
126
127 include_once($path_to_root . "/includes/current_user.inc");
128 include_once($path_to_root . "/frontaccounting.php");
129 include_once($path_to_root . "/admin/db/security_db.inc");
130 include_once($path_to_root . "/includes/lang/language.php");
131 include_once($path_to_root . "/config_db.php");
132 include_once($path_to_root . "/includes/ajax.inc");
133 include_once($path_to_root . "/includes/ui/ui_msgs.inc");
134
135 /*
136         Uncomment the setting below when using FA on shared hosting
137         to avoid unexpeced session timeouts.
138         Make sure this directory exists and is writable!
139 */
140 //ini_set('session.save_path', dirname(__FILE__).'/../tmp/');
141
142 ini_set('session.gc_maxlifetime', 36000); // 10hrs
143
144 session_name('FrontAccounting');
145 session_start();
146 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
147 header("Cache-control: private");
148
149 get_text_init();
150
151 // Page Initialisation
152 if (!isset($_SESSION['languages'])) 
153 {
154         load_languages(); // sets also default $_SESSION['language']
155 }
156
157 $_SESSION['language']->set_language($_SESSION['language']->code);
158
159 // include $Hooks object if locale file exists
160 if(@include_once($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc")) 
161 {
162         $Hooks = new Hooks();
163 }
164
165 include_once($path_to_root . "/includes/access_levels.inc");
166 include_once($path_to_root . "/config.php");
167 include_once($path_to_root . "/includes/main.inc");
168
169 // Ajax communication object
170 $Ajax =& new Ajax();
171
172 // js/php validation rules container
173 $Validate = array();
174 // bindings for editors
175 $Editors = array();
176 // page help. Currently help for function keys.
177 $Pagehelp = array();
178
179 $SysPrefs = new sys_prefs();
180
181 $Refs = new references();
182
183 // intercept all output to destroy it in case of ajax call
184 register_shutdown_function('end_flush');
185 ob_start('output_html',0);
186
187 // colect all error msgs
188 set_error_handler('error_handler' /*, errtypes */);
189
190 if (!isset($_SESSION["wa_current_user"]))
191         $_SESSION["wa_current_user"] = new current_user();
192
193 set_global_connection();
194
195 // logout.php is the only page we should have always 
196 // accessable regardless of access level and current login status.
197 if (strstr($_SERVER['PHP_SELF'], 'logout.php') == false){
198
199         login_timeout();
200
201         if (!$_SESSION["wa_current_user"]->logged_in())
202         {
203                 // Show login screen
204                 if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
205                 {
206                         $_SESSION['timeout'] = array( 'uri'=> $_SERVER['REQUEST_URI'],
207                                 'post' => $_POST);
208
209                         if (!in_ajax()) {
210                                 include($path_to_root . "/access/login.php");
211                         } else {
212                                 // ajax update of current page elements - open login window in popup
213                                 // to not interfere with ajaxified page.
214                                 $Ajax->popup($path_to_root . "/access/timeout.php");
215                         }
216                         exit;
217                 } else {
218                         $succeed = $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
219                                 $_POST["user_name_entry_field"], md5($_POST["password"]));
220                         // select full vs fallback ui mode on login
221                         $_SESSION["wa_current_user"]->ui_mode = $_POST['ui_mode'];
222                         if (!$succeed)
223                         {
224                         // Incorrect password
225                                 login_fail();
226                         }
227                         $lang = &$_SESSION['language'];
228                         $lang->set_language($_SESSION['language']->code);
229                 }
230         }
231
232         include_once($path_to_root . '/company/'.user_company().'/installed_extensions.php');
233
234         if (!isset($_SESSION["App"])) {
235                 $_SESSION["App"] = new front_accounting();
236                 $_SESSION["App"]->init();
237         }
238
239         /*
240         This call is necessary only at:
241         . on any page with non-standard security areas
242         . in security roles editor
243         To be optmized  after.
244         */
245         add_access_extensions();
246
247         check_page_security($page_security);
248 }
249 // POST vars cleanup needed for direct reuse.
250 // We quote all values later with db_escape() before db update.
251         $_POST = strip_quotes($_POST);
252
253 ?>