/includes/ip_handling_include.php

https://github.com/necrophcodr/Muks · PHP · 72 lines · 49 code · 2 blank · 21 comment · 7 complexity · 1f1ebbe6db0b5dfa470a6bcdaae65872 MD5 · raw file

  1. <?php
  2. /*-------------------------------------------------------+
  3. | PHP-Fusion Content Management System
  4. | Copyright (C) 2002 - 2011 Nick Jones
  5. | http://www.php-fusion.co.uk/
  6. +--------------------------------------------------------+
  7. | Filename: ip_handling_include.php
  8. | Author: Karoly Nagy (Korcsii)
  9. +--------------------------------------------------------+
  10. | This program is released as free software under the
  11. | Affero GPL license. You can redistribute it and/or
  12. | modify it under the terms of this license which you
  13. | can read by viewing the included agpl.txt or online
  14. | at www.gnu.org/licenses/agpl.html. Removal of this
  15. | copyright header is strictly prohibited without
  16. | written permission from the original author(s).
  17. +--------------------------------------------------------*/
  18. if (!defined("IN_FUSION")) { die("Access Denied"); }
  19. // Uncompress an IPv6 address
  20. function uncompressIPv6($ip, $count=7) {
  21. if (strpos($ip, "::") !== FALSE) {
  22. $ip = str_replace("::", str_repeat(":", $count + 2 - substr_count($ip, ":")), $ip);
  23. }
  24. $tmp_ip = explode(":", $ip);
  25. foreach ($tmp_ip as &$value) {
  26. $value = str_pad($value, 4, '0', STR_PAD_LEFT);
  27. }
  28. return implode(":", $tmp_ip);
  29. }
  30. // Check if users full or partial ip is blacklisted and set USER_IP and USER_IP_TYPE
  31. if (strpos(FUSION_IP, ".")) {
  32. if (strpos(FUSION_IP, ":") === FALSE) {
  33. // IPv4
  34. define("USER_IP_TYPE", 4);
  35. define("USER_IP", FUSION_IP);
  36. $check_value = "blacklist_ip_type='4' AND blacklist_ip REGEXP '^";
  37. $check_value .= str_replace(".", ".(", USER_IP, $i);
  38. $check_value .= str_repeat(")?", $i);
  39. $check_value .= "$'";
  40. } else {
  41. // Mixed IPv4 and IPv6
  42. define("USER_IP_TYPE", 5);
  43. $last_pos = strrpos(FUSION_IP, ":");
  44. $ipv4 = substr(FUSION_IP, $last_pos+1);
  45. $ipv6 = substr(FUSION_IP, 0, $last_pos);
  46. $ipv6 = uncompressIPv6($ipv6, 5);
  47. define("USER_IP", $ipv6.":".$ipv4);
  48. $check_value = "(blacklist_ip_type='4' AND blacklist_ip REGEXP '^";
  49. $check_value .= str_replace(".", ".(", $ipv4, $i);
  50. $check_value .= str_repeat(")?", $i);
  51. $check_value .= "$') OR (blacklist_ip_type='6' AND blacklist_ip REGEXP '^";
  52. $check_value .= str_replace(":", ":(", $ipv6, $i);
  53. $check_value .= str_repeat(")?", $i);
  54. $check_value .= "$') OR (blacklist_ip_type='5' AND blacklist_ip='".USER_IP."')";
  55. unset($ipv4, $ipv6, $last_pos);
  56. }
  57. } else {
  58. // IPv6
  59. define("USER_IP_TYPE", 6);
  60. define("USER_IP", uncompressIPv6(FUSION_IP, 7));
  61. $check_value = "blacklist_ip_type='6' AND blacklist_ip REGEXP '^";
  62. $check_value .= str_replace(":", ":(", USER_IP, $i);
  63. $check_value .= str_repeat(")?", $i);
  64. $check_value .= "$'";
  65. }
  66. if (dbcount("(blacklist_id)", DB_BLACKLIST, $check_value)) {
  67. redirect("http://www.google.com/");
  68. }
  69. unset($check_value);
  70. ?>