public static function ban($ban_ip = null, $ban_time = 86400) { if (!is_int($ban_time)) { return false; } if (empty($ban_ip)) { $ban_ip = $_SERVER['REMOTE_ADDR']; } // Process whitelist foreach (parent::$ignoreip as $range) { if (parent::ip_in_range($ban_ip, $range)) { return false; } } $now = time(); if (false === static::may_load_wordpress()) { return false; } $lockouts = get_option(static::$option); if (false === $lockouts) { $lockouts = array(); add_option(static::$option, $lockouts, 'yes'); } $expires = $now + $ban_time; $lockouts[$ban_ip] = $expires; update_option(static::$option, $lockouts); return true; }
/** * Initialize miniban * * Set path of configuration file, ignored IP addresses * and extra configuration data. * * @param string $config Full path of configuration file. * @param array $ignoreip Ignored IP addresses. * @param array $extra_config Additional configuration data. * @return boolean Success. */ public static final function init($config, $ignoreip = array(), $extra_config = array()) { self::$ignoreip = $ignoreip; self::$extra_config = $extra_config; if (!empty($config)) { self::$config = $config; return self::check_config(); } return true; }
public static function ban($ban_ip = null, $tarpit_time = 600) { if (!is_int($tarpit_time) || $tarpit_time < 1) { return false; } $max_execution_time = ini_get('max_execution_time'); $now = time(); if ($max_execution_time) { // Substract REQUEST_TIME if (!empty($_SERVER['REQUEST_TIME'])) { $max_execution_time -= $now - $_SERVER['REQUEST_TIME']; } else { // Approximate application execution time $max_execution_time -= 3; } } else { // Sensible default $max_execution_time = 30; } if ($tarpit_time > $max_execution_time) { $tarpit_time = $max_execution_time; } if (empty($ban_ip)) { $ban_ip = $_SERVER['REMOTE_ADDR']; } $content = sprintf("%s #%s\n", $ban_ip, $now); parent::alter_config('static::write_callback', array('content' => $content), 'a'); if (!headers_sent()) { // Prevent gzip encoding thus buffering output header('Content-Encoding: none'); header('Content-Length: 2457600'); for ($i = 0; $i < $tarpit_time; $i++) { sleep(1); // Send random bytes echo str_pad(chr(rand(0, 255)), 4096, chr(0)); flush(); ob_flush(); } } else { sleep($tarpit_time); } return true; }
public static function unban($unban_ip = null) { if (empty(parent::$config)) { return false; } if (empty(parent::$extra_config['header'])) { parent::$extra_config['header'] = 'Remote_Addr'; } $parameters = array('operation' => 'del'); if ($unban_ip) { // One IP $ban_line = sprintf('SetEnvIf %s "^%s$" mini_ban', parent::$extra_config['header'], preg_quote($unban_ip)); } else { // Unban all expired $parameters['now'] = time(); // Matches all ban lines in .htaccess $ban_line = sprintf('SetEnvIf %s "^', parent::$extra_config['header']); } $parameters['contents'] = $ban_line; return parent::alter_config('static::insert_with_markers', $parameters, 'r+'); }
public static function unban($unban_ip = null) { return parent::alter_config('static::unban_callback', array('ip' => $unban_ip)); }