-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
103 lines (93 loc) · 2.63 KB
/
db.php
File metadata and controls
103 lines (93 loc) · 2.63 KB
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
<?php
//require_once('db_credentials.php');
function db_connect() {
try {
//$pdo = new PDO("mysql:host=localhost;dbname=baumgc12",
//"baumgc12", "mysql884812", array(PDO::ATTR_ERRMODE =>
$pdo = new PDO("mysql:host=localhost;dbname=ffnh",
"root", "root", array(PDO::ATTR_ERRMODE =>
PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
echo "Cannot connect to the database";
exit();
}
return $pdo;
}
function db_disconnect() {
global $db;
$db = null;
}
$db = db_connect();
function register($username, $password) {
global $db;
$registered = FALSE;
include("globalconstants.php");
if (strlen($username) > $maxUsername){
return $registered;
}
try{
$statement = $db->prepare("INSERT INTO profiles
(username, password) values (?, ?)");
$statement->execute([$username, password_hash($password, PASSWORD_DEFAULT)]);
$registered = TRUE;
} catch (PDOException $e) {
//echo "ERROR";
//var_dump($e);
}
return $registered;
}
function is_password_correct($username, $password) {
global $db;
$password_correct = FALSE;
$statement = $db->prepare("SELECT password FROM profiles WHERE username = ?");
$statement->execute([$username]);
if ($statement) {
//$row = $statement->fetch();
foreach ($statement as $row) {
$correct_password = $row["password"];
/*if (hash_equals($hashed_password, crypt($password, $correct_password))) {
$password_correct = TRUE;
}*/
$password_correct = password_verify($password, $correct_password);
}
}
return $password_correct;
}
function get_chats($last_id = -1, $leagueid = -1) {
global $db;
$result = [];
try {
$sql = "SELECT id, user, message FROM chats WHERE id > ?
and leagueid = ? ORDER BY id";
$statement = $db->prepare($sql);
$statement->execute([$last_id, $leagueid]);
$chats = $statement->fetchAll(PDO::FETCH_ASSOC); // puts in associative array (ready for json)
$result["chats"] = $chats;
$result["status"] = "ok";
}
catch (PDOException $e) {
$result["status"] = "error";
$result["error"] = $e;
}
return $result;
}
/**
* Inserts a chat for the given user and message
* Returns associative array to indicate success or error
*/
function insert_chat($user, $message, $leagueid) {
global $db;
$result = [];
try {
$sql = "INSERT INTO chats(user, message, leagueid) VALUES(?, ?, ?)";
$statement = $db->prepare($sql);
$statement->execute([$user, $message, $leagueid]);
$result["status"] = "ok";
}
catch (PDOException $e) {
$result["status"] = "error";
$result["error"] = $e;
}
return $result;
}
?>