Merged changes from main CVS up to 2.1.5
[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, $startup_tab) {
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, $startup_tab);
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 function price_decimal_format($number, &$dec)
159 {
160         $dec = user_price_dec();
161         $str = strval($number);
162         $pos = strpos($str, '.');
163         if ($pos !== false)
164         {
165                 $len = strlen(substr($str, $pos + 1));
166                 if ($len > $dec)
167                         $dec = $len;
168         }
169         return number_format2($number, $dec);
170 }
171 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
172 //--------------------------------------------------------------------
173 function qty_format($number, $stock_id=null, &$dec) {
174         $dec = get_qty_dec($stock_id);
175     return number_format2($number, $dec);
176 }
177 // and get_qty_dec
178 function get_qty_dec($stock_id=null)
179 {
180         global $path_to_root;
181         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
182         if ($stock_id != null)
183                 $dec = get_unit_dec($stock_id);
184         if ($stock_id == null || $dec == -1 || $dec == null)
185                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
186         return $dec;
187 }
188 //-------------------------------------------------------------------
189 function exrate_format($number) {
190     return number_format2($number,
191         $_SESSION["wa_current_user"]->prefs->exrate_dec());
192 }
193
194 function percent_format($number) {
195     return number_format2($number,
196         $_SESSION["wa_current_user"]->prefs->percent_dec());
197 }
198
199 function user_numeric($input) {
200     global $decseps, $thoseps;
201
202     $num = trim($input);
203     $sep = $thoseps[user_tho_sep()];
204     if($sep!='') $num = str_replace( $sep, '', $num);
205         str_replace($sep, '', $num);
206     $sep = $decseps[user_dec_sep()];
207     if($sep!='.') $num = str_replace( $sep, '.', $num);
208
209     if (!is_numeric($num))
210           return false;
211     $num = (float)$num;
212     if ($num == (int)$num)
213           return (int)$num;
214     else
215           return $num;
216 }
217
218 function user_company()
219 {
220         return $_SESSION["wa_current_user"]->company;
221 }
222
223 function user_pos()
224 {
225         return $_SESSION["wa_current_user"]->pos;
226 }
227
228 function user_language()
229 {
230         return $_SESSION["wa_current_user"]->prefs->language();
231 }
232
233 function user_qty_dec()
234 {
235         return $_SESSION["wa_current_user"]->prefs->qty_dec();
236 }
237
238 function user_price_dec()
239 {
240         return $_SESSION["wa_current_user"]->prefs->price_dec();
241 }
242
243 function user_exrate_dec()
244 {
245         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
246 }
247
248 function user_percent_dec()
249 {
250         return $_SESSION["wa_current_user"]->prefs->percent_dec();
251 }
252
253 function user_show_gl_info()
254 {
255         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
256 }
257
258 function user_show_codes()
259 {
260         return $_SESSION["wa_current_user"]->prefs->show_codes();
261 }
262
263 function user_date_format()
264 {
265         return $_SESSION["wa_current_user"]->prefs->date_format();
266 }
267
268 function user_date_display()
269 {
270         return $_SESSION["wa_current_user"]->prefs->date_display();
271 }
272
273 function user_date_sep()
274 {
275         return $_SESSION["wa_current_user"]->prefs->date_sep();
276 }
277
278 function user_tho_sep()
279 {
280         return $_SESSION["wa_current_user"]->prefs->tho_sep();
281 }
282
283 function user_dec_sep()
284 {
285         return $_SESSION["wa_current_user"]->prefs->dec_sep();
286 }
287
288 function user_theme()
289 {
290         return $_SESSION["wa_current_user"]->prefs->get_theme();
291 }
292
293 function user_pagesize()
294 {
295         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
296 }
297
298 function user_hints()
299 {
300         return $_SESSION["wa_current_user"]->prefs->show_hints();
301 }
302
303 function user_print_profile()
304 {
305         return $_SESSION["wa_current_user"]->prefs->print_profile();
306 }
307
308 function user_rep_popup()
309 {
310         return $_SESSION["wa_current_user"]->prefs->rep_popup();
311 }
312
313 function user_query_size()
314 {
315         return $_SESSION["wa_current_user"]->prefs->query_size();
316 }
317
318 function user_graphic_links()
319 {
320         return $_SESSION["wa_current_user"]->prefs->graphic_links();
321 }
322
323 function sticky_doc_date()
324 {
325         return $_SESSION["wa_current_user"]->prefs->sticky_date();
326 }
327
328 function user_startup_tab()
329 {
330         return $_SESSION["wa_current_user"]->prefs->start_up_tab();
331 }
332
333 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
334         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
335         $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate, $startup_tab)
336 {
337
338         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
339                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
340                 $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate, $startup_tab);
341 }
342
343 function add_user_js_data() {
344         global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
345
346         $ts = $thoseps[user_tho_sep()];
347         $ds = $decseps[user_dec_sep()];
348
349     $js = "\n"
350           . "var user = {\n"
351           . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
352           . "loadtxt: '"._('Requesting data...')."',\n"
353           . "date: '".Today()."',\n"    // server date
354           . "datesys: ".$date_system.",\n"
355           . "datefmt: ".user_date_format().",\n"
356           . "datesep: '".$dateseps[user_date_sep()]."',\n"
357           . "ts: '$ts',\n"
358           . "ds: '$ds',\n"
359           . "pdec : " . user_price_dec() . "}\n";
360
361   add_js_source($js);
362 }
363
364 //--------------------------------------------------------------------------
365
366 function session_timeout()
367 {
368         $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
369         return $tout ? $tout : ini_get('session.gc_maxlifetime');
370 }
371 ?>