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 ***********************************************************************/
13 include_once($path_to_root . "/includes/prefs/userprefs.inc");
14 if (!defined('TB_PREF')) {
15 define('TB_PREF', '&TB_PREF&');
17 //--------------------------------------------------------------------------
25 var $company; // user's company
30 var $role_set = false;
36 var $cur_con; // current db connection (can be different from $company for superuser)
38 function current_user()
42 $this->loginname = $this->username = $this->name = "";
43 $this->company = isset($def_coy)? $def_coy : 0;
44 $this->logged = false;
46 $this->prefs = new user_prefs();
54 function set_company($company)
56 $this->company = $company;
59 function login($company, $loginname, $password)
61 global $security_areas, $security_groups, $security_headings, $path_to_root;
63 $this->set_company($company);
64 $this->logged = false;
66 $Auth_Result = get_user_for_login($loginname, $password);
68 if (db_num_rows($Auth_Result) > 0)
70 $myrow = db_fetch($Auth_Result);
71 $this->old_db = isset($myrow["full_access"]);
72 if (! @$myrow["inactive"]) {
75 // db was not yet upgraded after source update to v.2.2
76 // give enough access for admin user to continue upgrade
77 if (!isset($security_groups) || !isset($security_headings)) {
78 echo "<center><br><br><font size='5' color='red'><b>";
79 echo _('Before software upgrade you have to include old $security_groups and $security_headings arrays from old config.php file to the new one.');
80 echo '<br>'."<a href=$path_to_root/index.php>"._("Back")."</a>";
81 echo "</b></font><br><br></center>";
84 $this->access = $myrow["full_access"];
85 if (in_array(20, $security_groups[$this->access]))
86 // temporary access for admin users
87 $this->role_set[] = $security_areas['SA_SOFTWAREUPGRADE'][0];
89 echo "<center><br><br><font size='5' color='red'><b>";
90 echo _('System is available for site admin only until full database upgrade');
91 echo "</b></font><br><br></center>";
95 $this->role_set = array();
96 $this->access = $myrow["role_id"];
97 // store area codes available for current user role
98 $role = get_security_role($this->access);
101 foreach( $role['areas'] as $code )
102 // filter only area codes for enabled security sections
103 if (in_array($code&~0xff, $role['sections']))
104 $this->role_set[] = $code;
106 $this->name = $myrow["real_name"];
107 $this->pos = $myrow["pos"];
108 $this->loginname = $loginname;
109 $this->username = $this->loginname;
110 $this->prefs = new user_prefs($myrow);
111 $this->user = @$myrow["id"];
112 update_user_visitdate($this->username);
113 $this->logged = true;
114 $this->last_act = time();
115 $this->timeout = session_timeout();
118 return $this->logged;
121 function check_user_access()
123 global $security_groups;
125 // notification after upgrade from pre-2.2 version
126 return isset($security_groups) && is_array(@$security_groups[$this->access]);
128 return !isset($security_groups) && is_array($this->role_set);
131 function can_access($page_level)
133 global $security_groups, $security_areas;
134 if (isset($security_groups)) {
135 return $this->company == 0 &&
136 in_array(20, $security_groups[$this->access]);
139 if ($page_level === 'SA_OPEN')
141 if ($page_level === 'SA_DENIED' || $page_level === '')
144 $code = $security_areas[$page_level][0];
146 // only first registered company has site admin privileges
147 return $code && in_array($code, $this->role_set)
148 && ($this->company == 0 || (($code&~0xff) != SS_SADMIN));
151 function can_access_page($page_level)
153 return $this->can_access($page_level);
156 function set_db_connection($id = -1)
158 return set_global_connection($id);
161 function update_prefs($prefs)
163 global $allow_demo_mode;
165 if(!$allow_demo_mode) {
166 update_user_prefs($this->user, $prefs);
169 $this->prefs = new user_prefs(get_user($this->user));
173 //--------------------------------------------------------------------------
175 function round2($number, $decimals=0)
177 $delta = ($number < 0 ? -.0000000001 : .0000000001);
178 return round($number+$delta, $decimals);
182 Returns number formatted according to user setup and using $decimals digits after dot
183 (defualt is 0). When $decimals is set to 'max' maximum available precision is used
184 (decimals depend on value) and trailing zeros are trimmed.
186 function number_format2($number, $decimals=0)
188 global $thoseps, $decseps;
189 $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
190 $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
191 //return number_format($number, $decimals, $dsep, $tsep);
192 if($decimals==='max')
193 $dec = 15 - floor(log10(abs($number)));
195 $delta = ($number < 0 ? -.0000000001 : .0000000001);
200 $num = number_format($number, $dec, $dsep, $tsep);
202 return $decimals==='max' ? rtrim($num, '0') : $num;
208 function fallback_mode() {
209 return $_SESSION["wa_current_user"]->ui_mode==0;
212 function price_format($number) {
213 return number_format2($number,
214 $_SESSION["wa_current_user"]->prefs->price_dec());
217 function price_decimal_format($number, &$dec)
219 $dec = user_price_dec();
220 $str = strval($number);
221 $pos = strpos($str, '.');
224 $len = strlen(substr($str, $pos + 1));
228 return number_format2($number, $dec);
230 // function money_format doesn't exist in OS Win.
231 if (!function_exists('money_format'))
233 function money_format($format, $number)
235 return price_format($number);
239 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
240 //--------------------------------------------------------------------
241 function qty_format($number, $stock_id=null, &$dec) {
242 $dec = get_qty_dec($stock_id);
243 return number_format2($number, $dec);
247 function get_qty_dec($stock_id=null)
249 global $path_to_root;
250 include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
251 if ($stock_id != null)
252 $dec = get_unit_dec($stock_id);
253 if ($stock_id == null || $dec == -1 || $dec == null)
254 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
257 //-------------------------------------------------------------------
259 // Maximum precision format. Strips trailing unsignificant digits.
261 function maxprec_format($number) {
262 return number_format2($number, 'max');
265 function exrate_format($number) {
266 return number_format2($number,
267 $_SESSION["wa_current_user"]->prefs->exrate_dec());
270 function percent_format($number) {
271 return number_format2($number,
272 $_SESSION["wa_current_user"]->prefs->percent_dec());
275 function user_numeric($input) {
276 global $decseps, $thoseps;
279 $sep = $thoseps[user_tho_sep()];
281 $num = str_replace( $sep, '', $num);
283 $sep = $decseps[user_dec_sep()];
285 $num = str_replace( $sep, '.', $num);
287 if (!is_numeric($num))
290 if ($num == (int)$num)
296 function user_company()
300 return isset($_SESSION["wa_current_user"]) ? $_SESSION["wa_current_user"]->company : $def_coy;
305 return $_SESSION["wa_current_user"]->pos;
308 function user_language()
310 return $_SESSION["wa_current_user"]->prefs->language();
313 function user_qty_dec()
315 return $_SESSION["wa_current_user"]->prefs->qty_dec();
318 function user_price_dec()
320 return $_SESSION["wa_current_user"]->prefs->price_dec();
323 function user_exrate_dec()
325 return $_SESSION["wa_current_user"]->prefs->exrate_dec();
328 function user_percent_dec()
330 return $_SESSION["wa_current_user"]->prefs->percent_dec();
333 function user_show_gl_info()
335 return $_SESSION["wa_current_user"]->prefs->show_gl_info();
338 function user_show_codes()
340 return $_SESSION["wa_current_user"]->prefs->show_codes();
343 function user_date_format()
345 return $_SESSION["wa_current_user"]->prefs->date_format();
348 function user_date_display()
350 return $_SESSION["wa_current_user"]->prefs->date_display();
353 function user_date_sep()
355 return $_SESSION["wa_current_user"]->prefs->date_sep();
358 function user_tho_sep()
360 return $_SESSION["wa_current_user"]->prefs->tho_sep();
363 function user_dec_sep()
365 return $_SESSION["wa_current_user"]->prefs->dec_sep();
368 function user_theme()
370 return isset($_SESSION["wa_current_user"]) ?
371 $_SESSION["wa_current_user"]->prefs->get_theme() : 'default';
374 function user_pagesize()
376 return $_SESSION["wa_current_user"]->prefs->get_pagesize();
379 function user_hints()
381 return $_SESSION["wa_current_user"]->prefs->show_hints();
384 function user_print_profile()
386 return $_SESSION["wa_current_user"]->prefs->print_profile();
389 function user_rep_popup()
391 return $_SESSION["wa_current_user"]->prefs->rep_popup();
394 function user_query_size()
396 return $_SESSION["wa_current_user"]->prefs->query_size();
399 function user_graphic_links()
401 return $_SESSION["wa_current_user"]->prefs->graphic_links();
404 function sticky_doc_date()
406 return $_SESSION["wa_current_user"]->prefs->sticky_date();
409 function user_startup_tab()
411 return $_SESSION["wa_current_user"]->prefs->start_up_tab();
414 function user_check_access($sec_area)
416 return $_SESSION["wa_current_user"]->can_access($sec_area);
419 function set_user_prefs($prefs)
421 $_SESSION["wa_current_user"]->update_prefs($prefs);
424 function add_user_js_data() {
425 global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
427 $ts = $thoseps[user_tho_sep()];
428 $ds = $decseps[user_dec_sep()];
432 . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
433 . "loadtxt: '"._('Requesting data...')."',\n"
434 . "date: '".Today()."',\n" // server date
435 . "datesys: ".$date_system.",\n"
436 . "datefmt: ".user_date_format().",\n"
437 . "datesep: '".$dateseps[user_date_sep()]."',\n"
440 . "pdec : " . user_price_dec() . "}\n";
445 //--------------------------------------------------------------------------
447 function session_timeout()
449 $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
450 return $tout ? $tout : ini_get('session.gc_maxlifetime');
453 //-----------------------------------------------------------------------------
454 // Inserts $elements into $array at position $index.
455 // $elements is list of any objects
457 function array_insert(&$array, $index, $elements)
459 if (!is_array($elements)) $elements = array($elements);
461 $head = array_splice($array, 0, $index);
462 $array = array_merge($head, $elements, $array);
465 function array_remove(&$array, $index, $len=1)
467 array_splice($array, $index, $len);
470 function array_substitute(&$array, $index, $len, $elements)
472 array_splice($array, $index, $len);
473 array_insert($array, $index, $elements);
476 function array_append(&$array, $elements)
478 foreach($elements as $key => $el) {
486 // Search $needle in $haystack or in $haystack[][$valuekey]
487 // returns $needle found or null.
489 function array_search_value($needle, $haystack, $valuekey=null)
491 foreach($haystack as $key => $value) {
492 $val = isset($valuekey) ? @$value[$valuekey] : $value;
493 if ($needle == $val){
500 // Search $needle in $haystack or in $haystack[][$valuekey]
501 // returns array of keys of $haystack elements found
503 function array_search_keys($needle, $haystack, $valuekey=null)
507 foreach($haystack as $key => $value) {
508 $val = isset($valuekey) ? @$value[$valuekey] : $value;
509 if ($needle == $val){
516 // Find first (single) $needle in $haystack or in $haystack[][$valuekey]
517 // returns $haystack element found or null
519 function array_search_key($needle, $haystack, $valuekey=null)
521 $keys = array_search_keys($needle, $haystack, $valuekey);
525 function flush_dir($path, $wipe = false)
527 $dir = opendir($path);
528 while(false !== ($fname = readdir($dir))) {
529 if($fname=='.' || $fname=='..' || $fname=='CVS' || (!$wipe && $fname=='index.php')) continue;
530 if(is_dir($path.'/'.$fname)) {
531 flush_dir($path.'/'.$fname, $wipe);
532 if ($wipe) @rmdir($path.'/'.$fname);
534 @unlink($path.'/'.$fname);
538 Returns current path to company private folder.
539 (Current path can change after chdir).
541 function company_path($comp=null)
543 global $path_to_root, $comp_path;
546 $comp = user_company();
548 // if path is relative, set current path_to_root
549 return ($comp_path[0]=='.' ? $path_to_root.'/'.basename($comp_path) : $comp_path)