IpLocation.class.php 9.0 KB

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