<?php
class flooddam
{
/*
FloodDam 1.0.4 Copyright (C) 2005 Murat ATAY
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not,write to the
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// [TR] IP adresi bulunamayan kullanıcılara izin ver.
// [EN] Allow users when IP not found.
var $allow_noip = FALSE; // (TRUE / FALSE)
// [TR] Okuma veya yazma hatası durumunda izin ver.
// [EN] Allow when read or write error.
var $allow_error = FALSE; // (TRUE / FALSE)
// [TR] IP listesinin tutulacağı dizin adı. Yazılabilir olmalıdır.
// [EN] IP list log directory. Must be writeable.
var $directory = 'ip';
// [TR] Maksimum istek sayısı.
// [EN] Maximum request number.
var $hits = 10;
// [TR] Maksimum istek sayısı için minimum süre.
// [EN] Minimum time for the maximum request number.
var $seconds = 5;
// [TR] $ban_time süre sonra blokeleri kaldır.
// [EN] Remove bans after $ban_time later.
var $clear_bans = TRUE; // (TRUE / FALSE)
// [TR] IP nin ne kadar süre bloke edileceğini belirten süre.
// [EN] Time for how much time the IP will be banned.
var $ban_time = "+1 day";
// [TR] ÖRNEK: $seconds saniye içerisinde $hits adet istek yapılırsa IP bloke edilir.
// [EN] EXAMPLE: If requests are more than $hits within $seconds seconds than IP will be blocked.
// [TR] Ne yaptığınızı gerçekten bilmiyorsanız aşağıdaki satırları değiştirmeyin.
// [EN] Don't change lines below unless you really know what you are doing.
var $blocked = FALSE;
var $client_ip = NULL;
var $client_seconds = 0;
var $client_average = 0;
function flooddam()
{
if (!$this->client_ip = $this->find_client_ip())
{
if (!$this->$allow_noip)
{
exit();
}
}
else
{
if ($this->file_ip_banned($this->client_ip))
{
exit();
}
if ($this->file_ip_check($this->client_ip))
{
$time_array = explode(';', $this->file_ip_read($this->client_ip));
}
$time_array[] = time();
if (count($time_array) >= $this->hits)
{
if (($this->client_seconds = $time_array[count($time_array) - 1] - $time_array[count($time_array) - $this->hits]) < $this->seconds)
{
$this->blocked = TRUE;
}
$time_array = array_slice($time_array, -$this->hits);
}
$this->client_average = (int) ($time_array[count($time_array) - 1] - (array_sum($time_array) / count($time_array)));
$this->times = implode(';', $time_array);
if (!$this->file_ip_write($this->client_ip, $this->times))
{
if ($this->allow_error) return;
exit();
}
if ($this->blocked)
{
if ($this->clear_bans)
{
$this->file_clean_ip_ban();
}
$this->file_ip_ban($this->client_ip, time());
exit;
}
}
}
function file_ip_ban($ip, $text)
{
$fp = fopen($this->directory . '/' . $ip . '.banned', 'wt');
fwrite($fp, $text);
fclose($fp);
}
function file_clean_ip_ban()
{
if ($dir = opendir($this->directory))
{
while (($file = readdir($dir)) !== FALSE)
{
$fileinfo = pathinfo($file);
if ($fileinfo['extension'] == 'banned')
{
if (time() > strtotime($this->ban_time, $this->file_read($fileinfo['basename'])))
{
unlink($this->directory . '/' . $fileinfo['basename']);
}
}
}
}
closedir($dir);
}
function file_ip_read($ip)
{
$return = file_get_contents($this->directory . '/' . $ip . '.txt');
return $return;
}
function file_read($file)
{
$return = file_get_contents($this->directory . '/' . $file);
return $return;
}
function file_ip_write($ip, $text)
{
if ($fp = @fopen($this->directory . '/' . $ip . '.txt', 'wt'))
{
fwrite($fp, $text);
fclose($fp);
return TRUE;
}
else {return FALSE;}
}
function file_ip_check($ip)
{
return is_file($this->directory . '/' . $ip . '.txt');
}
function file_ip_banned($ip)
{
return is_file($this->directory . '/' . $ip . '.banned');
}
function find_client_ip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {return $_SERVER['HTTP_CLIENT_IP'];}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {return $_SERVER['HTTP_X_FORWARDED_FOR'];}
elseif (!empty($_SERVER['REMOTE_ADDR'])) {return $_SERVER['REMOTE_ADDR'];}
else {return NULL;}
}
}
?>