IpLocation.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * IP 地理位置查询类 修改自 CoolCode.CN
  13. * 由于使用UTF8编码 如果使用纯真IP地址库的话 需要对返回结果进行编码转换
  14. * @category ORG
  15. * @package ORG
  16. * @subpackage Net
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class IpLocation {
  20. /**
  21. * QQWry.Dat文件指针
  22. *
  23. * @var resource
  24. */
  25. private $fp;
  26. /**
  27. * 第一条IP记录的偏移地址
  28. *
  29. * @var int
  30. */
  31. private $firstip;
  32. /**
  33. * 最后一条IP记录的偏移地址
  34. *
  35. * @var int
  36. */
  37. private $lastip;
  38. /**
  39. * IP记录的总条数(不包含版本信息记录)
  40. *
  41. * @var int
  42. */
  43. private $totalip;
  44. /**
  45. * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
  46. *
  47. * @param string $filename
  48. * @return IpLocation
  49. */
  50. public function __construct($filename = "UTFWry.dat") {
  51. $this->fp = 0;
  52. if (($this->fp = fopen(dirname(__FILE__).'/'.$filename, 'rb')) !== false) {
  53. $this->firstip = $this->getlong();
  54. $this->lastip = $this->getlong();
  55. $this->totalip = ($this->lastip - $this->firstip) / 7;
  56. }
  57. }
  58. /**
  59. * 返回读取的长整型数
  60. *
  61. * @access private
  62. * @return int
  63. */
  64. private function getlong() {
  65. //将读取的little-endian编码的4个字节转化为长整型数
  66. $result = unpack('Vlong', fread($this->fp, 4));
  67. return $result['long'];
  68. }
  69. /**
  70. * 返回读取的3个字节的长整型数
  71. *
  72. * @access private
  73. * @return int
  74. */
  75. private function getlong3() {
  76. //将读取的little-endian编码的3个字节转化为长整型数
  77. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  78. return $result['long'];
  79. }
  80. /**
  81. * 返回压缩后可进行比较的IP地址
  82. *
  83. * @access private
  84. * @param string $ip
  85. * @return string
  86. */
  87. private function packip($ip) {
  88. // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,
  89. // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串
  90. return pack('N', intval(ip2long($ip)));
  91. }
  92. /**
  93. * 返回读取的字符串
  94. *
  95. * @access private
  96. * @param string $data
  97. * @return string
  98. */
  99. private function getstring($data = "") {
  100. $char = fread($this->fp, 1);
  101. while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束
  102. $data .= $char; // 将读取的字符连接到给定字符串之后
  103. $char = fread($this->fp, 1);
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 返回地区信息
  109. *
  110. * @access private
  111. * @return string
  112. */
  113. private function getarea() {
  114. $byte = fread($this->fp, 1); // 标志字节
  115. switch (ord($byte)) {
  116. case 0: // 没有区域信息
  117. $area = "";
  118. break;
  119. case 1:
  120. case 2: // 标志字节为1或2,表示区域信息被重定向
  121. fseek($this->fp, $this->getlong3());
  122. $area = $this->getstring();
  123. break;
  124. default: // 否则,表示区域信息没有被重定向
  125. $area = $this->getstring($byte);
  126. break;
  127. }
  128. return $area;
  129. }
  130. /**
  131. * 根据所给 IP 地址或域名返回所在地区信息
  132. *
  133. * @access public
  134. * @param string $ip
  135. * @return array
  136. */
  137. public function getlocation($ip='') {
  138. if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空
  139. if(empty($ip)) $ip = get_client_ip();
  140. $location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址
  141. $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址
  142. // 不合法的IP地址会被转化为255.255.255.255
  143. // 对分搜索
  144. $l = 0; // 搜索的下边界
  145. $u = $this->totalip; // 搜索的上边界
  146. $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
  147. while ($l <= $u) { // 当上边界小于下边界时,查找失败
  148. $i = floor(($l + $u) / 2); // 计算近似中间记录
  149. fseek($this->fp, $this->firstip + $i * 7);
  150. $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址
  151. // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式
  152. // 以便用于比较,后面相同。
  153. if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时
  154. $u = $i - 1; // 将搜索的上边界修改为中间记录减一
  155. }
  156. else {
  157. fseek($this->fp, $this->getlong3());
  158. $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址
  159. if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时
  160. $l = $i + 1; // 将搜索的下边界修改为中间记录加一
  161. }
  162. else { // 用户的IP在中间记录的IP范围内时
  163. $findip = $this->firstip + $i * 7;
  164. break; // 则表示找到结果,退出循环
  165. }
  166. }
  167. }
  168. //获取查找到的IP地理位置信息
  169. fseek($this->fp, $findip);
  170. $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
  171. $offset = $this->getlong3();
  172. fseek($this->fp, $offset);
  173. $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
  174. $byte = fread($this->fp, 1); // 标志字节
  175. switch (ord($byte)) {
  176. case 1: // 标志字节为1,表示国家和区域信息都被同时重定向
  177. $countryOffset = $this->getlong3(); // 重定向地址
  178. fseek($this->fp, $countryOffset);
  179. $byte = fread($this->fp, 1); // 标志字节
  180. switch (ord($byte)) {
  181. case 2: // 标志字节为2,表示国家信息又被重定向
  182. fseek($this->fp, $this->getlong3());
  183. $location['country'] = $this->getstring();
  184. fseek($this->fp, $countryOffset + 4);
  185. $location['area'] = $this->getarea();
  186. break;
  187. default: // 否则,表示国家信息没有被重定向
  188. $location['country'] = $this->getstring($byte);
  189. $location['area'] = $this->getarea();
  190. break;
  191. }
  192. break;
  193. case 2: // 标志字节为2,表示国家信息被重定向
  194. fseek($this->fp, $this->getlong3());
  195. $location['country'] = $this->getstring();
  196. fseek($this->fp, $offset + 8);
  197. $location['area'] = $this->getarea();
  198. break;
  199. default: // 否则,表示国家信息没有被重定向
  200. $location['country'] = $this->getstring($byte);
  201. $location['area'] = $this->getarea();
  202. break;
  203. }
  204. if (trim($location['country']) == 'CZ88.NET') { // CZ88.NET表示没有有效信息
  205. $location['country'] = '未知';
  206. }
  207. if (trim($location['area']) == 'CZ88.NET') {
  208. $location['area'] = '';
  209. }
  210. return $location;
  211. }
  212. /**
  213. * 析构函数,用于在页面执行结束后自动关闭打开的文件。
  214. *
  215. */
  216. public function __destruct() {
  217. if ($this->fp) {
  218. fclose($this->fp);
  219. }
  220. $this->fp = 0;
  221. }
  222. }