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 set_global_connection();
68 // Use external authentication source if any.
69 // Keep in mind you need to have user data set for $loginname
70 // in FA users table anyway to successfully log in.
71 $Auth_Result = hook_authenticate($loginname, $password);
73 if (!isset($Auth_Result)) // if not used: standard method
74 $Auth_Result = get_user_auth($loginname, md5($password));
78 $myrow = get_user_by_login($loginname);
79 $this->old_db = isset($myrow["full_access"]);
80 if (! @$myrow["inactive"]) {
83 // db was not yet upgraded after source update to v.2.2
84 // give enough access for admin user to continue upgrade
85 if (!isset($security_groups) || !isset($security_headings)) {
86 echo "<center><br><br><font size='5' color='red'><b>";
87 echo _('Before software upgrade you have to include old $security_groups and $security_headings arrays from old config.php file to the new one.');
88 echo '<br>'."<a href=$path_to_root/index.php>"._("Back")."</a>";
89 echo "</b></font><br><br></center>";
92 $this->access = $myrow["full_access"];
93 if (in_array(20, $security_groups[$this->access]))
94 // temporary access for admin users
95 $this->role_set[] = $security_areas['SA_SOFTWAREUPGRADE'][0];
97 echo "<center><br><br><font size='5' color='red'><b>";
98 echo _('System is available for site admin only until full database upgrade');
99 echo "</b></font><br><br></center>";
103 $this->role_set = array();
104 $this->access = $myrow["role_id"];
105 // store area codes available for current user role
106 $role = get_security_role($this->access);
109 foreach( $role['areas'] as $code )
110 // filter only area codes for enabled security sections
111 if (in_array($code&~0xff, $role['sections']))
112 $this->role_set[] = $code;
114 $this->name = $myrow["real_name"];
115 $this->pos = $myrow["pos"];
116 $this->loginname = $loginname;
117 $this->username = $this->loginname;
118 $this->prefs = new user_prefs($myrow);
119 $this->user = @$myrow["id"];
120 update_user_visitdate($this->username);
121 $this->logged = true;
122 $this->last_act = time();
123 $this->timeout = session_timeout();
126 return $this->logged;
129 function check_user_access()
131 global $security_groups;
133 // notification after upgrade from pre-2.2 version
134 return isset($security_groups) && is_array(@$security_groups[$this->access]);
136 return !isset($security_groups) && is_array($this->role_set);
139 function can_access($page_level)
141 global $security_groups, $security_areas;
142 if (isset($security_groups)) {
143 return $this->company == 0 &&
144 in_array(20, $security_groups[$this->access]);
147 if ($page_level === 'SA_OPEN')
149 if ($page_level === 'SA_DENIED' || $page_level === '')
152 $code = $security_areas[$page_level][0];
154 // only first registered company has site admin privileges
155 return $code && in_array($code, $this->role_set)
156 && ($this->company == 0 || (($code&~0xff) != SS_SADMIN));
159 function can_access_page($page_level)
161 return $this->can_access($page_level);
164 function set_db_connection($id = -1)
166 return set_global_connection($id);
169 function update_prefs($prefs)
171 global $allow_demo_mode;
173 if(!$allow_demo_mode) {
174 update_user_prefs($this->user, $prefs);
177 $this->prefs = new user_prefs(get_user($this->user));
181 //--------------------------------------------------------------------------
183 function round2($number, $decimals=0)
185 $delta = ($number < 0 ? -.0000000001 : .0000000001);
186 return round($number+$delta, $decimals);
190 Returns number formatted according to user setup and using $decimals digits after dot
191 (defualt is 0). When $decimals is set to 'max' maximum available precision is used
192 (decimals depend on value) and trailing zeros are trimmed.
194 function number_format2($number, $decimals=0)
196 global $thoseps, $decseps;
197 $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
198 $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
199 //return number_format($number, $decimals, $dsep, $tsep);
200 if($decimals==='max')
201 $dec = 15 - floor(log10(abs($number)));
203 $delta = ($number < 0 ? -.0000000001 : .0000000001);
208 $num = number_format($number, $dec, $dsep, $tsep);
210 return $decimals==='max' ? rtrim($num, '0') : $num;
214 /* price/float comparision helper to be used in any suspicious place for zero values?
216 if (!floatcmp($value1, $value2))
220 define('FLOAT_COMP_DELTA', 0.004);
222 function floatcmp($a, $b)
224 return $a - $b > FLOAT_COMP_DELTA ? 1 : $b - $a > FLOAT_COMP_DELTA ? -1 : 0;
230 function fallback_mode() {
231 return $_SESSION["wa_current_user"]->ui_mode==0;
234 function price_format($number) {
235 return number_format2($number,
236 $_SESSION["wa_current_user"]->prefs->price_dec());
239 function price_decimal_format($number, &$dec)
241 $dec = user_price_dec();
242 $str = strval($number);
243 $pos = strpos($str, '.');
246 $len = strlen(substr($str, $pos + 1));
250 return number_format2($number, $dec);
252 // function money_format doesn't exist in OS Win.
253 if (!function_exists('money_format'))
255 function money_format($format, $number)
257 return price_format($number);
261 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
262 //--------------------------------------------------------------------
263 function qty_format($number, $stock_id=null, &$dec) {
264 $dec = get_qty_dec($stock_id);
265 return number_format2($number, $dec);
269 function get_qty_dec($stock_id=null)
271 global $path_to_root;
272 include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
273 if ($stock_id != null)
274 $dec = get_unit_dec($stock_id);
275 if ($stock_id == null || $dec == -1 || $dec == null)
276 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
279 //-------------------------------------------------------------------
281 // Maximum precision format. Strips trailing unsignificant digits.
283 function maxprec_format($number) {
284 return number_format2($number, 'max');
287 function exrate_format($number) {
288 return number_format2($number,
289 $_SESSION["wa_current_user"]->prefs->exrate_dec());
292 function percent_format($number) {
293 return number_format2($number,
294 $_SESSION["wa_current_user"]->prefs->percent_dec());
297 function user_numeric($input) {
298 global $decseps, $thoseps;
301 $sep = $thoseps[user_tho_sep()];
303 $num = str_replace( $sep, '', $num);
305 $sep = $decseps[user_dec_sep()];
307 $num = str_replace( $sep, '.', $num);
309 if (!is_numeric($num))
312 if ($num == (int)$num)
318 function user_company()
322 return isset($_SESSION["wa_current_user"]) ? $_SESSION["wa_current_user"]->company : $def_coy;
327 return $_SESSION["wa_current_user"]->pos;
330 function user_language()
332 return $_SESSION["wa_current_user"]->prefs->language();
335 function user_qty_dec()
337 return $_SESSION["wa_current_user"]->prefs->qty_dec();
340 function user_price_dec()
342 return $_SESSION["wa_current_user"]->prefs->price_dec();
345 function user_exrate_dec()
347 return $_SESSION["wa_current_user"]->prefs->exrate_dec();
350 function user_percent_dec()
352 return $_SESSION["wa_current_user"]->prefs->percent_dec();
355 function user_show_gl_info()
357 return $_SESSION["wa_current_user"]->prefs->show_gl_info();
360 function user_show_codes()
362 return $_SESSION["wa_current_user"]->prefs->show_codes();
365 function user_date_format()
367 return $_SESSION["wa_current_user"]->prefs->date_format();
370 function user_date_display()
372 return $_SESSION["wa_current_user"]->prefs->date_display();
375 function user_date_sep()
377 return $_SESSION["wa_current_user"]->prefs->date_sep();
380 function user_tho_sep()
382 return $_SESSION["wa_current_user"]->prefs->tho_sep();
385 function user_dec_sep()
387 return $_SESSION["wa_current_user"]->prefs->dec_sep();
390 function user_theme()
392 return isset($_SESSION["wa_current_user"]) ?
393 $_SESSION["wa_current_user"]->prefs->get_theme() : 'default';
396 function user_pagesize()
398 return $_SESSION["wa_current_user"]->prefs->get_pagesize();
401 function user_hints()
403 return $_SESSION["wa_current_user"]->prefs->show_hints();
406 function user_print_profile()
408 return $_SESSION["wa_current_user"]->prefs->print_profile();
411 function user_rep_popup()
413 return $_SESSION["wa_current_user"]->prefs->rep_popup();
416 function user_query_size()
418 return $_SESSION["wa_current_user"]->prefs->query_size();
421 function user_graphic_links()
423 return $_SESSION["wa_current_user"]->prefs->graphic_links();
426 function sticky_doc_date()
428 return $_SESSION["wa_current_user"]->prefs->sticky_date();
431 function user_startup_tab()
433 return $_SESSION["wa_current_user"]->prefs->start_up_tab();
436 function set_user_prefs($prefs)
438 $_SESSION["wa_current_user"]->update_prefs($prefs);
441 function add_user_js_data() {
442 global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
444 $ts = $thoseps[user_tho_sep()];
445 $ds = $decseps[user_dec_sep()];
449 . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
450 . "loadtxt: '"._('Requesting data...')."',\n"
451 . "date: '".Today()."',\n" // server date
452 . "datesys: ".$date_system.",\n"
453 . "datefmt: ".user_date_format().",\n"
454 . "datesep: '".$dateseps[user_date_sep()]."',\n"
457 . "pdec : " . user_price_dec() . "}\n";
462 //--------------------------------------------------------------------------
464 function session_timeout()
466 $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
467 return $tout ? $tout : ini_get('session.gc_maxlifetime');
470 //-----------------------------------------------------------------------------
471 // Inserts $elements into $array at position $index.
472 // $elements is list of any objects
474 function array_insert(&$array, $index, $elements)
476 if (!is_array($elements)) $elements = array($elements);
478 $head = array_splice($array, 0, $index);
479 $array = array_merge($head, $elements, $array);
482 function array_remove(&$array, $index, $len=1)
484 array_splice($array, $index, $len);
487 function array_substitute(&$array, $index, $len, $elements)
489 array_splice($array, $index, $len);
490 array_insert($array, $index, $elements);
493 function array_append(&$array, $elements)
495 foreach($elements as $key => $el) {
503 // Search $needle in $haystack or in $haystack[][$valuekey]
504 // returns $needle found or null.
506 function array_search_value($needle, $haystack, $valuekey=null)
508 foreach($haystack as $key => $value) {
509 $val = isset($valuekey) ? @$value[$valuekey] : $value;
510 if ($needle == $val){
517 // Search $needle in $haystack or in $haystack[][$valuekey]
518 // returns array of keys of $haystack elements found
520 function array_search_keys($needle, $haystack, $valuekey=null)
524 foreach($haystack as $key => $value) {
525 $val = isset($valuekey) ? @$value[$valuekey] : $value;
526 if ($needle == $val){
533 // Find first (single) $needle in $haystack or in $haystack[][$valuekey]
534 // returns $haystack element found or null
536 function array_search_key($needle, $haystack, $valuekey=null)
538 $keys = array_search_keys($needle, $haystack, $valuekey);
542 function flush_dir($path, $wipe = false)
544 $dir = opendir($path);
547 while(false !== ($fname = readdir($dir))) {
548 if($fname=='.' || $fname=='..' || $fname=='CVS' || (!$wipe && $fname=='index.php')) continue;
549 if(is_dir($path.'/'.$fname)) {
550 flush_dir($path.'/'.$fname, $wipe);
551 if ($wipe) @rmdir($path.'/'.$fname);
553 @unlink($path.'/'.$fname);
557 Returns current path to company private folder.
558 (Current path can change after chdir).
560 function company_path($comp=null)
562 global $path_to_root, $comp_path;
565 $comp = user_company();
567 // if path is relative, set current path_to_root
568 return ($comp_path[0]=='.' ? $path_to_root.'/'.basename($comp_path) : $comp_path)