-
Notifications
You must be signed in to change notification settings - Fork 18
/
API.php
119 lines (90 loc) · 2.46 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
<?php
namespace Piwik\Plugins\IP2Location;
use Piwik\Container\StaticContainer;
use Piwik\Http;
use Piwik\Option;
class API extends \Piwik\Plugin\API
{
private static $instance = null;
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
public static function getDatabaseFile()
{
$files = scandir(PIWIK_DOCUMENT_ROOT . '/misc');
foreach ($files as $file) {
if (preg_match('/^(IP(V6)?-COUNTRY.+|IP2LOCATION-LITE-DB[0-9]+(\.IPV6)?)\.BIN$/', $file)) {
Option::set('IP2Location.BIN', $file);
return $file;
}
}
foreach ($files as $file) {
if (strtoupper(substr($file, -4)) == '.BIN') {
Option::set('IP2Location.BIN', $file);
return $file;
}
}
return null;
}
public static function getDatabaseDate($file)
{
if (!is_file(PIWIK_DOCUMENT_ROOT . '/misc/' . $file)) {
return;
}
require_once PIWIK_INCLUDE_PATH . '/plugins/IP2Location/lib/IP2Location.php';
$db = new \IP2Location\Database(PIWIK_DOCUMENT_ROOT . '/misc/' . $file, \IP2Location\Database::FILE_IO);
return $db->getDate();
}
public static function getDatabaseSize($file)
{
if (!file_exists(PIWIK_DOCUMENT_ROOT . '/misc/' . $file)) {
return 0;
}
return self::displayBytes(filesize(PIWIK_DOCUMENT_ROOT . '/misc/' . $file));
}
public static function getLookupMode()
{
return (Option::get('IP2Location.LookupMode')) ? Option::get('IP2Location.LookupMode') : 'BIN';
}
public static function getAPIKey()
{
return (Option::get('IP2Location.APIKey')) ? Option::get('IP2Location.APIKey') : '';
}
public static function setLookupMode($value)
{
Option::set('IP2Location.LookupMode', ($value == 'WS') ? 'WS' : 'BIN');
}
public static function setAPIKey($value)
{
Option::set('IP2Location.APIKey', $value);
}
public static function getWebServiceCredit($apiKey = '')
{
if (!$apiKey) {
$apiKey = self::getAPIKey();
}
if (!$apiKey) {
return 0;
}
if (($json = json_decode(Http::sendHttpRequest('https://api.ip2location.com/v2/?key=' . $apiKey . '&check=1', 30))) === null) {
return 0;
}
if (preg_match('/^[0-9]+$/', $json->response)) {
return (int) $json->response;
}
return 0;
}
private static function displayBytes($bytes)
{
$ext = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$unitCount = 0;
for (; $bytes > 1024; ++$unitCount) {
$bytes /= 1024;
}
return number_format($bytes, 2, '.', ',') . ' ' . $ext[$unitCount];
}
}