-
Notifications
You must be signed in to change notification settings - Fork 1
/
Users.php
82 lines (78 loc) · 2.4 KB
/
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
<?php
require_once __DIR__ . "/database.php";
require_once __DIR__ . "/configVars.php";
class User
{
public static $db;
protected static $insertStatement;
public static function createUserDB()
{
$create_command = "CREATE TABLE IF NOT EXISTS user(
email varchar(50) PRIMARY KEY,
firstName varchar(30) NOT NULL,
lastName varchar(30),
pass varchar(256) NOT NULL,
phone varchar(10),
check (phone REGEXP '^[6789][0-9]{9}$')
)";
User::$db->fullExecute($create_command);
}
public static function prepare_insert()
{
$insert_statement = "INSERT INTO user(firstName,lastName,email,pass,phone) VALUES (:firstName,:lastName,:email,:pass,:phone)";
$rs = User::$db->prepareAndReturn($insert_statement);
if ($rs) {
User::$insertStatement = $rs;
}
}
public static function insertData($userData)
{
$userData["pass"] = User::hashPass($userData["pass"]);
$rs = User::$insertStatement->execute($userData);
return $rs;
}
protected static function hashPass(string $pass): string
{
$hashed = password_hash($pass, 1);
return $hashed;
}
protected static function verifyPass(string $str, string $hashed): bool
{
return password_verify($str, $hashed);
}
public static function validate(array $userData)
{
return array("status" => true);
}
public static function retrieveByEmail(string $userEmail)
{
$userEmail = trim($userEmail);
$userEmail = explode(";", $userEmail)[0];
$query = "SELECT * FROM user WHERE email='$userEmail'";
$res = User::$db->fullFetch($query);
if (count($res)) {
return $res[0];
} else {
return null;
}
}
public static function alreadyExists($userEmail = null): bool
{
if (User::retrieveByEmail($userEmail) !== null) {
return true;
} else {
return false;
}
}
public static function verifyUser(string $userEmail, string $pass)
{
$userData = User::retrieveByEmail($userEmail);
if ($userData === null) {
return false;
} else {
return User::verifyPass($pass, $userData["pass"]);
}
}
}
User::$db = new Database(...$DB_CONFIG);
User::prepare_insert();