<?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($_POST['savemods']) {
  $boardID = $_POST['boardID'];
  $mods = $_POST['mods'];
	
    safe_query("DELETE FROM ".PREFIX."forum_moderators WHERE boardID='$boardID'");
	if(is_array($mods)) {
	    foreach($mods as $id) {
	        safe_query("INSERT INTO ".PREFIX."forum_moderators (boardID, userID) values ('$boardID', '$id') ");
	    }
	}
}
elseif($_GET['delete']) {
  $boardID = $_GET['boardID'];
	
  safe_query(" DELETE FROM ".PREFIX."forum_posts WHERE boardID='$boardID' ");
	safe_query(" DELETE FROM ".PREFIX."forum_topics WHERE boardID='$boardID' ");
	safe_query(" DELETE FROM ".PREFIX."forum_boards WHERE boardID='$boardID' ");
	safe_query(" DELETE FROM ".PREFIX."forum_moderators WHERE boardID='$boardID' ");
}
elseif($_GET['delcat']) {
	$catID = $_GET['catID'];
	
    safe_query(" UPDATE ".PREFIX."forum_boards SET category='0' WHERE category='$catID' ");
	safe_query(" DELETE FROM ".PREFIX."forum_categories WHERE catID='$catID' ");
	header("Location: admincenter.php?site=boards");
}
elseif($_POST['sortieren']) {
	$sortcat = $_POST['sortcat'];
	$sortboards = $_POST['sortboards'];
	$hideboards = $_POST['hideboards'];
	
	if(is_array($sortcat)) {
		foreach($sortcat as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."forum_categories SET sort='$sorter[1]' WHERE catID='$sorter[0]' ");
		}
	}
	if(is_array($sortboards)) {
		foreach($sortboards as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."forum_boards SET sort='$sorter[1]' WHERE boardID='$sorter[0]' ");
		}
	}
	safe_query("UPDATE ".PREFIX."settings SET hideboards='$hideboards'");
	header("Location: admincenter.php?site=boards");
}
elseif($_POST['save']) {
	$kath = $_POST['kath'];
	$name = $_POST['name'];
	$boardinfo = $_POST['boardinfo'];
	$intern = $_POST['intern'];
	
    if($kath=="") $kath=0;
       safe_query("UPDATE ".PREFIX."forum_boards SET sort=sort+1");
	safe_query("INSERT INTO ".PREFIX."forum_boards ( category, name, info, intern, sort )
                 values( '$kath', '$name', '$boardinfo', '$intern', '1' )");
	header("Location: admincenter.php?site=boards");
}
elseif($_POST['savecat']) {
	$catname = $_POST['catname'];
	$catinfo = $_POST['catinfo'];
	$intern = $_POST['intern'];

	
	safe_query("UPDATE ".PREFIX."forum_categories SET sort=sort+1");
	safe_query("INSERT INTO ".PREFIX."forum_categories ( intern, name, info, sort )
	             values( '$intern', '$catname', '$catinfo', '1' )");
	header("Location: admincenter.php?site=boards");
}
elseif($_POST['saveedit']) {
	$kath = $_POST['kath'];
	$name = $_POST['name'];
	$boardinfo = $_POST['boardinfo'];
	$intern = $_POST['intern'];
	$boardID = $_POST['boardID'];

	safe_query("UPDATE ".PREFIX."forum_boards SET category='$kath',
	                                     name='$name',
								         info='$boardinfo',
								         intern='$intern' WHERE boardID='$boardID' ");
	header("Location: admincenter.php?site=boards");
}
elseif($_POST['saveeditcat']) {
	$catname = $_POST['catname'];
	$catinfo = $_POST['catinfo'];
	$intern = $_POST['intern'];
	$catID = $_POST['catID'];
	
	safe_query("UPDATE ".PREFIX."forum_categories SET intern='$intern', name='$catname', info='$catinfo' WHERE catID='$catID' ");
	header("Location: admincenter.php?site=boards");
}


echo'<h2>boards</h2>';
if($_GET['action']=="mods") {

	$boardID = $_GET['boardID'];

    echo'<h2>moderators</h2>';
	$moderators=safe_query("SELECT * FROM ".PREFIX."user_groups WHERE moderator='1'");
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE boardID='$boardID'");
	$ds=mysql_fetch_array($ergebnis);
	echo'Choose moderators for: <b>'.$ds[name].'</b>';
	echo'<form method="post" action="admincenter.php?site=boards">
	       <select name="mods[]" multiple size="10">';
		   
	while($dm=mysql_fetch_array($moderators)) {
	    $nick=getnickname($dm[userID]);
	    $ismod=mysql_num_rows(safe_query("SELECT * FROM ".PREFIX."forum_moderators WHERE boardID='$boardID' AND userID='$dm[userID]'"));
		if($ismod) echo'<option value="'.$dm[userID].'" selected>'.$nick.'</option>';
		else echo'<option value="'.$dm[userID].'">'.$nick.'</option>';
	}
			
	echo'  </select>
	       <input type="hidden" name="boardID" value="'.$boardID.'">
	       <input type="submit" name="savemods" value="select moderators">
		 </form>';
}
elseif($_GET['action']=="add") {   // neues Board ertstellen
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_categories ORDER BY sort");
    $cats='<select name="kath">';
	while($ds=mysql_fetch_array($ergebnis)) {
	    $cats.='<option value="'.$ds[catID].'">'.$ds[name].'</option>';
	}
	$cats.='</select>';
		
	echo'<form method="post" action="admincenter.php?site=boards">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category:</td>
		   <td>'.$cats.'</td>
		 </tr>
		 <tr>
		   <td>Boardname:</td>
		   <td><input type="text" name="name" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Boardinfo:</td>
		   <td><input type="text" name="boardinfo" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Boardtype:</td>
		   <td>
		     <input class="input" name="intern" type="radio" value="0" checked> public
		     <input class="input" name="intern" type="radio" value="1"> intern
		   </td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Board"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") { // Board bearbeiten

	$boardID = $_GET['boardID'];

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE boardID='$boardID'");
	$ds=mysql_fetch_array($ergebnis);
	
	$category=safe_query("SELECT * FROM ".PREFIX."forum_categories ORDER BY sort");
    $cats='<select name="kath">';
	while($dc=mysql_fetch_array($category)) {
	    $cats.='<option value="'.$dc[catID].'">'.$dc[name].'</option>';
	}
	$cats.='</select>';
		
	echo'<form method="post" action="admincenter.php?site=boards">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category:</td>
		   <td>'.$cats.'</td>
		 </tr>
		 <tr>
		   <td>Boardname:</td>
		   <td><input type="text" name="name" value="'.$ds[name].'" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Boardinfo:</td>
		   <td><input type="text" name="boardinfo" value="'.$ds[info].'" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Boardtype:</td>
		   <td>';
	if($ds[intern]) echo'<input class="input" name="intern" type="radio" value="0"> public
	                     <input class="input" name="intern" type="radio" value="1" checked> intern';
	           else echo'<input class="input" name="intern" type="radio" value="0" checked> public
	                     <input class="input" name="intern" type="radio" value="1"> intern';					 
	echo'  </td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="boardID" value="'.$boardID.'"></td>
		   <td><input type="submit" name="saveedit" value="update board"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="addcat") { // Kategorie erstellen
    echo'<form method="post" action="admincenter.php?site=boards" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category-Name:</td>
		   <td><input type="text" name="catname" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Category-Type:</td>
		   <td>
		     <input class="input" name="intern" type="radio" value="0" checked> public
		     <input class="input" name="intern" type="radio" value="1"> intern
		   </td>
		 </tr>
		 <tr>
		   <td>Category-Info:</td>
		   <td><input type="text" name="catinfo" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="savecat" value="add Category"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="editcat") {  // Kategorie bearbeiten

	$catID = $_GET['catID'];

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_categories WHERE catID='$catID'");
	$ds=mysql_fetch_array($ergebnis);
	
	echo'<form method="post" action="admincenter.php?site=boards">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category-Name:</td>
		   <td><input type="text" name="catname" value="'.$ds[name].'" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Category-Type:</td>
		   <td>';
	if($ds[intern]) echo'<input class="input" name="intern" type="radio" value="0"> public
	                     <input class="input" name="intern" type="radio" value="1" checked> intern';
	           else echo'<input class="input" name="intern" type="radio" value="0" checked> public
	                     <input class="input" name="intern" type="radio" value="1"> intern';					 
	echo'  </td>
		 </tr>
		 <tr>
		   <td>Category-Info:</td>
		   <td><input type="text" name="catinfo" value="'.$ds[info].'" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="catID" value="'.$catID.'"></td>
		   <td><input type="submit" name="saveeditcat" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
else {

	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=add\');return document.MM_returnValue" value="new Board">
	     <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=addcat\');return document.MM_returnValue" value="new Category">';	

	echo'<form method="post" action="admincenter.php?site=boards">
		     <table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
		     <tr bgcolor="#CCCCCC">
			   <td class="title">Boardname:</td>
			   <td class="title">Type:</td>
			   <td class="title">Mods:</td>
			   <td class="title">Actions:</td>
			   <td class="title">Sort:</td>
			 </tr>';
			 
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_categories ORDER BY sort");
	$anz=safe_query("SELECT count(catID) FROM ".PREFIX."forum_categories");
	$anz=mysql_result($anz, 0);
	
	while($ds=mysql_fetch_array($ergebnis)) {
	    $ds[intern] ? $type="<b>INTERN</b>" : $type="public";
		echo'<tr><td colspan="5" bgcolor="#FFFFFF"></td></tr>
		     <tr bgcolor="#CCCCCC">
		       <td class="category_big"><b>&#8226; '.$ds[name].'</b></td>
			   <td class="category">'.$type.'</td>
			   <td class="category"></td>
			   <td class="category">
			     <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=editcat&catID='.$ds[catID].'\');return document.MM_returnValue" value="edit"> 
			     <input type="button" class="button" onClick="MM_confirm(\'really delete this category?\', \'admincenter.php?site=boards&delcat=true&catID='.$ds[catID].'\')" value="delete">
			   </td>
			   <td>
			     <select name="sortcat[]">';
		for($n=1; $n<=$anz; $n++) {
		    if($ds[sort] == $n) echo'<option value="'.$ds[catID].'-'.$n.'" selected>'.$n.'</option>';
			else echo'<option value="'.$ds[catID].'-'.$n.'">'.$n.'</option>';
	       }		
	    echo'    </select>
		       </td>
		     </tr>
			 <tr><td colspan="5" bgcolor="#FFFFFF"></td></tr>';		 
		
	    $boards=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='$ds[catID]' ORDER BY sort");
		$anzboards=safe_query("SELECT count(boardID) FROM ".PREFIX."forum_boards WHERE category='$ds[catID]'");
		$anzboards=mysql_result($anzboards, 0);

		while($db=mysql_fetch_array($boards)) {
			if($db[intern]) {
				$icon='<img src="../images/icons/boardicons/lock.gif">';
				$type='<b>INTERN</b>';
			}
			else {
				$icon='<img src="../images/icons/boardicons/off.gif">';
				$type="public";
			}
			echo'<tr>
		          <td bgcolor="#FFFFFF">'.$icon.' <b>'.$db[name].'</b></td>
		          <td bgcolor="#FFFFFF">'.$type.'</td>
				  <td bgcolor="#FFFFFF"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=mods&boardID='.$db[boardID].'\');return document.MM_returnValue" value="Mods"></td>
		          <td bgcolor="#FFFFFF">
				  <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=edit&boardID='.$db[boardID].'\');return document.MM_returnValue" value="edit"> 
				  <input type="button" class="button" onClick="MM_confirm(\'really delete this board?\', \'admincenter.php?site=boards&delete=true&boardID='.$db[boardID].'\')" value="delete">
				  </td>
		          <td bgcolor="#FFFFFF">
		            <select name="sortboards[]">';
	        for($j=1; $j<=$anzboards; $j++) {
	            if($db[sort] == $j) echo'<option value="'.$db[boardID].'-'.$j.'" selected>'.$j.'</option>';
		        else echo'<option value="'.$db[boardID].'-'.$j.'">'.$j.'</option>';
	           }		
	           echo'   </select>
	             </td>
	           </tr>';
		}
	}	 
	
	$boards=safe_query("SELECT * FROM ".PREFIX."forum_boards WHERE category='0' ORDER BY sort");
	$anzboards=safe_query("SELECT count(boardID) FROM ".PREFIX."forum_boards WHERE category='0'");
	$anzboards=mysql_result($anzboards, 0);
	
	while($db=mysql_fetch_array($boards)) {
		if($db[intern]) {
			$icon='<img src="../images/icons/boardicons/lock.gif">';
			$type='<b>INTERN</b>';
		}
		else {
			$icon='<img src="../images/icons/boardicons/off.gif">';
			$type="public";
		}
		
		echo'<tr bgcolor="dcdcdc">
	          <td bgcolor="#FFFFFF">'.$icon.' <b>'.$db[name].'</b></td>
			  <td bgcolor="#FFFFFF">'.$type.'</td>
			  <td bgcolor="#FFFFFF"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=mods&boardID='.$db[boardID].'\');return document.MM_returnValue" value="Mods"></td>
	          <td bgcolor="#FFFFFF">
			  <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=boards&action=edit&boardID='.$db[boardID].'\');return document.MM_returnValue" value="edit"> 
			  <input type="button" class="button" onClick="MM_confirm(\'really delete this board?\', \'admincenter.php?site=boards&delete=true&boardID='.$db[boardID].'\')" value="delete">
	          </td>
	          <td bgcolor="#FFFFFF">
	            <select name="sort[]">';
		for($n=1; $n<=$anzboards; $n++) {
	    	if($ds[sort] == $n) echo'<option value="'.$db[boardID].'-'.$n.'" selected>'.$n.'</option>';
	        else echo'<option value="'.$db[boardID].'-'.$n.'">'.$n.'</option>';
		}
		echo'</select></td></tr>';
	}
	
	$set=mysql_fetch_array(safe_query("SELECT hideboards FROM ".PREFIX."settings"));
	if($set[hideboards]) $hideinternboards='<input class="input" type="checkbox" name="hideboards" value="1" checked>';
	else $hideinternboards='<input class="input" type="checkbox" name="hideboards" value="1">';
	echo'<tr bgcolor="#CCCCCC">
		       <td>'.$hideinternboards.' Hide <b>intern</b> Boards and Categorys?</td>
			   <td colspan="5" align="right"><input type="submit" name="sortieren" value="update"></td>
			 </tr>
			 </table></form>';
}
?>	
