-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.php
120 lines (94 loc) · 3.19 KB
/
database.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
<?php
require_once './data.php';
require_once './functions.php';
$_mysqli;
$_mysqli_connected = false;
$_mysqli_numQueries = 0;
$_mysqli_insert_id = 0;
function getSQLConnection()
{
global $_mysqli, $_mysqli_connected;
if($_mysqli_connected) // Database connection is already established
{
if(!$_mysqli -> connect_error)
return $_mysqli;
else
// Attempt to reconnect
unset($_mysqli);
}
// Establish the database connection.
global $servername, $dbusername, $dbpassword, $dbname;
$_mysqli = new mysqli($servername, $dbusername, $dbpassword, $dbname);
if($_mysqli -> connect_error)
{
fatalError("Agora was unable to connect to the MySQL database. The database has either gone offline/unreachable, or Agora is not configured properly. Please contact the server administrator.<br><br>" . $_mysqli -> connect_error);
return false;
}
return $_mysqli;
}
function disconnectSQL()
{
global $_mysqli, $_mysqli_connected;
if($_mysqli_connected)
{
mysqli_close($_mysqli);
$_mysqli_connected = false;
}
return true;
}
function prepareStatement($statement)
{
$mysqli = getSQLConnection();
$statement = $mysqli -> prepare($statement);
return $statement;
}
function executeStatement($statement)
{
//$mysqli = getSQLConnection();
$result = $statement -> execute();
if($result === false)
{
fatalError("Agora encountered an SQL query error. This is most likely a bug in Agora, please report this occurence; but make sure that the data below doesn't contain any sensitive information (like your password hash). If it does, censor it before reporting.<br><br>Technical details:<br>\nError: " . $statement -> error . " \n<br>\nSource function: " . debug_backtrace()[1]['function'] . "\n<br>\nFull query: " . $statement -> sqlstate);
return false;
}
$result = $statement -> get_result();
global $_mysqli_numQueries, $_mysqli_insert_id;
$_mysqli_insert_id = $statement -> insert_id;
$_mysqli_numQueries++;
return $result;
}
function querySQL($query)
{
$mysqli = getSQLConnection();
$result = $mysqli -> query($query);
if($result === false)
{
// This needs be locked behind some sort of 'debug mode' config since it's a security risk... I'm sure it'll happen eventually.
fatalError("Agora encountered an SQL query error. This is most likely a bug in Agora, please report this occurence; but make sure that the data below doesn't contain any sensitive information (like your password hash). If it does, censor it before reporting.<br><br>Technical details:<br>\nError: " . $mysqli -> error . " \n<br>\nSource function: " . debug_backtrace()[1]['function'] . "\n<br>\nFull query: " . $query);
return false;
}
global $_mysqli_numQueries, $_mysqli_insert_id;
$_mysqli_insert_id = $mysqli -> insert_id;
$_mysqli_numQueries++;
return $result;
}
function getLastInsertID()
{
global $_mysqli_insert_id;
return $_mysqli_insert_id;
}
function sanitizeSQL($value)
{
$mysqli = getSQLConnection();
mysqli_set_charset($mysqli, "utf8");
$result = mysqli_real_escape_string($mysqli, $value);
return $result;
}
function fatalError($error)
{
global $_navBarEnabled;
$_navBarEnabled = false;
addToBody("<div class=\"fatalErrorBox\">\n<h1>FATAL ERROR</h1><br><br>" . $error . "</div>");
finishPage();
}
?>