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
|
|
|
use Exception; |
11
|
|
|
/** |
12
|
|
|
* class OptionsDAO |
13
|
|
|
* |
14
|
|
|
* @package SiteAnalyzer |
15
|
|
|
* @author Ruben Dorado <[email protected]> |
16
|
|
|
* @copyright 2018 Ruben Dorado |
17
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
18
|
|
|
*/ |
19
|
|
|
class OptionsDAO |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
* @param $pdo PDO |
|
|
|
|
24
|
|
|
* @param $config Configuration |
25
|
|
|
*/ |
26
|
|
|
public static function checkOptionsTable($pdo, $config) { |
27
|
|
|
try { |
28
|
|
|
$db_options_table = $config->getOptionsTableName(); |
29
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0"); |
30
|
|
|
$stmt->execute(); |
31
|
|
|
} catch (Exception $e) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/* |
38
|
|
|
* @param $pdo PDO |
39
|
|
|
* @param $config Configuration |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
public static function countOptions($pdo, $config, $options = []) { |
|
|
|
|
43
|
|
|
$user = null; |
44
|
|
|
if ($store_user) { |
|
|
|
|
45
|
|
|
if (array_key_exists('user', $options)) { |
46
|
|
|
$user = $options['user']; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($store_time || $store_user) { |
|
|
|
|
51
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)"); |
|
|
|
|
52
|
|
|
$stmt->execute([$id, time(), $user]); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* @param $pdo PDO |
58
|
|
|
* @param $config Configuration |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
public static function findIdByTimeUser($pdo, $config, $by = []) { |
62
|
|
|
$resp = []; |
63
|
|
|
try { |
64
|
|
|
$dbtable = $config->getOptionsTableName(); |
65
|
|
|
$qdata = []; |
66
|
|
|
$tquery = []; |
67
|
|
|
if (array_key_exists('from', $by)) { |
68
|
|
|
$qdata[] = $by['from']; |
69
|
|
|
$tquery[] = "time >= ?"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (array_key_exists('to', $by)) { |
73
|
|
|
$qdata[] = $by['to']; |
74
|
|
|
$tquery[] = "time <= ?"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (array_key_exists('user', $by)) { |
78
|
|
|
$qdata[] = $by['user']; |
79
|
|
|
$tquery[] = "user = ?"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$sql = "SELECT id,time,user FROM $dbtable"; |
83
|
|
|
if (count($tquery) > 0) { |
84
|
|
|
$sql = $sql." WHERE ".join(" AND ", $tquery); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$stmt = $pdo->prepare($sql); |
88
|
|
|
if ($stmt->execute($qdata)) { |
89
|
|
|
while ($row = $stmt->fetch()) { |
90
|
|
|
$resp[] = [$row['id'], $row['time'], $row['user']]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
96
|
|
|
} |
97
|
|
|
return $resp; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/* |
101
|
|
|
* @param $pdo PDO |
102
|
|
|
* @param $config Configuration |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public static function getHitsWithOptions($pdo, $config) { |
106
|
|
|
$resp = []; |
107
|
|
|
try { |
108
|
|
|
|
109
|
|
|
$dbOptionsTable = $config->getOptionsTableName(); |
110
|
|
|
$stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o"); |
111
|
|
|
if ($stmt->execute()) { |
112
|
|
|
while ($row = $stmt->fetch()) { |
113
|
|
|
$resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} catch (Exception $e) { |
118
|
|
|
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
119
|
|
|
} |
120
|
|
|
return $resp; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|