Implemented customizable authentication timeout.
[fa-stable.git] / includes / current_user.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 include_once($path_to_root . "/includes/prefs/userprefs.inc");
13
14 //--------------------------------------------------------------------------
15
16 class current_user
17 {
18         var $user;
19         var $loginname;
20         var $username;
21         var $name;
22         var $company;
23         var $pos;
24         var $access;
25         var $timeout;
26         var $last_act;
27
28         var $logged;
29         var $ui_mode = 0;
30         
31         var $prefs;
32
33         function current_user()
34         {
35                 global $def_coy;
36                 
37                 $this->loginname = $this->username = $this->name = "";
38                 $this->company = isset($def_coy)? $def_coy : 0;
39                 $this->logged = false;
40
41                 $this->prefs = new user_prefs();
42         }
43
44         function logged_in()
45         {
46                 return $this->logged;
47         }
48
49         function set_company($company)
50         {
51                 $this->company = $company;
52         }
53
54         function login($company, $loginname, $password)
55         {
56                 $this->set_company($company);
57             $this->logged = false;
58
59                 $Auth_Result = get_user_for_login($loginname, $password);
60
61                 if (db_num_rows($Auth_Result) > 0)
62                 {
63                         $myrow = db_fetch($Auth_Result);
64                         if (! @$myrow["inactive"]) {
65                     $this->access = $myrow["full_access"];
66                     $this->name = $myrow["real_name"];
67                     $this->pos = $myrow["pos"];
68                     $this->loginname = $loginname;
69                     $this->username = $this->loginname;
70                     $this->prefs = new user_prefs($myrow);
71                     $this->user = @$myrow["id"];
72                         update_user_visitdate($this->username);
73                         $this->logged = true;
74                                 $this->last_act = time();
75                                 $this->timeout = session_timeout();
76                         }
77                 }
78
79                 return $this->logged;
80         }
81
82         function check_user_access()
83         {
84                 global $security_groups;
85                 return is_array($security_groups[$this->access]);
86         }
87
88         function can_access_page($page_level)
89         {
90                 global $security_groups;
91                 // first registered company has site admin privileges
92                 return isset($page_level) && in_array($page_level, $security_groups[$this->access])
93                         && ($this->company == 0 || $page_level != 20); 
94         }
95
96         function get_db_connection()
97         {
98         global $db_connections;
99
100         $connection = $db_connections[$this->company];
101
102         //print_r($connection);
103
104         $db = mysql_connect($connection["host"] ,
105                 $connection["dbuser"], $connection["dbpassword"]);
106         mysql_select_db($connection["dbname"],$db);
107
108                 if (!defined('TB_PREF'))
109                         define('TB_PREF', $connection["tbpref"]);
110
111         return $db;
112         }
113
114         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, 
115                 $showgl, $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, 
116                 $theme, $pagesize, $show_hints, $profile, $rep_popup, $query_size, 
117                 $graphic_links, $lang, $stickydate) {
118                 update_user_display_prefs($this->user, $price_dec, 
119                         $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes, 
120                         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, 
121                         $show_hints, $profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate);
122
123                 // re-read the prefs
124                 $user = get_user($this->user);
125                 $this->prefs = new user_prefs($user);
126         }
127 }
128
129 //--------------------------------------------------------------------------
130
131 function round2($number, $decimals=0)
132 {
133         $delta = ($number < 0 ? -.000001 : .000001);
134         return round($number+$delta, $decimals);
135 }
136
137 function number_format2($number, $decimals=0)
138 {
139         global $thoseps, $decseps;
140         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
141         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
142         //return number_format($number, $decimals, $dsep,       $tsep);
143         $delta = ($number < 0 ? -.000001 : .000001);
144         return number_format($number+$delta, $decimals, $dsep,  $tsep);
145 }
146 //
147 //      Current ui mode.
148 //
149 function fallback_mode() {
150     return $_SESSION["wa_current_user"]->ui_mode==0;
151 }
152
153 function price_format($number) {
154     return number_format2($number,
155         $_SESSION["wa_current_user"]->prefs->price_dec());
156 }
157
158 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
159 //--------------------------------------------------------------------
160 function qty_format($number, $stock_id=null, &$dec) {
161         $dec = get_qty_dec($stock_id);
162     return number_format2($number, $dec);
163 }
164 // and get_qty_dec
165 function get_qty_dec($stock_id=null)
166 {
167         global $path_to_root;
168         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
169         if ($stock_id != null)
170                 $dec = get_unit_dec($stock_id);
171         if ($stock_id == null || $dec == -1 || $dec == null)
172                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
173         return $dec;
174 }
175 //-------------------------------------------------------------------
176 function exrate_format($number) {
177     return number_format2($number,
178         $_SESSION["wa_current_user"]->prefs->exrate_dec());
179 }
180
181 function percent_format($number) {
182     return number_format2($number,
183         $_SESSION["wa_current_user"]->prefs->percent_dec());
184 }
185
186 function user_numeric($input) {
187     global $decseps, $thoseps;
188
189     $num = trim($input);
190     $sep = $thoseps[user_tho_sep()];
191     if($sep!='') $num = str_replace( $sep, '', $num);
192         str_replace($sep, '', $num);
193     $sep = $decseps[user_dec_sep()];
194     if($sep!='.') $num = str_replace( $sep, '.', $num);
195
196     if (!is_numeric($num))
197           return false;
198     $num = (float)$num;
199     if ($num == (int)$num)
200           return (int)$num;
201     else
202           return $num;
203 }
204
205 function user_company()
206 {
207         return $_SESSION["wa_current_user"]->company;
208 }
209
210 function user_pos()
211 {
212         return $_SESSION["wa_current_user"]->pos;
213 }
214
215 function user_language()
216 {
217         return $_SESSION["wa_current_user"]->prefs->language();
218 }
219
220 function user_qty_dec()
221 {
222         return $_SESSION["wa_current_user"]->prefs->qty_dec();
223 }
224
225 function user_price_dec()
226 {
227         return $_SESSION["wa_current_user"]->prefs->price_dec();
228 }
229
230 function user_exrate_dec()
231 {
232         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
233 }
234
235 function user_percent_dec()
236 {
237         return $_SESSION["wa_current_user"]->prefs->percent_dec();
238 }
239
240 function user_show_gl_info()
241 {
242         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
243 }
244
245 function user_show_codes()
246 {
247         return $_SESSION["wa_current_user"]->prefs->show_codes();
248 }
249
250 function user_date_format()
251 {
252         return $_SESSION["wa_current_user"]->prefs->date_format();
253 }
254
255 function user_date_display()
256 {
257         return $_SESSION["wa_current_user"]->prefs->date_display();
258 }
259
260 function user_date_sep()
261 {
262         return $_SESSION["wa_current_user"]->prefs->date_sep();
263 }
264
265 function user_tho_sep()
266 {
267         return $_SESSION["wa_current_user"]->prefs->tho_sep();
268 }
269
270 function user_dec_sep()
271 {
272         return $_SESSION["wa_current_user"]->prefs->dec_sep();
273 }
274
275 function user_theme()
276 {
277         return $_SESSION["wa_current_user"]->prefs->get_theme();
278 }
279
280 function user_pagesize()
281 {
282         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
283 }
284
285 function user_hints()
286 {
287         return $_SESSION["wa_current_user"]->prefs->show_hints();
288 }
289
290 function user_print_profile()
291 {
292         return $_SESSION["wa_current_user"]->prefs->print_profile();
293 }
294
295 function user_rep_popup()
296 {
297         return $_SESSION["wa_current_user"]->prefs->rep_popup();
298 }
299
300 function user_query_size()
301 {
302         return $_SESSION["wa_current_user"]->prefs->query_size();
303 }
304
305 function user_graphic_links()
306 {
307         return $_SESSION["wa_current_user"]->prefs->graphic_links();
308 }
309
310 function sticky_doc_date()
311 {
312         return $_SESSION["wa_current_user"]->prefs->sticky_date();
313 }
314
315 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
316         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
317         $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate)
318 {
319
320         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
321                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
322                 $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate);
323 }
324
325 function add_user_js_data() {
326         global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
327
328         $ts = $thoseps[user_tho_sep()];
329         $ds = $decseps[user_dec_sep()];
330
331     $js = "\n"
332           . "var user = {\n"
333           . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
334           . "loadtxt: '"._('Requesting data...')."',\n"
335           . "date: '".Today()."',\n"    // server date
336           . "datesys: ".$date_system.",\n"
337           . "datefmt: ".user_date_format().",\n"
338           . "datesep: '".$dateseps[user_date_sep()]."',\n"
339           . "ts: '$ts',\n"
340           . "ds: '$ds',\n"
341           . "pdec : " . user_price_dec() . "}\n";
342
343   add_js_source($js);
344 }
345
346 //--------------------------------------------------------------------------
347
348 function session_timeout()
349 {
350         return get_company_pref('login_tout');
351 }
352 ?>