1
0

ip_in_range.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /*
  3. * ip_in_range.php - Function to determine if an IP is located in a
  4. * specific range as specified via several alternative
  5. * formats.
  6. *
  7. * Network ranges can be specified as:
  8. * 1. Wildcard format: 1.2.3.*
  9. * 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
  10. * 3. Start-End IP format: 1.2.3.0-1.2.3.255
  11. *
  12. * Return value BOOLEAN : ip_in_range($ip, $range);
  13. *
  14. * Copyright 2008: Paul Gregg <[email protected]>
  15. * 10 January 2008
  16. * Version: 1.2
  17. *
  18. * Source website: http://www.pgregg.com/projects/php/ip_in_range/
  19. * Version 1.2
  20. *
  21. * This software is Donationware - if you feel you have benefited from
  22. * the use of this tool then please consider a donation. The value of
  23. * which is entirely left up to your discretion.
  24. * http://www.pgregg.com/donate/
  25. *
  26. * Please do not remove this header, or source attibution from this file.
  27. */
  28. /*
  29. * Modified by James Greene <[email protected]> to include IPV6 support
  30. * (original version only supported IPV4).
  31. * 21 May 2012
  32. */
  33. // decbin32
  34. // In order to simplify working with IP addresses (in binary) and their
  35. // netmasks, it is easier to ensure that the binary strings are padded
  36. // with zeros out to 32 characters - IP addresses are 32 bit numbers
  37. function decbin32 ($dec) {
  38. return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
  39. }
  40. // ipv4_in_range
  41. // This function takes 2 arguments, an IP address and a "range" in several
  42. // different formats.
  43. // Network ranges can be specified as:
  44. // 1. Wildcard format: 1.2.3.*
  45. // 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
  46. // 3. Start-End IP format: 1.2.3.0-1.2.3.255
  47. // The function will return true if the supplied IP is within the range.
  48. // Note little validation is done on the range inputs - it expects you to
  49. // use one of the above 3 formats.
  50. function ipv4_in_range($ip, $range) {
  51. if (strpos($range, '/') !== false) {
  52. // $range is in IP/NETMASK format
  53. list($range, $netmask) = explode('/', $range, 2);
  54. if (strpos($netmask, '.') !== false) {
  55. // $netmask is a 255.255.0.0 format
  56. $netmask = str_replace('*', '0', $netmask);
  57. $netmask_dec = ip2long($netmask);
  58. return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  59. } else {
  60. // $netmask is a CIDR size block
  61. // fix the range argument
  62. $x = explode('.', $range);
  63. while(count($x)<4) $x[] = '0';
  64. list($a,$b,$c,$d) = $x;
  65. $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
  66. $range_dec = ip2long($range);
  67. $ip_dec = ip2long($ip);
  68. # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
  69. #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
  70. # Strategy 2 - Use math to create it
  71. $wildcard_dec = pow(2, (32-$netmask)) - 1;
  72. $netmask_dec = ~ $wildcard_dec;
  73. return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
  74. }
  75. } else {
  76. // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  77. if (strpos($range, '*') !==false) { // a.b.*.* format
  78. // Just convert to A-B format by setting * to 0 for A and 255 for B
  79. $lower = str_replace('*', '0', $range);
  80. $upper = str_replace('*', '255', $range);
  81. $range = "$lower-$upper";
  82. }
  83. if (strpos($range, '-')!==false) { // A-B format
  84. list($lower, $upper) = explode('-', $range, 2);
  85. $lower_dec = (float)sprintf("%u",ip2long($lower));
  86. $upper_dec = (float)sprintf("%u",ip2long($upper));
  87. $ip_dec = (float)sprintf("%u",ip2long($ip));
  88. return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
  89. }
  90. return false;
  91. }
  92. }
  93. function ip2long6($ip) {
  94. if (substr_count($ip, '::')) {
  95. $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
  96. }
  97. $ip = explode(':', $ip);
  98. $r_ip = '';
  99. foreach ($ip as $v) {
  100. $r_ip .= str_pad(base_convert($v, 16, 2), 16, 0, STR_PAD_LEFT);
  101. }
  102. return base_convert($r_ip, 2, 10);
  103. }
  104. // Get the ipv6 full format and return it as a decimal value.
  105. function get_ipv6_full($ip)
  106. {
  107. $pieces = explode ("/", $ip, 2);
  108. $left_piece = $pieces[0];
  109. $right_piece = $pieces[1];
  110. // Extract out the main IP pieces
  111. $ip_pieces = explode("::", $left_piece, 2);
  112. $main_ip_piece = $ip_pieces[0];
  113. $last_ip_piece = $ip_pieces[1];
  114. // Pad out the shorthand entries.
  115. $main_ip_pieces = explode(":", $main_ip_piece);
  116. foreach($main_ip_pieces as $key=>$val) {
  117. $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
  118. }
  119. // Check to see if the last IP block (part after ::) is set
  120. $last_piece = "";
  121. $size = count($main_ip_pieces);
  122. if (trim($last_ip_piece) != "") {
  123. $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
  124. // Build the full form of the IPV6 address considering the last IP block set
  125. for ($i = $size; $i < 7; $i++) {
  126. $main_ip_pieces[$i] = "0000";
  127. }
  128. $main_ip_pieces[7] = $last_piece;
  129. }
  130. else {
  131. // Build the full form of the IPV6 address
  132. for ($i = $size; $i < 8; $i++) {
  133. $main_ip_pieces[$i] = "0000";
  134. }
  135. }
  136. // Rebuild the final long form IPV6 address
  137. $final_ip = implode(":", $main_ip_pieces);
  138. return ip2long6($final_ip);
  139. }
  140. // Determine whether the IPV6 address is within range.
  141. // $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip.
  142. // $ip and $range_ip are converted to full IPV6 format.
  143. // Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise.
  144. function ipv6_in_range($ip, $range_ip)
  145. {
  146. $pieces = explode ("/", $range_ip, 2);
  147. $left_piece = $pieces[0];
  148. $right_piece = $pieces[1];
  149. // Extract out the main IP pieces
  150. $ip_pieces = explode("::", $left_piece, 2);
  151. $main_ip_piece = $ip_pieces[0];
  152. $last_ip_piece = $ip_pieces[1];
  153. // Pad out the shorthand entries.
  154. $main_ip_pieces = explode(":", $main_ip_piece);
  155. foreach($main_ip_pieces as $key=>$val) {
  156. $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
  157. }
  158. // Create the first and last pieces that will denote the IPV6 range.
  159. $first = $main_ip_pieces;
  160. $last = $main_ip_pieces;
  161. // Check to see if the last IP block (part after ::) is set
  162. $last_piece = "";
  163. $size = count($main_ip_pieces);
  164. if (trim($last_ip_piece) != "") {
  165. $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
  166. // Build the full form of the IPV6 address considering the last IP block set
  167. for ($i = $size; $i < 7; $i++) {
  168. $first[$i] = "0000";
  169. $last[$i] = "ffff";
  170. }
  171. $main_ip_pieces[7] = $last_piece;
  172. }
  173. else {
  174. // Build the full form of the IPV6 address
  175. for ($i = $size; $i < 8; $i++) {
  176. $first[$i] = "0000";
  177. $last[$i] = "ffff";
  178. }
  179. }
  180. // Rebuild the final long form IPV6 address
  181. $first = ip2long6(implode(":", $first));
  182. $last = ip2long6(implode(":", $last));
  183. $in_range = ($ip >= $first && $ip <= $last);
  184. return $in_range;
  185. }
  186. ?>