Repository navigation
Expand file tree
/
Copy pathTasks.php
More file actions
125 lines (100 loc) · 3.86 KB
/
Copy pathTasks.php
File metadata and controls
125 lines (100 loc) · 3.86 KB
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
<?php
namespace Piwik\Plugins\IP2Location;
use Piwik\Http;
use Piwik\Option;
use Piwik\Plugin\Tasks as PluginTasks;
class Tasks extends PluginTasks
{
/**
* Registers the database update task.
*/
public function schedule()
{
$scheduledTask = Option::get('IP2Location.ScheduledTask');
if ($scheduledTask == 'monthly') {
$this->monthly('downloadBinDatabase');
} elseif ($scheduledTask == 'weekly') {
$this->weekly('downloadBinDatabase');
}
}
/**
* Updates the BIN database when a new build is available.
*/
public function downloadBinDatabase()
{
$token = Option::get('IP2Location.DownloadToken');
$code = Option::get('IP2Location.DatabaseCode');
if (empty($token) || empty($code)) {
return;
}
$response = Http::sendHttpRequest('https://www.ip2location.com/download-info?' . http_build_query([
'token' => $token,
'package' => $code,
]), 30);
if (strpos($response, 'OK') === false) {
return;
}
$parts = explode(';', $response);
$size = $parts[3] ?? 0;
$databasePath = Option::get('IP2Location.DatabasePath');
$hasLocalDatabase = is_file($databasePath);
if (!$hasLocalDatabase && is_dir($databasePath)) {
foreach ((array) scandir($databasePath) as $localFile) {
if (preg_match('/^IP(V6)?-COUNTRY.*\.BIN$/', $localFile) || preg_match('/^IP2LOCATION-LITE-DB[0-9]+(\.IPV6)?\.BIN$/', $localFile)) {
$hasLocalDatabase = true;
break;
}
}
}
if ($size == Option::get('IP2Location.DownloadedDatabaseSize') && $hasLocalDatabase) {
return;
}
$databasePath = rtrim((substr($databasePath, -3) == 'BIN') ? dirname($databasePath) : $databasePath, '/');
if (empty($databasePath)) {
$databasePath = str_replace('\\', '/', PIWIK_DOCUMENT_ROOT) . '/misc';
}
$zipFile = $databasePath . '/ip2location.zip';
$extractPath = $databasePath . '/tmp/';
if (is_dir($extractPath)) {
$files = scandir($extractPath);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
@unlink($extractPath . $file);
}
} else {
@mkdir($extractPath);
}
try {
$success = Http::sendHttpRequest('https://www.ip2location.com/download?' . http_build_query([
'token' => $token,
'file' => $code,
]), 600, null, $zipFile);
if ($success !== true) {
return;
}
$zip = new \ZipArchive();
if ($zip->open($zipFile) === true) {
$zip->extractTo($extractPath);
$zip->close();
$files = scandir($extractPath);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
if (preg_match('/^IP(V6)?-COUNTRY.*\.BIN$/', $file) || preg_match('/^IP2LOCATION-LITE-DB[0-9]+(\.IPV6)?\.BIN$/', $file)) {
copy($extractPath . $file, $databasePath . '/' . $file);
Option::set('IP2Location.DatabasePath', str_replace('\\', '/', $databasePath . '/' . $file));
Option::set('IP2Location.DownloadedDatabaseSize', $size);
Option::set('IP2Location.LastScheduledTaskDate', date('Y-m-d H:i:s'));
}
@unlink($extractPath . $file);
}
}
@unlink($zipFile);
@rmdir($extractPath);
} catch (\Exception $e) {
}
}
}