|
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
|
|
|
* @param $pdo PDO |
|
58
|
|
|
* @param $config Configuration |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function crateDatabase($pdo, $config) { |
|
62
|
|
|
try { |
|
63
|
|
|
$db_hit_table = $config->getHitTableName(); |
|
64
|
|
|
$db_options_table = $config->getOptionsTableName(); |
|
65
|
|
|
$db_from_table = $config->getFromTableName(); |
|
66
|
|
|
$db_url_table = $config->getUrlTableName(); |
|
67
|
|
|
|
|
68
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)"); |
|
69
|
|
|
$stmt->execute(); |
|
70
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))"); |
|
71
|
|
|
$stmt->execute(); |
|
72
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)"); |
|
73
|
|
|
$stmt->execute(); |
|
74
|
|
|
$stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
|
75
|
|
|
$stmt->execute(); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
throw new Exception("Could not create the database. ".$e->getMessage()); |
|
78
|
|
|
} |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/* |
|
83
|
|
|
* @param $pdo PDO |
|
84
|
|
|
* @param $config Configuration |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function deleteDatabase($pdo, $config) { |
|
88
|
|
|
$resp = true; |
|
89
|
|
|
|
|
90
|
|
|
$db_hit_table = $config->getHitTableName(); |
|
91
|
|
|
$db_options_table = $config->getOptionsTableName(); |
|
92
|
|
|
$db_from_table = $config->getFromTableName(); |
|
93
|
|
|
$db_url_table = $config->getUrlTableName(); |
|
94
|
|
|
|
|
95
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_hit_table); |
|
96
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_options_table); |
|
97
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_from_table); |
|
98
|
|
|
$resp = $resp && Persistence::dropTable($pdo, $db_url_table); |
|
99
|
|
|
|
|
100
|
|
|
return $resp; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* |
|
104
|
|
|
* @param $pdo PDO |
|
105
|
|
|
* @param $config Configuration |
|
106
|
|
|
* |
|
107
|
|
|
*/ |
|
108
|
|
|
private static function dropTable($pdo, $tableName) { |
|
109
|
|
|
try { |
|
110
|
|
|
$stmt = $pdo->prepare("DROP TABLE $tableName"); |
|
111
|
|
|
$stmt->execute(); |
|
112
|
|
|
|
|
113
|
|
|
} catch (Exception $e) { |
|
114
|
|
|
throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
|
115
|
|
|
} |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/* |
|
120
|
|
|
* @param $pdo PDO |
|
121
|
|
|
* @param $config Configuration |
|
122
|
|
|
* |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function checkTables($pdo, $config) { |
|
125
|
|
|
$resp = true; |
|
126
|
|
|
try { |
|
127
|
|
|
|
|
128
|
|
|
$resp = $resp && HitDAO::checkHitTable($pdo, $config); |
|
129
|
|
|
$resp = $resp && OptionsDAO::checkOptionsTable($pdo, $config); |
|
130
|
|
|
$resp = $resp && FromDAO::checkFromTable($pdo, $config); |
|
131
|
|
|
$resp = $resp && UrlDAO::checkUrlTable($pdo, $config); |
|
132
|
|
|
} catch (Exception $e) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
return $resp; |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* |
|
140
|
|
|
* @param options |
|
141
|
|
|
* |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function getURL($config, $options = []) { |
|
144
|
|
|
if (array_key_exists('url', $options)) { |
|
145
|
|
|
$url = $options['url']; |
|
146
|
|
|
} else if (array_key_exists('HTTP_HOST', $_SERVER)) { |
|
147
|
|
|
$url = "http://".$_SERVER['HTTP_HOST']; |
|
148
|
|
|
if (array_key_exists('REQUEST_URI', $_SERVER)) { |
|
149
|
|
|
$url = $url.$_SERVER['REQUEST_URI']; |
|
150
|
|
|
} |
|
151
|
|
|
} else { |
|
152
|
|
|
$url = "No Info"; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($config->getRemoveQueryString()) { |
|
156
|
|
|
$url = preg_replace('/\?.*/', '', $url); |
|
157
|
|
|
} |
|
158
|
|
|
return $url; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/* |
|
162
|
|
|
* @param $pdo PDO |
|
163
|
|
|
* @param $config Configuration |
|
164
|
|
|
* |
|
165
|
|
|
*/ |
|
166
|
|
|
public static function updateCount($pdo, $config, $options = []) { |
|
167
|
|
|
$url = Persistence::getUrl($config, $options); |
|
168
|
|
|
|
|
169
|
|
|
if (array_key_exists('id', $options)) { |
|
170
|
|
|
$id = $options['id']; |
|
171
|
|
|
} else { |
|
172
|
|
|
$id = $url; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
HitDAO::countHit($pdo, $config, $id, $url); |
|
176
|
|
|
FromDAO::countFrom($pdo, $config, $id, $options); |
|
177
|
|
|
OptionsDAO::countOptions($pdo, $config, $id, $options); |
|
178
|
|
|
|
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/* |
|
183
|
|
|
* @param $pdo PDO |
|
184
|
|
|
* @param $config Configuration |
|
185
|
|
|
* |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function getCounts($pdo, $config) |
|
188
|
|
|
{ |
|
189
|
|
|
$resp = []; |
|
190
|
|
|
try { |
|
191
|
|
|
|
|
192
|
|
|
$dbHitTable = $config->getHitTableName(); |
|
193
|
|
|
$dbUrlTable = $config->getUrlTableName(); |
|
194
|
|
|
$stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id"); |
|
195
|
|
|
if ($stmt->execute()) { |
|
196
|
|
|
while ($row = $stmt->fetch()) { |
|
197
|
|
|
$resp[] = [$row[0], $row[1], $row[2]]; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
} catch (Exception $e) { |
|
202
|
|
|
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
|
203
|
|
|
} |
|
204
|
|
|
return $resp; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/* |
|
208
|
|
|
* @param $pdo PDO |
|
209
|
|
|
* @param $config Configuration |
|
210
|
|
|
*/ |
|
211
|
|
|
public static function checkUrlTable($pdo, $config) { |
|
212
|
|
|
return UrlDAO::checkUrlTable($pdo, $config); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/* |
|
216
|
|
|
* @param $pdo PDO |
|
217
|
|
|
* @param $config Configuration |
|
218
|
|
|
* |
|
219
|
|
|
*/ |
|
220
|
|
|
public static function getCountsById($pdo, $config) { |
|
221
|
|
|
return "Works ".$pdo." ".$config; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/* |
|
225
|
|
|
* @param $pdo PDO |
|
226
|
|
|
* @param $config Configuration |
|
227
|
|
|
* |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function getAllHits($pdo, $config) { |
|
230
|
|
|
return HitDAO::getAllHits($pdo, $config); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/* |
|
234
|
|
|
* @param $pdo PDO |
|
235
|
|
|
* @param $config Configuration |
|
236
|
|
|
* |
|
237
|
|
|
*/ |
|
238
|
|
|
public static function findUrls($pdo, $config, $by = []) { |
|
239
|
|
|
return UrlDAO::findUrls($pdo, $config, $by); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/* |
|
243
|
|
|
* @param $pdo PDO |
|
244
|
|
|
* @param $config Configuration |
|
245
|
|
|
* |
|
246
|
|
|
*/ |
|
247
|
|
|
public static function findIdByTimeUser($pdo, $config, $by=[]) { |
|
248
|
|
|
return OptionsDAO::findIdByTimeUser($pdo, $config, $by); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/* |
|
252
|
|
|
* @param $pdo PDO |
|
253
|
|
|
* @param $config Configuration |
|
254
|
|
|
* |
|
255
|
|
|
*/ |
|
256
|
|
|
public static function findByFrom($pdo, $config, $by = []) { |
|
257
|
|
|
return FromDAO::findByFrom($pdo, $config, $by); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
|