-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
API.php
330 lines (284 loc) · 11.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPtoCompany;
use Piwik\Db;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Piwik;
use Piwik\API\Request;
use Piwik\Plugins\IPtoCompany\Libraries\IPInfo;
use Piwik\Container\StaticContainer;
use \Exception;
const EMPTY_HOSTNAME = "-";
/**
* API for plugin IPtoCompany
*
* @method static \Piwik\Plugins\IPtoCompany\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
public $cacheLifeTimeForResults;
private $staticContainer;
public function __construct(StaticContainer $staticContainer)
{
// Get the access token
$systemSettings = new \Piwik\Plugins\IPtoCompany\SystemSettings();
$cacheLifeTimeForResults = $systemSettings->cacheLifeTimeForResults->getValue();
$this->cacheLifeTimeForResults = $cacheLifeTimeForResults <= 0 ? 2 : $cacheLifeTimeForResults;
$this->staticContainer = $staticContainer;
}
/**
* Returns a data table with the visits.
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @param int $filterLimit
* @return DataTable
*/
public function getCompanies($idSite, $period, $date, $segment = false, $filterLimit = 200)
{
Piwik::checkUserHasViewAccess($idSite);
$logger = $this->staticContainer->getContainer()->get('Psr\Log\LoggerInterface');
$response = Request::processRequest('Live.getLastVisitsDetails', [
'idSite' => $idSite,
'period' => $period,
'date' => $date,
'segment' => $segment,
'flat' => FALSE,
'doNotFetchActions' => FALSE,
'countVisitorsToFetch' => $filterLimit
// 'token_auth' => $_ENV['AUTH_TOKEN']
]);
$response->applyQueuedFilters();
$result = $response->getEmptyClone($keepFilters = false);
// Prepare an array containing the list of IP addresses to avoid multiple calls for the same IP address
$ipList = [];
// Get the list of IPs saved in the DB
$dbList = \Piwik\API\Request::processRequest('IPtoCompany.getStoredCompanies', []);
foreach ($response->getRows() as $visitRow) {
$visitIp = $visitRow->getColumn('visitIp');
// try and get the row in the result DataTable for the IP
$ipRow = $result->getRowFromLabel($visitIp);
// Get the company name based on the IP
$ipList = $this->getIPDetails($visitIp, $ipList, $dbList);
$companyName = $ipList[$visitIp];
// if there is no row for this IP, create it
if ($ipRow === false) {
$result->addRowFromSimpleArray(array(
'IP' => $visitIp,
'company' => stripslashes($companyName),
'last_visit_time' => $visitRow->getColumn('lastActionDateTime'),
'type' => $visitRow->getColumn('visitorType'),
'nb_visits' => $visitRow->getColumn('visitCount'),
'last_visit_duration' => $visitRow->getColumn('visitDurationPretty'),
'referrer_type' => $visitRow->getColumn('referrerType'),
'referrer_name' => $visitRow->getColumn('referrerName'),
'device' => $visitRow->getColumn('deviceType'),
'country' => $visitRow->getColumn('country'),
'city' => $visitRow->getColumn('city'),
));
}
// if there is a row, increment the counter
/*else {
$counter = $ipRow->getColumn('nb_visits');
$ipRow->setColumn('nb_visits', $counter + 1);
}*/
}
return $result;
}
/**
* Returns an array containing the IP and the company
* @param string $period
* @param string $date
* @return array
*/
public function getStoredCompanies()
{
$rows = NULL;
try {
$rows = Db::fetchAll("SELECT * FROM " . Common::prefixTable('ip_to_company'));
} catch (Exception $e) {
// ignore error if table already exists (1050 code is for 'table already exists')
if (!Db::get()->isErrNo($e, '1050')) {
throw $e;
}
}
return $rows;
}
/**
* Another example method that returns a data table.
* @param string $ip
* @return array
*/
private function getIPDetails($ip, $ipList, $dbList)
{
$itemFound = FALSE;
$companyName = NULL;
$hostname = filter_var($ip, FILTER_VALIDATE_IP) ? gethostbyaddr($ip) : EMPTY_HOSTNAME;
if(!isset($ipList[$ip])) {
$delay = new \Datetime();
$delay->sub(new \DateInterval('P' . $this->cacheLifeTimeForResults . 'W'));
// Check if the IP address exists in the DB and if the record is younger than the defined cache
foreach ($dbList as $item) {
$itemDate = new \Datetime($item['updated_at']);
if(($item['ip'] == $ip) && ($delay <= $itemDate)) {
$ipList[$ip] = $item['as_name'];
$itemFound = TRUE;
}
elseif(($item['ip'] == $ip) && ($itemDate < $delay)) {
$companyDetails = $this->getCompanyDetails($ip);
$companyName = $companyDetails['as_name'];
$itemFound = $companyName ? TRUE : FALSE;
$ipList[$ip] = $companyName ? $companyName : ($hostname === $ip ? EMPTY_HOSTNAME : $hostname);
// We update the DB only if we got results from the getCompanyDetails method.
if(($ipList[$ip] != $hostname) && $companyName) {
$this->updateCompanyDetails($item, [
'as_number' => $companyDetails['as_number'],
'as_name' => $companyDetails['as_name']
]);
}
}
}
}
// If the IP doesn't exist in the DB, and if it is a valid IP, try to get the details
if(!isset($ipList[$ip]) && !$itemFound && filter_var($ip, FILTER_VALIDATE_IP)) {
$companyDetails = $this->getCompanyDetails($ip);
$companyName = $companyDetails['as_name'];
$itemFound = $companyName ? TRUE : FALSE;
$ipList[$ip] = $companyName ? $companyName : ($hostname === $ip ? EMPTY_HOSTNAME : $hostname);
// We insert the item in the DB only if we got results from the getCompanyDetails method.
if(($ipList[$ip] != $hostname) && $companyName) {
$this->insertCompanyDetails([
'ip' => $ip,
'as_number' => $companyDetails['as_number'],
'as_name' => $companyDetails['as_name']
]);
}
}
// If the IP is not valid, just return the empty hostname
elseif(!filter_var($ip, FILTER_VALIDATE_IP)) {
$ipList[$ip] = EMPTY_HOSTNAME;
}
return $ipList;
}
/**
* Another example method that returns a data table.
* @param string $ip
* @return array
*/
private function getCompanyDetails($ip)
{
$ipInfo = new IPInfo();
// If no token has been set, stop here.
if(!$ipInfo->accessToken) {
return [
"as_name" => NULL,
"as_number" => NULL
];
}
try {
$details = $ipInfo->getDetails($ip);
} catch (\Exception $e) {
return [
"as_name" => NULL,
"as_number" => NULL
];
}
$details = json_decode($details);
$companyName = NULL;
$asNumber = NULL;
if(isset($details->company) && isset($details->company->name)) {
$companyName = $details->company->name;
if($details->company->domain) {
$companyName .= " (" . $details->company->domain . ")";
}
}
elseif(isset($details->org) && !isset($details->org->name)) {
$orgElements = explode(" ", $details->org);
$asNumber = array_shift($orgElements);
$asName = count($orgElements) > 1 ? implode(" ", $orgElements) : $orgElements[0];
$companyName = $asName;
}
return [
"as_name" => $companyName,
"as_number" => $asNumber
];
}
/**
* A private methode to update the company details that we found in the DB
* @param array $item
* @param array $data
* @return boolean
*/
private function updateCompanyDetails($item, $data)
{
try {
// If the server is running PHP 7.4.0 or newer
if($this->isPHPVersionMoreRecentThan("7.4.0")) {
$asName = filter_var($data['as_name'], FILTER_SANITIZE_ADD_SLASHES);
}
else {
$asName = filter_var($data['as_name'], FILTER_SANITIZE_MAGIC_QUOTES);
}
$sql = "UPDATE " . Common::prefixTable('ip_to_company') . "
SET as_number = '{$data['as_number']}', as_name = '{$asName}'
WHERE id = {$item['id']}";
Db::exec($sql);
} catch (Exception $e) {
throw $e;
}
return TRUE;
}
/**
* A private methode to save the company details in the DB
* @param array $data
* @return boolean
*/
private function insertCompanyDetails($data)
{
try {
if($this->isPHPVersionMoreRecentThan("7.4.0")) {
$asName = filter_var($data['as_name'], FILTER_SANITIZE_ADD_SLASHES);
}
else {
$asName = filter_var($data['as_name'], FILTER_SANITIZE_MAGIC_QUOTES);
}
$sql = "INSERT INTO " . Common::prefixTable('ip_to_company') . "
(ip, as_number, as_name) VALUES
('{$data['ip']}', '{$data['as_number']}', '{$asName}')";
Db::exec($sql);
} catch (Exception $e) {
throw $e;
}
return TRUE;
}
/**
* A private methode to save the company details in the DB
* @param array $data
* @return boolean
*/
private function isPHPVersionMoreRecentThan($version)
{
$phpVersion = phpversion();
$phpVersionParts = explode(".", $phpVersion);
$phpMinVersionParts = explode(".", $version);
if((int)$phpVersionParts[0] < (int)$phpMinVersionParts[0]) {
return FALSE;
}
elseif((int)$phpVersionParts[1] < (int)$phpMinVersionParts[1]) {
return FALSE;
}
elseif((int)$phpVersionParts[2] < (int)$phpMinVersionParts[2]) {
return FALSE;
}
return TRUE;
}
}