-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAPI.php
260 lines (227 loc) · 9.98 KB
/
API.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?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: API.php 4448 2011-04-14 08:20:49Z matt $
*
* @category Piwik_Plugins
* @package Piwik_BotTracker
*/
namespace Piwik\Plugins\BotTracker;
use Piwik\DataTable\Row;
use Piwik\Db;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Site;
use Piwik\Date;
use Piwik\Piwik;
/**
* @package Piwik_BotTracker
*/
class API extends \Piwik\Plugin\API
{
private static $instance = null;
public function __construct()
{
$dir = \Piwik\Plugin\Manager::getPluginDirectory('BotTracker');
require_once $dir . '/functions.php';
}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self;
}
return self::$instance;
}
static function getAllBotData($idSite)
{
$rows = Db::get()->fetchAll("SELECT * FROM ".Common::prefixTable('bot_db')." WHERE idSite= ? ORDER BY `botId`", array($idSite));
$rows = self::convertBotLastVisitToLocalTime($rows, $idSite);
// convert this array to a DataTable object
return DataTable::makeFromIndexedArray($rows);
}
static function getAllBotDataForConfig($idsite)
{
$rows = Db::get()->fetchAll("SELECT `idsite`, `botId`, `botName`, `botActive`, `botKeyword`, `extra_stats` FROM ".Common::prefixTable('bot_db')." WHERE `idsite` = ? ORDER BY `botId`", array($idsite));
return $rows;
}
public static function getActiveBotData($idSite)
{
$rows = Db::get()->fetchAll("SELECT `botName`, `botLastVisit`, `botCount` FROM ".Common::prefixTable('bot_db')." WHERE `botActive` = 1 AND idSite= ? ORDER BY `botId`", array($idSite));
$rows = self::convertBotLastVisitToLocalTime($rows, $idSite);
// convert this array to a DataTable object
return DataTable::makeFromIndexedArray($rows);
}
function getAllBotDataWithIcon($idSite)
{
$dataTable = $this->getAllBotData($idSite);
$dataTable->renameColumn('botActive', 'label');
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getActiveIcon'));
$dataTable->filter('ColumnCallbackReplace', array('label', function ($label) {
return ' ';
}));
$dataTable->queueFilter('ColumnCallbackAddMetadata', array(array(), 'logoWidth', function () {
return 16;
}));
$dataTable->queueFilter('ColumnCallbackAddMetadata', array(array(), 'logoHeight', function () {
return 16;
}));
return $dataTable;
}
static function getAllBotDataPie($idSite)
{
$rows = Db::get()->fetchAll("SELECT `botName`, `botCount` FROM ".Common::prefixTable('bot_db')." WHERE `idSite`= ? ORDER BY `botCount` DESC LIMIT 10", array($idSite));
$i = 0;
$keys[0] = "";
$values[0] = "";
foreach ($rows as $row) {
$keys[$i] = $row['botName'];
$values[$i] = $row['botCount'];
$i++;
}
$pieArray = array_combine($keys, $values);
// convert this array to a DataTable object
return DataTable::makeFromIndexedArray($pieArray);
}
static function updateBot($botName, $botKeyword, $botActive, $botId, $extraStats)
{
Db::get()->query("UPDATE `".Common::prefixTable('bot_db')."`
SET `botName` = ?
, `botKeyword` = ?
, `botActive` = ?
, `extra_stats` = ?
WHERE `botId` = ?", array(self::htmlentities2utf8($botName), self::htmlentities2utf8($botKeyword), $botActive, $extraStats, $botId));
}
static function insertBot($idSite, $botName, $botActive, $botKeyword, $extraStats)
{
Db::get()->query(
"INSERT INTO `".Common::prefixTable('bot_db')."`
(`idsite`,`botName`, `botActive`, `botKeyword`, `botCount`, `extra_stats`)
VALUES (?,?,?,?,0,?)",
array($idSite, self::htmlentities2utf8($botName), $botActive, self::htmlentities2utf8($botKeyword), $extraStats)
);
}
static function insert_default_bots($idsite = 0)
{
$i = 0;
if ($idsite <> 0) {
// Only admin is allowed to do this!
Piwik::checkUserHasSuperUserAccess();
$botList = array();
$botList[] = array('MSN Search' ,'MSNBOT' );
$botList[] = array('MSN Bot Media' ,'msnbot-media' );
$botList[] = array('Bingbot' ,'bingbot' );
$botList[] = array('GoogleBot' ,'Googlebot' );
$botList[] = array('Google Instant' ,'Google Web Preview' );
$botList[] = array('Media Partners GoogleBot','Mediapartners-Google');
$botList[] = array('Baiduspider' ,'Baiduspider' );
$botList[] = array('Ezooms' ,'Ezooms' );
$botList[] = array('YahooSeeker' ,'YahooSeeker' );
$botList[] = array('Yahoo! Slurp' ,'Slurp' );
$botList[] = array('Altavista 1' ,'AltaVista' );
$botList[] = array('Altavista 2' ,'AVSearch' );
$botList[] = array('Altavista 3' ,'Mercator' );
$botList[] = array('Altavista 4' ,'Scooter' );
$botList[] = array('Infoseek 1' ,'InfoSeek' );
$botList[] = array('Infoseek 2' ,'Ultraseek' );
$botList[] = array('Lycos' ,'Lycos' );
$botList[] = array('Wget' ,'Wget' );
$botList[] = array('Yandex' ,'YandexBot' );
$botList[] = array('Yandex ?' ,'Java/1.4.1_04' );
$botList[] = array('SiteBot' ,'SiteBot' );
$botList[] = array('Exabot' ,'Exabot' );
$botList[] = array('AhrefsBot' ,'AhrefsBot' );
$botList[] = array('MJ12Bot' ,'MJ12bot' );
$botList[] = array('NetSeer Crawler' ,'NetSeer crawler' );
$botList[] = array('TurnitinBot' ,'TurnitinBot' );
$botList[] = array('Magpie Crawler' ,'magpie-crawler' );
$botList[] = array('Nutch Crawler' ,'Nutch Crawler' );
$botList[] = array('CMS Crawler' ,'CMS Crawler' );
$botList[] = array('RogerBot' ,'rogerbot' );
$botList[] = array('Domnutch-Bot' ,'Domnutch' );
$botList[] = array('Ssearch Crawler' ,'ssearch_bot' );
$botList[] = array('XoviBot' ,'XoviBot' );
$botList[] = array('Digincore' ,'digincore' );
$botList[] = array('Fr-Crawler' ,'fr-crawler' );
foreach ($botList as $bot) {
$botX = self::getBotByName($idsite, $bot[0]);
if (empty($botX)) {
self::insertBot($idsite, $bot[0], 1, $bot[1], 0);
$i++;
}
}
}
return $i;
}
static function deleteBot($botId)
{
Db::get()->query("DELETE FROM `".Common::prefixTable('bot_db')."` WHERE `botId` = ?", array($botId));
}
static function getBotByName($idSite, $botName)
{
$rows = Db::get()->fetchAll("SELECT * FROM ".Common::prefixTable('bot_db')." WHERE `botName` = ? AND `idSite`= ? ORDER BY `botId`", array($botName, $idSite));
$rows = self::convertBotLastVisitToLocalTime($rows, $idSite);
return $rows;
}
static function convertBotLastVisitToLocalTime($rows, $idSite)
{
// convert lastVisit to localtime
$timezone = Site::getTimezoneFor($idSite);
foreach ($rows as &$row) {
if ($row['botLastVisit'] == '0000-00-00 00:00:00') {
$row['botLastVisit'] = " - ";
} elseif ($row['botLastVisit'] == '2000-01-01 00:00:00') {
$row['botLastVisit'] = " - ";
} else {
$botLastVisit = Date::adjustForTimezone(strtotime($row['botLastVisit']), $timezone);
$row['botLastVisit'] = date('Y-m-d H:i:s', $botLastVisit);
}
}
return $rows;
}
static function htmlentities2utf8($string)
{
$output = preg_replace_callback("/(&#[0-9]+;)/", function ($m) {
return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
}, $string);
return html_entity_decode($output);
}
/**
* Get Data for the Report "Top10"
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable
*/
public function getTop10($idSite, $period, $date, $segment = false)
{
return $this->getAllBotDataPie($idSite);
}
/**
* Get Data for the Report "BotTracker"
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable
*/
public function getBotTracker($idSite, $period, $date, $segment = false)
{
return $this->getAllBotDataWithIcon($idSite);
}
/**
* Get Data for Dashboard-Widget
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable
*/
public function getBotTrackerAnzeige($idSite, $period, $date, $segment = false)
{
return $this->getActiveBotData($idSite);
}
}