<?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(!isanyadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');
$filepath = "../images/games/";
if($_POST['save']) {
  $tag = strtolower($_POST['tag']);
  $name = $_POST['name'];
  $icon = $_FILES[icon];

	if($icon[name] != "") {
		$icon_name = $tag;

    move_uploaded_file($icon[tmp_name], $filepath.$icon_name);
    @chmod($filepath.$icon_name, 0755);
    $info = getimagesize($filepath.$icon_name);
		if($info[0] < 300 && $info[1] < 300) {
	        if($info[2] == 2) {
			    $pic=$tag.'.jpg';
       		if(file_exists($filepath.$pic)) @unlink($filepath.$pic);
          rename($filepath.$icon_name, $filepath.$pic);
			}
			elseif($info[2] == 1) {
			    $pic=$tag.'.gif';
       		if(file_exists($filepath.$pic)) @unlink($filepath.$pic);
			    rename($filepath.$icon_name, $filepath.$pic);
			}
			else {
			    @unlink($filepath.$icon_name);
				$error = "invalid picture-format (allowed: gif or jpg)";
			    die('ERROR: '.$error.'<br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
		    }
	    }
		else {
	        @unlink($filepath.$icon_name);
			$error = "picture is to big max 300/300px";
		    die('ERROR: '.$error.'<br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	    }
	safe_query("INSERT INTO ".PREFIX."games ( name, tag ) values( '$name', '$tag' ) ");
  } else {
  die('ERROR: Please fill all fields.<br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
  }
}
elseif($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."games SET name='".$_POST['name']."', tag='".$_POST['tag']."' WHERE gameID='".$_POST['gameID']."'");
}
elseif($_GET['delete']) {
	safe_query("DELETE FROM ".PREFIX."games WHERE gameID='".$_GET['gameID']."'");
}

echo'<h2>games</h2>';
if($_GET['action']=="add") {
    echo'<form method="post" action="admincenter.php?site=games" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>game-name:</td>
		   <td><input type="text" name="name" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		 <tr>
		   <td>game-tag:</td>
		   <td><input type="text" name="tag" size="3" maxlength="3" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>

		 <tr>
		   <td>game-picture:</td>
 		   <td><input name="icon" type="file" class="form_off"></td>
		 </tr>
		 <tr>


		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Game"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {

  $gameID = $_GET['gameID'];
  $ergebnis=safe_query("SELECT * FROM ".PREFIX."games WHERE gameID='$gameID'");
	$ds=mysql_fetch_array($ergebnis);

	echo'<form method="post" action="admincenter.php?site=games">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>games-name:</td>
		   <td><input type="text" name="name" value="'.$ds[name].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>games-tag:</td>
		   <td><input type="text" size="3" maxlength="3" name="tag" value="'.$ds[tag].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="gameID" value="'.$ds[gameID].'"></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=games&action=add\');return document.MM_returnValue" value="new Game"><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."games ORDER BY name");
	echo'<table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
   		<tr bgcolor="#CCCCCC">
   		<td class="title" align="center">game-name:</td>
 		<td class="title" align="center" colspan="2">Actions:</td>
   		</tr>
		<tr bgcolor="#FFFFFF"><td colspan="3"></td></tr>';

	while($ds=mysql_fetch_array($ergebnis)) {
    	echo'<tr bgcolor="#FFFFFF">
	       		<td align="center"><b>'.$ds[name].'</b></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=games&action=edit&gameID='.$ds[gameID].'\');return document.MM_returnValue" value="edit"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_confirm(\'really delete this category?\', \'admincenter.php?site=games&delete=true&gameID='.$ds[gameID].'\')" value="delete"></td>
		 	</tr>';
	}
	echo'</table>';
}
?>
