Single-sign-on and self-serve account management. https://netsyms.biz/apps/accounthub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iputils.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * Check if a given ipv4 address is in a given cidr
  7. * @param string $ip IP to check in IPV4 format eg. 127.0.0.1
  8. * @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
  9. * @return boolean true if the ip is in this range / false if not.
  10. * @author Thorsten Ott <https://gist.github.com/tott/7684443>
  11. */
  12. function ip4_in_cidr($ip, $cidr) {
  13. if (strpos($cidr, '/') == false) {
  14. $cidr .= '/32';
  15. }
  16. // $range is in IP/CIDR format eg 127.0.0.1/24
  17. list( $cidr, $netmask ) = explode('/', $cidr, 2);
  18. $range_decimal = ip2long($cidr);
  19. $ip_decimal = ip2long($ip);
  20. $wildcard_decimal = pow(2, ( 32 - $netmask)) - 1;
  21. $netmask_decimal = ~ $wildcard_decimal;
  22. return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
  23. }
  24. /**
  25. * Check if a given ipv6 address is in a given cidr
  26. * @param string $ip IP to check in IPV6 format
  27. * @param string $cidr CIDR netmask
  28. * @return boolean true if the IP is in this range, false otherwise.
  29. * @author MW. <https://stackoverflow.com/a/7952169>
  30. */
  31. function ip6_in_cidr($ip, $cidr) {
  32. $address = inet_pton($ip);
  33. $subnetAddress = inet_pton(explode("/", $cidr)[0]);
  34. $subnetMask = explode("/", $cidr)[1];
  35. $addr = str_repeat("f", $subnetMask / 4);
  36. switch ($subnetMask % 4) {
  37. case 0:
  38. break;
  39. case 1:
  40. $addr .= "8";
  41. break;
  42. case 2:
  43. $addr .= "c";
  44. break;
  45. case 3:
  46. $addr .= "e";
  47. break;
  48. }
  49. $addr = str_pad($addr, 32, '0');
  50. $addr = pack("H*", $addr);
  51. $binMask = $addr;
  52. return ($address & $binMask) == $subnetAddress;
  53. }
  54. /**
  55. * Check if the REMOTE_ADDR is on Cloudflare's network.
  56. * @return boolean true if it is, otherwise false
  57. */
  58. function validateCloudflare() {
  59. if (filter_var($_SERVER["REMOTE_ADDR"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  60. // Using IPv6
  61. $cloudflare_ips_v6 = [
  62. "2400:cb00::/32",
  63. "2405:8100::/32",
  64. "2405:b500::/32",
  65. "2606:4700::/32",
  66. "2803:f800::/32",
  67. "2c0f:f248::/32",
  68. "2a06:98c0::/29"
  69. ];
  70. $valid = false;
  71. foreach ($cloudflare_ips_v6 as $cidr) {
  72. if (ip6_in_cidr($_SERVER["REMOTE_ADDR"], $cidr)) {
  73. $valid = true;
  74. break;
  75. }
  76. }
  77. } else {
  78. // Using IPv4
  79. $cloudflare_ips_v4 = [
  80. "103.21.244.0/22",
  81. "103.22.200.0/22",
  82. "103.31.4.0/22",
  83. "104.16.0.0/12",
  84. "108.162.192.0/18",
  85. "131.0.72.0/22",
  86. "141.101.64.0/18",
  87. "162.158.0.0/15",
  88. "172.64.0.0/13",
  89. "173.245.48.0/20",
  90. "188.114.96.0/20",
  91. "190.93.240.0/20",
  92. "197.234.240.0/22",
  93. "198.41.128.0/17"
  94. ];
  95. $valid = false;
  96. foreach ($cloudflare_ips_v4 as $cidr) {
  97. if (ip4_in_cidr($_SERVER["REMOTE_ADDR"], $cidr)) {
  98. $valid = true;
  99. break;
  100. }
  101. }
  102. }
  103. return $valid;
  104. }
  105. /**
  106. * Makes a good guess at the client's real IP address.
  107. *
  108. * @return string Client IP or `0.0.0.0` if we can't find anything
  109. */
  110. function getClientIP() {
  111. // If CloudFlare is in the mix, we should use it.
  112. // Check if the request is actually from CloudFlare before trusting it.
  113. if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  114. if (validateCloudflare()) {
  115. return $_SERVER["HTTP_CF_CONNECTING_IP"];
  116. }
  117. }
  118. if (isset($_SERVER["REMOTE_ADDR"])) {
  119. return $_SERVER["REMOTE_ADDR"];
  120. }
  121. return "0.0.0.0"; // This will not happen unless we aren't a web server
  122. }