-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTasks.php
More file actions
97 lines (78 loc) · 2.37 KB
/
Copy pathTasks.php
File metadata and controls
97 lines (78 loc) · 2.37 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
<?php
namespace Piwik\Plugins\IP2Location;
use Piwik\Http;
use Piwik\Plugin\Tasks as PluginTasks;
use Piwik\Plugins\IP2Location\API as IP2LocationPlugin;
class Tasks extends PluginTasks
{
public function schedule()
{
if (IP2LocationPlugin::getScheduledTask() == 'monthly') {
$this->monthly('downloadBinDatabase');
} elseif (IP2LocationPlugin::getScheduledTask() == 'weekly') {
$this->weekly('downloadBinDatabase');
}
}
public function downloadBinDatabase()
{
$token = IP2LocationPlugin::getDownloadToken();
$code = IP2LocationPlugin::getDatabaseCode();
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;
if ($size == IP2LocationPlugin::getDownloadedDatabaseSize()) {
return;
}
$databasePath = IP2LocationPlugin::getDatabasePath();
$databasePath = rtrim((substr($databasePath, -3) == 'BIN') ? dirname($databasePath) : $databasePath, '/');
$zipFile = $databasePath . '/ip2location.zip';
$extractPath = $databasePath . '/tmp/';
echo $zipFile;
exit;
if (is_dir($extractPath)) {
$files = scandir($extractPath);
foreach ($files as $file) {
@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();
// Find the BIN file
$files = scandir($extractPath);
foreach ($files as $file) {
if (preg_match('/^IP(V6)?-COUNTRY.*\.BIN$/', $file) || preg_match('/^IP2LOCATION-LITE-DB[0-9]+(\.IPV6)?\.BIN$/', $file)) {
copy($extractPath . $file, $databasePath . $file);
IP2LocationPlugin::setDatabasePath($databasePath . $file);
IP2LocationPlugin::setDownloadedDatabaseSize($size);
IP2LocationPlugin::setLastScheduledTaskDate(date('Y-m-d H:i:s'));
}
@unlink($extractPath . $file);
}
}
@unlink($zipFile);
@rmdir($extractPath);
} catch (\Exception $e) {
}
}
}