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 //--------------------------------------------------------------------------
33 function current_user()
37 $this->loginname = $this->username = $this->name = "";
38 $this->company = isset($def_coy)? $def_coy : 0;
39 $this->logged = false;
41 $this->prefs = new user_prefs();
49 function set_company($company)
51 $this->company = $company;
54 function login($company, $loginname, $password)
56 $this->set_company($company);
57 $this->logged = false;
59 $Auth_Result = get_user_for_login($loginname, $password);
61 if (db_num_rows($Auth_Result) > 0)
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);
74 $this->last_act = time();
75 $this->timeout = session_timeout();
82 function check_user_access()
84 global $security_groups;
85 return is_array($security_groups[$this->access]);
88 function can_access_page($page_level)
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);
96 function get_db_connection()
98 global $db_connections;
100 $connection = $db_connections[$this->company];
102 //print_r($connection);
104 $db = mysql_connect($connection["host"] ,
105 $connection["dbuser"], $connection["dbpassword"]);
106 mysql_select_db($connection["dbname"],$db);
108 if (!defined('TB_PREF'))
109 define('TB_PREF', $connection["tbpref"]);
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);
124 $user = get_user($this->user);
125 $this->prefs = new user_prefs($user);
129 //--------------------------------------------------------------------------
131 function round2($number, $decimals=0)
133 $delta = ($number < 0 ? -.000001 : .000001);
134 return round($number+$delta, $decimals);
137 function number_format2($number, $decimals=0)
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);
149 function fallback_mode() {
150 return $_SESSION["wa_current_user"]->ui_mode==0;
153 function price_format($number) {
154 return number_format2($number,
155 $_SESSION["wa_current_user"]->prefs->price_dec());
158 function price_decimal_format($number, &$dec)
160 $dec = user_price_dec();
161 $str = strval($number);
162 $pos = strpos($str, '.');
165 $len = strlen(substr($str, $pos + 1));
169 return number_format2($number, $dec);
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);
178 function get_qty_dec($stock_id=null)
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();
188 //-------------------------------------------------------------------
189 function exrate_format($number) {
190 return number_format2($number,
191 $_SESSION["wa_current_user"]->prefs->exrate_dec());
194 function percent_format($number) {
195 return number_format2($number,
196 $_SESSION["wa_current_user"]->prefs->percent_dec());
199 function user_numeric($input) {
200 global $decseps, $thoseps;
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);
209 if (!is_numeric($num))
212 if ($num == (int)$num)
218 function user_company()
220 return $_SESSION["wa_current_user"]->company;
225 return $_SESSION["wa_current_user"]->pos;
228 function user_language()
230 return $_SESSION["wa_current_user"]->prefs->language();
233 function user_qty_dec()
235 return $_SESSION["wa_current_user"]->prefs->qty_dec();
238 function user_price_dec()
240 return $_SESSION["wa_current_user"]->prefs->price_dec();
243 function user_exrate_dec()
245 return $_SESSION["wa_current_user"]->prefs->exrate_dec();
248 function user_percent_dec()
250 return $_SESSION["wa_current_user"]->prefs->percent_dec();
253 function user_show_gl_info()
255 return $_SESSION["wa_current_user"]->prefs->show_gl_info();
258 function user_show_codes()
260 return $_SESSION["wa_current_user"]->prefs->show_codes();
263 function user_date_format()
265 return $_SESSION["wa_current_user"]->prefs->date_format();
268 function user_date_display()
270 return $_SESSION["wa_current_user"]->prefs->date_display();
273 function user_date_sep()
275 return $_SESSION["wa_current_user"]->prefs->date_sep();
278 function user_tho_sep()
280 return $_SESSION["wa_current_user"]->prefs->tho_sep();
283 function user_dec_sep()
285 return $_SESSION["wa_current_user"]->prefs->dec_sep();
288 function user_theme()
290 return $_SESSION["wa_current_user"]->prefs->get_theme();
293 function user_pagesize()
295 return $_SESSION["wa_current_user"]->prefs->get_pagesize();
298 function user_hints()
300 return $_SESSION["wa_current_user"]->prefs->show_hints();
303 function user_print_profile()
305 return $_SESSION["wa_current_user"]->prefs->print_profile();
308 function user_rep_popup()
310 return $_SESSION["wa_current_user"]->prefs->rep_popup();
313 function user_query_size()
315 return $_SESSION["wa_current_user"]->prefs->query_size();
318 function user_graphic_links()
320 return $_SESSION["wa_current_user"]->prefs->graphic_links();
323 function sticky_doc_date()
325 return $_SESSION["wa_current_user"]->prefs->sticky_date();
328 function user_startup_tab()
330 return $_SESSION["wa_current_user"]->prefs->start_up_tab();
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)
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);
343 function add_user_js_data() {
344 global $path_to_root, $thoseps, $decseps, $date_system, $dateseps;
346 $ts = $thoseps[user_tho_sep()];
347 $ds = $decseps[user_dec_sep()];
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"
359 . "pdec : " . user_price_dec() . "}\n";
364 //--------------------------------------------------------------------------
366 function session_timeout()
368 $tout = @get_company_pref('login_tout'); // mask warning for db ver. 2.2
369 return $tout ? $tout : ini_get('session.gc_maxlifetime');