Source of file Blacklist.php
Size: 7,287 Bytes - Last Modified: 2021-12-24T06:43:27+00:00
/var/www/docs.ssmods.com/process/src/code/Blacklist.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 | <?php /** * Blacklist * * Logs user details and blocks users based on IP, host, or referer. * * @package silverstripe-blacklist * @license MIT License https://github.com/cyber-duck/silverstripe-blacklist/blob/master/LICENSE * @author <andrewm@cyber-duck.co.uk> **/ class BlackList { /** * Array of bot names * * @since version 1.0.0 * * @var array $bots **/ private $bots = []; /** * Current user type * * @since version 1.0.0 * * @var string $type **/ private $type = 'human'; /** * Current user IP * * @since version 1.0.0 * * @var string $ip **/ private $ip; /** * * * @since version 1.0.0 * * @var string $host **/ private $host; /** * Current user referer * * @since version 1.0.0 * * @var string $referer **/ private $referer; /** * Log users * * @since version 1.0.0 * * @var bool $logUsers **/ private $logUsers = true; /** * Log bots * * @since version 1.0.0 * * @var bool $logBots **/ private $logBots = true; /** * Array of Server headers * * @since version 1.0.0 * * @var array $ipHeaders **/ private $ipHeaders = [ 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ]; /** * Set up default configuration * * @since version 1.0.0 * * @return void **/ public function __construct() { $this->bots = Config::inst()->get('Blacklist', 'bots'); $this->setUserType(); $this->setUserIP(); $this->setUserHost(); $this->setUserReferer(); } /** * Enable logging of users * * @since version 1.0.0 * * @param bool $enabled * * @return void **/ public function logUsers($enabled = true) { $this->logUsers = $enabled; } /** * Enable logging of bots * * @since version 1.0.0 * * @param bool $enabled * * @return void **/ public function logBots($enabled = true) { $this->logBots = $enabled; } /** * Set the current user type * * @since version 1.0.0 * * @param string $type * * @return void **/ public function setType($type) { $this->type = $type; } /** * Set the current user IP * * @since version 1.0.0 * * @param string $ip * * @return void **/ public function setIP($ip) { $this->ip = $ip; } /** * Set the current user host * * @since version 1.0.0 * * @param string $host * * @return void **/ public function setHost($host) { $this->host = $host; } /** * Set the current user referer * * @since version 1.0.0 * * @param string $referer * * @return void **/ public function setReferer($referer) { $this->referer = $referer; } /** * Get the current user type * * @since version 1.0.0 * * @return string **/ public function getType() { return $this->type; } /** * Get the current user IP * * @since version 1.0.0 * * @return string **/ public function getIP() { return $this->ip; } /** * Get the current user host * * @since version 1.0.0 * * @return string **/ public function getHost() { return $this->host; } /** * Get the current user referer * * @since version 1.0.0 * * @return string **/ public function getReferer() { return $this->referer; } /** * Perform the logging action * * @since version 1.0.0 * * @return void **/ public function doLog() { if($this->logUsers === true) { if($this->type == 'human') { $this->writeLog(); } } if($this->logBots === true) { if($this->type == 'bot') { $this->writeLog(); } } } /** * Perform the blocking action * * @since version 1.0.0 * * @return void **/ public function doBlock() { foreach(BlockedUser::get() as $blocked) { if($blocked->Ip) { if(trim($this->ip) == trim($blocked->Ip)) $this->forbidden(); } if($blocked->IpMin && $blocked->IpMax) { $ip = ip2long($this->ip); $min = ip2long($blocked->ipMin); $max = ip2long($blocked->ipMax); if($ip >= $min && $ip <= $max) $this->forbidden(); } if(strpos($this->host, $blocked->host) !== false) $this->forbidden(); if(strpos($this->referer, $blocked->referer) !== false) $this->forbidden(); } } /** * Set the default user type * * @since version 1.0.0 * * @return void **/ private function setUserType() { foreach($this->bots as $bot) { if(strpos($this->host, $bot) !== false) { return $this->setType('bot'); } } } /** * Set the default user IP * * @since version 1.0.0 * * @return void **/ private function setUserIP() { foreach($this->ipHeaders as $header) { if(array_key_exists($header, $_SERVER)) { foreach (explode(',', $_SERVER[$header]) as $ip) { $ip = trim($ip); if(filter_var($ip, FILTER_VALIDATE_IP)) { return $this->setIP($ip); } } } } } /** * Set the default user host * * @since version 1.0.0 * * @return void **/ private function setUserHost() { if(isset($this->ip)) $this->setHost(gethostbyaddr($this->ip)); } /** * Set the default user referer * * @since version 1.0.0 * * @return void **/ private function setUserReferer() { if(isset($_SERVER['HTTP_REFERER'])) $this->setReferer($_SERVER['HTTP_REFERER']); } /** * Write the current user log * * @since version 1.0.0 * * @return void **/ private function writeLog() { $logger = LoggedUser::create(); $logger->Type = $this->getType(); $logger->Ip = $this->getIP(); $logger->Host = $this->getHost(); $logger->Referer = $this->getReferer(); $logger->Url = Controller::curr()->getRequest()->getUrl(); $logger->write(); } /** * Forbidden user redirect * * @since version 1.0.0 * * @return void **/ private function forbidden() { return Controller::curr()->httpError(403, 'Blacklisted'); } } |