<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if(!ispageadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_POST['save']) {
	safe_query("INSERT INTO ".PREFIX."servers ( name, ip, game, info ) values( '".$_POST['name']."', '".$_POST['serverip']."', '".$_POST['game']."', '".$_POST['info']."' ) ");
}
elseif($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."servers SET name='".$_POST['name']."', ip='".$_POST['serverip']."', game='".$_POST['game']."', info='".$_POST['info']."' WHERE serverID='".$_POST['serverID']."'");
}
elseif($_POST['sort']) {
	if(is_array($_POST['sortlist'])) {
		foreach($_POST['sortlist'] as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."servers SET sort='$sorter[1]' WHERE serverID='$sorter[0]' ");
		}
	}
}
elseif($_GET['delete']) {
	safe_query("DELETE FROM ".PREFIX."servers WHERE serverID='".$_GET['serverID']."'");
}
	
echo'<h2>servers</h2>';

		$gamesa=safe_query("SELECT tag, name FROM ".PREFIX."games ORDER BY name");
 		while($dv=mysql_fetch_array($gamesa)) {
		 $games.='<option value="'.$dv[tag].'">'.$dv[name].'</option>';
		}

if($_GET['action']=="add") {
    echo'<form method="post" action="admincenter.php?site=servers">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Server-Name:</td>
		   <td><input type="text" name="name" size="30" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Game:</td>
		   <td><select name="game">'.$games.'</select></td>
		 </tr>
		 <tr>
		   <td>Server IP:</td>
		   <td><input type="text" name="serverip" size="30" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td valign="top">Info:</td>
		   <td><textarea name="info" cols="30" rows="5" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></textarea></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Server"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {
  $serverID = $_GET['serverID'];
  $ergebnis=safe_query("SELECT * FROM ".PREFIX."servers WHERE serverID='".$serverID."'");
	$ds=mysql_fetch_array($ergebnis);
		
	$games=str_replace(' selected', '', $games);
	$games=str_replace('value="'.$ds[game].'"', 'value="'.$ds[game].'" selected', $games);
	echo'<form method="post" action="admincenter.php?site=servers">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Server-Name:</td>
		   <td><input type="text" name="name" size="30" value="'.$ds[name].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Game:</td>
		   <td><select name="game">'.$games.'</select></td>
		 </tr>
		 <tr>
		   <td>Server IP:</td>
		   <td><input type="text" name="serverip" size="30" value="'.$ds[ip].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td valign="top">Info:</td>
		   <td><textarea name="info" cols="30" rows="5" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">'.$ds[info].'</textarea></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="serverID" value="'.$serverID.'"></td>
		   <td><input type="submit" name="saveedit" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=servers&action=add\');return document.MM_returnValue" value="new Server"><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."servers ORDER BY sort");
	$anz=mysql_num_rows($ergebnis);
	if($anz) {
   		echo'<form method="post" name="ws_servers" action="admincenter.php?site=servers"><table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
	       		<tr bgcolor="#CCCCCC">
		     		<td class="title" align="center">Server:</td>
			 		<td class="title" align="center" colspan="3">Actions:</td>
		   		</tr>
				<tr bgcolor="#ffffff"><td colspan="4"></td></tr>';
	
		while($ds=mysql_fetch_array($ergebnis)) {
    	$list = '<select name="sortlist[]">';
      for($i=1;$i<=mysql_num_rows($ergebnis);$i++) {
       $list.='<option value="'.$ds[serverID].'-'.$i.'">'.$i.'</option>';
      }
      $list .= '</select>';
      $list = str_replace('value="'.$ds[serverID].'-'.$ds[sort].'"','value="'.$ds[serverID].'-'.$ds[sort].'" selected',$list);

      	echo'<tr bgcolor="#FFFFFF">
		       		<td><img src="../images/games/'.$ds[game].'.gif" width="13" height="13" border="0"> - IP: <b>'.$ds[ip].'</b><br>'.$ds[name].'</td>
			   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=servers&action=edit&serverID='.$ds[serverID].'\');return document.MM_returnValue" value="edit"></td>
			   		<td align="center"><input type="button" class="button" onClick="MM_confirm(\'really delete this server?\', \'admincenter.php?site=servers&delete=true&serverID='.$ds[serverID].'\')" value="delete"></td>
  		   		<td align="center">'.$list.'</td>
			 	</tr>';
		}
	echo'<tr bgcolor="#CCCCCC"><td colspan="3"></td><td align="center"><input type="submit" name="sort" value="update"></td></table></form>';
	}
	else echo'no servers available';
}	
?>	
