· 4 years ago · Jul 07, 2021, 11:02 PM
1<?php if (!defined("IN_WALLET")) { die("Auth Error!"); } ?>
2
3<?php
4//ini_set('error_reporting', E_ALL);
5//ini_set('display_errors', 'On');
6class User {
7
8 private $mysqli;
9
10
11
12 function __construct($mysqli)
13
14 {
15
16 $this->mysqli = $mysqli;
17
18 }
19
20
21
22 function logIn($username, $password)
23
24 {
25 if (empty($username) || empty($password))
26
27 {
28 return false;
29 }
30 else {
31 $auth=$_POST['auth'];
32 $username = $this->mysqli->real_escape_string(strip_tags($username));
33 //$salt = random_int(0, 64);
34 $password = md5(addslashes(strip_tags($password)));
35 $auth = $this->mysqli->real_escape_string( strip_tags( $auth));
36 $result = $this->mysqli->query("SELECT * FROM users WHERE username='" . $username . "'");
37 $user = $result->fetch_assoc();
38 $secret = $user['secret'];
39 $oneCode = $this->getCode($secret);
40
41 if (($user) && ($user['password'] == $password) && ($user['locked'] == 0) && ($user['authused'] == 0))
42 {
43 return $user;
44 }
45 elseif (($user) && ($user['password'] == $password) && ($user['locked'] == 1)) {
46 $pin = $user['supportpin'];
47 return "Account is locked. Contact support for more information. $pin";
48}
49
50 elseif (($user) && ($user['password'] == $password) && ($user['locked'] == 0) && ($user['authused'] == 1 && ($oneCode == $_POST['auth']))) {
51 return $user;
52
53 } else {
54
55 return "Username, password or 2 factor is incorrect";
56
57 }
58
59 }
60
61 }
62
63
64
65 function add($username, $password, $confirmPassword)
66
67 {
68
69 if (empty($username) || empty($password) || empty($confirmPassword))
70
71 {
72
73 return "Please, fill all the fields";
74
75 } elseif ($password != $confirmPassword)
76
77 {
78
79 return "Passwords did not match";
80
81 } elseif ((strlen($username) < 3) || (strlen($username) > 30))
82
83 {
84
85 return "Username must be between 3 and 30 characters";
86
87 } elseif (strlen($password) < 3)
88
89 {
90
91 return "Password must be longer than 3 characters";
92
93 } else {
94
95 //Let's do a database check
96
97 $username = $this->mysqli->real_escape_string(strip_tags($username));
98
99 $salt = random_int(0, 64);
100 $password = md5(addslashes(strip_tags($password.$salt)));
101
102
103 $user = $this->mysqli->query("SELECT * FROM users WHERE username='" . $username . "'");
104
105 if ($user->num_rows > 0)
106
107 {
108
109 return "Username already taken";
110
111 } else {
112
113 $query = $this->mysqli->query("INSERT INTO users (`date`, `ip`, `username`, `password`, `supportpin`) VALUES (\"" . date("n/j/Y g:i a") . "\", \"". $_SERVER['REMOTE_ADDR'] . "\", \"" . $username ."\", \"" . $password . "\", \"". rand(10000,99999) . "\");");
114
115 if ($query)
116 {
117
118 return true;
119 } else {
120 return "System error";
121
122 }
123
124 }
125 }
126 }
127
128
129 function updatePassword($user_session, $oldPassword, $newPassword, $confirmPassword)
130
131 {
132 global $hide_ids;
133 if ($newPassword != $confirmPassword)
134 {
135 return "Passwords did not match.";
136 } else {
137 //Get old password
138 $result = $this->mysqli->query("SELECT * FROM users WHERE username='" . $user_session . "'");
139 if ($result->num_rows > 0)
140 {
141 $user = $result->fetch_assoc();
142 $salt = random_int(0, 64);
143 $oldPassword = md5(addslashes(strip_tags($oldPassword)));
144 $newPassword = md5(addslashes(strip_tags($newPassword.$salt)));
145 if ($user['password'] != $oldPassword)
146
147 {
148
149 return "Password is incorrect.";
150
151 } else {
152
153 $result = $this->mysqli->query("UPDATE users SET password='" . $newPassword . "', supportpin='" . rand(10000,99999) . "' WHERE id=" . $user['id']);
154
155 if ($result)
156
157 {
158
159 return true;
160
161 } else {
162
163 return "Some sort of error occured.";
164
165 }
166
167 }
168
169 } else {
170
171 return "Some sort of error occured.";
172
173 }
174
175 }
176
177 }
178
179
180 function adminGetUserList()
181
182 {
183
184 global $hide_ids;
185
186 $users = $this->mysqli->query("SELECT * FROM users");
187
188 $return = array();
189
190 while ($user = $users->fetch_assoc())
191
192 {
193 if (!in_array($user['id'], $hide_ids))
194
195 {
196 $return[] = $user;
197 }
198 }
199 return $return;
200 }
201
202
203 function adminGetUserInfo($id)
204 {
205 global $hide_ids;
206 if (is_numeric($id) && !in_array($id, $hide_ids))
207 {
208 $users = $this->mysqli->query("SELECT * FROM users WHERE id=" . $id);
209 if ($users->num_rows > 0)
210 {
211 return $users->fetch_assoc();
212 } else {
213 return false;
214 }
215 } else {
216 return false;
217 }
218 }
219
220
221 function adminUpdatePassword($id, $newPassword)
222 {
223 global $hide_ids;
224 $salt = random_int(0, 64);
225 $password = md5(addslashes(strip_tags($newPassword.$salt)));
226 if (is_numeric($id) && !in_array($id, $hide_ids))
227 {
228 $result = $this->mysqli->query("UPDATE users SET password='" . $password . "' WHERE id=" . $id . ";");
229 if ($result)
230 {
231 return true;
232 } else {
233 return "Error.";
234 }
235 } else {
236 return "User does not exist";
237 }
238 }
239
240 function enableauth()
241
242 {
243
244
245
246 // global $hide_ids;
247 include 'settings.php';
248 $id=$_SESSION['user_id'];
249 $secret=$this->createSecret();
250 $qrcode=$this->getQRCodeGoogleUrl(urlencode(''.$fullname.' Wallet'), $secret);
251 $oneCode = $this->getCode($secret);
252
253 if (($id))
254 {
255 $msg = "Secret Key: $secret *Please write this down and keep in a secure area*<br><img src='$qrcode'<br>Please scan this with the Google Authenticator app on your mobile phone. This page will clear on refresh, please be careful.";
256 $this->mysqli->query("UPDATE users SET authused=1, secret='" . $secret . "' WHERE id=" . $id); return "$msg";
257 }
258 }
259
260 function disauth()
261 {
262 $id=$_SESSION['user_id'];
263 if (($id))
264 {
265 $msg = "Two Factor Auth has been disabled for your account and will no longer be required when you sign in.";
266 $this->mysqli->query("UPDATE users SET authused=0, secret='' WHERE id=" . $id); return "$msg";
267 }
268 }
269
270 function adminDeleteAccount($id)
271
272 {
273 global $hide_ids;
274 if (is_numeric($id) && !in_array($id, $hide_ids))
275 {
276 $this->mysqli->query("DELETE FROM users WHERE id=" . $id);
277 }
278 }
279
280 function adminLockAccount($id)
281
282 {
283 global $hide_ids;
284 if (is_numeric($id) && !in_array($id, $hide_ids))
285 {
286 $users = $this->mysqli->query("UPDATE users SET locked=1 WHERE id=" . $id);
287 }
288 }
289
290 function adminUnlockAccount($id)
291
292 {
293 global $hide_ids;
294 if (is_numeric($id) && !in_array($id, $hide_ids))
295 {
296 $users = $this->mysqli->query("UPDATE users SET locked=0 WHERE id=" . $id);
297 }
298 }
299
300 function adminPrivilegeAccount($id)
301 {
302 global $hide_ids;
303 if (is_numeric($id) && !in_array($id, $hide_ids))
304 {
305 $users = $this->mysqli->query("UPDATE users SET admin=1 WHERE id=" . $id);
306 }
307 }
308
309 function adminDeprivilegeAccount($id)
310 {
311 global $hide_ids;
312 if (is_numeric($id) && !in_array($id, $hide_ids))
313
314 {
315 $users = $this->mysqli->query("UPDATE users SET admin=0 WHERE id=" . $id);
316 }
317 }
318
319//GoogleAuthenticator
320//Created by PHPGangsta
321
322protected $_codeLength = 6;
323 /**
324 * Create new secret.
325 * 16 characters, randomly chosen from the allowed base32 characters.
326 *
327 * @param int $secretLength
328 * @return string
329 */
330 public function createSecret($secretLength = 16)
331 {
332 $validChars = $this->_getBase32LookupTable();
333 unset($validChars[32]);
334 $secret = '';
335 for ($i = 0; $i < $secretLength; $i++) {
336 $secret .= $validChars[array_rand($validChars)];
337 }
338 return $secret;
339 }
340 /**
341 * Calculate the code, with given secret and point in time
342 *
343 * @param string $secret
344 * @param int|null $timeSlice
345 * @return string
346 */
347 public function getCode($secret, $timeSlice = null)
348 {
349 if ($timeSlice === null) {
350 $timeSlice = floor(time() / 30);
351 }
352 $secretkey = $this->_base32Decode($secret);
353 // Pack time into binary string
354 $time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice);
355 // Hash it with users secret key
356 $hm = hash_hmac('SHA1', $time, $secretkey, true);
357 // Use last nipple of result as index/offset
358 $offset = ord(substr($hm, -1)) & 0x0F;
359 // grab 4 bytes of the result
360 $hashpart = substr($hm, $offset, 4);
361 // Unpak binary value
362 $value = unpack('N', $hashpart);
363 $value = $value[1];
364 // Only 32 bits
365 $value = $value & 0x7FFFFFFF;
366 $modulo = pow(10, $this->_codeLength);
367 return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
368 }
369 /**
370 * Get QR-Code URL for image, from google charts
371 *
372 * @param string $name
373 * @param string $secret
374 * @param string $title
375 * @return string
376 */
377 public function getQRCodeGoogleUrl($name, $secret, $title = null) {
378 $urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
379 if(isset($title)) {
380 $urlencoded .= urlencode('&issuer='.urlencode($title));
381 }
382 return 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl='.$urlencoded.'';
383 }
384 /**
385 * Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now
386 *
387 * @param string $secret
388 * @param string $code
389 * @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
390 * @param int|null $currentTimeSlice time slice if we want use other that time()
391 * @return bool
392 */
393 public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null)
394 {
395 if ($currentTimeSlice === null) {
396 $currentTimeSlice = floor(time() / 30);
397 }
398 for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
399 $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);
400 if ($calculatedCode == $code ) {
401 return true;
402 }
403 }
404 return false;
405 }
406 /**
407 * Set the code length, should be >=6
408 *
409 * @param int $length
410 * @return PHPGangsta_GoogleAuthenticator
411 */
412 public function setCodeLength($length)
413 {
414 $this->_codeLength = $length;
415 return $this;
416 }
417 /**
418 * Helper class to decode base32
419 *
420 * @param $secret
421 * @return bool|string
422 */
423 protected function _base32Decode($secret)
424 {
425 if (empty($secret)) return '';
426 $base32chars = $this->_getBase32LookupTable();
427 $base32charsFlipped = array_flip($base32chars);
428 $paddingCharCount = substr_count($secret, $base32chars[32]);
429 $allowedValues = array(6, 4, 3, 1, 0);
430 if (!in_array($paddingCharCount, $allowedValues)) return false;
431 for ($i = 0; $i < 4; $i++){
432 if ($paddingCharCount == $allowedValues[$i] &&
433 substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])) return false;
434 }
435 $secret = str_replace('=','', $secret);
436 $secret = str_split($secret);
437 $binaryString = "";
438 for ($i = 0; $i < count($secret); $i = $i+8) {
439 $x = "";
440 if (!in_array($secret[$i], $base32chars)) return false;
441 for ($j = 0; $j < 8; $j++) {
442 $x .= str_pad(base_convert(@$base32charsFlipped[@$secret[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
443 }
444 $eightBits = str_split($x, 8);
445 for ($z = 0; $z < count($eightBits); $z++) {
446 $binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:"";
447 }
448 }
449 return $binaryString;
450 }
451 /**
452 * Helper class to encode base32
453 *
454 * @param string $secret
455 * @param bool $padding
456 * @return string
457 */
458 protected function _base32Encode($secret, $padding = true)
459 {
460 if (empty($secret)) return '';
461 $base32chars = $this->_getBase32LookupTable();
462 $secret = str_split($secret);
463 $binaryString = "";
464 for ($i = 0; $i < count($secret); $i++) {
465 $binaryString .= str_pad(base_convert(ord($secret[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
466 }
467 $fiveBitBinaryArray = str_split($binaryString, 5);
468 $base32 = "";
469 $i = 0;
470 while ($i < count($fiveBitBinaryArray)) {
471 $base32 .= $base32chars[base_convert(str_pad($fiveBitBinaryArray[$i], 5, '0'), 2, 10)];
472 $i++;
473 }
474 if ($padding && ($x = strlen($binaryString) % 40) != 0) {
475 if ($x == 8) $base32 .= str_repeat($base32chars[32], 6);
476 elseif ($x == 16) $base32 .= str_repeat($base32chars[32], 4);
477 elseif ($x == 24) $base32 .= str_repeat($base32chars[32], 3);
478 elseif ($x == 32) $base32 .= $base32chars[32];
479 }
480 return $base32;
481 }
482 /**
483 * Get array with all 32 characters for decoding from/encoding to base32
484 *
485 * @return array
486 */
487 protected function _getBase32LookupTable()
488 {
489 return array(
490 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
491 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
492 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
493 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
494 '=' // padding char
495 );
496 }
497}
498
499?>