-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserLibrary.php
123 lines (110 loc) · 3.13 KB
/
UserLibrary.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
<?php
// If you installed via composer, just use this code to require autoloader on the top of your projects.
require 'vendor/autoload.php';
// Using Medoo namespace
use Medoo\Medoo;
if(!empty($databasename) && !empty($ipaddress) && !empty($username) && !empty($password) && !empty($usertablename)){
$database = new Medoo([
// required
'database_type' => 'mysql',
'database_name' => $databasename,
'server' => $ipaddress,
'username' => $username,
'password' => $password
]);
function CreateUserDataTable() {
global $database;
global $usertablename;
$database->query("CREATE TABLE IF NOT EXISTS " . $usertablename . " (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
username text NOT NULL,
password text NOT NULL,
PRIMARY KEY (id)
) ENGINE NDB;");
}
function addUserTableColumn($newcolumnname, $type, $nullvalue = "") {
global $database;
global $usertablename;
$database->query("ALTER TABLE " . $usertablename ." ADD column_name " . $type . $nullvalue . ";");
}
function checkSignUp($user, $password) {
global $usertablename;
global $database;
$query = $database->select($usertablename, "*", [
"username" => $user
]);
if (!$query) {
createUser($user, $password);
var_dump( $database->error() );
} else {
echo "Sorry. Username already exists.";
}
};
function setUserSession($user, $password) {
global $database;
global $usertablename;
$query = $database->select($usertablename, ["id", "password"], [
"username" => $user
]);
if (!$query) {
echo "Uh oh. Something big broke.";
} else {
$id = $query[0]["id"];
$hash = $query[0]["password"];
}
};
function createUser($user, $password) {
global $database;
global $usertablename;
$newpassword = hashpassword($password);
$query = $database->insert($usertablename, array(
"username" => $user,
"password" => $newpassword
));
if (!$query) {
echo "Uh oh. Something broke.";
} else {
setUserSession($user, $password);
$getid = $database->select($usertablename, "id", [
"username" => $user
]);
if ($getid){
login($getid, $newpassword);
if (verifyLogin()) {
echo "It worked!";
}
}
else {"There's been a slight problem.";}
}
};
function hashpassword($oldpassword) {
$oldpassword = password_hash($oldpassword, PASSWORD_BCRYPT);
return $oldpassword;
};
function SanitizeString($string) {
$new_string = preg_replace('~[^a-zA-Z0-9]+~', '', $string);
return $new_string;
};
function login($id, $hash) {
$_SESSION['id'] = $id;
$_SESSION['hash'] = $hash;
}
function verifyLogin() {
global $usertablename;
global $database;
if (isset($_SESSION["id"]) && isset($_SESSION["hash"])) {
$query = $database->select($usertablename, "*", [
"id" => $_SESSION['id'],
"password" => $_SESSION["hash"]
]);
if (!$query) {
return false;
} else {
return true;
}
} else {
return false;
}
}
};
?>