Skip to content

Commit 077daf5

Browse files
committed
update code
1 parent f5fd8c8 commit 077daf5

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# ipdb-php
22
IPIP.net officially supported IP database ipdb format parsing library
3+
4+
# Installing
5+
composer install ipip/db

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_once __DIR__ . '/src/ipip/db/Info.php';
44
require_once __DIR__ . '/src/ipip/db/Reader.php';
55

6-
$db = new ipip\db\Reader('c:\work\tiantexin\bb\v6\mydata6vipday4.ipdb');
6+
$db = new ipip\db\Reader('c:\tmp\ipdb\mydata6vipday4.ipdb');
77
$loc = $db->find("2001:250:200::");
88
$map = $db->findMap("2001:250:200::");
99
$obj = $db->findInfo("2001:250:200::");

src/ipip/db/Reader.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,17 @@ class Reader
2323
*/
2424
public function __construct($database)
2525
{
26-
if (is_readable($database) === FALSE)
27-
{
28-
throw new \InvalidArgumentException("The IP Database file \"{$database}\" does not exist or is not readable.");
29-
}
30-
3126
$this->database = $database;
3227

3328
$this->init();
3429
}
3530

36-
/**
37-
* @throws \Exception
38-
*/
3931
private function init()
4032
{
33+
if (is_readable($this->database) === FALSE)
34+
{
35+
throw new \InvalidArgumentException("The IP Database file \"{$this->database}\" does not exist or is not readable.");
36+
}
4137
$this->file = @fopen($this->database, 'rb');
4238
if ($this->file === FALSE)
4339
{
@@ -71,7 +67,7 @@ private function init()
7167
/**
7268
* @param $ip
7369
* @param string $language
74-
* @return array|null
70+
* @return array|NULL
7571
*/
7672
public function find($ip, $language = 'CN')
7773
{
@@ -90,29 +86,30 @@ public function find($ip, $language = 'CN')
9086
throw new \InvalidArgumentException("The value \"$ip\" is not a valid IP address.");
9187
}
9288

93-
if (strpos($ip, '.') !== FALSE)
89+
if (strpos($ip, '.') !== FALSE && !$this->supportV4())
9490
{
95-
if (!$this->supportV4())
96-
{
97-
throw new \InvalidArgumentException("The Database not support IPv4 address.");
98-
}
91+
throw new \InvalidArgumentException("The Database not support IPv4 address.");
9992
}
100-
elseif (strpos($ip, ':') !== FALSE)
93+
elseif (strpos($ip, ':') !== FALSE && !$this->supportV6())
10194
{
102-
if (!$this->supportV6())
103-
{
104-
throw new \InvalidArgumentException("The Database not support IPv6 address.");
105-
}
95+
throw new \InvalidArgumentException("The Database not support IPv6 address.");
10696
}
10797

108-
$node = $this->findNode($ip);
109-
if ($node > 0)
98+
try
11099
{
111-
$data = $this->resolve($node);
100+
$node = $this->findNode($ip);
101+
if ($node > 0)
102+
{
103+
$data = $this->resolve($node);
112104

113-
$values = explode("\t", $data);
105+
$values = explode("\t", $data);
114106

115-
return array_slice($values, $this->meta['languages'][$language], count($this->meta['fields']));
107+
return array_slice($values, $this->meta['languages'][$language], count($this->meta['fields']));
108+
}
109+
}
110+
catch (\Exception $e)
111+
{
112+
return NULL;
116113
}
117114

118115
return NULL;
@@ -121,7 +118,7 @@ public function find($ip, $language = 'CN')
121118
public function findMap($ip, $language = 'CN')
122119
{
123120
$array = $this->find($ip, $language);
124-
if (NULL === $array)
121+
if (NULL == $array)
125122
{
126123
return NULL;
127124
}
@@ -132,7 +129,7 @@ public function findMap($ip, $language = 'CN')
132129
public function findInfo($ip, $language = 'CN')
133130
{
134131
$map = $this->findMap($ip, $language);
135-
if (NULL === $map)
132+
if (NULL == $map)
136133
{
137134
return NULL;
138135
}
@@ -143,7 +140,7 @@ public function findInfo($ip, $language = 'CN')
143140
/**
144141
* @param $ip
145142
* @return int
146-
* @throws \RuntimeException
143+
* @throws \Exception
147144
*/
148145
private function findNode($ip)
149146
{
@@ -215,7 +212,7 @@ private function findNode($ip)
215212
return $node;
216213
}
217214

218-
throw new \RuntimeException("find node failed");
215+
throw new \Exception("find node failed");
219216
}
220217

221218
/**
@@ -278,7 +275,7 @@ private function read($stream, $offset, $length)
278275
}
279276
}
280277

281-
throw new \Exception("The DB file read bad data");
278+
throw new \Exception("The Database file read bad data");
282279
}
283280

284281
return '';

0 commit comments

Comments
 (0)