ip.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. class ip
  3. {
  4. public $fp;
  5. public $firstip;
  6. public $lastip;
  7. public $totalip;
  8. public function ip($datfile = IPDATA)
  9. {
  10. $this->fp = fopen($datfile, 'rb');
  11. $this->firstip = $this->get4b();
  12. $this->lastip = $this->get4b();
  13. $this->totalip = ($this->lastip - $this->firstip) / 7;
  14. register_shutdown_function(array($this, 'closefp'));
  15. }
  16. public function closefp()
  17. {
  18. fclose($this->fp);
  19. }
  20. public function get4b()
  21. {
  22. $str = unpack('V', fread($this->fp, 4));
  23. return $str[1];
  24. }
  25. public function getoffset()
  26. {
  27. $str = unpack('V', fread($this->fp, 3) . chr(0));
  28. return $str[1];
  29. }
  30. public function getstr()
  31. {
  32. $split = fread($this->fp, 1);
  33. while (ord($split) != 0) {
  34. $str .= $split;
  35. $split = fread($this->fp, 1);
  36. }
  37. return $str;
  38. }
  39. public function iptoint($ip)
  40. {
  41. return pack('N', intval(ip2long($ip)));
  42. }
  43. public function readaddress()
  44. {
  45. $now_offset = ftell($this->fp);
  46. $flag = $this->getflag();
  47. switch (ord($flag)) {
  48. case 0:
  49. $address = '';
  50. break;
  51. case 1:
  52. case 2:
  53. fseek($this->fp, $this->getoffset());
  54. $address = $this->getstr();
  55. break;
  56. default:
  57. fseek($this->fp, $now_offset);
  58. $address = $this->getstr();
  59. break;
  60. }
  61. return $address;
  62. }
  63. public function getflag()
  64. {
  65. return fread($this->fp, 1);
  66. }
  67. public function searchip($ip)
  68. {
  69. $ip = gethostbyname($ip);
  70. $ip_offset['ip'] = $ip;
  71. $ip = $this->iptoint($ip);
  72. $firstip = 0;
  73. $lastip = $this->totalip;
  74. $ipoffset = $this->lastip;
  75. while ($firstip <= $lastip) {
  76. $i = floor(($firstip + $lastip) / 2);
  77. fseek($this->fp, $this->firstip + $i * 7);
  78. $startip = strrev(fread($this->fp, 4));
  79. if ($ip < $startip) {
  80. $lastip = $i - 1;
  81. }
  82. else {
  83. fseek($this->fp, $this->getoffset());
  84. $endip = strrev(fread($this->fp, 4));
  85. if ($endip < $ip) {
  86. $firstip = $i + 1;
  87. }
  88. else {
  89. $ip_offset['offset'] = $this->firstip + $i * 7;
  90. break;
  91. }
  92. }
  93. }
  94. return $ip_offset;
  95. }
  96. public function getaddress($ip)
  97. {
  98. $ip_offset = $this->searchip($ip);
  99. $ipoffset = $ip_offset['offset'];
  100. $address['ip'] = $ip_offset['ip'];
  101. fseek($this->fp, $ipoffset);
  102. $address['startip'] = long2ip($this->get4b());
  103. $address_offset = $this->getoffset();
  104. fseek($this->fp, $address_offset);
  105. $address['endip'] = long2ip($this->get4b());
  106. $flag = $this->getflag();
  107. switch (ord($flag)) {
  108. case 1:
  109. $address_offset = $this->getoffset();
  110. fseek($this->fp, $address_offset);
  111. $flag = $this->getflag();
  112. switch (ord($flag)) {
  113. case 2:
  114. fseek($this->fp, $this->getoffset());
  115. $address['area1'] = $this->getstr();
  116. fseek($this->fp, $address_offset + 4);
  117. $address['area2'] = $this->readaddress();
  118. break;
  119. default:
  120. fseek($this->fp, $address_offset);
  121. $address['area1'] = $this->getstr();
  122. $address['area2'] = $this->readaddress();
  123. break;
  124. }
  125. break;
  126. case 2:
  127. $address1_offset = $this->getoffset();
  128. fseek($this->fp, $address1_offset);
  129. $address['area1'] = $this->getstr();
  130. fseek($this->fp, $address_offset + 8);
  131. $address['area2'] = $this->readaddress();
  132. break;
  133. default:
  134. fseek($this->fp, $address_offset + 4);
  135. $address['area1'] = $this->getstr();
  136. $address['area2'] = $this->readaddress();
  137. break;
  138. }
  139. if (strpos($address['area1'], 'CZ88.NET') != false) {
  140. $address['area1'] = '';
  141. }
  142. if (strpos($address['area2'], 'CZ88.NET') != false) {
  143. $address['area2'] = '';
  144. }
  145. return $address;
  146. }
  147. }
  148. !defined('IN_MYMPS') && exit('Access Denied');
  149. define('IPDATA', MYMPS_DATA . '/ipdat/ipdata.dat');
  150. ?>