This is a new version of a previous snippet I wrote. This version is smaller, more efficient, and allows the use of regular expressions. To use:
$blocker = new blocker();
if ($blocker->isBlocked === true) {
//blocked user, do whatever
} else {}
OR if you want to test IF a certain IP or hostname or user agent WOULD be blocked:
$blocker = new blocker();
list($isBlocked, $reasonsBlocked) = $blocker->testIfBlocked([userIP], [userAgent], [userHostname]);
if ($isBlocked === true) {
print_r($reasonsBlocked);
} else {}
The 3 arguments are optional, but if specified, must be specified in that order. Any unspecified arguments will default to the values of the user executing the call. For example, if you do not specify a user agent and host name, and you're the one executing the test, it will use YOUR user agent and host name. An array of reasons why the test said you failed will be returned (along with a boolean true/false as to if the criteria would be blocked or not).
<?php /** * This is the Security/Blocking class * @package Skyward_Landing_Page * @subpackage Tier_1_Security * @filesource * @author Matt Ford * @version 1.0 */ //if (!defined("parentFile")) {die("Direct Access Not Allowed");} else {} class blocker { /** * array if IP regexp's * @var array */ "^10\.40\.", "\.3\.105$", "^195\.178\.177" ); /** * array if User Agent regexp's * @var array */ ); /** * array if Hostname regexp's * @var array */ ); /** * array if reasons the user is blocked * @var array */ /** * true/false if user is blocked * @var boolean */ public $isBlocked = false; public function __construct() { $this->isBlocked = $isBlocked; $this->reasonsBlocked = $reasonsBlocked; } public function testIfBlocked($ip = "", $ua = "", $host = "") { $check_ip = ($ip == "") ? $_SERVER["REMOTE_ADDR"] : $ip; $check_ua = ($ua == "") ? $_SERVER["HTTP_USER_AGENT"] : $ua; $isBlocked = false; foreach ($this->ips as $ip) { $reasonsBlocked[] = $this->registerMatch("IP", $ip, $check_ip, $tempMatches); $isBlocked = true; } else {} } foreach ($this->userAgents as $ua) { $reasonsBlocked[] = $this->registerMatch("User Agent", $ua, $check_ua, $tempMatches); $isBlocked = true; } else {} } foreach ($this->hostNames as $hn) { $reasonsBlocked[] = $this->registerMatch("Host Name", $hn, $check_host, $tempMatches); $isBlocked = true; } else {} } } /** * This method registers the block/match arguments into the array of reasons the user was blocked * @access public * @author Matt Ford * @category Blocking * @param string $type match type, such as IP match, User Agent Match, etc * @param string $rule the RegExp rule responsible for the match * @param string $source the source string the RegExp rule matched against successfully * @param array $matches the matches output from the preg_match function call */ private function registerMatch($type, $rule, $source, $matches) { "type" => $type, "rule" => $rule, "source" => $source, "matches" => $matches ); } } ?>
You need to login to post a comment.
