<?php
session_start();
/***************************************************************/
//Some vars you can adjust//
/***************************************************************/
$file = '.htaccess';
$login = 'Dark';
$pass = 'passwoord';
$page = $_SERVER['PHP_SELF'];
$login_code=md5($login.$pass);
/***************************************************************/
//Controll if your logged in//
/***************************************************************/
if(isset($_POST['login']) && isset($_POST['password'])){
$_SESSION['code']=md5($_POST['login'].$_POST['password']);
header('Location: '.$page);
}
if(isset($_POST['logout'])){
$_SESSION['code']='';
header('Location: '.$page);
}
if(isset($_SESSION['code'])){
$code=$_SESSION['code'];
}
else{
$code='';
}
if($code!=$login_code){
echo '<table width="500" cellspacing="0" cellpadding="2" border="1" align="center">';
echo '<form action="'.$page.'" method="POST">';
echo '<tr><td colspan="2"><h1>Login</h1></td></tr>';
if($code!=''){
echo '<tr><td colspan="2">Wrong login/password.</td></tr>';	
}
echo '<tr><td>Login:</td><td><input type="text" name="login"></td></tr>';
echo '<tr><td>Password:</td><td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Login"></td></tr>';
echo '</form>';
echo '</table>';
exit();
}
/***************************************************************/
//Function read opens a page and reads its source (in this case .htaccess)//
/***************************************************************/
function read($site){
$contents='';
$handle = @fopen ($site, "r");
if($handle){
while (!feof ($handle)) {
$buffer = fgets($handle, 4096);
$contents=$contents.$buffer;
}
fclose ($handle);
return $contents;
}
}
/***************************************************************/
//Check of a .htaccess excist, if not create one//
/***************************************************************/
if(!file_exists($file)){
$handle=fopen($file, "w");
fwrite($handle, 'allow from all');	
}
/***************************************************************/
//open .htaccess//
/***************************************************************/
$file_content=strtolower(read($file));
/***************************************************************/
//Update .htaccess//
/***************************************************************/
if(isset($_GET['d']) && isset($_GET['t'])){
	if(substr_count($file_content, $_GET['d'])==1){
		if($_GET['t']=='a'){
			$string='allow from '.$_GET['d'];
		}
		else{
			$string='deny from '.$_GET['d'];
		}
		$file_content=str_replace($string, '',$file_content);
		$handle=fopen($file, "w");
		fwrite($handle, $file_content);
	}
}
elseif(isset($_POST['submit'])){
$add='';
	if(isset($_POST['status'])){
		if(substr_count($file_content, 'deny from all')==1){
		$status='deny';	
		}
		elseif(substr_count($file_content, 'allow from all')==1){
		$status='allow';	
		}
		else{
		$status='empty';
		}
	 	if($_POST['status']=='allow' && $status!='allow'){
		 	$string='deny from all';
			$file_content=str_replace($string, '',$file_content);
			$file_content="allow from all\n".$file_content;
			$handle=fopen($file, "w");
			fwrite($handle, trim($file_content));
	 	}
	 	elseif($status=='empty'){
	 		$file_content="allow from all\n".$file_content;
			$handle=fopen($file, "w");
			fwrite($handle, trim($file_content));
		}
	 	else{
	 		$string='allow from all';
			$file_content=str_replace($string, '',$file_content);
			$file_content="deny from all\n".$file_content;
			$handle=fopen($file, "w");
			fwrite($handle, trim($file_content));
		}
	}
	if(isset($_POST['allow'])){
		if($_POST['allow']!=''){	
			if(substr_count($file_content, $_POST['allow'])==0){
				$add="\nallow from ".$_POST['allow'];
			}
		}
	}
	if(isset($_POST['deny'])){
		if($_POST['deny']!=''){
			if(substr_count($file_content, $_POST['deny'])==0){
				$add.="\ndeny from ".$_POST['deny'];
			}	
		}
	}
	if(isset($add)){
	$handle=fopen($file, "a");
	fwrite($handle, $add);
	}
}
/***************************************************************/
//open .htaccess//
/***************************************************************/
$file_content=strtolower(read($file));
$file_split=explode("\n", $file_content);
$count=count($file_split);
/***************************************************************/
$allowall = 0;
$denyall = 0;
$denies = array();
$allows = array();
for($i=0;$i<$count;$i++){
$this_var=$file_split[$i];
	if(substr_count($this_var, 'allow from all')>0){
		$allowall=1;
	}
	elseif(substr_count($this_var, 'deny from all')>0){
		$denyall=1;
	}
	elseif(substr_count($this_var, 'deny from')>0){
		$denies[$i]=trim(str_replace('deny from', '', $this_var));
	}
	elseif(substr_count($this_var, 'allow from')>0){
		$allows[$i]=trim(str_replace('allow from', '', $this_var));
	}
}
/***************************************************************/
echo '<table width="500" cellspacing="0" cellpadding="2" border="1" align="center">';
echo '<tr><td colspan="2"><h1>Htaccess Banner</h1></td></tr>';
echo '<tr><td colspan="2"><b>Status:</b> ';
if($denyall==0){
echo 'everybody is allowed.';
}
else{
echo 'everybody is banned.';
}
echo '</td></tr>';
$allows=implode("\n",$allows);
$allows=explode("\n",$allows);
$denies=implode("\n",$denies);
$denies=explode("\n",$denies);
if($denies[0]==""){
$denies=array();
}
if($allows[0]==""){
$allows=array();
}
if(count($denies)>0){
	echo '<tr><td colspan="2"><h3><b>» Banned</b></h3></td></tr>';
	for($i=0;$i<count($denies);$i++){
		echo '<tr><td>'.$denies[$i].' [<a href="'.$page.'?d='.$denies[$i].'&t=b">Del</a>]</td>';
		$i++;
		if(empty($denies[$i])){
			echo '<td>&nbsp;</td></tr>';
		}
		else{
			echo '<td>'.$denies[$i].' [<a href="'.$page.'?d='.$denies[$i].'&t=b">Del</a>]</td></tr>';
		}
	}
}
if(count($allows)>0){
	echo '<tr><td colspan="2"><h3><b>» Allowed</b></h3></td></tr>';
	for($i=0;$i<count($allows);$i++){
		echo '<tr><td>'.$allows[$i].' [<a href="'.$page.'?d='.$allows[$i].'&t=a">Del</a>]</td>';
		$i++;
		if(empty($allows[$i])){
			echo '<td>&nbsp;</td></tr>';
		}
		else{
			echo '<td>'.$allows[$i].' [<a href="'.$page.'?d='.$allows[$i].'&t=a">Del</a>]</td></tr>';
		}
	}
}
if(isset($_POST['allow'])){
	$allow=$_POST['allow'];
}
else{
	$allow='';
}
if(isset($_POST['deny'])){
	$deny=$_POST['deny'];
}
else{
	$deny='';
}
$one='';
$two='';
if($denyall==0){
$one=' checked';
}
else{
$tow=' checked';
}
echo '<tr><td colspan="2"><h3><b>» Edit</b></h3></td></tr>';
echo '<form action="'.$page.'" method="POST">';
echo '<tr><td width="50%"><b>Allow:</b></td><td width="50%"><input type="text" name="allow" value="'.$allow.'"></td></tr>';
echo '<tr><td><b>Ban:</b></td><td><input type="text" name="deny" value="'.$deny.'"></td></tr>';
echo '<tr><td><input type="radio" name="status" value="allow"'.$one.'> Allow from all</td>';
echo '<td><input type="radio" name="status" value="deny"'.$two.'> Deny from all</td></tr>';
echo '<tr><td align="center" colspan="2"><input type="submit" value="Update" name="submit">';
echo '<input type="submit" value="Logout" name="logout"></td></tr>';
echo '</form>';
echo '</table>';
?>