1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* (c) Ruben Dorado <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace SiteAnalyzer; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use PDO; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* class Persistence |
16
|
|
|
* |
17
|
|
|
* @package SiteAnalyzer |
18
|
|
|
* @author Ruben Dorado <[email protected]> |
19
|
|
|
* @copyright 2018 Ruben Dorado |
20
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
21
|
|
|
*/ |
22
|
|
|
class Persistence |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/* |
26
|
|
|
* @param Configuration $config |
27
|
|
|
* |
28
|
|
|
* @return PDO |
29
|
|
|
*/ |
30
|
|
|
public static function getPDO($config) { |
31
|
|
|
$options = array( |
32
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
if ($config->getDsn()) { |
36
|
|
|
try { |
37
|
|
|
return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options); |
38
|
|
|
} catch (Exception $e) { |
39
|
|
|
if (!$config->getUseOnMemoryDB()) { |
40
|
|
|
throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($config->getUseOnMemoryDB()) { |
46
|
|
|
try { |
47
|
|
|
return new PDO("sqlite::memory:", null, null, $options); |
48
|
|
|
} catch (Exception $e) { |
49
|
|
|
throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
throw new Exception("Error when trying to obtain a connection to a database. Check the configuration. "); |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/* |
58
|
|
|
* @param $pdo PDO |
59
|
|
|
* @param $config Configuration |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public static function crateDatabase($pdo, $config) { |
63
|
|
|
try { |
64
|
|
|
$db_hit_table = $config->getHitTableName(); |
65
|
|
|
$db_options_table = $config->getOptionsTableName(); |
66
|
|
|
$db_from_table = $config->getFromTableName(); |
67
|
|
|
$db_url_table = $config->getUrlTableName(); |
68
|
|
|
|
69
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)"); |
70
|
|
|
$stmt->execute(); |
71
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))"); |
72
|
|
|
$stmt->execute(); |
73
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)"); |
74
|
|
|
$stmt->execute(); |
75
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
76
|
|
|
$stmt->execute(); |
77
|
|
|
} catch (Exception $e) { |
78
|
|
|
throw new Exception("Could not create the database. ".$e->getMessage()); |
79
|
|
|
} |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* @param $pdo PDO |
86
|
|
|
* @param $config Configuration |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
public static function deleteDatabase($pdo, $config) { |
90
|
|
|
$resp = true; |
91
|
|
|
|
92
|
|
|
$db_hit_table = $config->getHitTableName(); |
93
|
|
|
$db_options_table = $config->getOptionsTableName(); |
94
|
|
|
$db_from_table = $config->getFromTableName(); |
95
|
|
|
$db_url_table = $config->getUrlTableName(); |
96
|
|
|
|
97
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_hit_table); |
98
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_options_table); |
99
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_from_table); |
100
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_url_table); |
101
|
|
|
|
102
|
|
|
return $resp; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* @param $pdo PDO |
108
|
|
|
* @param $config Configuration |
109
|
|
|
* |
110
|
|
|
*/ |
111
|
|
|
private static function dropTable($pdo, $tableName) { |
112
|
|
|
try { |
113
|
|
|
$stmt = $pdo->prepare("DROP TABLE $tableName"); |
114
|
|
|
$stmt->execute(); |
115
|
|
|
|
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
118
|
|
|
} |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/* |
124
|
|
|
* @param $pdo PDO |
125
|
|
|
* @param $config Configuration |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
|
|
public static function checkTables($pdo, $config) { |
129
|
|
|
$resp = true; |
130
|
|
|
try { |
131
|
|
|
|
132
|
|
|
$resp = $resp && Persistence::checkHitTable($pdo, $config); |
|
|
|
|
133
|
|
|
$resp = $resp && Persistence::checkOptionsTable($pdo, $config); |
134
|
|
|
$resp = $resp && Persistence::checkFromTable($pdo, $config); |
135
|
|
|
$resp = $resp && Persistence::checkUrlTable($pdo, $config); |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
return $resp; |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/* |
144
|
|
|
* @param $pdo PDO |
145
|
|
|
* @param $config Configuration |
146
|
|
|
*/ |
147
|
|
|
public static function checkFromTable($pdo, $config) { |
148
|
|
|
try { |
149
|
|
|
$db_from_table = $config->getFromTableName(); |
150
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0"); |
151
|
|
|
$stmt->execute(); |
152
|
|
|
} catch (Exception $e) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/* |
159
|
|
|
* @param $pdo PDO |
160
|
|
|
* @param $config Configuration |
161
|
|
|
*/ |
162
|
|
|
public static function checkUrlTable($pdo, $config) { |
163
|
|
|
try { |
164
|
|
|
$db_url_table = $config->getUrlTableName(); |
165
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0"); |
166
|
|
|
$stmt->execute(); |
167
|
|
|
} catch (Exception $e) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/* |
176
|
|
|
* @param $pdo PDO |
177
|
|
|
* @param $config Configuration |
178
|
|
|
*/ |
179
|
|
|
public static function checkOptionsTable($pdo, $config) { |
180
|
|
|
try { |
181
|
|
|
$db_options_table = $config->getOptionsTableName(); |
182
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0"); |
183
|
|
|
$stmt->execute(); |
184
|
|
|
} catch (Exception $e) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
|
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/* |
195
|
|
|
* @param $pdo PDO |
196
|
|
|
* @param $config Configuration |
197
|
|
|
* |
198
|
|
|
*/ |
199
|
|
|
public static function countFrom($pdo, $config, $options = []) { |
200
|
|
|
if (array_key_exists('from_id', $options)) { |
201
|
|
|
$ids = [$options['from_id']]; |
202
|
|
|
} else { |
203
|
|
|
$from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info'; |
204
|
|
|
$ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url); |
205
|
|
|
if (count($ids)==0) { |
206
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
|
|
|
|
207
|
|
|
$stmt->execute([$from_url, $from_url]); |
208
|
|
|
$ids = [$from_url]; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
foreach ($ids as $from_id) { |
212
|
|
|
$stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?"); |
|
|
|
|
213
|
|
|
$stmt->execute([$id, $from_id]); |
|
|
|
|
214
|
|
|
if ($stmt->rowCount()==0) { |
215
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)"); |
216
|
|
|
$stmt->execute([$id, $from_id]); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/* |
222
|
|
|
* @param $pdo PDO |
223
|
|
|
* @param $config Configuration |
224
|
|
|
* |
225
|
|
|
*/ |
226
|
|
|
public static function countOptions($pdo, $config, $options = []) { |
|
|
|
|
227
|
|
|
$user = null; |
228
|
|
|
if ($store_user) { |
|
|
|
|
229
|
|
|
if (array_key_exists('user', $options)) { |
230
|
|
|
$user = $options['user']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($store_time || $store_user) { |
|
|
|
|
235
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)"); |
|
|
|
|
236
|
|
|
$stmt->execute([$id, time(), $user]); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/* |
241
|
|
|
* @param $pdo PDO |
242
|
|
|
* @param $config Configuration |
243
|
|
|
* |
244
|
|
|
*/ |
245
|
|
|
public static function updateCount($pdo, $config, $options = []) { |
246
|
|
|
|
247
|
|
|
$db_hit_table = $config->getHitTableName(); |
|
|
|
|
248
|
|
|
$db_options_table = $config->getOptionsTableName(); |
|
|
|
|
249
|
|
|
$db_from_table = $config->getFromTableName(); |
|
|
|
|
250
|
|
|
$db_url_table = $config->getUrlTableName(); |
|
|
|
|
251
|
|
|
|
252
|
|
|
$store_from = true; |
|
|
|
|
253
|
|
|
$store_time = true; |
|
|
|
|
254
|
|
|
$store_user = true; |
|
|
|
|
255
|
|
|
|
256
|
|
|
Persistence::countHit($pdo, $config, $options); |
|
|
|
|
257
|
|
|
Persistence::countFrom($pdo, $config, $options); |
258
|
|
|
Persistence::countOptions($pdo, $config, $options); |
259
|
|
|
|
260
|
|
|
return true; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/* |
264
|
|
|
* @param $pdo PDO |
265
|
|
|
* @param $config Configuration |
266
|
|
|
* |
267
|
|
|
*/ |
268
|
|
|
public static function findHitIdsByUrl($pdo, $config, $url) { |
269
|
|
|
$resp = []; |
270
|
|
|
try { |
271
|
|
|
|
272
|
|
|
$dbtable = $config->getUrlTableName(); |
273
|
|
|
$stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'"); |
274
|
|
|
if ($stmt->execute()) { |
275
|
|
|
while ($row = $stmt->fetch()) { |
276
|
|
|
$resp[] = $row['id']; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} catch (Exception $e) { |
280
|
|
|
throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage()); |
281
|
|
|
} |
282
|
|
|
return $resp; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/* |
288
|
|
|
* @param $pdo PDO |
289
|
|
|
* @param $config Configuration |
290
|
|
|
* |
291
|
|
|
*/ |
292
|
|
|
public static function findUrls($pdo, $config, $by = []) { |
293
|
|
|
$resp = []; |
294
|
|
|
try { |
295
|
|
|
$dbtable = $config->getUrlTableName(); |
296
|
|
|
$qdata = []; |
297
|
|
|
$tquery = []; |
298
|
|
|
if (array_key_exists('id', $by)) { |
299
|
|
|
$qdata[] = $by['id']; |
300
|
|
|
$tquery[] = "id = ?"; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (array_key_exists('url', $by)) { |
304
|
|
|
$qdata[] = $by['url']; |
305
|
|
|
$tquery[] = "url = ?"; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$sql = "SELECT id,url,count FROM $dbtable"; |
309
|
|
|
if (count($tquery) > 0) { |
310
|
|
|
$sql = $sql." WHERE ".join(" AND ", $tquery); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$stmt = $pdo->prepare($sql); |
314
|
|
|
if ($stmt->execute($qdata)) { |
315
|
|
|
while ($row = $stmt->fetch()) { |
316
|
|
|
$resp[] = [$row['id'], $row['url'], $row['count']]; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} catch (Exception $e) { |
321
|
|
|
throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
322
|
|
|
} |
323
|
|
|
return $resp; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/* |
327
|
|
|
* @param $pdo PDO |
328
|
|
|
* @param $config Configuration |
329
|
|
|
* |
330
|
|
|
*/ |
331
|
|
|
public static function findIdByTimeUser($pdo, $config, $by = []) { |
332
|
|
|
$resp = []; |
333
|
|
|
try { |
334
|
|
|
$dbtable = $config->getOptionsTableName(); |
335
|
|
|
$qdata = []; |
336
|
|
|
$tquery = []; |
337
|
|
|
if (array_key_exists('from', $by)) { |
338
|
|
|
$qdata[] = $by['from']; |
339
|
|
|
$tquery[] = "time >= ?"; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (array_key_exists('to', $by)) { |
343
|
|
|
$qdata[] = $by['to']; |
344
|
|
|
$tquery[] = "time <= ?"; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
if (array_key_exists('user', $by)) { |
348
|
|
|
$qdata[] = $by['user']; |
349
|
|
|
$tquery[] = "user = ?"; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$sql = "SELECT id,time,user FROM $dbtable"; |
353
|
|
|
if (count($tquery) > 0) { |
354
|
|
|
$sql = $sql." WHERE ".join(" AND ", $tquery); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$stmt = $pdo->prepare($sql); |
358
|
|
|
if ($stmt->execute($qdata)) { |
359
|
|
|
while ($row = $stmt->fetch()) { |
360
|
|
|
$resp[] = [$row['id'], $row['time'], $row['user']]; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
} catch (Exception $e) { |
365
|
|
|
throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
366
|
|
|
} |
367
|
|
|
return $resp; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/* |
372
|
|
|
* @param $pdo PDO |
373
|
|
|
* @param $config Configuration |
374
|
|
|
* |
375
|
|
|
*/ |
376
|
|
|
public static function findByFrom($pdo, $config, $by = []) { |
377
|
|
|
$resp = []; |
378
|
|
|
try { |
379
|
|
|
$dbFromtable = $config->getFromTableName(); |
380
|
|
|
$dbUrltable = $config->getUrlTableName(); |
381
|
|
|
$qdata = []; |
382
|
|
|
$tquery = []; |
383
|
|
|
|
384
|
|
|
if (array_key_exists('url', $by) && array_key_exists('id', $by)) { |
385
|
|
|
$qdata = [$by['url'], $by['id']]; |
386
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?"; |
387
|
|
|
} else if (array_key_exists('url', $by)) { |
388
|
|
|
$qdata = [$by['url']]; |
389
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?"; |
390
|
|
|
} else if (array_key_exists('id', $by)) { |
391
|
|
|
$qdata = [$by['id']]; |
392
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?"; |
393
|
|
|
} else { |
394
|
|
|
$qdata = []; |
395
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f"; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$stmt = $pdo->prepare($tquery); |
399
|
|
|
if ($stmt->execute($qdata)) { |
400
|
|
|
while ($row = $stmt->fetch()) { |
401
|
|
|
$resp[] = [$row['id'], $row['from_id'], $row['count']]; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
} catch (Exception $e) { |
406
|
|
|
throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage()); |
407
|
|
|
} |
408
|
|
|
return $resp; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
|
413
|
|
|
/* |
414
|
|
|
* @param $pdo PDO |
415
|
|
|
* @param $config Configuration |
416
|
|
|
* |
417
|
|
|
*/ |
418
|
|
|
public static function getCounts($pdo, $config) |
419
|
|
|
{ |
420
|
|
|
$resp = []; |
421
|
|
|
try { |
422
|
|
|
|
423
|
|
|
$dbHitTable = $config->getHitTableName(); |
424
|
|
|
$dbUrlTable = $config->getUrlTableName(); |
425
|
|
|
$stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id"); |
426
|
|
|
if ($stmt->execute()) { |
427
|
|
|
while ($row = $stmt->fetch()) { |
428
|
|
|
$resp[] = [$row[0], $row[1], $row[2]]; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
} catch (Exception $e) { |
433
|
|
|
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
434
|
|
|
} |
435
|
|
|
return $resp; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
|
439
|
|
|
/* |
440
|
|
|
* @param $pdo PDO |
441
|
|
|
* @param $config Configuration |
442
|
|
|
* |
443
|
|
|
*/ |
444
|
|
|
public static function getHitsWithOptions($pdo, $config) { |
445
|
|
|
$resp = []; |
446
|
|
|
try { |
447
|
|
|
|
448
|
|
|
$dbOptionsTable = $config->getOptionsTableName(); |
449
|
|
|
$stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o"); |
450
|
|
|
if ($stmt->execute()) { |
451
|
|
|
while ($row = $stmt->fetch()) { |
452
|
|
|
$resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]]; |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
} catch (Exception $e) { |
457
|
|
|
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
458
|
|
|
} |
459
|
|
|
return $resp; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/* |
463
|
|
|
* @param $pdo PDO |
464
|
|
|
* @param $config Configuration |
465
|
|
|
* |
466
|
|
|
*/ |
467
|
|
|
public static function getCountsById($pdo, $config) { |
|
|
|
|
468
|
|
|
return "Works"; |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
|
473
|
|
|
|
474
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.