1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
use Monolog\Handler\StreamHandler; |
27
|
|
|
use Monolog\Logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return null|PDO |
31
|
|
|
* @internal param null|string $db |
32
|
|
|
*/ |
33
|
|
|
function openDB() |
34
|
|
|
{ |
35
|
|
|
$logger = new Logger('Db'); |
36
|
|
|
$logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/libraryError.log', Logger::DEBUG)); |
37
|
|
|
$db = __DIR__ . '/../../database/dramiel.sqlite'; |
38
|
|
|
|
39
|
|
|
$dsn = "sqlite:$db"; |
40
|
|
|
try { |
41
|
|
|
$pdo = new PDO($dsn, '', '', array( |
42
|
|
|
PDO::ATTR_PERSISTENT => false, |
43
|
|
|
PDO::ATTR_EMULATE_PREPARES => true, |
44
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
} catch (Exception $e) { |
48
|
|
|
$logger->error($e->getMessage()); |
49
|
|
|
$pdo = null; |
50
|
|
|
return $pdo; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $pdo; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $query |
58
|
|
|
* @param string $field |
59
|
|
|
* @param array $params |
60
|
|
|
* @return string |
61
|
|
|
* @internal param string $db |
62
|
|
|
*/ |
63
|
|
|
function dbQueryField($query, $field, array $params = array()) |
64
|
|
|
{ |
65
|
|
|
$pdo = openDB(); |
66
|
|
|
if ($pdo == NULL) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$stmt = $pdo->prepare($query); |
71
|
|
|
$stmt->execute($params); |
72
|
|
|
|
73
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
74
|
|
|
$stmt->closeCursor(); |
75
|
|
|
|
76
|
|
|
if (count($result) == 0) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$resultRow = $result[0]; |
81
|
|
|
return $resultRow[$field]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $query |
86
|
|
|
* @param array $params |
87
|
|
|
* @return null|void |
88
|
|
|
* @internal param string $db |
89
|
|
|
*/ |
90
|
|
|
function dbQueryRow($query, array $params = array()) |
91
|
|
|
{ |
92
|
|
|
$pdo = openDB(); |
93
|
|
|
if ($pdo == NULL) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$stmt = $pdo->prepare($query); |
98
|
|
|
$stmt->execute($params); |
99
|
|
|
|
100
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
101
|
|
|
$stmt->closeCursor(); |
102
|
|
|
|
103
|
|
|
if (count($result) >= 1) { |
104
|
|
|
return $result[0]; |
105
|
|
|
} |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $query |
111
|
|
|
* @param array $params |
112
|
|
|
* @return array|void |
113
|
|
|
* @internal param string $db |
114
|
|
|
*/ |
115
|
|
|
function dbQuery($query, array $params = array()) |
116
|
|
|
{ |
117
|
|
|
$pdo = openDB(); |
118
|
|
|
if ($pdo === NULL) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$stmt = $pdo->prepare($query); |
123
|
|
|
$stmt->execute($params); |
124
|
|
|
|
125
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
126
|
|
|
$stmt->closeCursor(); |
127
|
|
|
|
128
|
|
|
return $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $query |
133
|
|
|
* @param array $params |
134
|
|
|
* @internal param string $db |
135
|
|
|
*/ |
136
|
|
|
function dbExecute($query, array $params = array()) |
137
|
|
|
{ |
138
|
|
|
$pdo = openDB(); |
139
|
|
|
if ($pdo === NULL) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// This is ugly, but, yeah.. |
144
|
|
|
if (strstr($query, ';')) { |
145
|
|
|
$explodedQuery = explode(';', $query); |
146
|
|
|
$stmt = null; |
147
|
|
|
foreach ($explodedQuery as $newQry) { |
148
|
|
|
$stmt = $pdo->prepare($newQry); |
149
|
|
|
$stmt->execute($params); |
150
|
|
|
} |
151
|
|
|
$stmt->closeCursor(); |
152
|
|
|
} else { |
153
|
|
|
$stmt = $pdo->prepare($query); |
154
|
|
|
$stmt->execute($params); |
155
|
|
|
$stmt->closeCursor(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//MESSAGE QUEUE |
160
|
|
|
function queueMessage($message, $channel, $guild) |
161
|
|
|
{ |
162
|
|
|
dbExecute('REPLACE INTO messageQueue (`message`, `channel`, `guild`) VALUES (:message,:channel,:guild)', array(':message' => $message, ':channel' => $channel, ':guild' => $guild)); |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
function getQueuedMessage($id) |
167
|
|
|
{ |
168
|
|
|
return dbQueryRow('SELECT * FROM messageQueue WHERE `id` = :id', array(':id' => $id)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function clearQueuedMessages($id) |
172
|
|
|
{ |
173
|
|
|
dbQueryRow('DELETE from messageQueue where id = :id', array(':id' => $id)); |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function getOldestMessage() |
178
|
|
|
{ |
179
|
|
|
return dbQueryRow('SELECT MIN(id) from messageQueue'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
function priorityQueueMessage($message, $channel, $guild) |
183
|
|
|
{ |
184
|
|
|
$currentOldest = getOldestMessage(); |
|
|
|
|
185
|
|
|
$id = $currentOldest['MIN(id)'] - 1; |
186
|
|
|
dbExecute('REPLACE INTO messageQueue (`id`, `message`, `channel`, `guild`) VALUES (:id,:message,:channel,:guild)', array(':id' => $id, ':message' => $message, ':channel' => $channel, ':guild' => $guild)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
//RENAME QUEUE |
191
|
|
|
function queueRename($discordID, $nick, $guild) |
192
|
|
|
{ |
193
|
|
|
dbExecute('REPLACE INTO renameQueue (`discordID`, `nick`, `guild`) VALUES (:discordID,:nick,:guild)', array(':discordID' => $discordID, ':nick' => $nick, ':guild' => $guild)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
function getQueuedRename($id) |
197
|
|
|
{ |
198
|
|
|
return dbQueryRow('SELECT * FROM renameQueue WHERE `id` = :id', array(':id' => $id)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
function getOldestRename() |
202
|
|
|
{ |
203
|
|
|
return dbQueryRow('SELECT MIN(id) from renameQueue'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
function clearQueuedRename($id) |
207
|
|
|
{ |
208
|
|
|
dbQueryRow('DELETE from renameQueue where id = :id', array(':id' => $id)); |
209
|
|
|
return null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// |
213
|
|
|
function clearQueueCheck() |
214
|
|
|
{ |
215
|
|
|
$result = dbQueryRow('SELECT * FROM messageQueue'); |
|
|
|
|
216
|
|
|
if (@$result->num_rows > 35) { |
217
|
|
|
clearAllMessageQueue(); |
218
|
|
|
} |
219
|
|
|
return null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
//Clear Queue |
223
|
|
|
function clearAllMessageQueue() |
224
|
|
|
{ |
225
|
|
|
dbQueryRow('DELETE from messageQueue'); |
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
//CORP INFO |
230
|
|
|
function addCorpInfo($corpID, $corpTicker, $corpName) |
231
|
|
|
{ |
232
|
|
|
dbExecute('REPLACE INTO corpCache (`corpID`, `corpTicker`, `corpName`) VALUES (:corpID,:corpTicker,:corpName)', array(':corpID' => $corpID, ':corpTicker' => $corpTicker, ':corpName' => $corpName)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
function getCorpInfo($corpID) |
236
|
|
|
{ |
237
|
|
|
return dbQueryRow('SELECT * FROM corpCache WHERE `corpID` = :corpID', array(':corpID' => $corpID)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
function deleteCorpInfo($corpID) |
241
|
|
|
{ |
242
|
|
|
return dbQueryRow('DELETE from corpCache WHERE `corpID` = :corpID', array(':corpID' => $corpID)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
//Remove old DB's |
246
|
|
|
function dbPrune() |
247
|
|
|
{ |
248
|
|
|
$oldDatabases = array('corpIDs', 'users', 'usersSeen'); |
249
|
|
|
foreach ($oldDatabases as $db) { |
250
|
|
|
dbExecute("DROP TABLE IF EXISTS $db"); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
//Add Contacts |
255
|
|
|
function addContactInfo($contactID, $contactName, $standing) |
256
|
|
|
{ |
257
|
|
|
dbExecute('REPLACE INTO contactList (`contactID`, `contactName`, `standing`) VALUES (:contactID,:contactName,:standing)', array(':contactID' => $contactID, ':contactName' => $contactName, ':standing' => $standing)); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
//Get Contacts |
261
|
|
|
function getContacts($contactID) |
262
|
|
|
{ |
263
|
|
|
return dbQueryRow('SELECT * FROM contactList WHERE `contactID` = :contactID', array(':contactID' => $contactID)); |
264
|
|
|
} |