*/ class action_plugin_ipban extends DokuWiki_Action_Plugin { /** * register the eventhandlers and initialize some options */ public function register(Doku_Event_Handler $controller) { $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_start', array()); } /** * Do the magic * * @param Doku_Event $event * @param $param */ public function handle_start(Doku_Event $event, $param) { global $conf; $bans = @file($conf['cachedir'] . '/ipbanplugin.txt'); $client = clientIP(true); if (!is_array($bans)) return; // if the client isn't banned, we're done $banreason = $this->isBanned($client, $bans); if (!$banreason) return; // prepare template $text = $this->locale_xhtml('banned'); $text .= vsprintf('

' . $this->getLang('banned') . '

', array_map('hsc', $banreason)); $title = $this->getLang('denied'); // output http_status(403, 'Forbidden'); echo ''; echo ''; echo "$title"; echo ''; echo '
'; echo $text; echo '
'; echo ''; echo ''; exit; } /** * Check if the given client IP is in the list of ban lines * * @param string $client IP of the client * @param string[] $banconf List of ban lines * @return false|array false or ban info [ip, date, reason] */ protected function isBanned($client, $banconf) { require_once(__DIR__ . '/ip-lib/ip-lib.php'); $ip = \IPLib\Factory::addressFromString($client); foreach ($banconf as $ban) { list($range, $dt, /*user*/, $reason) = explode("\t", trim($ban)); $ipRange = \IPLib\Factory::rangeFromString($range); if($ipRange === null) continue; if ($ip->matches($ipRange)) { return [ $ip->toString(), dformat($dt), $reason, ]; } } return false; } }