-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool-ip.php
155 lines (125 loc) · 5.48 KB
/
tool-ip.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
<?php
/**
* @Project NUKEVIET 4.x
* @Author VINADES.,JSC <contact@vinades.vn>
* @Copyright (C) 2014 VINADES.,JSC. All rights reserved
* @License GNU/GPL version 2 or any later version
* @Createdate 31/05/2010, 00:36
*/
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
if (isset($_GET['response_headers_detect'])) {
exit(0);
}
define('NV_SYSTEM', true);
// Xac dinh thu muc goc cua site
define('NV_ROOTDIR', pathinfo(str_replace(DIRECTORY_SEPARATOR, '/', __file__), PATHINFO_DIRNAME));
require NV_ROOTDIR . '/includes/mainfile.php';
require NV_ROOTDIR . '/includes/core/user_functions.php';
require NV_ROOTDIR . '/src/functions.php';
require NV_ROOTDIR . '/src/Large-CSVReader.php';
require NV_ROOTDIR . '/src/geoidInfo.php';
if ($sys_info['ini_set_support']) {
set_time_limit(0);
ini_set('memory_limit', '-1');
}
/**
* Bắt đầu tool
*/
// Tạo các thư mục nếu chưa có
if (!is_dir(NV_ROOTDIR . '/release')) {
nv_mkdir(NV_ROOTDIR, 'release');
}
if (!is_dir(NV_ROOTDIR . '/release/ip')) {
nv_mkdir(NV_ROOTDIR . '/release', 'ip');
}
if (!is_dir(NV_ROOTDIR . '/release/ip6')) {
nv_mkdir(NV_ROOTDIR . '/release', 'ip6');
}
$inputFileType = 'Csv';
$inputFileName = NV_ROOTDIR . '/libs/ip/GeoLite2-Country-Blocks-IPv4.csv';
// Số row mỗi chunk
$chunkSize = 10000;
try {
$chunkFilter = new NukeViet\Files\ChunkReadFilter();
$reader = IOFactory::createReader($inputFileType);
$reader->setReadFilter($chunkFilter)->setContiguous(true);
$spreadsheet = new Spreadsheet();
$offsetRow = $nv_Request->get_int('offsetRow', 'get', 0);
$sheet = 0;
$startRow = 2 + $offsetRow;
$chunkFilter->setRows($startRow, $chunkSize);
$reader->setSheetIndex($sheet);
$reader->loadIntoExisting($inputFileName, $spreadsheet);
$sheetData = $spreadsheet->getActiveSheet();
$maxRow = $sheetData->getHighestRow();
$loaded_file = array();
$loaded_file_reverse = array();
echo ('<pre><code>');
if ($maxRow <= 1) {
// Ghi lại các file không có từ 0 - 255
for ($i = 0; $i <= 255; $i++) {
if (!file_exists(NV_ROOTDIR . '/release/ip/' . $i . '.php')) {
$file_content = "<?php\n\n" . IP_FILEHEAD . "\n\$ranges=array();\n";
file_put_contents(NV_ROOTDIR . '/release/ip/' . $i . '.php', $file_content, LOCK_EX);
}
}
echo ('Kết thúc');
} else {
for ($i = $startRow; $i <= $maxRow; $i++) {
$ip_range = $sheetData->getCell('A' . $i)->getValue();
$geo_id = $sheetData->getCell('B' . $i)->getValue();
if (isset($array_geo_info[$geo_id])) {
list($range, $netmask) = explode('/', $ip_range, 2);
if (!empty($netmask)) {
$x = explode('.', $range);
while (count($x) < 4) {
$x[] = '0';
}
list($a, $b, $c, $d) = $x;
//$range = sprintf("%u.%u.%u.%u", empty($a) ? '0' : $a, empty($b) ? '0' : $b, empty($c) ? '0' : $c, empty($d) ? '0' : $d);
$ip_start = ($a * 16777216) + ($b * 65536) + ($c * 256) + ($d);
$ip_end = $ip_start + pow(2, 32 - intval($netmask)) - 1;
if (!isset($loaded_file[$a]) and file_exists(NV_ROOTDIR . '/release/ip/' . $a . '.php')) {
$ranges = array();
$ranges_reverse = array();
include NV_ROOTDIR . '/release/ip/' . $a . '.php';
$loaded_file[$a] = $ranges;
foreach ($ranges as $rkey => $rval) {
$ranges_reverse[$rval[0]] = array($rkey, $rval[1]);
}
$loaded_file_reverse[$a] = $ranges_reverse;
} elseif (isset($loaded_file[$a])) {
$ranges = $loaded_file[$a];
$ranges_reverse = $loaded_file_reverse[$a];
} else {
$ranges = array();
$ranges_reverse = array();
}
$ip_sbefore = $ip_start - 1;
if (isset($ranges_reverse[$ip_sbefore]) and $ranges_reverse[$ip_sbefore][1] == $array_geo_info[$geo_id]) {
$ip_start = $ranges_reverse[$ip_sbefore][0];
unset($ranges_reverse[$ip_sbefore]);
}
$ranges[$ip_start] = array($ip_end, $array_geo_info[$geo_id]);
$ranges_reverse[$ip_end] = array($ip_start, $array_geo_info[$geo_id]);
$loaded_file[$a] = $ranges;
$loaded_file_reverse[$a] = $ranges_reverse;
} else {
trigger_error('IP range invalid on line ' . $startRow, 256);
}
}
}
echo('Hãy đợi, tiến trình còn đang chạy. Vui lòng không tắt trình duyệt');
echo ('<meta http-equiv="refresh" content="0;url=/' . basename(__file__) . '?offsetRow=' . ($offsetRow + $chunkSize) . '&t=' . time() . '">');
}
foreach ($loaded_file as $fname => $fdata) {
$file_content = "<?php\n\n" . IP_FILEHEAD . "\n\$ranges=" . nv_print_variable_ip($fdata) . ";\n";
file_put_contents(NV_ROOTDIR . '/release/ip/' . $fname . '.php', $file_content, LOCK_EX);
}
echo ('</code></pre>');
} catch (Exception $ex) {
echo ('<pre><code>');
print_r($ex);
echo ('</code></pre>');
}