Ip2Region.php 10 KB

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