用ipip.net库替换了之前版本的ip库,更精确

This commit is contained in:
wlx 2015-06-25 20:38:07 +08:00
parent 0e8b254f1a
commit 780ba5a891
6 changed files with 133 additions and 81 deletions

View File

@ -1043,11 +1043,11 @@ where ds.uuid=? )) ) a ) p on r.id=p.refid where r.language<>'zh' order by p.pla
$row=$this->db->fetchRow($this->db->quoteInto($sql,$uuid)); $row=$this->db->fetchRow($this->db->quoteInto($sql,$uuid));
$jump=$this->_request->getParam('jump'); $jump=$this->_request->getParam('jump');
if ($jump=="") $jump=1;//默认跳转 if ($jump=="") $jump=1;//默认跳转
include_once("iplimit/iplimit.class.php"); include_once("ipip/IP.class.php");
$ip=new iplimit(); $ip=new IP();
if (@$row->has_pages && ($jump==1)) if (@$row->has_pages && ($jump==1))
{ {
if (!$ip->setup($ip->GetIP())) if (!$ip->mainland_verify())
$this->_helper->viewRenderer($row->code.'/view',null,true); $this->_helper->viewRenderer($row->code.'/view',null,true);
else else
$this->_helper->viewRenderer($row->code.'/view-tianditu',null,true); $this->_helper->viewRenderer($row->code.'/view-tianditu',null,true);

View File

@ -537,9 +537,9 @@ class HiwaterController extends DataController
function viewAction() function viewAction()
{ {
parent::viewAction(); parent::viewAction();
include_once("iplimit/iplimit.class.php"); include_once("ipip/IP.class.php");
$ip=new iplimit(); $ip=new IP();
if (!$ip->setup($ip->GetIP())) if (!$ip->mainland_verify())
$this->_helper->viewRenderer('hiwater/view'); $this->_helper->viewRenderer('hiwater/view');
else else
$this->_helper->viewRenderer('hiwater/view-tianditu'); $this->_helper->viewRenderer('hiwater/view-tianditu');

BIN
include/ipip/17monipdb.dat Executable file

Binary file not shown.

127
include/ipip/IP.class.php Executable file
View File

@ -0,0 +1,127 @@
<?php
/*
全球 IPv4 地址归属地数据库(17MON.CN )
高春辉(pAUL gAO) <gaochunhui@gmail.com>
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 getip()
{
if ($ip = getenv('HTTP_CLIENT_IP'));
elseif ($ip = getenv('HTTP_X_FORWARDED_FOR'));
elseif ($ip = getenv('HTTP_X_FORWARDED'));
elseif ($ip = getenv('HTTP_FORWARDED_FOR'));
elseif ($ip = getenv('HTTP_FORWARDED'));
else $ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
public function mainland_verify($ip="")
{
if ($ip=="") $ip=$this->getip();
$t=$this->find($ip);
return ($t[0]=='中国');
}
public function __destruct()
{
if (self::$fp !== NULL)
{
@fclose(self::$fp);
}
}
}
?>

File diff suppressed because one or more lines are too long

View File

@ -1,74 +0,0 @@
<?php
class iplimit
{
function iplimit() {
header('content-type:text/html;charset=utf-8;');
$this->path = "ipdata.db";
}
function setup($ip = '') {
$content = file_get_contents($this->path,true);
if(empty($content)) {
$this->show('1');
exit('IP数据库破损');
}
eval("\$this->iptable = $content;");
return $this->CheckIp($ip);
}
function GetIP() {
if ($ip = getenv('HTTP_CLIENT_IP'));
elseif ($ip = getenv('HTTP_X_FORWARDED_FOR'));
elseif ($ip = getenv('HTTP_X_FORWARDED'));
elseif ($ip = getenv('HTTP_FORWARDED_FOR'));
elseif ($ip = getenv('HTTP_FORWARDED'));
else $ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
function CheckIp($ip = '') {
!$ip &&$ip = $this->GetIp();
$ip2 = sprintf('%u',ip2long($ip));
$tag = reset(explode('.',$ip));
if(!$ip) {
$this->show(2);
return true;
}
if('192'== $tag ||'127'== $tag) {
$this->show(4);
return true;
}
if(!isset($this->iptable[$tag])) {
$this->show(3);
return false;
}
foreach($this->iptable[$tag] as $k =>$v) {
if($v[0] <= $ip2 &&$v[1] >= $ip2) {
$this->show('in');
return true;
}
}
$this->show('out');
return false;
}
function show($code) {
$msg = array(
1 =>"IP数据库文件破损",
2 =>"取不到IP地址 可能使用了代理",
4 =>"在局域网内",
'out'=>"IP地址在国外",
'in'=>"IP地址在国内",
);
$this->code = $code;
$this->msg = $msg[$code];
}
function __destruct() {
unset($this->iptable);
}
}
?>