-
Notifications
You must be signed in to change notification settings - Fork 14
/
BotTracker.php
176 lines (153 loc) · 5.47 KB
/
BotTracker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id: $
*/
/**
* @package Piwik_BotTracker
*/
namespace Piwik\Plugins\BotTracker;
use Piwik\Common;
use Piwik\Db;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Piwik;
use Piwik\Tracker;
use Piwik\Plugin;
use Piwik\Plugins\BotTracker\API as BotTrackerAPI;
class BotTracker extends \Piwik\Plugin
{
protected $botDb = null;
public function __destruct()
{
if (!is_null($this->botDb)) {
// What is this?
//botDb_close($this->botDb);
// returns true as I can't find botDb_close.
return 0;
}
}
public function postLoad()
{
$dir = Plugin\Manager::getPluginDirectory('BotTracker');
require_once $dir . '/functions.php';
}
public function install()
{
$tableExists = false;
// create new table "botDB"
$query = "CREATE TABLE `".Common::prefixTable('bot_db')."`
(`botId` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`idsite` INTEGER(10) UNSIGNED NOT NULL,
`botName` VARCHAR(100) NOT NULL,
`botActive` BOOLEAN NOT NULL,
`botKeyword` VARCHAR(32) NOT NULL,
`botCount` INTEGER(10) UNSIGNED NOT NULL,
`botLastVisit` TIMESTAMP NOT NULL,
`extra_stats` BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY(`botId`)
) DEFAULT CHARSET=utf8";
// if the table already exist do not throw error. Could be installed twice...
try {
Db::query($query);
} catch (\Exception $e) {
$tableExists = true;
}
if (!$tableExists) {
$sites = APISitesManager::getInstance()->getSitesWithAdminAccess();
foreach ($sites as $site) {
BotTrackerAPI::insert_default_bots($site['idsite']);
}
}
// Create extendet_stats_table
$query4 = 'CREATE TABLE IF NOT EXISTS `'.Common::prefixTable('bot_db_stat').'`
(
`visitId` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`botId` INTEGER(10) UNSIGNED NOT NULL,
`idsite` INTEGER(10) UNSIGNED NOT NULL,
`page` VARCHAR(100) NOT NULL,
`visit_timestamp` TIMESTAMP NOT NULL,
`useragent` VARCHAR(100) NOT NULL,
PRIMARY KEY(`visitId`,`botId`,`idsite`)
) DEFAULT CHARSET=utf8';
Db::query($query4);
}
public function uninstall()
{
$query = "DROP TABLE `".Common::prefixTable('bot_db')."` ";
Db::query($query);
$query2 = "DROP TABLE `".Common::prefixTable('bot_db_stat')."` ";
Db::query($query2);
}
public function registerEvents()
{
return [
'Tracker.isExcludedVisit' => 'checkBot',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
];
}
/**
* Get translations strings to js.
*/
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = 'BotTracker_BotTracker';
$translationKeys[] = 'BotTracker_PluginDescription';
$translationKeys[] = 'BotTracker_insert_db';
$translationKeys[] = 'BotTracker_NoOfActiveBots';
}
function checkBot(&$exclude, $request)
{
$ua = $request->getUserAgent();
$idSite = $request->getIdSite();
$currentTimestamp = gmdate("Y-m-d H:i:s");
// max length of url can be 100 Bytes
$currentUrl = substr($request->getParam('url'), 0, 100);
self::logToFile('SiteID:'.$idSite.' user Agent: '.$ua.' TS:'.$currentTimestamp.' page:'.$currentUrl);
$db = Tracker::getDatabase();
$result = $db->fetchRow("SELECT `botId`, `extra_stats` FROM ".Common::prefixTable('bot_db')."
WHERE `botActive` = 1
AND `idSite` = ?
AND LOCATE(`botKeyword`,?) >0
LIMIT 1", array($idSite, $ua));
$botId = $result['botId']??0;
if ($botId > 0) {
self::logToFile('SiteID:'.$idSite.' found Bot: '.$botId);
$db->query("UPDATE `".Common::prefixTable('bot_db')."`
SET botCount = botCount + 1
, botLastVisit = ?
WHERE botId = ?", array($currentTimestamp, $botId));
$exclude = true;
if ($result['extra_stats'] > 0) {
$query = "INSERT INTO `".Common::prefixTable('bot_db_stat')."`
(idsite, botid, page, visit_timestamp, useragent) VALUES (?,?,?,?,?)";
// max length of useragent can be 100 Bytes
$params = array($idSite,$botId,$currentUrl,$currentTimestamp,substr($ua, 0, 100));
$db->query($query, $params);
}
}
}
public function logToFile($msg)
{
// to aktivate logging just change the value to "true".
$logActive = false;
if ($logActive) {
$pfad = "tmp/logs/";
$filename = "log.txt";
// open file
$fd = fopen($pfad.$filename, "a");
// append date/time to message
if (is_array($msg)) {
$str = "[" . date("Y/m/d H:i:s", time()) . "] " . var_export($msg, true);
} else {
$str = "[" . date("Y/m/d H:i:s", time()) . "] " . $msg;
}
// write string
fwrite($fd, $str . "\n");
// close file
fclose($fd);
}
}
}