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
|
|
|
* @param null $db |
31
|
|
|
* @return null|PDO |
32
|
|
|
* @internal param null|string $db |
33
|
|
|
*/ |
34
|
|
|
function openDB($db = null) |
35
|
|
|
{ |
36
|
|
|
$logger = new Logger('Db'); |
37
|
|
|
$logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/libraryError.log', Logger::DEBUG)); |
38
|
|
|
if ($db === null) { |
39
|
|
|
$db = __DIR__ . '/../../database/dramiel.sqlite'; |
40
|
|
|
} elseif ($db === 'eve'){ |
41
|
|
|
$db = __DIR__ . '/../../database/eveData.db'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$dsn = "sqlite:$db"; |
45
|
|
|
try { |
46
|
|
|
$pdo = new PDO($dsn, '', '', array( |
47
|
|
|
PDO::ATTR_PERSISTENT => false, |
48
|
|
|
PDO::ATTR_EMULATE_PREPARES => true, |
49
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} catch (Exception $e) { |
53
|
|
|
$logger->error($e->getMessage()); |
54
|
|
|
$pdo = null; |
55
|
|
|
return $pdo; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $pdo; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $query |
63
|
|
|
* @param string $field |
64
|
|
|
* @param array $params |
65
|
|
|
* @param null $db |
66
|
|
|
* @return string |
67
|
|
|
* @internal param string $db |
68
|
|
|
*/ |
69
|
|
|
function dbQueryField($query, $field, array $params = array(), $db = null) |
70
|
|
|
{ |
71
|
|
|
$pdo = openDB($db); |
72
|
|
|
if ($pdo == NULL) { |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$stmt = $pdo->prepare($query); |
77
|
|
|
$stmt->execute($params); |
78
|
|
|
|
79
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
80
|
|
|
$stmt->closeCursor(); |
81
|
|
|
|
82
|
|
|
if (count($result) == 0) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$resultRow = $result[0]; |
87
|
|
|
return $resultRow[$field]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $query |
92
|
|
|
* @param array $params |
93
|
|
|
* @param null $db |
94
|
|
|
* @return null|void |
95
|
|
|
* @internal param string $db |
96
|
|
|
*/ |
97
|
|
|
function dbQueryRow($query, array $params = array(), $db = null) |
98
|
|
|
{ |
99
|
|
|
$pdo = openDB($db); |
100
|
|
|
if ($pdo == NULL) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$stmt = $pdo->prepare($query); |
105
|
|
|
$stmt->execute($params); |
106
|
|
|
|
107
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
108
|
|
|
$stmt->closeCursor(); |
109
|
|
|
|
110
|
|
|
if (count($result) >= 1) { |
111
|
|
|
return $result[0]; |
112
|
|
|
} |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $query |
118
|
|
|
* @param array $params |
119
|
|
|
* @param null $db |
120
|
|
|
* @return array|void |
121
|
|
|
* @internal param string $db |
122
|
|
|
*/ |
123
|
|
|
function dbQuery($query, array $params = array(), $db = null) |
124
|
|
|
{ |
125
|
|
|
$pdo = openDB($db); |
126
|
|
|
if ($pdo === NULL) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$stmt = $pdo->prepare($query); |
131
|
|
|
$stmt->execute($params); |
132
|
|
|
|
133
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); |
134
|
|
|
$stmt->closeCursor(); |
135
|
|
|
|
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $query |
141
|
|
|
* @param array $params |
142
|
|
|
* @param null $db |
143
|
|
|
* @internal param string $db |
144
|
|
|
*/ |
145
|
|
|
function dbExecute($query, array $params = array(), $db = null) |
146
|
|
|
{ |
147
|
|
|
$pdo = openDB($db); |
148
|
|
|
if ($pdo === NULL) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// This is ugly, but, yeah.. |
153
|
|
|
if (strstr($query, ';')) { |
154
|
|
|
$explodedQuery = explode(';', $query); |
155
|
|
|
$stmt = null; |
156
|
|
|
foreach ($explodedQuery as $newQry) { |
157
|
|
|
$stmt = $pdo->prepare($newQry); |
158
|
|
|
$stmt->execute($params); |
159
|
|
|
} |
160
|
|
|
$stmt->closeCursor(); |
161
|
|
|
} else { |
162
|
|
|
$stmt = $pdo->prepare($query); |
163
|
|
|
$stmt->execute($params); |
164
|
|
|
$stmt->closeCursor(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
//MESSAGE QUEUE |
169
|
|
|
function queueMessage($message, $channel, $guild) |
170
|
|
|
{ |
171
|
|
|
dbExecute('REPLACE INTO messageQueue (`message`, `channel`, `guild`) VALUES (:message,:channel,:guild)', array(':message' => $message, ':channel' => $channel, ':guild' => $guild)); |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
function getQueuedMessage($id) |
176
|
|
|
{ |
177
|
|
|
return dbQueryRow('SELECT * FROM messageQueue WHERE `id` = :id', array(':id' => $id)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
function clearQueuedMessages($id) |
181
|
|
|
{ |
182
|
|
|
dbQueryRow('DELETE from messageQueue where id = :id', array(':id' => $id)); |
183
|
|
|
return null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
function getOldestMessage() |
187
|
|
|
{ |
188
|
|
|
return dbQueryRow('SELECT MIN(id) from messageQueue'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
function priorityQueueMessage($message, $channel, $guild) |
192
|
|
|
{ |
193
|
|
|
$currentOldest = getOldestMessage(); |
|
|
|
|
194
|
|
|
$id = $currentOldest['MIN(id)'] - 1; |
195
|
|
|
dbExecute('REPLACE INTO messageQueue (`id`, `message`, `channel`, `guild`) VALUES (:id,:message,:channel,:guild)', array(':id' => $id, ':message' => $message, ':channel' => $channel, ':guild' => $guild)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
//RENAME QUEUE |
200
|
|
|
function queueRename($discordID, $nick, $guild) |
201
|
|
|
{ |
202
|
|
|
dbExecute('REPLACE INTO renameQueue (`discordID`, `nick`, `guild`) VALUES (:discordID,:nick,:guild)', array(':discordID' => $discordID, ':nick' => $nick, ':guild' => $guild)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
function getQueuedRename($id) |
206
|
|
|
{ |
207
|
|
|
return dbQueryRow('SELECT * FROM renameQueue WHERE `id` = :id', array(':id' => $id)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
function getOldestRename() |
211
|
|
|
{ |
212
|
|
|
return dbQueryRow('SELECT MIN(id) from renameQueue'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
function clearQueuedRename($id) |
216
|
|
|
{ |
217
|
|
|
dbQueryRow('DELETE from renameQueue where id = :id', array(':id' => $id)); |
218
|
|
|
return null; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// |
222
|
|
|
function clearQueueCheck() |
223
|
|
|
{ |
224
|
|
|
$result = dbQueryRow('SELECT * FROM messageQueue'); |
|
|
|
|
225
|
|
|
if (@$result->num_rows > 35) { |
226
|
|
|
clearAllMessageQueue(); |
227
|
|
|
} |
228
|
|
|
return null; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
//Clear Queue |
232
|
|
|
function clearAllMessageQueue() |
233
|
|
|
{ |
234
|
|
|
dbQueryRow('DELETE from messageQueue'); |
235
|
|
|
return null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
//CORP INFO |
239
|
|
|
/** |
240
|
|
|
* @param $corpID |
241
|
|
|
* @param $corpTicker |
242
|
|
|
* @param string $corpName |
243
|
|
|
*/ |
244
|
|
|
function addCorpInfo($corpID, $corpTicker, $corpName) |
245
|
|
|
{ |
246
|
|
|
dbExecute('REPLACE INTO corpCache (`corpID`, `corpTicker`, `corpName`) VALUES (:corpID,:corpTicker,:corpName)', array(':corpID' => $corpID, ':corpTicker' => $corpTicker, ':corpName' => $corpName)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
function getCorpInfo($corpID) |
250
|
|
|
{ |
251
|
|
|
return dbQueryRow('SELECT * FROM corpCache WHERE `corpID` = :corpID', array(':corpID' => $corpID)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
function deleteCorpInfo($corpID) |
255
|
|
|
{ |
256
|
|
|
return dbQueryRow('DELETE from corpCache WHERE `corpID` = :corpID', array(':corpID' => $corpID)); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
//Remove old DB's |
260
|
|
|
function dbPrune() |
261
|
|
|
{ |
262
|
|
|
$oldDatabases = array('corpIDs', 'users', 'usersSeen'); |
263
|
|
|
foreach ($oldDatabases as $db) { |
264
|
|
|
dbExecute("DROP TABLE IF EXISTS $db"); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
//Add Contacts |
269
|
|
|
function addContactInfo($contactID, $contactName, $standing) |
270
|
|
|
{ |
271
|
|
|
dbExecute('REPLACE INTO contactList (`contactID`, `contactName`, `standing`) VALUES (:contactID,:contactName,:standing)', array(':contactID' => $contactID, ':contactName' => $contactName, ':standing' => $standing)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
//Get Contacts |
275
|
|
|
function getContacts($contactID) |
276
|
|
|
{ |
277
|
|
|
return dbQueryRow('SELECT * FROM contactList WHERE `contactID` = :contactID', array(':contactID' => $contactID)); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
//eveDb interaction |
281
|
|
|
function getTypeID($typeName) |
282
|
|
|
{ |
283
|
|
|
$query = dbQueryRow('SELECT * FROM invTypes WHERE lower(`typeName`) = :typeName', array(':typeName' => $typeName), 'eve'); |
|
|
|
|
284
|
|
|
return $query['typeID']; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
function getTypeName($typeID) |
288
|
|
|
{ |
289
|
|
|
$query = dbQueryRow('SELECT * FROM invTypes WHERE `typeID` = :typeID', array(':typeID' => $typeID), 'eve'); |
|
|
|
|
290
|
|
|
return $query['typeName']; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
function getSystemID($solarSystemName) |
294
|
|
|
{ |
295
|
|
|
$query = dbQueryRow('SELECT * FROM mapSolarSystems WHERE lower(`solarSystemName`) = :solarSystemName', array(':solarSystemName' => $solarSystemName), 'eve'); |
|
|
|
|
296
|
|
|
return $query['solarSystemID']; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
function getSystemName($systemID) |
300
|
|
|
{ |
301
|
|
|
$query = dbQueryRow('SELECT * FROM mapSolarSystems WHERE `solarSystemID` = :systemID', array(':systemID' => $systemID), 'eve'); |
|
|
|
|
302
|
|
|
return $query['solarSystemName']; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
function getRegionID($regionName) |
306
|
|
|
{ |
307
|
|
|
$query = dbQueryRow('SELECT * FROM mapRegions WHERE lower(`regionName`) = :regionName', array(':regionName' => $regionName), 'eve'); |
|
|
|
|
308
|
|
|
return $query['regionName']; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
function getRegionName($regionID) |
312
|
|
|
{ |
313
|
|
|
$query = dbQueryRow('SELECT * FROM mapRegions WHERE `regionID` = :regionID', array(':regionID' => $regionID), 'eve'); |
|
|
|
|
314
|
|
|
return $query['regionName']; |
315
|
|
|
} |
316
|
|
|
|