<?php
//admin section redirections
if (!isset($afbAccount) || $afbAccount->account['account_admin'] !== 'yes'){
	//jump to home
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL);
	exit();
}



//------------------------------
$thispage['title'] = getLangTerm('blacklists',false);
//------------------------------




//create-type
//-----------------------------------------------------------------------------------------------------
if (isset($_GET['create-type'])){
	
	if (isset($_POST['submit'])){
		
		if (strlen(str_replace('.','',$_POST['type_ext'])) > 0) {
			//check if already exists.
			$sql = "SELECT COUNT(`blacklist_id`) AS `cnt` FROM `".TABLE_PREFIX."blacklist_ext` WHERE `blacklist_extension` LIKE '".str_replace('.','',$_POST['type_ext'])."'";
			$result = $db->Execute($sql);
			if ($result->fields['cnt'] == 0) {
				//insert
				$sql = "INSERT INTO `".TABLE_PREFIX."blacklist_ext` (`blacklist_extension`) VALUES ('" . addslashes(str_replace('.','',stripslashes($_POST['type_ext']))) . "')";
				if ($db->Execute($sql) === false) { 
					$errors[] = 'Error: '.$db->ErrorMsg();
				}
			}
		}
		
		//redirect
		header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
		exit();
		
	} // end submit check
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------

	?>
	
	<form action="index.php?admin&section=blacklists&create-type" name="create" method="post">
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td colspan="2">
					<?php echo getLangTerm('add-type',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td width="200">
					<?php echo getLangTerm('bl-type-name',false); ?>
				</td>
				<td>
					<input type="text" name="type_ext" value="<?php echo stripslashes($_POST['type_ext']); ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('type-reqd',false); ?>" />
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists" class="button"><?php echo getLangTerm('cancel',false); ?></a>
					<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
				</td>
			</tr>
		</tfoot>
	</table>
	</form>
	<?php
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------

//create-name
//-----------------------------------------------------------------------------------------------------
} elseif (isset($_GET['create-name'])){

	if (isset($_POST['submit'])){
		if (strlen($_POST['filename']) > 0) {
			//check if already exists.
			$sql = "SELECT COUNT(`blacklist_id`) AS `cnt` FROM `".TABLE_PREFIX."blacklist_files` WHERE `blacklist_file` LIKE '".addslashes(stripslashes($_POST['filename']))."'";
			$result = $db->Execute($sql);
			if ($result->fields['cnt'] == 0) {
				//insert
				$sql = "INSERT INTO `".TABLE_PREFIX."blacklist_files` (`blacklist_file`) VALUES ('".addslashes(stripslashes($_POST['filename']))."')";
				if ($db->Execute($sql) === false) { 
					$errors[] = 'Error: '.$db->ErrorMsg();
				}
			}
		}
		
		//redirect
		header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
		exit();
		
	} // end submit check
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------

	?>
	
	<form action="index.php?admin&section=blacklists&create-name" name="create" method="post">
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td colspan="2">
					<?php echo getLangTerm('add-name',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td width="200">
					<?php echo getLangTerm('bl-file-name',false); ?>
				</td>
				<td>
					<input type="text" name="filename" value="<?php echo stripslashes($_POST['filename']); ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('filename-reqd',false); ?>" />
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists" class="button"><?php echo getLangTerm('cancel',false); ?></a>
					<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
				</td>
			</tr>
		</tfoot>
	</table>
	</form>
	<?php
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------
	
//create-dir
//-----------------------------------------------------------------------------------------------------
} elseif (isset($_GET['create-dir'])){
	
	if (isset($_POST['submit'])){
		if (strlen($_POST['dir']) > 0) {
			//check if already exists.
			$sql = "SELECT COUNT(`blacklist_id`) AS `cnt` FROM `".TABLE_PREFIX."blacklist_dirs` WHERE `blacklist_dir` LIKE '".addslashes(stripslashes($_POST['dir']))."'";
			$result = $db->Execute($sql);
			if ($result->fields['cnt'] == 0) {
				//insert
				$sql = "INSERT INTO `".TABLE_PREFIX."blacklist_dirs` (`blacklist_dir`) VALUES ('".addslashes(stripslashes($_POST['dir']))."')";
				if ($db->Execute($sql) === false) { 
					$errors[] = 'Error: '.$db->ErrorMsg();
				}
			}
		}
		
		//redirect
		header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
		exit();
		
	} // end submit check
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------

	?>
	
	<form action="index.php?admin&section=blacklists&create-dir" name="create" method="post">
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td colspan="2">
					<?php echo getLangTerm('add-dir',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td width="200">
					<?php echo getLangTerm('bl-dir-name',false); ?>
				</td>
				<td>
					<input type="text" name="dir" value="<?php echo stripslashes($_POST['dir']); ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('dir-name-reqd',false); ?>" />
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists" class="button"><?php echo getLangTerm('cancel',false); ?></a>
					<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
				</td>
			</tr>
		</tfoot>
	</table>
	</form>
	<?php
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------

//remove-type
//-----------------------------------------------------------------------------------------------------
} elseif (isset($_GET['remove-type'])){
	$sql = "DELETE FROM `".TABLE_PREFIX."blacklist_ext` WHERE `blacklist_id`='".addslashes(stripslashes($_GET['remove-type']))."'";
	if ($db->Execute($sql) === false) { 
		//$negative = 'Error: '.$db->ErrorMsg();
	} else {
		if ($db->Affected_Rows()) {
			//$affirmative = 'Definition has been deleted';
		}
	}
	//redir
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
	exit();
	
//remove-name
//-----------------------------------------------------------------------------------------------------
} elseif (isset($_GET['remove-name'])){
	$sql = "DELETE FROM `".TABLE_PREFIX."blacklist_files` WHERE `blacklist_id`='".addslashes(stripslashes($_GET['remove-name']))."'";
	if ($db->Execute($sql) === false) { 
		//$negative = 'Error: '.$db->ErrorMsg();
	} else {
		if ($db->Affected_Rows()) {
			//$affirmative = 'Definition has been deleted';
		}
	}
	//redir
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
	exit();
	
//remove-dir
//-----------------------------------------------------------------------------------------------------
} elseif (isset($_GET['remove-dir'])){
	$sql = "DELETE FROM `".TABLE_PREFIX."blacklist_dirs` WHERE `blacklist_id`='".addslashes(stripslashes($_GET['remove-dir']))."'";
	if ($db->Execute($sql) === false) { 
		//$negative = 'Error: '.$db->ErrorMsg();
	} else {
		if ($db->Affected_Rows()) {
			//$affirmative = 'Definition has been deleted';
		}
	}
	//redir
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=blacklists');
	exit();
	
//default
//-----------------------------------------------------------------------------------------------------
} else {

	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------
	?>
	
	<table border="0" cellspacing="0">
		<caption><?php echo getLangTerm('bl-items',false); ?></caption>
		<thead>
			<tr>
				<td colspan="2">
					<?php echo getLangTerm('filetypes',false); ?>
				</td>
				<td colspan="2">
					<?php echo getLangTerm('filenames',false); ?>
				</td>
				<td colspan="2">
					<?php echo getLangTerm('dir-names',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<?php
			//get types
			$sql = "SELECT * FROM `".TABLE_PREFIX."blacklist_ext` ORDER BY `blacklist_extension`";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			$types = array();
			while (!$result->EOF){
				$types[] = array('id'=>$result->fields['blacklist_id'],'ext'=>stripslashes($result->fields['blacklist_extension']));
				$result->MoveNext();
			}
			
			//get names
			$sql = "SELECT * FROM `".TABLE_PREFIX."blacklist_files` ORDER BY `blacklist_file`";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			$names = array();
			while (!$result->EOF){
				$names[] = array('id'=>$result->fields['blacklist_id'],'file'=>stripslashes($result->fields['blacklist_file']));
				$result->MoveNext();
			}
			
			//get folders
			$sql = "SELECT * FROM `".TABLE_PREFIX."blacklist_dirs` ORDER BY `blacklist_dir`";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			$dirs = array();
			while (!$result->EOF){
				$dirs[] = array('id'=>$result->fields['blacklist_id'],'dir'=>stripslashes($result->fields['blacklist_dir']));
				$result->MoveNext();
			}
			
			
			for ($x=0;$x<max(count($types),count($names),count($dirs));$x++){
				?>
				<tr>
					<?php // Type 
					if (strlen($types[$x]['id']) > 0){
						?>
						<td>
							<?php echo $types[$x]['ext']; ?>
						</td>
						<td width="16">
							<a href="javascript:void(AskQuestion('<?php echo getLangTerm('remove-ques'); ?>','index.php?admin&section=blacklists&remove-type=<?php echo $types[$x]['id']; ?>'));" title="<?php echo getLangTerm('remove',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/remove.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('remove',false); ?>" /></a>
						</td>
						<?php
					} else {
						?>
						<td colspan="2">&nbsp;
							
						</td>
						<?php
					}
					
					// Name 
					if (strlen($names[$x]['id']) > 0){
						?>
						<td style="border-left:1px solid">
							<?php echo $names[$x]['file']; ?>
						</td>
						<td width="16">
							<a href="javascript:void(AskQuestion('<?php echo getLangTerm('remove-ques'); ?>','index.php?admin&section=blacklists&remove-name=<?php echo $names[$x]['id']; ?>'));" title="<?php echo getLangTerm('remove',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/remove.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('remove',false); ?>" /></a>
						</td>
						<?php
					} else {
						?>
						<td colspan="2" style="border-left:1px solid">&nbsp;
							
						</td>
						<?php
					}
					
					// Dir
					if (strlen($dirs[$x]['id']) > 0){
						?>
						<td style="border-left:1px solid">
							<?php echo $dirs[$x]['dir']; ?>
						</td>
						<td width="16">
							<a href="javascript:void(AskQuestion('<?php echo getLangTerm('remove-ques'); ?>','index.php?admin&section=blacklists&remove-dir=<?php echo $dirs[$x]['id']; ?>'));" title="<?php echo getLangTerm('remove',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/remove.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('remove',false); ?>" /></a>
						</td>
						<?php
					} else {
						?>
						<td colspan="2" style="border-left:1px solid">&nbsp;
							
						</td>
						<?php
					}
					?>
				</tr>
				<?php			
			}
			
			?>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists&create-type"><?php echo getLangTerm('add-type',false); ?></a>
				</td>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists&create-name"><?php echo getLangTerm('add-name',false); ?></a>
				</td>
				<td colspan="2">
					<a href="index.php?admin&section=blacklists&create-dir"><?php echo getLangTerm('add-dir',false); ?></a>
				</td>
			</tr>
		</tfoot>
	</table>
	
	<?php
	
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------
	
}
?>