diff --git a/backend/ChinaCity.db b/backend/ChinaCity.db new file mode 100644 index 0000000..07f9640 Binary files /dev/null and b/backend/ChinaCity.db differ diff --git a/backend/city.php b/backend/city.php new file mode 100644 index 0000000..796a5aa --- /dev/null +++ b/backend/city.php @@ -0,0 +1,44 @@ +open('ChinaCity.db'); // 中国省市经纬度数据库 + } +} + +function getLoc($region, $city) { // 根据省份/城市信息查询经纬度 + $db = new cityDB; + $data['region'] = $region; + $data['city'] = $city; + $query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="'.$city.'";'; + $raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); + if (!$raw) { // 查无数据 + $query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="-";'; // 尝试仅查询省份数据 + $raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); + if (!$raw) { // 省份错误,返回北京经纬度 + $data['lat'] = '39.91'; + $data['lon'] = '116.73'; + return $data; + } + if ($city == '') { + $query_str='SELECT * FROM main WHERE level1="'.$region.'" LIMIT 1,1;'; // 获取省会记录 + $raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); + $data['city'] = $raw['level2']; + } + } + $data['lat'] = $raw['lat']; + $data['lon'] = $raw['lon']; + return $data; +} + +?> \ No newline at end of file diff --git a/backend/queryInfo.php b/backend/queryInfo.php index 3a7140d..bb8b84e 100644 --- a/backend/queryInfo.php +++ b/backend/queryInfo.php @@ -1,14 +1,12 @@ getDetail($ip); $specialInfo = checkSpecial($ip); if (is_string($specialInfo)) { $info['ip'] = $ip; @@ -20,9 +18,9 @@ function getIPInfo($ip) { $info['loc'] = null; $info['isp'] = $specialInfo; } else { - $data = IPinfo::getInfo($ip); $IPIP = new IPDB('ipipfree.ipdb'); $addr = $IPIP->getDistrict($ip); + $data = IPinfo::getInfo($ip); $country = getCountry($data['country']); $info['ip'] = $data['ip']; $info['as'] = $data['as']; @@ -36,14 +34,24 @@ function getIPInfo($ip) { if ($addr[0] == '中国') { $info['country'] = 'CN - China(中国)'; $info['timezone'] = 'Asia/Shanghai'; - if ($addr[1] && $addr[2]) { - $info['region'] = $addr[1]; - $info['city'] = $addr[2]; + if ($addr[1] == '') { + $addr[1] = '北京'; } + $cityLoc = getLoc($addr[1], $addr[2]); + $info['region'] = $cityLoc['region']; + $info['city'] = $cityLoc['city']; + $info['loc'] = $cityLoc['lat'] . ',' . $cityLoc['lon']; } } - $info['scope'] = tryCIDR($detail['beginIP'], $detail['endIP']); - $info['detail'] = $detail['dataA'] . $detail['dataB']; + if (filter_var($ip, \FILTER_VALIDATE_IP,\FILTER_FLAG_IPV4)) { + $qqwry = new QQWry('qqwry.dat'); + $detail = $qqwry->getDetail($ip); + $info['scope'] = tryCIDR($detail['beginIP'], $detail['endIP']); + $info['detail'] = $detail['dataA'] . $detail['dataB']; + } else { + $info['scope'] = $info['ip']; + $info['detail'] = $info['as'] . ' ' . $info['isp']; + } if ($_GET['cli'] == "true") { // 使用命令行模式 $cli = "IP: ".$info['ip'] . PHP_EOL;