QQWry.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // 本文件编辑必须是GB2312
  3. // 文件名:QQWry.php
  4. /*
  5. * ++++++++++++++++++++++++++++++++++++
  6. * 程序名称:IP解析程序
  7. * 程序功能:基于QQ的二进制数据库QQWry.Dat
  8. * 程序作者:未知
  9. * 使用方法:
  10. *
  11. * 请将文件 QQWry.Dat 置于当前目录中
  12. *
  13. * #实例+++++++++++++++++++++++++++++++
  14. * $ip="202.201.48.1";#
  15. * $QQWry=new QQWry($ip);
  16. * echo "$QQWry->Country$QQWry->Local";
  17. *
  18. * +++++++++++++++++++++++++++++++++++++
  19. *
  20. * ip.php是测试程序
  21. * ++++++++++++++++++++++++++++++++++
  22. *
  23. * 欢迎访问:[url]http://www.zh5j.com[/url]
  24. * +++++++++++++++++++++++++++++++++++++
  25. */
  26. // echo __QQWRY__;
  27. class QQWry
  28. {
  29. var $StartIP = 0;
  30. var $EndIP = 0;
  31. var $Country = '';
  32. var $Local = '';
  33. var $CountryFlag = 0;
  34. // 标识 Country位置
  35. // 0x01,随后3字节为Country偏移,没有Local
  36. // 0x02,随后3字节为Country偏移,接着是Local
  37. // 其他,Country,Local,Local有类似的压缩。可能多重引用。
  38. var $fp;
  39. var $FirstStartIp = 0;
  40. var $LastStartIp = 0;
  41. var $EndIpOff = 0;
  42. function getStartIp($RecNo)
  43. {
  44. $offset = $this->FirstStartIp + $RecNo * 7;
  45. @fseek($this->fp, $offset, SEEK_SET);
  46. $buf = fread($this->fp, 7);
  47. $this->EndIpOff = ord($buf[4]) + (ord($buf[5]) * 256) + (ord($buf[6]) * 256 * 256);
  48. $this->StartIp = ord($buf[0]) + (ord($buf[1]) * 256) + (ord($buf[2]) * 256 * 256) + (ord($buf[3]) * 256 * 256 * 256);
  49. return $this->StartIp;
  50. }
  51. function getEndIp()
  52. {
  53. @fseek($this->fp, $this->EndIpOff, SEEK_SET);
  54. $buf = fread($this->fp, 5);
  55. $this->EndIp = ord($buf[0]) + (ord($buf[1]) * 256) + (ord($buf[2]) * 256 * 256) + (ord($buf[3]) * 256 * 256 * 256);
  56. $this->CountryFlag = ord($buf[4]);
  57. return $this->EndIp;
  58. }
  59. function getCountry()
  60. {
  61. switch ($this->CountryFlag) {
  62. case 1:
  63. case 2:
  64. $this->Country = $this->getFlagStr($this->EndIpOff + 4);
  65. // echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );
  66. $this->Local = (1 == $this->CountryFlag) ? '' : $this->getFlagStr($this->EndIpOff + 8);
  67. break;
  68. default:
  69. $this->Country = $this->getFlagStr($this->EndIpOff + 4);
  70. $this->Local = $this->getFlagStr(ftell($this->fp));
  71. }
  72. }
  73. function getFlagStr($offset)
  74. {
  75. $flag = 0;
  76. while (1) {
  77. @fseek($this->fp, $offset, SEEK_SET);
  78. $flag = ord(fgetc($this->fp));
  79. if ($flag == 1 || $flag == 2) {
  80. $buf = fread($this->fp, 3);
  81. if ($flag == 2) {
  82. $this->CountryFlag = 2;
  83. $this->EndIpOff = $offset - 4;
  84. }
  85. $offset = ord($buf[0]) + (ord($buf[1]) * 256) + (ord($buf[2]) * 256 * 256);
  86. } else {
  87. break;
  88. }
  89. }
  90. if ($offset < 12)
  91. return '';
  92. @fseek($this->fp, $offset, SEEK_SET);
  93. return $this->getStr();
  94. }
  95. function getStr()
  96. {
  97. $str = '';
  98. while (1) {
  99. $c = fgetc($this->fp);
  100. // echo "$cn" ;
  101. if (ord($c[0]) == 0)
  102. break;
  103. $str .= $c;
  104. }
  105. // echo "$str n";
  106. return $str;
  107. }
  108. function __construct($dotip = '')
  109. {
  110. if (! is_string($dotip) || $dotip == '') {
  111. return;
  112. }
  113. if (preg_match("/^127/", $dotip)) {
  114. $this->Country = "本地网络";
  115. return;
  116. } elseif (preg_match("/^192/", $dotip)) {
  117. $this->Country = "局域网";
  118. return;
  119. }
  120. $nRet;
  121. $ip = $this->IpToInt($dotip);
  122. $this->fp = fopen(__QQWRY__, "rb");
  123. if ($this->fp == NULL) {
  124. $szLocal = "OpenFileError";
  125. return 1;
  126. }
  127. @fseek($this->fp, 0, SEEK_SET);
  128. $buf = fread($this->fp, 8);
  129. $this->FirstStartIp = ord($buf[0]) + (ord($buf[1]) * 256) + (ord($buf[2]) * 256 * 256) + (ord($buf[3]) * 256 * 256 * 256);
  130. $this->LastStartIp = ord($buf[4]) + (ord($buf[5]) * 256) + (ord($buf[6]) * 256 * 256) + (ord($buf[7]) * 256 * 256 * 256);
  131. $RecordCount = floor(($this->LastStartIp - $this->FirstStartIp) / 7);
  132. if ($RecordCount <= 1) {
  133. $this->Country = "FileDataError";
  134. fclose($this->fp);
  135. return 2;
  136. }
  137. $RangB = 0;
  138. $RangE = $RecordCount;
  139. // Match ...
  140. while ($RangB < $RangE - 1) {
  141. $RecNo = floor(($RangB + $RangE) / 2);
  142. $this->getStartIp($RecNo);
  143. if ($ip == $this->StartIp) {
  144. $RangB = $RecNo;
  145. break;
  146. }
  147. if ($ip > $this->StartIp)
  148. $RangB = $RecNo;
  149. else
  150. $RangE = $RecNo;
  151. }
  152. $this->getStartIp($RangB);
  153. $this->getEndIp();
  154. if (($this->StartIp <= $ip) && ($this->EndIp >= $ip)) {
  155. $nRet = 0;
  156. $this->getCountry();
  157. // 这样不太好..............所以..........
  158. $this->Local = str_replace("(我们一定要解放台湾!!!)", "", $this->Local);
  159. } else {
  160. $nRet = 3;
  161. $this->Country = '未知';
  162. $this->Local = '';
  163. }
  164. fclose($this->fp);
  165. $this->Country = preg_replace("/(CZ88.NET)|(纯真网络)/", "局域网/未知", $this->Country);
  166. $this->Local = preg_replace("/(CZ88.NET)|(纯真网络)/", "局域网/未知", $this->Local);
  167. // ////////////看看 $nRet在上面的值是什么0和3,于是将下面的行注释掉
  168. return $nRet;
  169. // return "$this->Country $this->Local";#如此直接返回位置和国家便可以了
  170. }
  171. function IpToInt($Ip)
  172. {
  173. $array = explode('.', $Ip);
  174. $Int = ($array[0] * 256 * 256 * 256) + ($array[1] * 256 * 256) + ($array[2] * 256) + $array[3];
  175. return $Int;
  176. }
  177. }
  178. ?>