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 HitDAO |
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 HitDAO |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
* @param $pdo PDO |
|
|
|
|
24
|
|
|
* @param $config Configuration |
25
|
|
|
*/ |
26
|
|
|
public static function checkHitTable($pdo, $config) { |
27
|
|
|
try { |
28
|
|
|
$db_hit_table = $config->getHitTableName(); |
29
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_hit_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 countHit($pdo, $config, $options = []) { |
43
|
|
|
if (array_key_exists('url', $options)) { |
44
|
|
|
$url = $options['url']; |
45
|
|
|
} else if (array_key_exists('HTTP_HOST', $_SERVER)) { |
46
|
|
|
$url = "http://".$_SERVER['HTTP_HOST']; |
47
|
|
|
if (array_key_exists('REQUEST_URI', $_SERVER)) { |
48
|
|
|
$url = $url.$_SERVER['REQUEST_URI']; |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
$url = "No Info"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($config->getRemoveQueryString()) { |
55
|
|
|
$url = preg_replace('/\?.*/', '', $url); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (array_key_exists('id', $options)) { |
59
|
|
|
$id = $options['id']; |
60
|
|
|
} else { |
61
|
|
|
$id = $url; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?"); |
|
|
|
|
65
|
|
|
$stmt->execute([$id]); |
66
|
|
|
if ($stmt->rowCount()==0) { |
67
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)"); |
68
|
|
|
$stmt->execute([$id]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?"); |
|
|
|
|
72
|
|
|
$stmt->execute([$id, $url]); |
73
|
|
|
if ($stmt->rowCount()==0) { |
74
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
75
|
|
|
$stmt->execute([$id, $url]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* @param $pdo PDO |
83
|
|
|
* @param $config Configuration |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
public static function getAllHits($pdo, $config) { |
87
|
|
|
$resp = []; |
88
|
|
|
try { |
89
|
|
|
$dbtable = $config->getHitTableName(); |
90
|
|
|
$stmt = $pdo->prepare("SELECT id,count FROM $dbtable"); |
91
|
|
|
if ($stmt->execute()) { |
92
|
|
|
while ($row = $stmt->fetch()) { |
93
|
|
|
$resp[] = [$row['id'], $row['count']]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} catch (Exception $e) { |
98
|
|
|
throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage()); |
99
|
|
|
} |
100
|
|
|
return $resp; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|