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 get_db_connection($id=-1)
158 global $db_connections;
160 $this->cur_con = $id == -1 ? $this->company : $id;
161 $connection = $db_connections[$this->cur_con];
163 //print_r($connection);
165 $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
166 mysql_select_db($connection["dbname"], $db);
171 function update_prefs($prefs)
173 global $allow_demo_mode;
175 if(!$allow_demo_mode) {
176 update_user_prefs($this->user, $prefs);
179 $this->prefs = new user_prefs(get_user($this->user));
183 //--------------------------------------------------------------------------
185 function round2($number, $decimals=0)
187 $delta = ($number < 0 ? -.0000000001 : .0000000001);
188 return round($number+$delta, $decimals);
192 Returns number formatted according to user setup and using $decimals digits after dot
193 (defualt is 0). When $decimals is set to 'max' maximum available precision is used
194 (decimals depend on value) and trailing zeros are trimmed.
196 function number_format2($number, $decimals=0)
198 global $thoseps, $decseps;
199 $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
200 $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
201 //return number_format($number, $decimals, $dsep, $tsep);
202 if($decimals==='max')
203 $dec = 15 - floor(log10(abs($number)));
205 $delta = ($number < 0 ? -.0000000001 : .0000000001);
210 $num = number_format($number, $dec, $dsep, $tsep);
212 return $decimals==='max' ? rtrim($num, '0') : $num;
218 function fallback_mode() {
219 return $_SESSION["wa_current_user"]->ui_mode==0;
222 function price_format($number) {
223 return number_format2($number,
224 $_SESSION["wa_current_user"]->prefs->price_dec());
227 function price_decimal_format($number, &$dec)
229 $dec = user_price_dec();
230 $str = strval($number);
231 $pos = strpos($str, '.');
234 $len = strlen(substr($str, $pos + 1));
238 return number_format2($number, $dec);
240 // function money_format doesn't exist in OS Win.
241 if (!function_exists('money_format'))
243 function money_format($format, $number)
245 return price_format($number);
249 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
250 //--------------------------------------------------------------------
251 function qty_format($number, $stock_id=null, &$dec) {
252 $dec = get_qty_dec($stock_id);
253 return number_format2($number, $dec);
257 function get_qty_dec($stock_id=null)
259 global $path_to_root;
260 include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
261 if ($stock_id != null)
262 $dec = get_unit_dec($stock_id);
263 if ($stock_id == null || $dec == -1 || $dec == null)
264 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
267 //-------------------------------------------------------------------
269 // Maximum precision format. Strips trailing unsignificant digits.
271 function maxprec_format($number) {
272 return number_format2($number, 'max');
275 function exrate_format($number) {
276 return number_format2($number,
277 $_SESSION["wa_current_user"]->prefs->exrate_dec());
280 function percent_format($number) {
281 return number_format2($number,
282 $_SESSION["wa_current_user"]->prefs->percent_dec());
285 function user_numeric($input) {
286 global $decseps, $thoseps;
289 $sep = $thoseps[user_tho_sep()];
291 $num = str_replace( $sep, '', $num);
293 $sep = $decseps[user_dec_sep()];
295 $num = str_replace( $sep, '.', $num);
297 if (!is_numeric($num))
300 if ($num == (int)$num)
306 function user_company()
310 return isset($_SESSION["wa_current_user"]) ? $_SESSION["wa_current_user"]->company : $def_coy;
315 return $_SESSION["wa_current_user"]->pos;
318 function user_language()
320 return $_SESSION["wa_current_user"]->prefs->language();
323 function user_qty_dec()
325 return $_SESSION["wa_current_user"]->prefs->qty_dec();
328 function user_price_dec()
330 return $_SESSION["wa_current_user"]->prefs->price_dec();
333 function user_exrate_dec()
335 return $_SESSION["wa_current_user"]->prefs->exrate_dec();
338 function user_percent_dec()
340 return $_SESSION["wa_current_user"]->prefs->percent_dec();
343 function user_show_gl_info()
345 return $_SESSION["wa_current_user"]->prefs->show_gl_info();
348 function user_show_codes()
350 return $_SESSION["wa_current_user"]->prefs->show_codes();
353 function user_date_format()
355 return $_SESSION["wa_current_user"]->prefs->date_format();
358 function user_date_display()
360 return $_SESSION["wa_current_user"]->prefs->date_display();
363 function user_date_sep()
365 return $_SESSION["wa_current_user"]->prefs->date_sep();
368 function user_tho_sep()
370 return $_SESSION["wa_current_user"]->prefs->tho_sep();
373 function user_dec_sep()
375 return $_SESSION["wa_current_user"]->prefs->dec_sep();
378 function user_theme()
380 return isset($_SESSION["wa_current_user"]) ?
381 $_SESSION["wa_current_user"]->prefs->get_theme() : 'default';
384 function user_pagesize()
386 return $_SESSION["wa_current_user"]->prefs->get_pagesize();
389 function user_hints()
391 return $_SESSION["wa_current_user"]->prefs->show_hints();
394 function user_print_profile()
396 return $_SESSION["wa_current_user"]->prefs->print_profile();
399 function user_rep_popup()
401 return $_SESSION["wa_current_user"]->prefs->rep_popup();
404 function user_query_size()
406 return $_SESSION["wa_current_user"]->prefs->query_size();
409 function user_graphic_links()
411 return $_SESSION["wa_current_user"]->prefs->graphic_links();
414 function sticky_doc_date()
416 return $_SESSION["wa_current_user"]->prefs->sticky_date();
419 function user_startup_tab()
421 return $_SESSION["wa_current_user"]->prefs->start_up_tab();
424 function set_user_prefs($prefs)
426 $_SESSION["wa_current_user"]->update_prefs($prefs);
429 function add_user_js_data() {
430 global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
432 $ts = $thoseps[user_tho_sep()];
433 $ds = $decseps[user_dec_sep()];
437 . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
438 . "loadtxt: '"._('Requesting data...')."',\n"
439 . "date: '".Today()."',\n" // server date
440 . "datesys: ".$date_system.",\n"
441 . "datefmt: ".user_date_format().",\n"
442 . "datesep: '".$dateseps[user_date_sep()]."',\n"
445 . "pdec : " . user_price_dec() . "}\n";
450 //--------------------------------------------------------------------------
452 function session_timeout()
454 $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
455 return $tout ? $tout : ini_get('session.gc_maxlifetime');
458 //-----------------------------------------------------------------------------
459 // Inserts $elements into $array at position $index.
460 // $elements is list of any objects
462 function array_insert(&$array, $index, $elements)
464 if (!is_array($elements)) $elements = array($elements);
466 $head = array_splice($array, 0, $index);
467 $array = array_merge($head, $elements, $array);
470 function array_remove(&$array, $index, $len=1)
472 array_splice($array, $index, $len);
475 function array_substitute(&$array, $index, $len, $elements)
477 array_splice($array, $index, $len);
478 array_insert($array, $index, $elements);
481 function array_append(&$array, $elements)
483 foreach($elements as $key => $el) {
491 // Search $needle in $haystack or in $haystack[][$valuekey]
492 // returns $needle found or null.
494 function array_search_value($needle, $haystack, $valuekey=null)
496 foreach($haystack as $key => $value) {
497 $val = isset($valuekey) ? @$value[$valuekey] : $value;
498 if ($needle == $val){
505 // Search $needle in $haystack or in $haystack[][$valuekey]
506 // returns array of keys of $haystack elements found
508 function array_search_keys($needle, $haystack, $valuekey=null)
512 foreach($haystack as $key => $value) {
513 $val = isset($valuekey) ? @$value[$valuekey] : $value;
514 if ($needle == $val){
521 // Find first (single) $needle in $haystack or in $haystack[][$valuekey]
522 // returns $haystack element found or null
524 function array_search_key($needle, $haystack, $valuekey=null)
526 $keys = array_search_keys($needle, $haystack, $valuekey);
530 function flush_dir($path, $wipe = false)
532 $dir = opendir($path);
533 while(false !== ($fname = readdir($dir))) {
534 if($fname=='.' || $fname=='..' || $fname=='CVS' || (!$wipe && $fname=='index.php')) continue;
535 if(is_dir($path.'/'.$fname)) {
536 flush_dir($path.'/'.$fname, $wipe);
537 if ($wipe) @rmdir($path.'/'.$fname);
539 @unlink($path.'/'.$fname);
543 Returns current path to company private folder.
544 (Current path can change after chdir).
546 function company_path($comp=null)
548 global $path_to_root, $comp_path;
551 $comp = user_company();
553 // if path is relative, set current path_to_root
554 return ($comp_path[0]=='.' ? $path_to_root.'/'.basename($comp_path) : $comp_path)