<?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.');

if($_GET['delete']) {
	safe_query(" DELETE FROM ".PREFIX."forum_ranks WHERE rankID='".$_GET['rankID']."' ");
}
elseif($_POST['saveedit']) {
    $id = $_POST['id'];
    $rank = $_POST['rank'];
    $min = $_POST['min'];
    $max = $_POST['max'];
    
    $ergebnis = safe_query("SELECT * FROM ".PREFIX."forum_ranks ORDER BY rankID");
    $anz=mysql_num_rows($ergebnis);
    if($anz) {
        while($ds=mysql_fetch_array($ergebnis)) {
		    if($ds[rank] != "Administrator" && $ds[rank] != "Moderator") {
			    $id=$ds[rankID];
				if($max[$id]=="MAX") $maximum=2147483647;
				else $maximum=$max[$id];
	            safe_query("UPDATE ".PREFIX."forum_ranks SET rank='$rank[$id]' WHERE rankID='$id'");
		        safe_query("UPDATE ".PREFIX."forum_ranks SET postmin='$min[$id]' WHERE rankID='$id'");
		        safe_query("UPDATE ".PREFIX."forum_ranks SET postmax='$maximum' WHERE rankID='$id'");
			}	
	    }
    }
}
if($_POST['save']) {
  $name = $_POST['name'];
  $rank = $_FILES[rank];
  $max = $_POST['max'];
  
  if($max=="MAX") $maximum=2147483647;
	else $maximum=$max;
	
	safe_query("INSERT INTO ".PREFIX."forum_ranks ( rank, postmin, postmax ) values( '$name', '$min', '$maximum' )");
	$id=mysql_insert_id();
				 
	$filepath = "../images/icons/ranks/";
	if ($rank[name] != "") {
        move_uploaded_file($rank[tmp_name], $filepath.$rank[name]);
		@chmod($filepath.$rank[name], 0755);
		$file_ext=strtolower(substr($rank[name], strrpos($rank[name], ".")));
		$file=$id.$file_ext;
	    rename($filepath.$rank[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."forum_ranks SET pic='$file' WHERE rankID='$id' ");
	}
}
	
echo'<h2>RANKS</h2>';
// neuen Rank ertstellen
if($_GET['action']=="add") {
    echo'<form method="post" action="admincenter.php?site=ranks" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Rank-Image:</td>
		   <td><input name="rank" type="file"></td>
		 </tr>
		 <tr>
		   <td>Ranktitle:</td>
		   <td><input type="text" name="name" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>min Posts:</td>
		   <td><input type="text" name="min" size="4" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			 </tr>
		 <tr>
		   <td>max Posts:</td>
		   <td><input type="text" name="max" size="4" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add rank"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=ranks&action=add\');return document.MM_returnValue" value="new Rank">';
	echo'<form method="post" action="admincenter.php?site=ranks">
	     <table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
		 <tr bgcolor="#CCCCCC">
		   <td class="title">Rank-Picture:</td>
		   <td class="title">Ranktitle:</td>
		   <td class="title">Posts:</td>
		   <td class="title">delete:</td>
		 </tr>';
	
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_ranks ORDER BY postmax");	 
	while($ds=mysql_fetch_array($ergebnis)) {
		if($ds[rank]=="Administrator" || $ds[rank]=="Moderator") {
		    echo'<tr>
		           <td bgcolor="#FFFFFF" align="center"><img src="../images/icons/ranks/'.$ds[pic].'"></td>
			       <td bgcolor="#FFFFFF">'.$ds[rank].'</td>
			       <td bgcolor="#FFFFFF">&nbsp;</td>
				   <td bgcolor="#FFFFFF">&nbsp;</td>
			    </tr>';
		}
		else {
		    if(strlen(trim($ds[postmax]))>8) $max="MAX";
			else $max=$ds[postmax];
			echo'<tr>
		           <td bgcolor="#FFFFFF" align="center"><img src="../images/icons/ranks/'.$ds[pic].'"></td>
			       <td bgcolor="#FFFFFF"><input type="text" name="rank['.$ds[rankID].']" value="'.$ds[rank].'" size="40" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			       <td bgcolor="#FFFFFF">
				     <input type="text" name="min['.$ds[rankID].']" value="'.$ds[postmin].'" size="6" dir="rtl" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"> - 
			         <input type="text" name="max['.$ds[rankID].']" value="'.$max.'" size="6" dir="rtl" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">
				  </td>
				  <td bgcolor="#FFFFFF"><input type="button" class="button" onClick="MM_confirm(\'really delete this rank?\', \'admincenter.php?site=ranks&delete=true&rankID='.$ds[rankID].'\')" value="delete"></td>
			    </tr>';
		}
		
		$n++;
	}	 
	echo'<tr bgcolor="#CCCCCC">
	       <td colspan="4" align="right"><input type="submit" name="saveedit" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
?>
