diff --git a/ip/17monipdb.dat b/ip/17monipdb.dat new file mode 100644 index 0000000..c586055 Binary files /dev/null and b/ip/17monipdb.dat differ diff --git a/ip/IP.class.php b/ip/IP.class.php new file mode 100644 index 0000000..e1ba44e --- /dev/null +++ b/ip/IP.class.php @@ -0,0 +1,109 @@ + + Build 20141009 版权所有 17MON.CN + (C) 2006 - 2014 保留所有权利 + 请注意及时更新 IP 数据库版本 + 数据问题请加 QQ 群: 346280296 + Code for PHP 5.3+ only +*/ + +class IP +{ + private static $ip = NULL; + + private static $fp = NULL; + private static $offset = NULL; + private static $index = NULL; + + private static $cached = array(); + + public static function find($ip) + { + if (empty($ip) === TRUE) + { + return 'N/A'; + } + + $nip = gethostbyname($ip); + $ipdot = explode('.', $nip); + + if ($ipdot[0] < 0 || $ipdot[0] > 255 || count($ipdot) !== 4) + { + return 'N/A'; + } + + if (isset(self::$cached[$nip]) === TRUE) + { + return self::$cached[$nip]; + } + + if (self::$fp === NULL) + { + self::init(); + } + + $nip2 = pack('N', ip2long($nip)); + + $tmp_offset = (int)$ipdot[0] * 4; + $start = unpack('Vlen', self::$index[$tmp_offset] . self::$index[$tmp_offset + 1] . self::$index[$tmp_offset + 2] . self::$index[$tmp_offset + 3]); + + $index_offset = $index_length = NULL; + $max_comp_len = self::$offset['len'] - 1024 - 4; + for ($start = $start['len'] * 8 + 1024; $start < $max_comp_len; $start += 8) + { + if (self::$index{$start} . self::$index{$start + 1} . self::$index{$start + 2} . self::$index{$start + 3} >= $nip2) + { + $index_offset = unpack('Vlen', self::$index{$start + 4} . self::$index{$start + 5} . self::$index{$start + 6} . "\x0"); + $index_length = unpack('Clen', self::$index{$start + 7}); + + break; + } + } + + if ($index_offset === NULL) + { + return 'N/A'; + } + + fseek(self::$fp, self::$offset['len'] + $index_offset['len'] - 1024); + + self::$cached[$nip] = explode("\t", fread(self::$fp, $index_length['len'])); + + return self::$cached[$nip]; + } + + private static function init() + { + if (self::$fp === NULL) + { + self::$ip = new self(); + + self::$fp = fopen(__DIR__ . '/17monipdb.dat', 'rb'); + if (self::$fp === FALSE) + { + throw new Exception('Invalid 17monipdb.dat file!'); + } + + self::$offset = unpack('Nlen', fread(self::$fp, 4)); + if (self::$offset['len'] < 4) + { + throw new Exception('Invalid 17monipdb.dat file!'); + } + + self::$index = fread(self::$fp, self::$offset['len'] - 4); + } + } + + public function __destruct() + { + if (self::$fp !== NULL) + { + fclose(self::$fp); + } + } +} + +?> \ No newline at end of file diff --git a/ip/dbip-client.class.php b/ip/dbip-client.class.php new file mode 100644 index 0000000..27b3f53 --- /dev/null +++ b/ip/dbip-client.class.php @@ -0,0 +1,83 @@ +api_key = $api_key; + + if (isset($base_url)) { + $this->base_url = $base_url; + } + + } + + protected function Do_API_Call($method, $params = array()) { + + $qp = array("api_key=" . $this->api_key); + foreach ($params as $k => $v) { + $qp[] = $k . "=" . urlencode($v); + } + + $url = $this->base_url . $method . "?" . implode("&", $qp); + + if (!$jdata = @file_get_contents($url)) { + throw new DBIP_Client_Exception("{$method}: unable to fetch URL: {$url}"); + } + + if (!$data = @json_decode($jdata)) { + throw new DBIP_Client_Exception("{$method}: error decoding server response"); + } + + if (property_exists($data, 'error') && $data->error) { + throw new DBIP_Client_Exception("{$method}: server reported an error: {$data->error}"); + } + + return $data; + + } + + public function Get_Address_Info($addr) { + + return $this->Do_API_Call("addrinfo", array("addr" => $addr)); + + } + + public function Get_Key_Info() { + + return $this->Do_API_Call("keyinfo"); + + } + +} + +?> diff --git a/ip/example.php b/ip/example.php new file mode 100755 index 0000000..bcb2c49 --- /dev/null +++ b/ip/example.php @@ -0,0 +1,31 @@ +#!/usr/local/bin/php +\n"); + +try { + + $dbip = new DBIP_Client($api_key); + + echo "keyinfo:\n"; + foreach ($dbip->Get_Key_Info() as $k => $v) { + echo "{$k}: {$v}\n"; + } + + echo "\naddrinfo:\n"; + foreach ($dbip->Get_Address_Info($ip_addr) as $k => $v) { + echo "{$k}: {$v}\n"; + } + +} catch (Exception $e) { + + die("error: {$e->getMessage()}\n"); + +} + +?> diff --git a/ip/index.php b/ip/index.php new file mode 100644 index 0000000..89cecc2 --- /dev/null +++ b/ip/index.php @@ -0,0 +1,43 @@ +Get_Address_Info($ip_addr) as $k => $v) { +# echo "$k:$v,"; + if ($k=="country" && $v=="CN") { + $finds = IP::find($ip_addr); + $result = ""; + foreach ($finds as $res) { + if (!empty($res)) { + if ($result!=$res) + $result = empty($result)?$res:"$result,"."$res"; + } + } + echo $result; + break; + } + if ($k=="address") { + echo ""; + } else if ($k=="city") { + echo "$v"; + } else { + echo "$v,"; + } + } + } +?> diff --git a/ip/index2.php b/ip/index2.php new file mode 100644 index 0000000..a9edddd --- /dev/null +++ b/ip/index2.php @@ -0,0 +1,7 @@ +