QQWry.Dat读取


  1. <?php
  2. define(‘QQWRY’ , ‘/var/www/QQWry.Dat’ ) ;
  3.  
  4. class TQQwry
  5. {
  6.     var $StartIp = 0;
  7.     var $EndIp   = 0;
  8.     var $Country = ;
  9.     var $Local   = ;
  10.  
  11.     var $CountryFlag = 0; // 标识 Country位置
  12.                           // 0×01,随后3字节为Country偏移,没有Local
  13.                           // 0×02,随后3字节为Country偏移,接着是Local
  14.                           // 其他,Country,Local,Local有类似的压缩。可能多重引用。
  15.     var $fp;
  16.  
  17.     var $FirstStartIp = 0;
  18.     var $LastStartIp = 0;
  19.     var $EndIpOff = 0 ;
  20.  
  21.     function getStartIp ( $RecNo ) {
  22.         $offset = $this->FirstStartIp + $RecNo * 7 ;
  23.         @fseek ( $this->fp , $offset , SEEK_SET ) ;
  24.         $buf = fread ( $this->fp , 7 ) ;
  25.         $this->EndIpOff = ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])* 256*256);
  26.         $this->StartIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  27.         return $this->StartIp ;
  28.     }
  29.  
  30.     function getEndIp ( ) {
  31.         @fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ;
  32.         $buf = fread ( $this->fp , 5 ) ;
  33.         $this->EndIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  34.         $this->CountryFlag = ord ( $buf[4] ) ;
  35.         return $this->EndIp ;
  36.     }
  37.  
  38.     function getCountry ( ) {
  39.  
  40.         switch ( $this->CountryFlag ) {
  41.             case 1:
  42.             case 2:
  43.                 $this->Country = iconv(‘gbk’,‘utf-8′,$this->getFlagStr ( $this->EndIpOff+4)) ;
  44.                 $this->Local = ( 1 == $this->CountryFlag )? : $this->getFlagStr ( $this->EndIpOff+8);
  45.   break ;
  46.             default :
  47.                 $this->Country = iconv(‘gbk’,‘utf-8′,$this->getFlagStr ($this->EndIpOff+4)) ;
  48.                 $this->Local =   $this->getFlagStr ( ftell ( $this->fp )) ;
  49.  
  50.         }
  51.     }
  52.  
  53.  
  54.     function getFlagStr ( $offset )
  55.     {
  56.  
  57.         $flag = 0 ;
  58.         while ( 1 ){
  59.             @fseek ( $this->fp , $offset , SEEK_SET ) ;
  60.             $flag = ord ( fgetc ( $this->fp ) ) ;
  61.             if ( $flag == 1 || $flag == 2 ) {
  62.                 $buf = fread ($this->fp , 3 ) ;
  63.                 if ($flag == 2 ){
  64.                     $this->CountryFlag = 2 ;
  65.                     $this->EndIpOff = $offset4 ;
  66.                 }
  67.                 $offset = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);
  68.             }else{
  69.                 break ;
  70.             }
  71.  
  72.         }
  73.         if ( $offset < 12 )
  74.             return ;
  75.         @fseek($this->fp , $offset , SEEK_SET ) ;
  76.         return $this->getStr();
  77.     }
  78.     function getStr ( )
  79.     {
  80.         $str = ;
  81.         while ( 1 ) {
  82.             $c = fgetc ( $this->fp ) ;
  83.             if ( ord ( $c[0] ) == 0  )
  84.                break ;
  85.             $str .= $c ;
  86.         }
  87.         return $str ;
  88.     }
  89.  
  90.  
  91.     function qqwry ($dotip) {
  92.         $nRet;
  93.         $ip = ip2long ( $dotip );
  94.         $this->fp= fopen(QQWRY, "rb");
  95.         if ($this->fp == NULL) {
  96.               $szLocal= "OpenFileError";
  97.             return 1;
  98.  
  99.           }
  100.         @fseek ( $this->fp , 0 , SEEK_SET ) ;
  101.         $buf = fread ( $this->fp , 8 ) ;
  102.         $this->FirstStartIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  103.         $this->LastStartIp  = ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256);
  104.  
  105.         $RecordCount= floor( ( $this->LastStartIp$this->FirstStartIp ) / 7);
  106.         if ($RecordCount <= 1){
  107.             $this->Country = "FileDataError";
  108.             fclose ( $this->fp ) ;
  109.             return 2 ;
  110.         }
  111.         $RangB= 0;
  112.         $RangE= $RecordCount;
  113.         // Match …
  114.         while ($RangB < $RangE-1)
  115.         {
  116.           $RecNo= floor(($RangB + $RangE) / 2);
  117.           $this->getStartIp ( $RecNo ) ;
  118.  
  119.             if ( $ip == $this->StartIp )
  120.             {
  121.                 $RangB = $RecNo ;
  122.                 break ;
  123.             }
  124.                 if ( $ip > $this->StartIp)
  125.             $RangB= $RecNo;
  126.           else
  127.             $RangE= $RecNo;
  128.         }
  129.         $this->getStartIp ( $RangB ) ;
  130.         $this->getEndIp ( ) ;
  131.  
  132.         if ( ( $this->StartIp  <= $ip ) && ( $this->EndIp >= $ip ) ){
  133.             $nRet = 0 ;
  134.             $this->getCountry ( ) ;
  135.             //这样不太好…………..所以……….
  136.             $this->Local = str_replace(array("(我们一定要解放台湾!!!)","CZ88.NET"), "", $this->Local);
  137.  
  138.         }else {
  139.             $nRet = 3 ;
  140.             $this->Country = ‘未知’ ;
  141.             $this->Local = ;
  142.         }
  143.         fclose ( $this->fp ) ;
  144.         return  $this->Country.‘ ‘.$this->Local ;
  145.     }
  146. }
  147.  
  148.  
  149.     $wry = new TQQwry ;
  150. if(isset($_GET[‘IP’]) && strlen($_GET[‘IP’])>7)
  151. echo $wry->qqwry ( $_GET[‘IP’] );
  152. ?>
  1. No comments yet.
(will not be published)
  1. No trackbacks yet.