Recurrent Invoices: fixed buggy call to non existing function and payment terms type...
[fa-stable.git] / admin / db / users_db.inc
1 <?php
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
13 function add_user($user_id, $real_name, $password, $phone, $email, $role_id, 
14         $language, $profile, $rep_popup, $pos)
15 {
16         begin_transaction(__FUNCTION__, func_get_args());
17         $sql = "INSERT INTO ".TB_PREF."users (user_id, real_name, password"
18                 .", phone, email, role_id, language, pos, print_profile, rep_popup)
19                 VALUES (".db_escape($user_id).", 
20                 ".db_escape($real_name).", ".db_escape($password) .",".db_escape($phone)
21                 .",".db_escape($email).", ".db_escape($role_id).", ".db_escape($language)
22                 .", ".db_escape($pos).",".db_escape($profile).",".db_escape($rep_popup)
23                 ." )";
24
25         $result = db_query($sql, "could not add user for $user_id");
26         commit_transaction();
27         return $result;
28 }
29
30 //-----------------------------------------------------------------------------------------------
31
32 function update_user_password($id, $user_id, $password)
33 {
34         begin_transaction(__FUNCTION__, func_get_args());
35
36         $sql = "UPDATE ".TB_PREF."users SET password=".db_escape($password) . ",
37                 user_id = ".db_escape($user_id). " WHERE id=".db_escape($id);
38         $result = db_query($sql, "could not update user password for $user_id");
39
40         commit_transaction();
41         return $result;
42 }
43
44 //-----------------------------------------------------------------------------------------------
45
46 function update_user($id, $user_id, $real_name, $phone, $email, $role_id, 
47         $language, $profile, $rep_popup, $pos)
48 {
49         begin_transaction(__FUNCTION__, func_get_args());
50         $sql = "UPDATE ".TB_PREF."users SET real_name=".db_escape($real_name).
51         ", phone=".db_escape($phone).",
52                 email=".db_escape($email).",
53                 role_id=".db_escape($role_id).",
54                 language=".db_escape($language).",
55                 print_profile=".db_escape($profile).",
56                 rep_popup=".db_escape($rep_popup).",
57                 pos=".db_escape($pos).",
58                 user_id = " . db_escape($user_id)
59                 . " WHERE id=" . db_escape($id);
60         return db_query($sql, "could not update user for $user_id");
61         commit_transaction();
62 }
63
64 //-----------------------------------------------------------------------------------------------
65
66 function update_user_prefs($id, $prefs)
67 {
68         begin_transaction(__FUNCTION__, func_get_args());
69
70         $sql = "UPDATE ".TB_PREF."users SET ";
71         foreach($prefs as $name => $value) {
72                 $prefs[$name] = $name.'='. db_escape($value);
73         }
74         $sql .= implode(',', $prefs) . " WHERE id=".db_escape($id);
75
76         $result = db_query($sql, "could not update user display prefs for $id");
77
78         commit_transaction();
79         return $result;
80 }
81
82 //-----------------------------------------------------------------------------------------------
83
84
85 function get_users($all=false)
86 {
87         $sql = "SELECT u.*, r.role FROM ".TB_PREF."users u, ".TB_PREF."security_roles r
88                 WHERE u.role_id=r.id";
89         if (!$all) $sql .= " AND !u.inactive";
90         
91         return db_query($sql, "could not get users");
92 }
93
94 //-----------------------------------------------------------------------------------------------
95
96 function get_user($id)
97 {
98         $sql = "SELECT * FROM ".TB_PREF."users WHERE id=".db_escape($id);
99
100         $result = db_query($sql, "could not get user $id");
101
102         return db_fetch($result);
103 }
104
105 //-----------------------------------------------------------------------------------------------
106
107 function get_user_by_login($user_id)
108 {
109         $sql = "SELECT * FROM ".TB_PREF."users WHERE user_id=".db_escape($user_id);
110
111         $result = db_query($sql, "could not get user $user_id");
112
113         return db_fetch($result);
114 }
115
116 //-----------------------------------------------------------------------------------------------
117
118 function get_user_by_email($email)
119 {
120         $sql = "SELECT * FROM ".TB_PREF."users WHERE email=".db_escape($email);
121
122         $result = db_query($sql, "could not get user for email $email");
123
124         if (db_num_rows($result) != 1)
125                 return false;
126
127         return db_fetch($result);
128 }
129
130 //-----------------------------------------------------------------------------------------------
131
132 function delete_user($id)
133 {
134         begin_transaction(__FUNCTION__, func_get_args());
135
136         $sql="DELETE FROM ".TB_PREF."users WHERE id=".db_escape($id);
137
138         db_query($sql, "could not delete user $id");
139
140         commit_transaction();
141 }
142
143 //-----------------------------------------------------------------------------------------------
144
145 function get_user_auth($user_id, $password)
146 {
147
148         $sql = "SELECT * FROM ".TB_PREF."users WHERE user_id = ".db_escape($user_id)." AND"
149                 ." password=".db_escape($password);
150
151         return db_num_rows(db_query($sql, "could not get validate user login for $user_id")) != 0;
152 }
153
154 //-----------------------------------------------------------------------------------------------
155 //
156 //      Record user login. When logging is on also current FA state is recorded.
157 //
158 function update_user_visitdate($user_id, $src_version, $db_version, $extensions)
159 {
160         begin_transaction(__FUNCTION__, func_get_args());
161
162         $sql = "UPDATE ".TB_PREF."users SET last_visit_date='". date("Y-m-d H:i:s") ."'
163                 WHERE user_id=".db_escape($user_id);
164
165         db_query($sql, "could not update last visit date for user $user_id");
166
167         commit_transaction();
168 }
169
170 //-----------------------------------------------------------------------------------------------
171 function check_user_activity($id) 
172 {
173         $sql = "SELECT COUNT(*) FROM ".TB_PREF."audit_trail WHERE audit_trail.user="
174                 . db_escape($id);
175         $result = db_query($sql,"Cant check user activity");
176         $ret = db_fetch($result);
177
178         return $ret[0];
179 }
180
181 //-----------------------------------------------------------------------------------------------
182 function show_users_online()
183 {
184         global $db, $GetText, $SysPrefs;
185
186         if (!isset($SysPrefs->show_users_online) || $SysPrefs->show_users_online == 0 || !defined('TB_PREF') || 
187                 !isset($GetText) || !isset($db))
188                 return "";
189         $result = db_query("SHOW TABLES LIKE '".TB_PREF."useronline'"); 
190         if (db_num_rows($result) == 1)
191         {
192                 $timeoutseconds = 120;
193
194                 $timestamp=time();
195                 $timeout=$timestamp-$timeoutseconds;
196                 /*
197                 This will find out if user is from behind proxy server. 
198                 In that case, the script would count them all as 1 user.
199                 This function tryes to get real IP address.
200                 */
201                 if (isset($_SERVER['HTTP_CLIENT_IP'])) {
202                         $ip = $_SERVER['HTTP_CLIENT_IP'];
203                 }
204                 elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
205                         $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
206                 }
207                 elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
208                         $ip = $_SERVER['HTTP_X_FORWARDED'];
209                 }
210                 elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
211                         $ip = $_SERVER['HTTP_FORWARDED_FOR'];
212                 }
213                 elseif (isset($_SERVER['HTTP_FORWARDED'])) {
214                         $ip = $_SERVER['HTTP_FORWARDED'];
215                 }
216                 else {
217                         $ip = $_SERVER['REMOTE_ADDR'];
218                 }
219
220                 // Add user to database
221                 db_query("INSERT INTO ".TB_PREF."useronline (timestamp, ip, file) VALUES ('". $timestamp ."',". db_escape($ip) .",". db_escape($_SERVER['PHP_SELF']) .")");
222                 //Remove users that were not online within $timeoutseconds.
223                 db_query("DELETE FROM ".TB_PREF."useronline WHERE timestamp<". $timeout);
224
225                 // Select online users
226                 $result = db_query("SELECT DISTINCT ip FROM ".TB_PREF."useronline");
227                 $users = db_num_rows($result);
228         }
229         else
230                 $users = 1;
231         return "$users ".($users == 1 ? _("user online") : _("users online"));
232
233 }