QwikClock is an employee time tracking app.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

iputils.php 3.7KB

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