-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.php
More file actions
352 lines (298 loc) · 9.19 KB
/
user.php
File metadata and controls
352 lines (298 loc) · 9.19 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
// -------------------------
// USER MANAGEMENT FUNCTIONS
// -------------------------
// LOGIN
// start a new login session with cleartext password
function login($username,$password)
{
$user_id = authenticate($username,$password,"CLEAR");
if ( $user_id ) return generate_key($user_id);
else return false;
}
// LOGIN_SECURE
// start a new login session with MD5 encrypted password
function login_secure($username,$password)
{
$user_id = authenticate($username,$password,"MD5");
if ( $user_id ) return generate_key($user_id);
else return false;
}
// USER_CREATE
// create a new 'real' user
function user_create($username,$password,$fname,$lname,$email,$phone)
{
// check for required fields
if ( !$username or !$password or !$fname or !$lname ) return false;
// clean up user data
$verified = true;
$email = ($email) ? $email : "";
$phone = phone_clean($phone);
$password_clear = $password;
$password = crypt_pass($password);
$password_type = "MD5";
// insert new user entry
global $db;
$statement = $db->prepare("INSERT INTO users (username, password, fname, lname, phone, verified, created, password_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$statement->bind_param("sssssiis",$username,$password,$fname,$lname,$phone,$verified,time(),$password_type);
if ( $statement->execute() ) {
$id = $statement->insert_id;
$statement->close();
// send confirmation email
email_welcome($id,$password_clear,$email);
// set user's email address
email_set($id,$email);
return $id;
}
else return false;
}
// USER_CREATE_SPECIAL
// create a new 'owned' user
function user_create_special($owner_id,$fname,$lname,$email,$phone)
{
// check for required fields
if ( !$fname or !$lname ) return false;
// clean up user data
$verified = 0;
$email = ($email) ? $email : "";
$phone = phone_clean($phone);
// insert new user entry
global $db;
$statement = $db->prepare("INSERT INTO users (owner_id,fname,lname,email,phone,created,verified) VALUES (?,?,?,?,?,?,?)");
$statement->bind_param("issssi",$owner_id,$fname,$lname,$email,$phone,time(),$verified);
if ( $statement->execute() ) {
$id = $statement->insert_id;
$statement->close();
// send confirmation email
email_invite($owner_id,$id,$password,$email);
return $id;
}
else return false;
}
// USER_UPDATE
// update user details
function user_update($user_id,$username,$pass,$new_pass,$fname,$lname,$email,$phone,$photo="")
{
// get user details
$user = user_details($user_id);
// check for user validity
global $remote_user;
if ( $remote_user != $user_id and $remote_user != $user["owner_id"] ) return 1;
// set new user details
global $db;
$statement = null;
$pass_type = "MD5";
if ( strlen($new_pass) < 1) {
// no password update
$statement = $db->prepare("UPDATE users SET fname = ?, lname = ?, phone = ?, password_type = ?, photo = ? WHERE user_id = ?");
$statement->bind_param("sssssi",$fname,$lname,$phone,$pass_type,$photo,$user_id);
}
else {
// encrypt password
$new_pass = crypt_pass($new_pass);
// update password
$statement = $db->prepare("UPDATE users SET password = ?, fname = ?, lname = ?, phone = ?, password_type = ?, photo = ? WHERE user_id = ?");
$statement->bind_param("ssssssi",$new_pass,$fname,$lname,$phone,$pass_type,$photo,$user_id);
}
if ( $statement->execute() ) {
$statement->close();
// set user's email address
email_set($user_id,$email);
return 0;
}
else return 2;
}
// USER_DETAILS
// get all details for a user
function user_details($user_id)
{
// get user details
global $db;
$statement = $db->prepare("SELECT user_id, username, fname, lname, email, phone, photo, owner_id, email_valid FROM users WHERE user_id = ?");
$statement->bind_param("i",$user_id);
if ( $statement->execute() ) {
$d = array();
$statement->bind_result($d["user_id"],$d["username"],$d["fname"],$d["lname"],$d["email"],$d["phone"],$d["photo"],$d["owner_id"],$d["email_valid"]);
$statement->fetch();
$statement->close();
return $d;
}
else return false;
}
// USER_SEARCH
// search for an existing user, or create a new one if needed
function user_search($term)
{
// prepare search term
$term = "%".$term."%";
// search for users that match the term
global $db;
$statement = $db->prepare("SELECT user_id FROM users WHERE username LIKE ? OR fname LIKE ? OR lname LIKE ? OR email LIKE ? OR phone LIKE ?");
$statement->bind_param("sssss",$term,$term,$term,$term,$term);
if ( $statement->execute() ) {
$list = array();
$statement->bind_result($user_id);
// retrieve ids for each found user
while ( $statement->fetch() ) {
$list[] = $user_id;
}
$statement->close();
// retrieve details for each user
$results = array();
foreach ( $list as $user ) {
$results[] = user_details($user);
}
return $results;
}
else return false;
}
// USER_SEARCH_FIELDS
// allow searhing of specific fields
function user_search_fields($username,$email,$phone,$name)
{
// don't allow all blank search
if ( !$username and !$email and !$phone and !$name ) return false;
// prepare search terms
$username = "%".$username."%";
$email = "%".$email."%";
$phone = "%".$phone."%";
$name = "%".$name."%";
global $db;
$statement = $db->prepare("SELECT user_id FROM search WHERE username LIKE ? AND email LIKE ? AND phone LIKE ? AND name LIKE ?");
$statement->bind_param("ssss",$username,$email,$phone,$name);
if ( $statement->execute() ) {
$list = array();
$statement->bind_result($user_id);
// retrieve ids for each found user
while ( $statement->fetch() ) {
$list[] = $user_id;
}
$statement->close();
// retrieve details for each user
$results = array();
foreach ( $list as $user ) {
$results[] = user_details($user);
}
return $results;
}
else return false;
}
// ---------------------------
// FRIEND MANAGEMENT FUNCTIONS
// ---------------------------
// FRIEND_ADD
// add a friend relationship
function friend_add($u1,$u2)
{
// check for self friending
if ( $u1 == $u2 ) return 3;
// check for an existing relationship
global $db;
$statement = $db->prepare("SELECT * FROM friends WHERE (friend1 = ? AND friend2 = ?) OR (friend1 = ? AND friend2 = ?)");
$statement->bind_param("iiii",$u2,$u1,$u1,$u2);
if ( $statement->execute() ) {
$statement->store_result();
if ( $statement->num_rows > 0 ) {
// relationship already exists
$statement->close();
return 1;
}
}
$statement->close();
// insert a new relationship
$time = time();
$statement = $db->prepare("INSERT INTO friends (friend1, friend2, confirmed, timestamp) VALUES (?, ?, 1, ?)");
$statement->bind_param("iii",$u1,$u2,$time);
if ( $statement->execute() ) {
$statement->close();
// send confirmation email
email_friend_confirm($u1,$u2);
return 0;
}
else {
$statement->close();
return 2;
}
}
// FRIEND_CONFIRM
// confirm a friend relationship
function friend_confirm($u1,$u2)
{
global $db;
$statement = $db->prepare("UPDATE friends SET confirmed = 2 WHERE friend1 = ? AND friend2 = ?");
$statement->bind_param("ii",$u2,$u1);
if ( $statement->execute() ) {
$statement->close();
return true;
}
else return false;
}
// FRIEND_REMOVE
// remove a friend
function friend_remove($u1,$u2)
{
global $db;
$statement = $db->prepare("DELETE FROM friends WHERE (friend1 = ? AND friend2 = ?) OR (friend1 = ? AND friend2 = ?)");
$statement->bind_param("iiii",$u1,$u2,$u2,$u1);
if ( $statement->execute() ) {
$statement->close();
return true;
}
else return false;
}
// FRIEND_LIST
// list all of a user's friends (with details)
function friend_list($owner_id)
{
global $db;
$statement = $db->prepare("SELECT fname, lname, friend1, friend2, confirmed, timestamp FROM users, friends WHERE (user_id = friend1 AND friend2 = ?) OR (user_id = friend2 AND friend1 = ?)");
$statement->bind_param("ii",$owner_id,$owner_id);
if ( $statement->execute() ) {
$statement->bind_result($fname,$lname,$friend1,$friend2,$confirmed,$timestamp);
// set information for each user
$list = array();
while ( $statement->fetch() ) {
$friendship = array();
$friendship["timestamp"] = $timestamp;
// set friend information
$friendship["fname"] = $fname;
$friendship["lname"] = $lname;
// set friend_id
if ( $owner_id == $friend1 ) $friendship["user_id"] = $friend2;
else $friendship["user_id"] = $friend1;
// set confirmation status
if ( $confirmed == 0 ) {
if ( $owner_id == $friend1 ) $friendship["confirmed"] = 0;
else $friendship["confirmed"] = 1;
}
elseif ( $confirmed == 1 ) {
if ( $owner_id == $friend2 ) $friendship["confirmed"] = 0;
else $friendship["confirmed"] = 1;
}
else $friendship["confirmed"] = 2;
$list[] = $friendship;
}
$statement->close();
return $list;
}
else return false;
}
// FRIEND_CHECK
// check friend relationship status
function friend_check($user_id,$friend_id)
{
// 0,1 - needs confirmation, 2-confirmed, 3-deleted, 4-not friends
$status = 4;
global $db;
$statement = $db->prepare("SELECT confirmed FROM friends WHERE (friend1 = ? AND friend2 = ?) OR (friend1 = ? AND friend2 = ?)");
$statement->bind_param("iiii",$user_id,$friend_id,$friend_id,$user_id);
if ( $statement->execute() ) {
$statement->bind_result($confirmed);
if ( $statement->fetch() ) {
$status = $confirmed;
}
}
$statement->close();
return $status;
}
?>