<?php

/* 	

	AMXBans v6.0
	
	Copyright 2009, 2010 by SeToY & |PJ|ShOrTy

	This file is part of AMXBans.

    AMXBans is free software, but it's licensed under the
	Creative Commons - Attribution-NonCommercial-ShareAlike 2.0

    AMXBans is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    You should have received a copy of the cc-nC-SA along with AMXBans.  
	If not, see <http://creativecommons.org/licenses/by-nc-sa/2.0/>.

*/

	session_start();
	
	if(!$_SESSION["loggedin"]) {
		header("Location:index.php");
		#exit;
	}

    $admin_site="av";
	$title2 ="_TITLEAMXADMINS";
	
	$aid=(int)$_POST["aid"];
	
	//amxadmin delete
	if(isset($_POST["del"])) {
		$query = mysql_query("DELETE FROM `".$config->db_prefix."_amxadmins` WHERE `id`=".$aid." LIMIT 1") or die (mysql_error());
		$query = mysql_query("DELETE FROM `".$config->db_prefix."_admins_servers` WHERE `admin_id`=".$aid) or die (mysql_error());
		$user_msg[]='_AMXADMINDELETED';
		log_to_db("AMXXAdmin config","Deleted admin: ".mysql_real_escape_string($_POST["username"]));
	}
	//validate input values
	if(isset($_POST["save"]) || isset($_POST["new"])) {
		$username=sql_safe($_POST["username"]);
		$password=sql_safe($_POST["password"]);
		$access=sql_safe($_POST["access"]);
		$flags=sql_safe($_POST["flags"]);
		$steamid=sql_safe($_POST["steamid"]);
		$nickname=sql_safe($_POST["nickname"]);
		
		if(!validate_value($username,"name",$error,4,31,"USERNAME")) $user_msg[]=$error;
		if(strrpos($flags,"a")===true || strrpos($flags,"e")===false) if(!validate_value($password,"name",$error,4,31,"PASSWORD")) $user_msg[]=$error;
		if(!validate_value($access,"amxxaccess",$error)) $user_msg[]=$error;
		if(!validate_value($flags,"amxxflags",$error)) $user_msg[]=$error;
		//validate steamid depending on flags (steamid, name, ip)
		if(strrpos($flags,"b")!==false) { //clantag
			if(!validate_value($steamid,"name",$error,1,30,"TAG")) $user_msg[]=$error;
		} else if(strrpos($flags,"c")!==false) { //steamid
			if(!validate_value($steamid,"steamid",$error)) $user_msg[]=$error;
		} else if(strrpos($flags,"d")!==false) { //ip
			if(!validate_value($steamid,"ip",$error)) $user_msg[]=$error;
		}
		if(!validate_value($nickname,"name",$error,4,31,"NICKNAME")) $user_msg[]=$error;
	}
	//amxadmin edit
	if(isset($_POST["save"])) {
		if(isset($_POST["noend"])) {
			$days=0;
			$exp="0";
		} elseif (isset($_POST["moredays"]) && (int)$_POST["moredays"]<>"") {
			$days=(int)$_POST["days"] + (int)$_POST["moredays"];
			$exp="(`created`+(".($days * 86400)."))";
		} else {
			$days=(int)$_POST["days"];
			$exp=($days<=0)?"0":"(`created`+(".($days * 86400)."))";
		}
		if(!$user_msg) {
			$query = mysql_query("UPDATE `".$config->db_prefix."_amxadmins` SET 
						`username`='".$username."',
						`password`='".$password."',
						`access`='".$access."',
						`flags`='".$flags."',
						`steamid`='".$steamid."',
						`nickname`='".$nickname."',
						`ashow`='".(int)$_POST["ashow"]."',
						`expired`=".$exp.",
						`days`=".$days."
						WHERE `id`=".$aid." LIMIT 1") or die (mysql_error());
			$user_msg[]='_AMXADMINSAVESUCCESS';
			log_to_db("AMXXAdmin config","Edited admin: ".mysql_real_escape_string($_POST["username"])." (nick: ".mysql_real_escape_string($_POST["nickname"]).")");
		}
	}
	//amxadmin add
	if(isset($_POST["new"])) {
		$exp="0,";
		if((int)$_POST["days"]=="" && (!isset($_POST["noend"]))) {
			$user_msg[]='_NOVALIDTIME';
		} elseif (isset($_POST["noend"])) {
			$days=0;
		} else { 
			$days=(int)$_POST["days"];
			$exp="(UNIX_TIMESTAMP()+(".($days * 86400).")),";
		}
		if(!$user_msg) {
			$name=mysql_real_escape_string($_POST["username"]);
			//add new amxxadmin to db
			$query = mysql_query("INSERT INTO `".$config->db_prefix."_amxadmins` 
							(`username`,`password`,`access`,`flags`,`steamid`,`nickname`,`ashow`,`created`,`expired`,`days`) 
							VALUES (
							'".$username."',
							'".$password."',
							'".$access."',
							'".$flags."',
							'".$steamid."',
							'".$nickname."',
							".(int)$_POST["ashow"].",
							UNIX_TIMESTAMP(),
							".$exp."
							".$days."
							)") or die (mysql_error());
			//add as admin to selected servers
			$adminid=mysql_insert_id();
			$addtoserver=$_POST["addtoserver"];
			$sban=mysql_real_escape_string($_POST["staticbantime"]);
			if(is_array($addtoserver)) {
				foreach($addtoserver as $k => $v) {
					$query = mysql_query("INSERT INTO `".$config->db_prefix."_admins_servers` 
							(`admin_id`,`server_id`,`custom_flags`,`use_static_bantime`) 
							VALUES 
							('".$adminid."','".$v."','','".$sban."')
							") or die (mysql_error());
					}
			}
			$user_msg[]='_AMXADMINADDED';
			log_to_db("AMXXAdmin config","Added admin: ".$name);
		} else {
			$input=array(
				"username"=>html_safe($username),
				"password"=>$password,
				"access"=>$access,
				"flags"=>$flags,
				"steamid"=>$steamid,
				"nickname"=>html_safe($nickname),
				"ashow"=>(int)$_POST["ashow"],
				"days"=>$_POST["days"],
				"moredays"=>(int)$_POST["moredays"],
				"noend"=>(isset($_POST["noend"])?1:0)
				);
			$smarty->assign("input",$input);
		}
	}
	//amxadmins holen
	$admins=sql_get_amxadmins();
	
	//server holen
	$servers=sql_get_server();
	if(is_array($servers)) {
		foreach($servers as $k => $v) {
			$svalues[]=$v["sid"];
			$soutput[]=$v["hostname"];
		}
	}
	
	$smarty->assign("yesno_choose",array("yes","no"));
	$smarty->assign("yesno_output",array("_YES","_NO"));
	$smarty->assign("ashow_output",array("_NO","_YES"));
	$smarty->assign("ashow",array(0,1));
	$smarty->assign("admins",$admins);
	$smarty->assign("svalues",$svalues);
	$smarty->assign("soutput",$soutput);
	
?>