<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
//Spam?
if(isset($_GET['spam'])) {
	$spamip=$_GET['spam'];
	$subnet=$spamip;
	//Isolate subnet
	while(strlen($subnet)>0&&substr($subnet,-1)!='.')
		$subnet=substr($subnet,0,strlen($subnet)-1);
	
	//Add to blacklist
	if(strlen($subnet)>0) {
		$ipblacklist=unserialize(substr(file_get_contents('../ipblacklist.php'),strlen(DIE_MSG)));
		$ipblacklist[]=$subnet;
		$_POST['settings']=implode("\n",$ipblacklist);
	}
	
	//Delete existing spam
	$existingSpam=$_p->tb_comments->select('strpos(%ip%,\''.fetch::qEscape($subnet).'\')===0');
	if(count($existingSpam)>1) {
		$spamText='<h2>Mass Delete</h2>
		<a href="managecomments.php?show_unapproved&amp;massDel='.$subnet.'">Delete these comments from this spam network</a>';
		$spamText.='<ul>';
		foreach($existingSpam as $data) {
			$spamText.='<li class="comment_list_item"><div class="comment_holder">'.htmlentities($data['content'],ENT_QUOTES,'UTF-8').'</div></li>';
		}
		$spamText.='</ul>';
	}
	else {
		$spamText='<h2>Marked As Spam</h2>
		<a href="managecomments.php?show_unapproved&amp;massDel='.$subnet.'">Delete and return to unapproved comments</a> | 
		<a href="editipblacklist.php?remove_last">Unmark as spam</a>';
	}
}
//Undo spam mark?
if(isset($_GET['remove_last'])) {
	//Remove from blacklist
	$ipblacklist=unserialize(substr(file_get_contents('../ipblacklist.php'),strlen(DIE_MSG)));
	array_pop($ipblacklist);
	$_POST['settings']=implode("\n",$ipblacklist);
	$spamText='<h2>Unmarked As Spam</h2>
	<a href="managecomments.php?show_unapproved">Return to unapproved comments</a>';
}
//Save data if given
$saved=false;
if(isset($_POST['settings'])) {
	$tmpFile=@fopen('../ipblacklist.php','w+');
	if($tmpFile===false)
		die('Could not open "../ipblacklist.php" for writing, please check the file permissions.');
	$presettingsdata=explode("\n",fetch::unescapePostVar($_POST['settings']));
	$settingsdata=array_unique(str_replace(array("\n","\r",'*'),array('','',''),$presettingsdata));
	foreach($settingsdata as $index=>$aword)
		if($aword=='')
			unset($settingsdata[$index]);
	fwrite($tmpFile,DIE_MSG.serialize($settingsdata));
	fclose($tmpFile);
	$saved=true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit IP Blacklist</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="editipblacklist.php" class="breadcrumb">Edit IP Blacklist</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
        <?php
			if($spamText!==false)
				echo($spamText);
		?>
		<h2>Edit IP Blacklist</h2>
		<form action="editipblacklist.php" method="post">
			One partial or full IP per line, empty lines and duplicate entries are ignored.
			<textarea name="settings" cols="60" rows="30"><?php
				$list=substr(file_get_contents('../ipblacklist.php'),strlen(DIE_MSG));
				$listentries=unserialize($list);
				if(count($listentries)>0) {
					foreach($listentries as $word)
						echo($word."\n");
				}
			?></textarea>
			<br />
			<input type="submit" value="Save"/>
		</form>
		<?php
		if($saved)
			echo(count($settingsdata).' IP addresses saved.');
		?>
        <br />
        <a href="managecomments.php?nuke">Nuke existing spam using this list?</a>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>