-
Notifications
You must be signed in to change notification settings - Fork 55
/
class.users.php
130 lines (112 loc) · 4.34 KB
/
class.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
<?php
/*
SceneGames
Copyright (C) 2018 GoodOldDownloads
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GoodOldDownloads;
require __DIR__ . '/db.php';
class Users
{
public function __construct()
{
require __DIR__ . '/config.php';
$this->CONFIG = $CONFIG;
$this->APIKEYS = $APIKEYS;
}
public function register($username, $password, $regcode)
{
global $dbh;
$username = trim($username);
$regcode = trim($regcode);
$hashed = password_hash($password, PASSWORD_BCRYPT);
$apikey = 'sg_'.implode('-', str_split(substr(md5(base64_encode(random_bytes(32))), 0, 25), 5));
$dbh->beginTransaction();
try {
// Check regcode
$checkReg = $dbh->prepare("SELECT COUNT(*) FROM `regcodes` WHERE `code` = :code");
$checkReg->bindParam(':code', $regcode, \PDO::PARAM_STR);
$checkReg->execute();
if ($checkReg->fetchColumn() != 1) {
throw new \Exception("Invalid Registration Code.");
}
// Regcode is good so delete it
$deleteReg = $dbh->prepare("DELETE FROM `regcodes` WHERE `code` = :code");
$deleteReg->bindParam(':code', $regcode, \PDO::PARAM_STR);
$deleteReg->execute();
// Check if user with that name exists
$checkUser = $dbh->prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :user");
$checkUser->bindParam(':user', $username, \PDO::PARAM_STR);
$checkUser->execute();
if ($checkUser->fetchColumn() > 0) {
throw new \Exception("User already exists.");
}
// Now make a user
$stmt = $dbh->prepare("INSERT INTO `users` (`username`, `password`, `class`)
VALUES (:user, :pass, 'DISABLED')");
$stmt->bindParam(':user', $username, \PDO::PARAM_STR);
$stmt->bindParam(':pass', $hashed, \PDO::PARAM_STR);
if ($stmt->execute() == false) {
throw new \Exception("Failed to create user.");
}
$dbh->commit();
} catch(\Exception $e){
$dbh->rollBack();
return $e->getMessage();
}
return true;
}
public function login($username, $password)
{
global $Memcached;
global $dbh;
$username = trim($username);
$dbh->beginTransaction();
try {
// Check if user exists
$checkExists = $dbh->prepare("SELECT * FROM `users` WHERE `username` = :username");
$checkExists->bindParam(':username', $username, \PDO::PARAM_STR);
$checkExists->execute();
$userData = $checkExists->fetch();
if ($userData === false) {
throw new \Exception("Incorrect Username/Password");
}
if ($userData['class'] === 'DISABLED') {
throw new \Exception("Account is Disabled (or has not been enabled yet)");
}
// check password
if (!password_verify($password, $userData['password'])) {
throw new \Exception("Incorrect Username/Password");
} else {
$_SESSION['user'] = $userData;
}
setcookie("was_user", "1", 2147483647, '/');
$dbh->commit();
} catch(\Exception $e){
$dbh->rollBack();
return $e->getMessage();
}
return true;
}
public function logout()
{
unset($_SESSION['user']);
}
public function get($userid = null)
{
if (isset($_SESSION['user'])) {
return $_SESSION['user'];
} else {
return false;
}
}
}