diff --git a/README.md b/README.md index f3f664a..8565fc9 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,45 @@ IPIP.net officially supported IP database ipdb format parsing library # Installing -composer install ipip/db +
+composer require ipip/db +# Example Code
-$db = new ipip\db\Reader('c:\tmp\ipdb\mydata6vipday4.ipdb'); -$loc = $db->find("2001:250:200::"); -$map = $db->findMap("2001:250:200::"); -$obj = $db->findInfo("2001:250:200::"); -var_dump($loc, $map, $obj); - -try -{ - var_dump($db->find("255.255.255.1")); -} -catch (Exception $e) -{ - exit($e->getMessage()); -} + + Language Support: CN(中文) / EN (English); + +// 全球 IPv4 地级市精度离线库(China:免费版,每周高级版,每日标准版,每日高级版,每日专业版,每日旗舰版) +$city = new ipip\db\City('c:\work\ipdb\city.free.ipdb'); +var_dump($city->find('118.28.1.1', 'CN')); +var_dump($city->findMap('118.28.1.1', 'CN')); +var_dump($city->findInfo('118.28.1.1', 'CN')); + +// 全球 IPv6 地级市精度离线库(China:每周高级版,每日标准版,每日高级版,每日专业版,每日旗舰版) +$city = new ipip\db\City('c:\work\ipdb\city.ipv6.ipdb'); +//var_dump($city->find('2001:250:200::', 'CN')); +//var_dump($city->findMap('2001:250:200::', 'CN')); +//var_dump($city->findInfo('2001:250:200::', 'CN')); + + +// for China +// 中国地区区县级IPv4离线库 +$district = new ipip\db\District('c:\work\ipdb\china_district.ipdb'); +var_dump($district->find('1.12.7.255', 'CN')); +var_dump($district->findMap('1.12.7.255', 'CN')); +var_dump($district->findInfo('1.12.7.255', 'CN')); + +// IDC IPv4 列表离线库 +$idc = new ipip\db\IDC('c:\work\ipdb\idc_list.ipdb'); +var_dump($idc->find('1.1.1.1', 'CN')); +var_dump($idc->findMap('1.1.1.1', 'CN')); +var_dump($idc->findInfo('1.1.1.1', 'CN')); + +// 基站IPv4 离线库 +$baseStation = new ipip\db\BaseStation('c:\work\ipdb\base_station.ipdb'); +var_dump($baseStation->find('223.220.221.255', 'CN')); +var_dump($baseStation->findMap('223.220.221.255', 'CN')); +var_dump($baseStation->findInfo('223.220.221.255', 'CN')); +\ No newline at end of file diff --git a/example.php b/example.php deleted file mode 100644 index 22b4a5c..0000000 --- a/example.php +++ /dev/null @@ -1,19 +0,0 @@ -find("2001:250:200::"); -$map = $db->findMap("2001:250:200::"); -$obj = $db->findInfo("2001:250:200::"); -var_dump($loc, $map, $obj); - -try -{ - var_dump($db->find("255.255.255.1")); -} -catch (Exception $e) -{ - exit($e->getMessage()); -} \ No newline at end of file diff --git a/src/ipip/db/BaseStation.php b/src/ipip/db/BaseStation.php new file mode 100644 index 0000000..a2e7aef --- /dev/null +++ b/src/ipip/db/BaseStation.php @@ -0,0 +1,40 @@ +reader = new Reader($db); + } + + public function find($ip, $language) + { + return $this->reader->find($ip, $language); + } + + public function findMap($ip, $language) + { + return $this->reader->findMap($ip, $language); + } + + public function findInfo($ip, $language) + { + $map = $this->findMap($ip, $language); + if (NULL === $map) + { + return NULL; + } + + return new BaseStationInfo($map); + } +} \ No newline at end of file diff --git a/src/ipip/db/BaseStationInfo.php b/src/ipip/db/BaseStationInfo.php new file mode 100644 index 0000000..164c460 --- /dev/null +++ b/src/ipip/db/BaseStationInfo.php @@ -0,0 +1,32 @@ + $value) + { + $this->{$field} = $value; + } + } + + public function __get($name) + { + return $this->{$name}; + } +} \ No newline at end of file diff --git a/src/ipip/db/City.php b/src/ipip/db/City.php new file mode 100644 index 0000000..d6afa0e --- /dev/null +++ b/src/ipip/db/City.php @@ -0,0 +1,40 @@ +reader = new Reader($db); + } + + public function find($ip, $language) + { + return $this->reader->find($ip, $language); + } + + public function findMap($ip, $language) + { + return $this->reader->findMap($ip, $language); + } + + public function findInfo($ip, $language) + { + $map = $this->findMap($ip, $language); + if (NULL === $map) + { + return NULL; + } + + return new CityInfo($map); + } +} \ No newline at end of file diff --git a/src/ipip/db/CityInfo.php b/src/ipip/db/CityInfo.php new file mode 100644 index 0000000..00a56ed --- /dev/null +++ b/src/ipip/db/CityInfo.php @@ -0,0 +1,46 @@ + $value) + { + $this->{$field} = $value; + } + } + + public function __get($name) + { + return $this->{$name}; + } +} \ No newline at end of file diff --git a/src/ipip/db/District.php b/src/ipip/db/District.php new file mode 100644 index 0000000..e867f1e --- /dev/null +++ b/src/ipip/db/District.php @@ -0,0 +1,40 @@ +reader = new Reader($db); + } + + public function find($ip, $language) + { + return $this->reader->find($ip, $language); + } + + public function findMap($ip, $language) + { + return $this->reader->findMap($ip, $language); + } + + public function findInfo($ip, $language) + { + $map = $this->findMap($ip, $language); + if (NULL === $map) + { + return NULL; + } + + return new DistrictInfo($map); + } +} \ No newline at end of file diff --git a/src/ipip/db/DistrictInfo.php b/src/ipip/db/DistrictInfo.php new file mode 100644 index 0000000..ae8336b --- /dev/null +++ b/src/ipip/db/DistrictInfo.php @@ -0,0 +1,34 @@ + $value) + { + $this->{$field} = $value; + } + } + + public function __get($name) + { + return $this->{$name}; + } +} \ No newline at end of file diff --git a/src/ipip/db/IDC.php b/src/ipip/db/IDC.php new file mode 100644 index 0000000..16afb66 --- /dev/null +++ b/src/ipip/db/IDC.php @@ -0,0 +1,40 @@ +reader = new Reader($db); + } + + public function find($ip, $language) + { + return $this->reader->find($ip, $language); + } + + public function findMap($ip, $language) + { + return $this->reader->findMap($ip, $language); + } + + public function findInfo($ip, $language) + { + $map = $this->findMap($ip, $language); + if (NULL === $map) + { + return NULL; + } + + return new IDCInfo($map); + } +} \ No newline at end of file diff --git a/src/ipip/db/IDCInfo.php b/src/ipip/db/IDCInfo.php new file mode 100644 index 0000000..0caf1eb --- /dev/null +++ b/src/ipip/db/IDCInfo.php @@ -0,0 +1,32 @@ + $value) + { + $this->{$field} = $value; + } + } + + public function __get($name) + { + return $this->{$name}; + } +} \ No newline at end of file diff --git a/src/ipip/db/Info.php b/src/ipip/db/Info.php deleted file mode 100644 index aada23d..0000000 --- a/src/ipip/db/Info.php +++ /dev/null @@ -1,40 +0,0 @@ - $value) - { - $this->{$field} = $value; - } - } - - public function __get($name) - { - return $this->{$name}; - } -} \ No newline at end of file diff --git a/src/ipip/db/Reader.php b/src/ipip/db/Reader.php index 2c63a8a..628b5cb 100644 --- a/src/ipip/db/Reader.php +++ b/src/ipip/db/Reader.php @@ -1,5 +1,11 @@ file, $metaLength); $this->meta = json_decode($text, 1); - if (!isset($this->meta['fields']) || !isset($this->meta['languages'])) + + if (isset($this->meta['fields']) === FALSE || isset($this->meta['languages']) === FALSE) { throw new \Exception('IP Database metadata error.'); } $fileSize = 4 + $metaLength + $this->meta['total_size']; - if ($fileSize < $this->fileSize) + if ($fileSize != $this->fileSize) { throw new \Exception('IP Database size error.'); } @@ -69,16 +76,16 @@ private function init() * @param string $language * @return array|NULL */ - public function find($ip, $language = 'CN') + public function find($ip, $language) { if (is_resource($this->file) === FALSE) { - throw new \BadMethodCallException('closed IPIP DB.'); + throw new \BadMethodCallException('IPIP DB closed.'); } - if (!isset($this->meta['languages'][$language])) + if (isset($this->meta['languages'][$language]) === FALSE) { - throw new \InvalidArgumentException("language : {$language} not support"); + throw new \InvalidArgumentException("language : {$language} not support."); } if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) === FALSE) @@ -98,6 +105,7 @@ public function find($ip, $language = 'CN') try { $node = $this->findNode($ip); + if ($node > 0) { $data = $this->resolve($node); @@ -115,10 +123,10 @@ public function find($ip, $language = 'CN') return NULL; } - public function findMap($ip, $language = 'CN') + public function findMap($ip, $language) { $array = $this->find($ip, $language); - if (NULL == $array) + if (NULL === $array) { return NULL; } @@ -126,16 +134,7 @@ public function findMap($ip, $language = 'CN') return array_combine($this->meta['fields'], $array); } - public function findInfo($ip, $language = 'CN') - { - $map = $this->findMap($ip, $language); - if (NULL == $map) - { - return NULL; - } - - return new Info($map); - } + private $v4offset = 0; /** * @param $ip @@ -144,9 +143,6 @@ public function findInfo($ip, $language = 'CN') */ private function findNode($ip) { - static $v4offset = 0; - static $v6offsetCache = []; - $binary = inet_pton($ip); $bitCount = strlen($binary) * 8; // 32 | 128 $key = substr($binary, 0, 2); @@ -154,7 +150,7 @@ private function findNode($ip) $index = 0; if ($bitCount === 32) { - if ($v4offset === 0) + if ($this->v4offset === 0) { for ($i = 0; $i < 96 && $node < $this->nodeCount; $i++) { @@ -172,19 +168,11 @@ private function findNode($ip) return 0; } } - $v4offset = $node; + $this->v4offset = $node; } else { - $node = $v4offset; - } - } - else - { - if (isset($v6offsetCache[$key])) - { - $index = 16; - $node = $v6offsetCache[$key]; + $node = $this->v4offset; } } @@ -196,11 +184,6 @@ private function findNode($ip) } $node = $this->readNode($node, 1 & ((0xFF & ord($binary[$i >> 3])) >> 7 - ($i % 8))); - - if ($i == 15) - { - $v6offsetCache[$key] = $node; - } } if ($node === $this->nodeCount) @@ -212,7 +195,7 @@ private function findNode($ip) return $node; } - throw new \Exception("find node failed"); + throw new \Exception("find node failed."); } /** @@ -275,7 +258,7 @@ private function read($stream, $offset, $length) } } - throw new \Exception("The Database file read bad data"); + throw new \Exception("The Database file read bad data."); } return ''; @@ -303,4 +286,4 @@ public function getBuildTime() { return $this->meta['build']; } -} \ No newline at end of file +}