-
Notifications
You must be signed in to change notification settings - Fork 16
/
lib_users.php
324 lines (210 loc) · 6.41 KB
/
lib_users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
#
# $Id$
#
# This file has been copied from the Citytracking fork of flamework.
# It has not been forked, or cloned or otherwise jiggery-poked, but
# copied: https://github.com/Citytracking/flamework (20101208/straup)
#################################################################
#
# create a user record. the fields pass in $user
# ARE NOT ESCAPED.
#
function users_create_user($user){
#
# set up some extra fields first
#
loadlib('random');
$user['password'] = login_encrypt_password($user['password']);
$user['created'] = time();
$user['conf_code'] = random_string(24);
$user['cluster_id'] = users_assign_cluster_id();
#
# now create the escaped version
#
$hash = array();
foreach ($user as $k => $v){
$hash[$k] = AddSlashes($v);
}
$rsp = db_insert('Users', $hash);
if (!$rsp['ok']){
return null;
}
#
# cache the unescaped version
#
$user['id'] = $rsp['insert_id'];
$cache_key = "user_{$user['id']}";
cache_set($cache_key, $user, 'cache locally');
return $user;
}
#################################################################
#
# update multiple fields on an user record. the hash passed
# in $update IS NOT ESCAPED.
#
function users_update_user(&$user, $update){
foreach ($update as $k => $v){
$update[$k] = AddSlashes($v);
}
$rsp = db_update('Users', $update, "id=$user[id]");
if (!$rsp['ok']){
return $rsp;
}
$cache_key = "user_{$user['id']}";
cache_unset($cache_key);
return $rsp;
}
#################################################################
function users_update_password(&$user, $new_password){
$enc_password = login_encrypt_password($new_password);
return users_update_user($user, array(
'password' => AddSlashes($enc_password),
));
}
#################################################################
function users_delete_user(&$user){
# rely on mysql to enforce a unique key
# on (email, deleted)
$new_email = "{$user['email']}.DELETED";
$rsp = users_update_user($user, array(
'deleted' => time(),
'email' => AddSlashes($new_email),
# reset the password here ?
));
if (! $rsp['ok']){
return $rsp;
}
# Hey look! A Dotspotting-ism !
users_reload_user($user);
$sheets_rsp = sheets_delete_sheets_for_user($user);
return $rsp;
}
#################################################################
function users_reload_user(&$user){
$user = users_get_by_id($user['id']);
}
#################################################################
function users_get_by_id($id){
$cache_key = "user_{$id}";
$cache = cache_get($cache_key);
if ($cache['ok']){
return $cache['data'];
}
$user = db_single(db_fetch("SELECT * FROM Users WHERE id=".intval($id)));
cache_set($cache_key, $user, 'cache locally');
return $user;
}
#################################################################
function users_get_by_email($email){
$enc_email = AddSlashes($email);
return db_single(db_fetch("SELECT * FROM Users WHERE email='{$enc_email}'"));
}
#################################################################
function users_get_by_login($email, $password){
$user = users_get_by_email($email);
if (!$user){
return null;
}
if ($user['deleted']){
return null;
}
if ($user['password'] != login_encrypt_password($password)){
return null;
}
return $user;
}
#################################################################
function users_is_email_taken($email){
$enc_email = AddSlashes($email);
$row = db_single(db_fetch("SELECT id FROM Users WHERE email='{$enc_email}' AND deleted=0"));
return $row['id'] ? 1 : 0;
}
#################################################################
function users_is_username_taken($username){
$enc_username = AddSlashes($username);
$row = db_single(db_fetch("SELECT id FROM Users WHERE username='{$enc_username}' AND deleted=0"));
return $row['id'] ? 1 : 0;
}
#################################################################
function users_get_by_password_reset_code($code){
$enc_code = AddSlashes($code);
$row = db_single(db_fetch("SELECT * FROM UsersPasswordReset WHERE reset_code='{$enc_code}'"));
if (!$row){
return null;
}
return users_get_by_id($row['user_id']);
}
#################################################################
function users_purge_password_reset_codes(&$user){
$rsp = db_write("DELETE FROM UsersPasswordReset WHERE user_id=$user[id]");
return $rsp['ok'];
}
#################################################################
function users_send_password_reset_code(&$user){
$code = users_generate_password_reset_code($user);
if (!$code) return 0;
$GLOBALS['smarty']->assign('code', $code);
$args = array(
'to_email' => $user['email'],
'template' => 'email_password_reset.txt',
);
if (isset($GLOBALS['cfg']['password_retrieval_from_email'])){
$args['from_email'] = $GLOBALS['cfg']['password_retrieval_from_email'];
}
if (isset($GLOBALS['cfg']['password_retrieval_from_name'])){
$args['from_name'] = $GLOBALS['cfg']['password_retrieval_from_name'];
}
email_send($args);
return 1;
}
#################################################################
function users_generate_password_reset_code(&$user){
loadlib('random');
users_purge_password_reset_codes($user);
$code = '';
while (!$code){
$code = random_string(32);
$enc_code = AddSlashes($code);
if (db_single(db_fetch("SELECT 1 FROM UsersPasswordReset WHERE reset_code='{$enc_code}'"))){
$code = '';
}
break;
}
$rsp = db_insert('UsersPasswordReset', array(
'user_id' => $user['id'],
'reset_code' => $enc_code,
'created' => time(),
));
if (!$rsp['ok']){
return null;
}
return $code;
}
#################################################################
function users_assign_cluster_id(){
if ($GLOBALS['cfg']['db_enable_poormans_federation']){
return 1;
}
# TO DO: an actual cluster ID if federated
return 1;
}
#################################################################
function users_ensure_valid_user_from_url($method=''){
if (strtolower($method) == 'post'){
$user_id = post_int64('user_id');
}
else {
$user_id = get_int64('user_id');
}
if (! $user_id){
error_404();
}
$user = users_get_by_id($user_id);
if ((! $user) || ($user['deleted'])){
error_404();
}
return $user;
}
#################################################################
?>