PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/plugins/slug/function.iplocation.php

https://github.com/sahilbabu/phpb2b
PHP | 209 lines | 169 code | 34 blank | 6 comment | 63 complexity | 5cded8704f722270cc72756580684b0c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * [PHPB2B] Copyright (C) 2007-2099, Ualink Inc. All Rights Reserved.
  4. * The contents of this file are subject to the License; you may not use this file except in compliance with the License.
  5. * Thanks_For_Discuz!
  6. * @version $Revision: 2053 $
  7. */
  8. function smarty_function_iplocation($params) {
  9. $return = '';
  10. extract($params);
  11. if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
  12. $iparray = explode('.', $ip);
  13. if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
  14. $return = '- LAN';
  15. } elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
  16. $return = '- Invalid IP Address';
  17. } else {
  18. $tinyipfile = DATA_PATH. 'ipdata/tiny.dat';
  19. $fullipfile = DATA_PATH. 'ipdata/qqwry.dat';
  20. if(@file_exists($tinyipfile)) {
  21. $return = convertip_tiny($ip, $tinyipfile);
  22. } elseif(@file_exists($fullipfile)) {
  23. $return = convertip_full($ip, $fullipfile);
  24. }
  25. }
  26. }
  27. return $return;
  28. }
  29. function convertip_tiny($ip, $ipdatafile) {
  30. static $fp = NULL, $offset = array(), $index = NULL;
  31. $ipdot = explode('.', $ip);
  32. $ip = pack('N', ip2long($ip));
  33. $ipdot[0] = (int)$ipdot[0];
  34. $ipdot[1] = (int)$ipdot[1];
  35. if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
  36. $offset = @unpack('Nlen', @fread($fp, 4));
  37. $index = @fread($fp, $offset['len'] - 4);
  38. } elseif($fp == FALSE) {
  39. return '- Invalid IP data file';
  40. }
  41. $length = $offset['len'] - 1028;
  42. $start = @unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
  43. for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
  44. if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
  45. $index_offset = @unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
  46. $index_length = @unpack('Clen', $index{$start + 7});
  47. break;
  48. }
  49. }
  50. @fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
  51. if($index_length['len']) {
  52. return '- '.@fread($fp, $index_length['len']);
  53. } else {
  54. return '- Unknown';
  55. }
  56. }
  57. function convertip_full($ip, $ipdatafile) {
  58. if(!$fd = @fopen($ipdatafile, 'rb')) {
  59. return '- Invalid IP data file';
  60. }
  61. $ip = explode('.', $ip);
  62. $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
  63. if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
  64. @$ipbegin = implode('', unpack('L', $DataBegin));
  65. if($ipbegin < 0) $ipbegin += pow(2, 32);
  66. @$ipend = implode('', unpack('L', $DataEnd));
  67. if($ipend < 0) $ipend += pow(2, 32);
  68. $ipAllNum = ($ipend - $ipbegin) / 7 + 1;
  69. $BeginNum = $ip2num = $ip1num = 0;
  70. $ipAddr1 = $ipAddr2 = '';
  71. $EndNum = $ipAllNum;
  72. while($ip1num > $ipNum || $ip2num < $ipNum) {
  73. $Middle= intval(($EndNum + $BeginNum) / 2);
  74. fseek($fd, $ipbegin + 7 * $Middle);
  75. $ipData1 = fread($fd, 4);
  76. if(strlen($ipData1) < 4) {
  77. fclose($fd);
  78. return '- System Error';
  79. }
  80. $ip1num = implode('', unpack('L', $ipData1));
  81. if($ip1num < 0) $ip1num += pow(2, 32);
  82. if($ip1num > $ipNum) {
  83. $EndNum = $Middle;
  84. continue;
  85. }
  86. $DataSeek = fread($fd, 3);
  87. if(strlen($DataSeek) < 3) {
  88. fclose($fd);
  89. return '- System Error';
  90. }
  91. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  92. fseek($fd, $DataSeek);
  93. $ipData2 = fread($fd, 4);
  94. if(strlen($ipData2) < 4) {
  95. fclose($fd);
  96. return '- System Error';
  97. }
  98. $ip2num = implode('', unpack('L', $ipData2));
  99. if($ip2num < 0) $ip2num += pow(2, 32);
  100. if($ip2num < $ipNum) {
  101. if($Middle == $BeginNum) {
  102. fclose($fd);
  103. return '- Unknown';
  104. }
  105. $BeginNum = $Middle;
  106. }
  107. }
  108. $ipFlag = fread($fd, 1);
  109. if($ipFlag == chr(1)) {
  110. $ipSeek = fread($fd, 3);
  111. if(strlen($ipSeek) < 3) {
  112. fclose($fd);
  113. return '- System Error';
  114. }
  115. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  116. fseek($fd, $ipSeek);
  117. $ipFlag = fread($fd, 1);
  118. }
  119. if($ipFlag == chr(2)) {
  120. $AddrSeek = fread($fd, 3);
  121. if(strlen($AddrSeek) < 3) {
  122. fclose($fd);
  123. return '- System Error';
  124. }
  125. $ipFlag = fread($fd, 1);
  126. if($ipFlag == chr(2)) {
  127. $AddrSeek2 = fread($fd, 3);
  128. if(strlen($AddrSeek2) < 3) {
  129. fclose($fd);
  130. return '- System Error';
  131. }
  132. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  133. fseek($fd, $AddrSeek2);
  134. } else {
  135. fseek($fd, -1, SEEK_CUR);
  136. }
  137. while(($char = fread($fd, 1)) != chr(0))
  138. $ipAddr2 .= $char;
  139. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  140. fseek($fd, $AddrSeek);
  141. while(($char = fread($fd, 1)) != chr(0))
  142. $ipAddr1 .= $char;
  143. } else {
  144. fseek($fd, -1, SEEK_CUR);
  145. while(($char = fread($fd, 1)) != chr(0))
  146. $ipAddr1 .= $char;
  147. $ipFlag = fread($fd, 1);
  148. if($ipFlag == chr(2)) {
  149. $AddrSeek2 = fread($fd, 3);
  150. if(strlen($AddrSeek2) < 3) {
  151. fclose($fd);
  152. return '- System Error';
  153. }
  154. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  155. fseek($fd, $AddrSeek2);
  156. } else {
  157. fseek($fd, -1, SEEK_CUR);
  158. }
  159. while(($char = fread($fd, 1)) != chr(0))
  160. $ipAddr2 .= $char;
  161. }
  162. fclose($fd);
  163. if(preg_match('/http/i', $ipAddr2)) {
  164. $ipAddr2 = '';
  165. }
  166. $ipaddr = "$ipAddr1 $ipAddr2";
  167. $ipaddr = preg_replace('/CZ88\.NET/is', '', $ipaddr);
  168. $ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
  169. $ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
  170. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  171. $ipaddr = '- Unknown';
  172. }
  173. return '- '.$ipaddr;
  174. }
  175. ?>