#!/usr/bin/php5
<?php
/*
*********************************
*Project Freedom User-Voted Administrator Script by AfroTHUNDER
*********************************
*/
$currvote="";
$curradmin="";
$votes=0;
$rstme=0;
$timeres=0;
$num=array("", "", "", "", "", "");
$disab=array("access_level","add_help","admin","ai_character","rename","screenmode","vsync","windowsize","authority","login","backward_compatibility","ban","big_brother","bookmark","check_errors","client_port","file","colordepth","config_rotation","ladderlog","dedicated_idle","kick","enable_chat","gl","include","kill","map_rotation","message_of_day","password","local_user","op","deop","promote","demote","debug","help","rinclude","say","dns","server","sinclude","silence","spam","suspend","talk_to_master","URL","voting","exit","quit");
while (1)  {
    $line = rtrim(fgets(STDIN, 1024));
$ln2=strtolower($line);
$adm=strtolower($curradmin);
    $help = strpos($line,": -elect");
    $vte = strpos($line,": -vote");
    $normal= strpos($line,": -reset");
    $cmd= strpos($line,": -admin");
    $ske= strpos($line,": -snake");
    $tsumo= strpos($line,": -teamsumo");
    $sty= strpos($line,": -styball");
    $ctf= strpos($line,": -ctf");

if($normal!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$normal){
print("include settings_dedicated.cfg\n");
print("include settings.cfg\n");
print("include default.cfg\n");
print("include autoexec.cfg\n");
print("start_new_match\n");
print("console_message Server settings reset!\n");
}
}else{
print("console_message You must be the Admin-elect to reset the server settings.\n");
}
}

if($ske!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$ske){
print("include settings_dedicated.cfg\n");
print("include settings.cfg\n");
print("include default.cfg\n");
print("include autoexec.cfg\n");
print("include snake.cfg\n");
print("start_new_match\n");
print("console_message Server settings reset!\n");
}
}else{
print("console_message You must be the Admin-elect to change the server settings.\n");
}
}

if($tsumo!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$tsumo){
print("include settings_dedicated.cfg\n");
print("include settings.cfg\n");
print("include default.cfg\n");
print("include autoexec.cfg\n");
print("include teamsumo.cfg\n");
print("start_new_match\n");
print("console_message Server settings reset!\n");
}
}else{
print("console_message You must be the Admin-elect to change the server settings.\n");
}
}

if($sty!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$sty){
print("include settings_dedicated.cfg\n");
print("include settings.cfg\n");
print("include default.cfg\n");
print("include autoexec.cfg\n");
print("include styball.cfg\n");
print("start_new_match\n");
print("console_message Server settings reset!\n");
}
}else{
print("console_message You must be the Admin-elect to change the server settings.\n");
}
}

if($ctf!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$ctf){
print("include settings_dedicated.cfg\n");
print("include settings.cfg\n");
print("include default.cfg\n");
print("include autoexec.cfg\n");
print("include ctf.cfg\n");
print("start_new_match\n");
print("console_message Server settings reset!\n");
}
}else{
print("console_message You must be the Admin-elect to change the server settings.\n");
}
}


	if ($help !== false){
	    	$elect = substr($line,$help+9);

		if($currvote == ""){
			$rstme=time();
			print("console_message ".$elect." has been nominated for Admin-elect!\\nType in -vote to vote for his/her election.\n");
			$currvote=$elect;
			$i=0;
			while ($i<6){
				if ($num[$i]==""){
					$num[$i]=substr($line,0,3);
					$votes=$votes+1;
					$i=6;
				}
				$i=$i+1;
			}
		}else{
			print("console_message An election is already in progress!\n");
		}
	}

	if ($vte !== false){
if($currvote !== ""){
		$i=0;
		$dup=false;
		while ($i<6){
			if ($num[$i]==substr($line,0,3)){
				$dup=true;
				print("console_message Duplicate vote detected!\n");
				$i=6;
			}
			$i=$i+1;
		}
		if($dup==false){
			$i=0;
			while ($i<6){
				if ($num[$i]==""){
					$num[$i]=substr($line,0,3);
					$votes=$votes+1;
					print("console_message Vote accepted\n");
					$i=6;
				}
				$i=$i+1;
			}
		}
}else{
					print("console_message No election is taking place\n");}	
}

	
		$file = "/etc/arma/free/var/players.txt";
		$lines = count(file($file));
		$playned=$lines-1;
if($playned>0){	
		$playned= $playned/2;
		if ($votes>=$playned){
			$curradmin=$currvote;
			$currvote="";
			$votes=0;
			$num=array("", "", "", "", "", "");
			print("console_message ".$curradmin." is the new admin elect!\n");
		}
	}
	if ($currvote!==""){
		$timedif=time()-$rstme;
		if ($timedif>60){
			$currvote="";
			$votes=0;
			$num=array("", "", "", "", "", "");
			print("console_message Nomination vote expired\n");
		}
	}
if($cmd!==false){
    $admin= strpos($ln2,$adm);
if ($admin!==false){
if($admin<$cmd){
$command = substr($line,$cmd+9);
$vld=true;
$i=0;
while ($i<49){
$validate=strpos($command,$disab[$i]);
			if ($validate!==false){
				$vld=false;
				print("console_message Command not allowed!\n");
				$i=49;
			}
			$i=$i+1;
		}
$validate=strpos($command,"size_factor");
if($validate!==false){
if ($command=="size_factor -10" ||$command=="size_factor -9"||$command=="size_factor -8"||$command=="size_factor -7"||$command=="size_factor -6"||$command=="size_factor -5"||$command=="size_factor -4"||$command=="size_factor -3"||$command=="size_factor -2"||$command=="size_factor -1"||$command=="size_factor 0"||$command=="size_factor 10" ||$command=="size_factor 9"||$command=="size_factor 8"||$command=="size_factor 7"||$command=="size_factor 6"||$command=="size_factor 5"||$command=="size_factor 4"||$command=="size_factor 3"||$command=="size_factor 2"||$command=="size_factor 1"||$command=="sp_size_factor -10" ||$command=="sp_size_factor -9"||$command=="sp_size_factor -8"||$command=="sp_size_factor -7"||$command=="sp_size_factor -6"||$command=="sp_size_factor -5"||$command=="sp_size_factor -4"||$command=="sp_size_factor -3"||$command=="sp_size_factor -2"||$command=="sp_size_factor -1"||$command=="sp_size_factor 0"||$command=="sp_size_factor 10" ||$command=="sp_size_factor 9"||$command=="sp_size_factor 8"||$command=="sp_size_factor 7"||$command=="sp_size_factor 6"||$command=="sp_size_factor 5"||$command=="sp_size_factor 4"||$command=="sp_size_factor 3"||$command=="sp_size_factor 2"||$command=="sp_size_factor 1"){}else{$vld=false;print("console_message Size factor invalid value!\n");}
}
if($vld==true){
				print($command."\n");
				print("console_message Command executed\n");
}
}
}else{
print("console_message You must be the Admin-elect to do admin commands\n");
}
}

}
?>