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");
14 //--------------------------------------------------------------------------
27 var $role_set = false;
34 function current_user()
38 $this->loginname = $this->username = $this->name = "";
39 $this->company = isset($def_coy)? $def_coy : 0;
40 $this->logged = false;
42 $this->prefs = new user_prefs();
50 function set_company($company)
52 $this->company = $company;
55 function login($company, $loginname, $password)
57 global $security_areas, $security_groups, $security_headings;
59 $this->set_company($company);
60 $this->logged = false;
62 $Auth_Result = get_user_for_login($loginname, $password);
64 if (db_num_rows($Auth_Result) > 0)
66 $myrow = db_fetch($Auth_Result);
67 if (! @$myrow["inactive"]) {
68 if (isset($myrow["full_access"])) {
70 // db was not yet upgraded after source update to v.2.2
71 // give enough access for admin user to continue upgrade
72 if (!isset($security_groups) || !isset($security_headings)) {
73 echo "<center><br><br><font size='5' color='red'><b>";
74 echo _('Before software upgrade you have to include old $security_groups and $security_headings arrays from old config.php file to the new one.');
75 echo "</b></font><br><br></center>";
78 $this->access = $myrow["full_access"];
79 if (in_array(20, $security_groups[$this->access]))
80 // temporary access for admin users
81 $this->role_set[] = $security_areas['SA_SOFTWAREUPGRADE'][0];
83 echo "<center><br><br><font size='5' color='red'><b>";
84 echo _('System is available for site admin only until full database upgrade');
85 echo "</b></font><br><br></center>";
89 $this->role_set = array();
90 $this->access = $myrow["role_id"];
91 // store area codes available for current user role
92 $role = get_security_role($this->access);
95 foreach( $role['areas'] as $code )
96 // filter only area codes for enabled security sections
97 if (in_array($code&~0xff, $role['sections']))
98 $this->role_set[] = $code;
100 $this->name = $myrow["real_name"];
101 $this->pos = $myrow["pos"];
102 $this->loginname = $loginname;
103 $this->username = $this->loginname;
104 $this->prefs = new user_prefs($myrow);
105 $this->user = @$myrow["id"];
106 update_user_visitdate($this->username);
107 $this->logged = true;
108 $this->last_act = time();
109 $this->timeout = session_timeout();
112 return $this->logged;
115 function check_user_access()
117 global $security_groups;
118 if (isset($security_groups)) {
119 // notification after upgrade from pre-2.2 version
120 // display_notification(_("After database upgrade don't forget to remove \$security_groups and \$security_headings from config.php file!"));
121 return is_array(@$security_groups[$this->access]);
123 return is_array($this->role_set);
126 function can_access_page($page_level)
128 global $security_groups, $security_areas;
130 if (isset($security_groups)) {
131 return $this->company == 0 &&
132 in_array(20, $security_groups[$this->access]);
135 if ($page_level === 'SA_OPEN')
137 $code = $security_areas[$page_level][0];
138 // only first registered company has site admin privileges
139 return $code && in_array($code, $this->role_set)
140 && ($this->company == 0 || ($code&~0xff != SS_SADMIN));
143 function get_db_connection()
145 global $db_connections;
147 $connection = $db_connections[$this->company];
149 //print_r($connection);
151 $db = mysql_connect($connection["host"] ,
152 $connection["dbuser"], $connection["dbpassword"]);
153 mysql_select_db($connection["dbname"],$db);
155 if (!defined('TB_PREF'))
156 define('TB_PREF', $connection["tbpref"]);
161 function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec,
162 $showgl, $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep,
163 $theme, $pagesize, $show_hints, $profile, $rep_popup, $query_size,
164 $graphic_links, $lang, $stickydate, $startup_tab) {
165 update_user_display_prefs($this->user, $price_dec,
166 $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
167 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize,
168 $show_hints, $profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate, $startup_tab);
171 $user = get_user($this->user);
172 $this->prefs = new user_prefs($user);
176 //--------------------------------------------------------------------------
178 function round2($number, $decimals=0)
180 $delta = ($number < 0 ? -.000001 : .000001);
181 return round($number+$delta, $decimals);
184 function number_format2($number, $decimals=0)
186 global $thoseps, $decseps;
187 $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
188 $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
189 //return number_format($number, $decimals, $dsep, $tsep);
190 $delta = ($number < 0 ? -.000001 : .000001);
191 return number_format($number+$delta, $decimals, $dsep, $tsep);
196 function fallback_mode() {
197 return $_SESSION["wa_current_user"]->ui_mode==0;
200 function price_format($number) {
201 return number_format2($number,
202 $_SESSION["wa_current_user"]->prefs->price_dec());
205 function price_decimal_format($number, &$dec)
207 $dec = user_price_dec();
208 $str = strval($number);
209 $pos = strpos($str, '.');
212 $len = strlen(substr($str, $pos + 1));
216 return number_format2($number, $dec);
218 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
219 //--------------------------------------------------------------------
220 function qty_format($number, $stock_id=null, &$dec) {
221 $dec = get_qty_dec($stock_id);
222 return number_format2($number, $dec);
225 function get_qty_dec($stock_id=null)
227 global $path_to_root;
228 include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
229 if ($stock_id != null)
230 $dec = get_unit_dec($stock_id);
231 if ($stock_id == null || $dec == -1 || $dec == null)
232 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
235 //-------------------------------------------------------------------
236 function exrate_format($number) {
237 return number_format2($number,
238 $_SESSION["wa_current_user"]->prefs->exrate_dec());
241 function percent_format($number) {
242 return number_format2($number,
243 $_SESSION["wa_current_user"]->prefs->percent_dec());
246 function user_numeric($input) {
247 global $decseps, $thoseps;
250 $sep = $thoseps[user_tho_sep()];
251 if($sep!='') $num = str_replace( $sep, '', $num);
252 str_replace($sep, '', $num);
253 $sep = $decseps[user_dec_sep()];
254 if($sep!='.') $num = str_replace( $sep, '.', $num);
256 if (!is_numeric($num))
259 if ($num == (int)$num)
265 function user_company()
267 return $_SESSION["wa_current_user"]->company;
272 return $_SESSION["wa_current_user"]->pos;
275 function user_language()
277 return $_SESSION["wa_current_user"]->prefs->language();
280 function user_qty_dec()
282 return $_SESSION["wa_current_user"]->prefs->qty_dec();
285 function user_price_dec()
287 return $_SESSION["wa_current_user"]->prefs->price_dec();
290 function user_exrate_dec()
292 return $_SESSION["wa_current_user"]->prefs->exrate_dec();
295 function user_percent_dec()
297 return $_SESSION["wa_current_user"]->prefs->percent_dec();
300 function user_show_gl_info()
302 return $_SESSION["wa_current_user"]->prefs->show_gl_info();
305 function user_show_codes()
307 return $_SESSION["wa_current_user"]->prefs->show_codes();
310 function user_date_format()
312 return $_SESSION["wa_current_user"]->prefs->date_format();
315 function user_date_display()
317 return $_SESSION["wa_current_user"]->prefs->date_display();
320 function user_date_sep()
322 return $_SESSION["wa_current_user"]->prefs->date_sep();
325 function user_tho_sep()
327 return $_SESSION["wa_current_user"]->prefs->tho_sep();
330 function user_dec_sep()
332 return $_SESSION["wa_current_user"]->prefs->dec_sep();
335 function user_theme()
337 return $_SESSION["wa_current_user"]->prefs->get_theme();
340 function user_pagesize()
342 return $_SESSION["wa_current_user"]->prefs->get_pagesize();
345 function user_hints()
347 return $_SESSION["wa_current_user"]->prefs->show_hints();
350 function user_print_profile()
352 return $_SESSION["wa_current_user"]->prefs->print_profile();
355 function user_rep_popup()
357 return $_SESSION["wa_current_user"]->prefs->rep_popup();
360 function user_query_size()
362 return $_SESSION["wa_current_user"]->prefs->query_size();
365 function user_graphic_links()
367 return $_SESSION["wa_current_user"]->prefs->graphic_links();
370 function sticky_doc_date()
372 return $_SESSION["wa_current_user"]->prefs->sticky_date();
375 function user_startup_tab()
377 return $_SESSION["wa_current_user"]->prefs->start_up_tab();
380 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
381 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
382 $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate, $startup_tab)
385 $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
386 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
387 $print_profile, $rep_popup, $query_size, $graphic_links, $lang, $stickydate, $startup_tab);
390 function add_user_js_data() {
391 global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
393 $ts = $thoseps[user_tho_sep()];
394 $ds = $decseps[user_dec_sep()];
398 . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
399 . "loadtxt: '"._('Requesting data...')."',\n"
400 . "date: '".Today()."',\n" // server date
401 . "datesys: ".$date_system.",\n"
402 . "datefmt: ".user_date_format().",\n"
403 . "datesep: '".$dateseps[user_date_sep()]."',\n"
406 . "pdec : " . user_price_dec() . "}\n";
411 //--------------------------------------------------------------------------
413 function session_timeout()
415 $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
416 return $tout ? $tout : ini_get('session.gc_maxlifetime');