<?php

$verbindungsinfo = array("Database"=>"DDOS", "UID"=>"sa", "PWD"=>"Test12345");
$verbindung = sqlsrv_connect("CYBER951-PC\SQLEXPRESS", $verbindungsinfo);
if(!$verbindung)
{
  die("Connection to MS SQL could not be established.\n");
}

$MaxDiffrence = 1; 
$MaxFactor = 40; 
  
$UserIP = $_SERVER['REMOTE_ADDR']; 
$PhpTime = time(); 
  
$blRequest = sqlsrv_query($verbindung, "SELECT [IP] FROM [DDOS].[dbo].[IP_BlackList] WHERE [IP]='".$_SERVER['REMOTE_ADDR']."';");
if($blRequest == false) {
     die( print_r( sqlsrv_errors(), true));
}
$blResult = sqlsrv_fetch_array($blRequest);
$blacklisted = $blResult[0];
  
if ($blacklisted != 0) {
    header('Location: http://google.com/');
    exit;
}
  
$tdbRequest = "SELECT [IP] FROM [DDOS].[dbo].[IP_TempList] WHERE [IP]='".$_SERVER['REMOTE_ADDR']."';";
$tdbRequestResult = sqlsrv_query($verbindung, $tdbRequest);
if($tdbRequestResult === false) {
     die( print_r("1". sqlsrv_errors(), true));
}
$tdbResult = sqlsrv_fetch_array($tdbRequestResult);
$templisted = $tdbResult[0];
  
if ($templisted == 0) {
    $Result1 = sqlsrv_query($verbindung, "INSERT INTO [DDOS].[dbo].[IP_TempList] ([IP], [Time], [Factor]) VALUES ('" . $UserIP . "', '" . $PhpTime . "', 1)"); 
	if($Result1 === false) {
		die( print_r("2". sqlsrv_errors(), true));
	}
}
else {
	$dbTimeRequest = "SELECT [Time] FROM [DDOS].[dbo].[IP_TempList] WHERE [IP] = '" . $UserIP . "'";
    $dbTimeRequestResult = sqlsrv_query($verbindung, $dbTimeRequest);
    $dbTimeResult = sqlsrv_fetch_array($dbTimeRequestResult);
    $DBTime = $dbTimeResult[0];
  
    $PhpTime = time(); 
  
    $Differenz = $PhpTime-$DBTime; 
	
	$NewFactor = 1;
    if($Differenz < $MaxDiffrence) {
        $dbFaktorRequest = "SELECT [Factor] FROM [DDOS].[dbo].[IP_TempList] WHERE [IP] = '" . $UserIP . "'";
        $dbFaktorRequestResult = sqlsrv_query($verbindung, $dbFaktorRequest);
		if($dbFaktorRequestResult === false) {
		die( print_r("3". sqlsrv_errors(), true));
		}
        $dbFaktorResult = sqlsrv_fetch_array($dbFaktorRequestResult);
        $dbFaktor = $dbFaktorResult[0];
        $NewFactor = $dbFaktor + 1;
        $Result2 = sqlsrv_query($verbindung, "UPDATE [DDOS].[dbo].[IP_TempList] SET [Factor] = '" . $NewFactor . "' WHERE [IP] = '" . $UserIP . "'");
		if($Result2 === false) {
		die( print_r("4". sqlsrv_errors(), true));
		}
	}
	elseif($Differenz >= 5) {
		$Result2 = sqlsrv_query($verbindung, "UPDATE [DDOS].[dbo].[IP_TempList] SET [Factor] = '1' WHERE [IP] = '" . $UserIP . "'");
		if($Result2 === false) {
		die( print_r("4-1". sqlsrv_errors(), true));
		}
	}
  
    $PhpTime = time(); 
    $Result3 = sqlsrv_query($verbindung, "UPDATE [DDOS].[dbo].[IP_TempList] SET [Time] = '" . $PhpTime . "' WHERE [IP] = '" . $UserIP . "'"); 
	if($Result3 === false) {
		die( print_r("5". sqlsrv_errors(), true));
	}
	
    if ($NewFactor == $MaxFactor) {
        $Result4 = sqlsrv_query($verbindung, "INSERT INTO [DDOS].[dbo].[IP_BlackList] ([IP]) VALUES ('" . $UserIP . "')"); 
		if($Result4 === false) {
		die( print_r("6".sqlsrv_errors(), true));
		}
        $Result5 = sqlsrv_query($verbindung, "DELETE FROM [DDOS].[dbo].[IP_TempList] WHERE [IP] = '" . $UserIP . "'"); 
		if($Result5 === false) {
		die( print_r("7". sqlsrv_errors(), true));
		}
    }
}  
?>