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 FromDAO |
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 FromDAO |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
* @param $pdo \PDO |
24
|
|
|
* @param $config Configuration |
25
|
|
|
*/ |
26
|
|
|
public static function checkFromTable($pdo, $config) { |
27
|
|
|
try { |
28
|
|
|
$db_from_table = $config->getFromTableName(); |
29
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_from_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 countFrom($pdo, $config, $id, $options = []) { |
43
|
|
|
$db_url_table = $config->getUrlTableName(); |
44
|
|
|
$db_from_table = $config->getFromTableName(); |
45
|
|
|
|
46
|
|
|
if (array_key_exists('from_id', $options)) { |
47
|
|
|
$ids = [$options['from_id']]; |
48
|
|
|
} else { |
49
|
|
|
$from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info'; |
50
|
|
|
$ids = UrlDAO::findHitIdsByUrl($pdo, $config, $from_url); |
51
|
|
|
if (count($ids)==0) { |
52
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
53
|
|
|
$stmt->execute([$from_url, $from_url]); |
54
|
|
|
$ids = [$from_url]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($ids as $from_id) { |
59
|
|
|
$stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?"); |
60
|
|
|
$stmt->execute([$id, $from_id]); |
61
|
|
|
if ($stmt->rowCount()==0) { |
62
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)"); |
63
|
|
|
$stmt->execute([$id, $from_id]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* @param $pdo \PDO |
70
|
|
|
* @param $config Configuration |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
public static function findByFrom($pdo, $config, $by = []) { |
74
|
|
|
$resp = []; |
75
|
|
|
|
76
|
|
|
$dbFromtable = $config->getFromTableName(); |
77
|
|
|
$dbUrltable = $config->getUrlTableName(); |
78
|
|
|
$qdata = []; |
79
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f"; |
80
|
|
|
|
81
|
|
|
if (array_key_exists('url', $by)) { |
82
|
|
|
$qdata = [$by['url']]; |
83
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?"; |
84
|
|
|
if (array_key_exists('id', $by)) { |
85
|
|
|
$qdata = [$by['url'], $by['id']]; |
86
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?"; |
87
|
|
|
} |
88
|
|
|
} else if (array_key_exists('id', $by)) { |
89
|
|
|
$qdata = [$by['id']]; |
90
|
|
|
$tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$stmt = $pdo->prepare($tquery); |
94
|
|
|
$stmt->execute($qdata); |
95
|
|
|
while ($row = $stmt->fetch()) { |
96
|
|
|
$resp[] = [$row['id'], $row['from_id'], $row['count']]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $resp; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|