This repository was archived by the owner on Sep 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLHandler.php
More file actions
executable file
·148 lines (144 loc) · 6.8 KB
/
SQLHandler.php
File metadata and controls
executable file
·148 lines (144 loc) · 6.8 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace SQL;
class Handler {
protected $SQL_Config = array('hostname' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'BRG18',
'dbprefix' => 'br_',
'dbtable' => 'annmcs',
'dieonerr' => True
); // Default Configuration Value(s)
public $SQLDB;
protected $ValidKeys = array('hostname', 'username', 'password', 'dbname', 'dbprefix', 'dbtable', 'dieonerr');
public function __construct(array $config)
{
if(is_array($config) && count($config) >= 1)
{
foreach($config as $cKey => $cValue)
{
for($t = 0; $t < count($this->ValidKeys); $t++)
{
if(strcmp($cKey, $this->ValidKeys[$t]) == False)
{
$this->SQL_Config[$cKey] = $cValue;
break;
}
else
{
if($t == count($this->ValidKeys)-1)
{
$exception = "[SQL-Handler]: Invalid SQL Configuration Key: " .$cKey. "(Valid Keys: " .join(', ', $this->ValidKeys). ")";
if(function_exists("WritetoFile"))
{
WritetoFile("logs/sql_errors.log", $exception);
}
else
{
throw new \Exception($exception);
}
break;
}
else
{
continue;
}
}
}
}
}
else
{
$notice = "[SQL-Handler]: Loaded Default SQL Configuration(s) Successfully.";
if(function_exists("WritetoFile"))
{
WritetoFile("logs/sql_debug.log", $notice);
}
}
$this->SQLConnect($this->SQL_Config['dieonerr']);
}
protected function SQLConnect($die_on_error)
{
$this->SQLDB = mysqli_connect($this->SQL_Config['hostname'], $this->SQL_Config['username'], $this->SQL_Config['password'], $this->SQL_Config['dbname']);
if(mysqli_connect_errno())
{
$exception = "[MySQL]: Connection to BrownTurbo Gaming's Database failed: " .(is_bool($this->SQLDB) ? 'SQL_ERROR_NOT_FOUND' : mysqli_error($this->SQLDB));
if(function_exists("WritetoFile"))
{
WritetoFile("logs/sql_errors.log", $exception);
}
if($die_on_error)
{
throw new \Exception(json_encode(array('errorcode' => 408, 'errormsg' => 'Request Timeout.', 'message' => $exception)));
}
}
else
{
$fIns = fopen('installed.lock', 'r');
if($fIns == False || (filesize('installed.lock') >= 1 ? strpos(fread($fIns, filesize('installed.lock')), 'installed successfully') == False : True))
{
$this->SQLQuery("CREATE DATABASE IF NOT EXISTS `" .$this->SQL_Config['dbname']. "`");
$this->SQLQuery("CREATE TABLE IF NOT EXISTS `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` (`ID` int(11) NOT NULL, `Subject` text NOT NULL, `Announcement` longtext NOT NULL, `Date` text NOT NULL)");
$this->SQLQuery("ALTER TABLE `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` ADD PRIMARY KEY IF NOT EXISTS (`ID`)");
$this->SQLQuery("ALTER TABLE `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT");
$this->SQLQuery("INSERT INTO `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` (`ID`, `Subject`, `Announcement`, `Date`) VALUES(NULL, 'Hey All', '<p>How are you ??? Maryem &lt;3</p>\r\n', '2018/06/09 - 07:48:13 PM'), (NULL, 'Hello all', '<p>How are you ??? Are you here ??</p>\r\n', '2018/06/09 - 11:43:34 PM'), (NULL, 'Hey All', '<p>Hey all, How are you ?? Are you here ???&nbsp;</p>\r\n', '2018/06/09 - 11:46:23 PM')");
fclose($fIns); $notice = 'BrownTurbo Gaming\'s Announcements Managing Module\'s Database have been installed successfully.';
if(function_exists("WritetoFile"))
{
WritetoFile("installed.lock", $notice, False, '\r');
WritetoFile("logs/sql_debug.log", "[MySQL]: " .$notice);
}
}
if(function_exists("WritetoFile"))
{
$notice = "[MySQL]: Connected to Brownturbo Gaming's Database Successfully.";
WritetoFile("logs/sql_debug.log", $notice);
}
}
}
public function SQLQuery($query, $die_on_error = NULL)
{
$SQL = mysqli_query($this->SQLDB, $query);
if (!$SQL || (is_bool($SQL) ? False : (mysqli_num_rows($SQL) == NULL || mysqli_num_rows($SQL) == False || is_numeric(mysqli_num_rows($SQL)) == False)))
{
$exception = "[MySQL]: Failed to Execute the SQL Query: " .(strlen(mysqli_error($this->SQLDB)) ? mysqli_error($this->SQLDB) : 'SQL_ERROR_NOT_FOUND');
if(function_exists("WritetoFile"))
{
WritetoFile("logs/sql_errors.log", $exception);
}
switch($die_on_error)
{
case NULL: {
if(boolval($this->SQL_Config['dieonerr']))
{
throw new \Exception(json_encode(array('errorcode' => 408, 'errormsg' => 'Request Timeout.', 'message' => $exception)));
}
}
break;
case ($die_on_error == 1 || $die_on_error == True): {
throw new \Exception(json_encode(array('errorcode' => 408, 'errormsg' => 'Request Timeout.', 'message' => $exception)));
}
break;
default: break;
}
$SQL = False;
}
return $SQL;
}
public function SQLCountRows()
{
$query = $this->SQLQuery("SELECT * FROM `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` ORDER BY ID DESC");
return mysqli_num_rows($query);
}
public function SQLDrop($rowID, $auto_increment = True)
{
$response = False;
if($this->SQLQuery("DELETE FROM `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` WHERE `ID` = '" .$rowID. "' ORDER BY ID DESC"))
{
$response = ($auto_increment ? $this->SQLQuery("ALTER TABLE `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = " .$this->SQLCountRows()) : True);
}
return $response;
}
//UPDATE `" .$this->SQL_Config['dbprefix'] . $this->SQL_Config['dbtable']. "` SET `Subject` = 'Hey All', `Announcement` = 'Hey all', `Date` = '2018/06' WHERE `ID` = ''" .$id. "'
}
?>