/
user.class.php
490 lines (457 loc) · 14.1 KB
/
user.class.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/*
* ---------------------------------------------------------------
* User functions
* ---------------------------------------------------------------
*
* This page Is all the user-based actions such as login, logout,
* session verification, etc. This needs to be included on any page.
*
* TODO:
*
* -Clean up functions with too many nested if statements
* -General code clean up
* -Maybe make exception codes mean something.
*
*/
/**
* Class user
*/
class user extends database
{
/* Login Functions */
// Check if the cookie is valid. If valid, set as session. This prevents deleted users with a cookie from being able to log in the site.
/**
* @param $email
* @return bool
* @throws Exception
*/
public function session_check($email)
{
if (empty($_SESSION['authenticated']) && $_COOKIE['rememberMe'] === true && !empty($email)) {
$_SESSION['authenticated'] = $email;
}
$params = [$email];
$sql = "SELECT * FROM $this->user_table WHERE email = $1";
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:session_check';
$code = 1;
throw new Exception($message, $code);
}
$row_cnt = pg_num_rows($result);
return $row_cnt < 1 ? false : true;
}
// Do stuff, I guess? right now only checks if logout was used. This needs to be expanded
/**
* @param $email
* @param null $action
* @return bool
*/
public function session_authenticate($action = null)
{
if ($action == 'logout') {
session_destroy();
setcookie('rememberMe', null, -1, '/');
setcookie('email', null, -1, '/');
return false;
}
return true;
}
// Process the login, set the session and cookies (if desired)
/**
* @param $email
* @param $password
* @return bool|string
*/
public function login($email, $password)
{
session_regenerate_id();
$params = [$email];
$sql = "SELECT * FROM $this->user_table WHERE email = $1";
$result = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
if ($result === false) {
return 'Account does not exist. Please register first.';
}
$hash = $result['password'];
$verified = $result['verified'];
$apikey = $result['api_key'];
if ($verified === '1') {
if (password_verify($password, $hash)) {
$_SESSION['authenticated'] = $email;
$_SESSION['api_key'] = $apikey;
if (isset($_POST['rememberMe']) && $_POST['rememberMe'] === 'true') {
setcookie('rememberMe', true, time() + 31000000);
setcookie('email', $email, time() + 31000000);
setcookie('api_key', $apikey, time() + 31000000);
}
return true;
} else {
$status = 'Login failed.';
}
} else {
$status = 'Account not verified. Please check your email.';
}
return $status;
}
/**
* @param $email
* @return array|bool
*/
// these functions will pull the user database for editing on the settings page, if the user is an admin
public function admindata($email)
{
$admincheck = $this->admincheck($email);
if ($admincheck === true) {
$sql = "SELECT email, verified, stream_key, api_key, channel_name, display_name FROM $this->user_table";
$result = pg_query($this->link, $sql);
$array = [];
while ($row = pg_fetch_assoc($result)) {
$array[] = $row;
}
return $array;
} else {
return false;
}
}
/**
* @param $email
* @return bool
*/
public function admincheck($email)
{
$params = [$email];
$sql = "SELECT is_admin FROM $this->user_table WHERE email = $1";
$result = pg_fetch_object(pg_query_params($this->link, $sql, $params));
if ($result->is_admin === 't') {
return true;
} else {
return false;
}
}
// create a new account and send registration verification email
/**
* @param $email
* @param $password
* @param $displayname
* @return bool|string
* @throws Exception
* @internal param $furl
*/
public function register($email, $password, $displayname)
{
$emailcheck = $this->emailcheck($email);
if ($emailcheck === true) {
return 'Account already exists!';
}
$dncheck = $this->dncheck($email, $displayname);
if ($dncheck === true) {
return 'Display name already exists!';
}
$authcode = bin2hex(random_bytes(32));
$hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO $this->user_table (email, password, auth_code, verified, channel_name, channel_title, display_name, profile_img, is_admin, offline_image) VALUES ($1, $2, $7, 0, $3, $4, $5, $6, false, $8)";
$params = [$email, $hash, "$displayname's channel", "Welcome to $displayname's stream!", $displayname, '/profiles/default/profile_default.png', $authcode, '/profiles/default/offline_default.jpg'];
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:register';
$code = 1;
throw new Exception($message, $code);
}
// edit emailtemplate.php as needed to change desired emails sent out
$subject = $GLOBALS['sitetitle'] . ' Account Verification';
ob_start();
include 'inc/emailtemplate.php';
$message = ob_get_contents();
ob_end_clean();
$headers = [];
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/html; charset=UTF-8";
$headers[] = "From: " . $GLOBALS['from_email'];
$headers[] = "Bcc: " . $GLOBALS['bcc_email'];
$headers[] = "Reply-To: " . $GLOBALS['reply_email'];
$headers[] = 'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $message, implode("\r\n", $headers));
return true;
}
// verify account from verification link in registration email
/**
* @param $email
* @return bool
* @throws Exception
*/
public function emailcheck($email)
{
$params = [$email];
$sql = "SELECT * FROM $this->user_table WHERE email = $1";
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:emailcheck';
$code = 1;
throw new Exception($message, $code);
}
$row_cnt = pg_num_rows($result);
return $row_cnt >= 1 ? true : false;
}
/**
* @param $email
* @param $displayname
* @return bool
* @throws Exception
*/
public function dncheck($email, $displayname)
{
$params = [$displayname, $email];
$sql = "SELECT * FROM $this->user_table WHERE display_name = $1 AND email != $2";
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:dncheck';
$code = 1;
throw new Exception($message, $code);
}
$row_cnt = pg_num_rows($result);
return $row_cnt >= 1 ? true : false;
}
/**
* @param $email
* @param $code
* @return string
* @throws Exception
*/
public function verify($email, $code)
{
$params = [$email, $code];
$sql = "SELECT * FROM $this->user_table WHERE email = $1 AND auth_code = $2";
$result = pg_query_params($this->link, $sql, $params);
$row_cnt = pg_num_rows($result);
if ($row_cnt >= 1) {
// simple function to generate private keys (stream and api). Doesn't need to be overly complex as this is private, and not much you can do if you can predict the string.
/**
* @param int $length
* @return string
*/
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$streamkey = generateRandomString();
$apikey = generateRandomString();
$params = [$streamkey, $email, $code, $apikey];
$sql = "UPDATE $this->user_table SET auth_code = null, verified = 1, stream_key = $1, api_key = $4 WHERE email = $2 AND auth_code = $3";
$check = pg_query_params($this->link, $sql, $params);
if ($check === false) {
$message = 'Error in: class:user | function:verify';
$code = 5;
throw new Exception($message, $code);
} else {
$status = 'true';
}
} else {
$status = 'Invalid email/authentication code!';
}
return $status;
}
// Check if email exists
/**
* @param $email
* @param $furl
* @return bool
* @throws Exception
*/
public function resetCode($email, $furl)
{
$authcode = bin2hex(random_bytes(32));
$sql = "UPDATE $this->user_table SET auth_code = $1 WHERE email = $2";
$params = [$authcode, $email];
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:resetCode';
$code = 1;
throw new Exception($message, $code);
}
$subject = $GLOBALS ["sitetitle"] . ' Password Reset';
$message = "Password reset auth code for $email:<br /><br /><b>$authcode</b><br /><br />Reset form: <a href='$furl/lostpass'>Click here</a>";
$headers = [];
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/html; charset=UTF-8";
$headers[] = "From: " . $GLOBALS['from_email'];
$headers[] = "Reply-To: issues@rirnef.net";
$headers[] = 'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $message, implode("\r\n", $headers));
return true;
}
// Check if display name exists
/**
* @param $email
* @param $code
* @param $password
* @return bool|string
* @throws Exception
*/
public function passwordReset($email, $code, $password)
{
$params = [$email, $code];
$sql = "SELECT * FROM $this->user_table WHERE email = $1 AND auth_code = $2";
$result = pg_query_params($this->link, $sql, $params);
$row_cnt = pg_num_rows($result);
if ($row_cnt >= 1) {
$hash = password_hash($password, PASSWORD_DEFAULT);
$params = [$hash, $email, $code];
$sql = "UPDATE $this->user_table SET auth_code = null, password = $1 WHERE email = $2 AND auth_code = $3";
$check = pg_query_params($this->link, $sql, $params);
if ($check === false) {
$message = 'Error in: class:user | function:verify';
$code = 2;
throw new Exception($message, $code);
} else {
$status = true;
}
} else {
$status = 'Invalid email/authentication code!';
}
return $status;
}
// Grab account info
/**
* @param $value
* @param string $field
* @return array
* @throws Exception
*/
public function info($value, $field = 'email')
{
$sql = "SELECT email, stream_key, channel_name, channel_title, display_name, profile_img, api_key, chat_jp_setting, offline_image FROM $this->user_table WHERE $field = $1";
$params = [$value];
$info = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
if ($info === null) {
$message = 'Error in: class:user | function:info';
$code = 1;
throw new Exception($message, $code);
}
return $info;
}
// updates channel_name, channel_title, and display_name.
/**
* @param $email
* @param $channelname
* @param $channeltitle
* @param $displayname
* @param $chatjp
* @return string
* @throws Exception
*/
public function update($email, $channelname, $channeltitle, $displayname, $chatjp)
{
$dncheck = $this->dncheck($email, $displayname);
if ($dncheck === true) {
$status = 'Display name already exists!';
} else {
$sql = "UPDATE $this->user_table SET channel_name = $1, channel_title = $2, display_name = $3, chat_jp_setting = '$chatjp' WHERE email = $4";
$params = [$channelname, $channeltitle, $displayname, $email];
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:update';
$code = 1;
throw new Exception($message, $code);
} else {
$status = 'Updated!';
}
}
return $status;
}
/**
* @param $email
* @param $path
* @param $type
* @return string
* @throws Exception
*/
public function imageUpdate($email, $path, $type)
{
if ($type === 'avatar') {
$sql = "UPDATE $this->user_table SET profile_img = $2 WHERE email = $1";
$params = [$email, $path];
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:imageUpdate:avatar';
$code = 1;
throw new Exception($message, $code);
} else {
$status = 'Updated!';
}
} elseif ($type === 'offline') {
$sql = "UPDATE $this->user_table SET offline_image = $2 WHERE email = $1";
$params = [$email, $path];
$result = pg_query_params($this->link, $sql, $params);
if ($result === false) {
$message = 'Error in: class:user | function:imageUpdate:offline';
$code = 1;
throw new Exception($message, $code);
} else {
$status = 'Updated!';
}
} else {
$status = 'Unknown type!';
}
return $status;
}
/**
* @param $input
* @param $function
* @return string
*/
public function updateStreamkey($input, $function)
{
if ($function === 'channel') {
$params = [$input];
$sql = "SELECT channel_name FROM $this->user_table WHERE display_name = $1";
$query = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
$channelname = $query['channel_name'];
return $channelname;
} elseif ($function === 'title') {
$params = [$input];
$sql = "SELECT channel_title FROM $this->user_table WHERE display_name = $1";
$query = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
$channeltitle = $query['channel_title'];
return $channeltitle;
} elseif ($function === 'email') {
$params = [$input];
$sql = "SELECT email FROM $this->user_table WHERE display_name = $1";
$query = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
$email = $query['email'];
return $email;
} elseif ($function === 'offline_image') {
$params = [$input];
$sql = "SELECT offline_image FROM $this->user_table WHERE display_name = $1";
$query = pg_fetch_assoc(pg_query_params($this->link, $sql, $params));
$offlineimage = $query['offline_image'];
return $offlineimage;
} else {
return 'Error in updateStreamkey()!';
}
}
/**
* @param $key
* @return bool
* @throws Exception
*/
public function verifyAPIkey($key)
{
$params = [$key];
$sql = "SELECT display_name FROM $this->user_table WHERE api_key = $1 AND api_key IS NOT null";
$query = pg_query_params($this->link, $sql, $params);
if ($query === false) {
$message = 'Error in: class:user | function:verifyAPIkey()';
$code = 1;
throw new Exception($message, $code);
}
$row_cnt = pg_num_rows($query);
return $row_cnt >= 1 ? true : false;
}
}