<?php
if (!defined('NTLCMS')) die('<center><b>Restricted access</b></center>');			
$doslimit = 150;
$sysip = preg_replace('|[^0-9\.]|', '', $_SERVER['REMOTE_ADDR']);
$sql = DB :: run() -> query("select * from `ddos`");  
while ($row = $sql -> fetch(PDO::FETCH_OBJ)){ 
if ($row -> time <= time()){
DB :: run() -> query("delete from `ddos` where `id` = ?", array($row -> id));
}
}
$sql = DB :: run() -> query("select * from `ddos` where `ip` = ?", array($sysip));  
while ($row = $sql -> fetch(PDO::FETCH_OBJ)){ 
if ($row -> time <= time()){
DB :: run() -> query("delete from `ddos` where `id` = ?", array($row -> id));
}
}
$sql = DB :: run() -> query("select * from `autobanip` where ip = ?", array($sysip));  
while ($row = $sql -> fetch(PDO::FETCH_OBJ)){ 
if ($row -> time <= time()){
DB :: run() -> query("delete from `autobanip` where `id` = ?", array($row -> id));
}
}
$count_ddos = DB :: run() -> querySingle("select count(`id`) from `ddos` where `ip` = ?", array($sysip));
$times = time() + 60; 
if ($count_ddos > $doslimit){
$ban = DB :: run() -> query("select * from `autobanip` where `ip` = ?", array($sysip)) -> fetch(PDO::FETCH_OBJ);
if (empty($ban -> ip)){
DB :: run() -> query("insert into `autobanip` (`ip`, `time`) values (?, ?)", array($sysip, $times));
DB :: run() -> query("delete from `ddos` where `ip` = ?", array($sysip));
}
}
$time = time() + 60;
$request_uri = isset($_SERVER['REQUEST_URI']) ? filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_STRING) : '-----';
$http_referer = isset($_SERVER['HTTP_REFERER']) ? filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_STRING) : '-----';
$arbuz = isset($_SERVER['HTTP_USER_AGENT']) ? filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_STRING) : '-----';
DB :: run() -> query("insert into `ddos`(`time`, `ip`, `url`, `refer`, `brow`) values (?, ?, ?, ?, ?)", array($time, $sysip, $request_uri, $http_referer, $arbuz));
if (DB :: run() -> querySingle("select count(`ip`) from `autobanip` where `ip` = ?", array($sysip)) != 0){
die('<div style="text-align: center; font-size: xx-large"><strong>Превышен допустимый лимит запросов!</strong></div>'); exit;
}
?>