public static function ip()
{
$file = self::arguments('file');
if (!file_exists($file)) {
Error::terminal('invalid file.');
}
static $citys;
$region = '';
$match = array();
$provinces = array_flip(Usr::findProvince());
$handle = @fopen($file, 'rb');
while (!feof($handle)) {
if (!$buffer = trim(fgets($handle))) {
continue;
}
$line = explode(' ', $buffer);
$line = array_values(array_filter($line));
if (!isset($line['2'])) {
continue;
}
$province = mb_substr($line['2'], 0, 2, 'UTF-8');
if (!($match = preg_grep("/{$province}/i", $provinces))) {
continue;
}
$region = array_rand($match, 1);
$city = trim(mb_substr($line[2], mb_strlen($match[$region], 'UTF-8'), 3, 'UTF-8'), '()./\\');
$output = $match[$region];
if (!isset($citys[$region])) {
$citys[$region] = array_flip(Usr::findCity($region));
}
if ($match = preg_grep("/{$city}/i", $citys[$region])) {
$region = array_rand($match, 1);
$output .= " " . $match[$region];
}
$ip = sprintf('%u', ip2long($line['0'])) . ',' . sprintf('%u', ip2long($line['1']));
$output .= " " . $ip;
self::output($output);
Usr::addRegionip($region, $ip);
}
self::output('OK!');
fclose($handle);
}
public static function china()
{
$file = self::arguments('file');
if (!file_exists($file)) {
Error::terminal('invalid file.');
}
$china = array();
$handle = @fopen($file, 'rb');
while (!feof($handle)) {
if (!$buffer = trim(fgets($handle))) {
continue;
}
$line = explode(' ', $buffer);
$line = array_values(array_filter($line));
$china[$line[0]] = $line[1];
}
foreach ($china as $pid => $pname) {
if (!preg_match("/\d{2}0{4}/", $pid)) {
continue;
}
Usr::addProvince($pid, $pname);
foreach ($china as $cid => $cname) {
$filter = substr($pid, 0 ,2);
if (!preg_match("/{$filter}\d{4}/", $cid) || preg_match("/{$filter}0{4}/", $cid)) {
continue;
}
if (in_array($cname, array('市辖区', '县'))) {
continue;
}
if (in_array($pname, array('北京市', '天津市', '上海市', '重庆市'))
|| preg_match("/{$filter}[0-9]{2}0{2}/", $cid)) {
Usr::addCity($pid, $cid, $cname);
$output = $pname . " " . $pid . " " . $cname . " " . $cid;
self::output($output);
}
}
}
self::output('OK!');
}
中国各地区的IP段解析
最新推荐文章于 2022-12-09 19:14:13 发布