Ip2Region.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/8/16
  6. * Time: 10:25
  7. */
  8. namespace App\Libs;
  9. use Exception;
  10. class Ip2Region
  11. {
  12. const INDEX_BLOCK_LENGTH = 12;
  13. const TOTAL_HEADER_LENGTH = 8192;
  14. /**
  15. * db file handler
  16. */
  17. private $dbFileHandler = NULL;
  18. /**
  19. * header block info
  20. */
  21. private $HeaderSip = NULL;
  22. private $HeaderPtr = NULL;
  23. private $headerLen = 0;
  24. /**
  25. * super block index info
  26. */
  27. private $firstIndexPtr = 0;
  28. private $lastIndexPtr = 0;
  29. private $totalBlocks = 0;
  30. /**
  31. * for memory mode only
  32. * the original db binary string
  33. */
  34. private $dbBinStr = NULL;
  35. private $dbFile = NULL;
  36. /**
  37. * construct method
  38. *
  39. * @param ip2regionFile
  40. */
  41. public function __construct($ip2regionFile = null)
  42. {
  43. $this->dbFile = is_null($ip2regionFile) ? __DIR__ . '/ip2region.db' : $ip2regionFile;
  44. }
  45. /**
  46. * all the db binary string will be loaded into memory
  47. * then search the memory only and this will a lot faster than disk base search
  48. * @Note:
  49. * invoke it once before put it to public invoke could make it thread safe
  50. *
  51. * @param $ip
  52. */
  53. public function memorySearch($ip)
  54. {
  55. //check and load the binary string for the first time
  56. if ( $this->dbBinStr == NULL ) {
  57. $this->dbBinStr = file_get_contents($this->dbFile);
  58. if ( $this->dbBinStr == false ) {
  59. throw new Exception("Fail to open the db file {$this->dbFile}");
  60. }
  61. $this->firstIndexPtr = self::getLong($this->dbBinStr, 0);
  62. $this->lastIndexPtr = self::getLong($this->dbBinStr, 4);
  63. $this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/self::INDEX_BLOCK_LENGTH + 1;
  64. }
  65. if ( is_string($ip) ) $ip = self::safeIp2long($ip);
  66. //binary search to define the data
  67. $l = 0;
  68. $h = $this->totalBlocks;
  69. $dataPtr = 0;
  70. while ( $l <= $h ) {
  71. $m = (($l + $h) >> 1);
  72. $p = $this->firstIndexPtr + $m * self::INDEX_BLOCK_LENGTH;
  73. $sip = self::getLong($this->dbBinStr, $p);
  74. if ( $ip < $sip ) {
  75. $h = $m - 1;
  76. } else {
  77. $eip = self::getLong($this->dbBinStr, $p + 4);
  78. if ( $ip > $eip ) {
  79. $l = $m + 1;
  80. } else {
  81. $dataPtr = self::getLong($this->dbBinStr, $p + 8);
  82. break;
  83. }
  84. }
  85. }
  86. //not matched just stop it here
  87. if ( $dataPtr == 0 ) return NULL;
  88. //get the data
  89. $dataLen = (($dataPtr >> 24) & 0xFF);
  90. $dataPtr = ($dataPtr & 0x00FFFFFF);
  91. return array(
  92. 'city_id' => self::getLong($this->dbBinStr, $dataPtr),
  93. 'region' => substr($this->dbBinStr, $dataPtr + 4, $dataLen - 4)
  94. );
  95. }
  96. /**
  97. * get the data block throught the specifield ip address or long ip numeric with binary search algorithm
  98. *
  99. * @param ip
  100. * @return mixed Array or NULL for any error
  101. */
  102. public function binarySearch( $ip )
  103. {
  104. //check and conver the ip address
  105. if ( is_string($ip) ) $ip = self::safeIp2long($ip);
  106. if ( $this->totalBlocks == 0 ) {
  107. //check and open the original db file
  108. if ( $this->dbFileHandler == NULL ) {
  109. $this->dbFileHandler = fopen($this->dbFile, 'r');
  110. if ( $this->dbFileHandler == false ) {
  111. throw new Exception("Fail to open the db file {$this->dbFile}");
  112. }
  113. }
  114. fseek($this->dbFileHandler, 0);
  115. $superBlock = fread($this->dbFileHandler, 8);
  116. $this->firstIndexPtr = self::getLong($superBlock, 0);
  117. $this->lastIndexPtr = self::getLong($superBlock, 4);
  118. $this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/self::INDEX_BLOCK_LENGTH + 1;
  119. }
  120. //binary search to define the data
  121. $l = 0;
  122. $h = $this->totalBlocks;
  123. $dataPtr = 0;
  124. while ( $l <= $h ) {
  125. $m = (($l + $h) >> 1);
  126. $p = $m * self::INDEX_BLOCK_LENGTH;
  127. fseek($this->dbFileHandler, $this->firstIndexPtr + $p);
  128. $buffer = fread($this->dbFileHandler, self::INDEX_BLOCK_LENGTH);
  129. $sip = self::getLong($buffer, 0);
  130. if ( $ip < $sip ) {
  131. $h = $m - 1;
  132. } else {
  133. $eip = self::getLong($buffer, 4);
  134. if ( $ip > $eip ) {
  135. $l = $m + 1;
  136. } else {
  137. $dataPtr = self::getLong($buffer, 8);
  138. break;
  139. }
  140. }
  141. }
  142. //not matched just stop it here
  143. if ( $dataPtr == 0 ) return NULL;
  144. //get the data
  145. $dataLen = (($dataPtr >> 24) & 0xFF);
  146. $dataPtr = ($dataPtr & 0x00FFFFFF);
  147. fseek($this->dbFileHandler, $dataPtr);
  148. $data = fread($this->dbFileHandler, $dataLen);
  149. return array(
  150. 'city_id' => self::getLong($data, 0),
  151. 'region' => substr($data, 4)
  152. );
  153. }
  154. /**
  155. * get the data block associated with the specifield ip with b-tree search algorithm
  156. * @Note: not thread safe
  157. *
  158. * @param ip
  159. * @return Mixed Array for NULL for any error
  160. */
  161. public function btreeSearch( $ip )
  162. {
  163. if ( is_string($ip) ) $ip = self::safeIp2long($ip);
  164. //check and load the header
  165. if ( $this->HeaderSip == NULL ) {
  166. //check and open the original db file
  167. if ( $this->dbFileHandler == NULL ) {
  168. $this->dbFileHandler = fopen($this->dbFile, 'r');
  169. if ( $this->dbFileHandler == false ) {
  170. throw new Exception("Fail to open the db file {$this->dbFile}");
  171. }
  172. }
  173. fseek($this->dbFileHandler, 8);
  174. $buffer = fread($this->dbFileHandler, self::TOTAL_HEADER_LENGTH);
  175. //fill the header
  176. $idx = 0;
  177. $this->HeaderSip = array();
  178. $this->HeaderPtr = array();
  179. for ( $i = 0; $i < self::TOTAL_HEADER_LENGTH; $i += 8 ) {
  180. $startIp = self::getLong($buffer, $i);
  181. $dataPtr = self::getLong($buffer, $i + 4);
  182. if ( $dataPtr == 0 ) break;
  183. $this->HeaderSip[] = $startIp;
  184. $this->HeaderPtr[] = $dataPtr;
  185. $idx++;
  186. }
  187. $this->headerLen = $idx;
  188. }
  189. //1. define the index block with the binary search
  190. $l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
  191. while ( $l <= $h ) {
  192. $m = (($l + $h) >> 1);
  193. //perfetc matched, just return it
  194. if ( $ip == $this->HeaderSip[$m] ) {
  195. if ( $m > 0 ) {
  196. $sptr = $this->HeaderPtr[$m-1];
  197. $eptr = $this->HeaderPtr[$m ];
  198. } else {
  199. $sptr = $this->HeaderPtr[$m ];
  200. $eptr = $this->HeaderPtr[$m+1];
  201. }
  202. break;
  203. }
  204. //less then the middle value
  205. if ( $ip < $this->HeaderSip[$m] ) {
  206. if ( $m == 0 ) {
  207. $sptr = $this->HeaderPtr[$m ];
  208. $eptr = $this->HeaderPtr[$m+1];
  209. break;
  210. } else if ( $ip > $this->HeaderSip[$m-1] ) {
  211. $sptr = $this->HeaderPtr[$m-1];
  212. $eptr = $this->HeaderPtr[$m ];
  213. break;
  214. }
  215. $h = $m - 1;
  216. } else {
  217. if ( $m == $this->headerLen - 1 ) {
  218. $sptr = $this->HeaderPtr[$m-1];
  219. $eptr = $this->HeaderPtr[$m ];
  220. break;
  221. } else if ( $ip <= $this->HeaderSip[$m+1] ) {
  222. $sptr = $this->HeaderPtr[$m ];
  223. $eptr = $this->HeaderPtr[$m+1];
  224. break;
  225. }
  226. $l = $m + 1;
  227. }
  228. }
  229. //match nothing just stop it
  230. if ( $sptr == 0 ) return NULL;
  231. //2. search the index blocks to define the data
  232. $blockLen = $eptr - $sptr;
  233. fseek($this->dbFileHandler, $sptr);
  234. $index = fread($this->dbFileHandler, $blockLen + self::INDEX_BLOCK_LENGTH);
  235. $dataptr = 0;
  236. $l = 0; $h = $blockLen / self::INDEX_BLOCK_LENGTH;
  237. while ( $l <= $h ) {
  238. $m = (($l + $h) >> 1);
  239. $p = (int)($m * self::INDEX_BLOCK_LENGTH);
  240. $sip = self::getLong($index, $p);
  241. if ( $ip < $sip ) {
  242. $h = $m - 1;
  243. } else {
  244. $eip = self::getLong($index, $p + 4);
  245. if ( $ip > $eip ) {
  246. $l = $m + 1;
  247. } else {
  248. $dataptr = self::getLong($index, $p + 8);
  249. break;
  250. }
  251. }
  252. }
  253. //not matched
  254. if ( $dataptr == 0 ) return NULL;
  255. //3. get the data
  256. $dataLen = (($dataptr >> 24) & 0xFF);
  257. $dataPtr = ($dataptr & 0x00FFFFFF);
  258. fseek($this->dbFileHandler, $dataPtr);
  259. $data = fread($this->dbFileHandler, $dataLen);
  260. return array(
  261. 'city_id' => self::getLong($data, 0),
  262. 'region' => substr($data, 4)
  263. );
  264. }
  265. /**
  266. * safe self::safeIp2long function
  267. *
  268. * @param ip
  269. * */
  270. public static function safeIp2long($ip)
  271. {
  272. $ip = ip2long($ip);
  273. // convert signed int to unsigned int if on 32 bit operating system
  274. if ($ip < 0 && PHP_INT_SIZE == 4) {
  275. $ip = sprintf("%u", $ip);
  276. }
  277. return $ip;
  278. }
  279. /**
  280. * read a long from a byte buffer
  281. *
  282. * @param b
  283. * @param offset
  284. */
  285. public static function getLong( $b, $offset )
  286. {
  287. $val = (
  288. (ord($b[$offset++])) |
  289. (ord($b[$offset++]) << 8) |
  290. (ord($b[$offset++]) << 16) |
  291. (ord($b[$offset ]) << 24)
  292. );
  293. // convert signed int to unsigned int if on 32 bit operating system
  294. if ($val < 0 && PHP_INT_SIZE == 4) {
  295. $val = sprintf("%u", $val);
  296. }
  297. return $val;
  298. }
  299. /**
  300. * destruct method, resource destroy
  301. */
  302. public function __destruct()
  303. {
  304. if ( $this->dbFileHandler != NULL ) {
  305. fclose($this->dbFileHandler);
  306. }
  307. $this->dbBinStr = NULL;
  308. $this->HeaderSip = NULL;
  309. $this->HeaderPtr = NULL;
  310. }
  311. }