<?php

/*
// +--------------------------------------------------------------------------+
// | Torrent-Kategorien verwalten                                             |
// | by truenoir                                                              |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
 */

require "include/bittorrent.php";
dbconn();
loggedinorreturn();

if (get_user_class() < UC_SYSOP)
    stderr("w00t","w00t");

// Kategorie hinzufügen
if ($_POST['action'] == "catadd") {
    $name=trim($_POST["name"]);
    $image=trim($_POST["image"]);
    if($name!=""&&$image!="")
    {
      $name=sqlesc($name);
      $image=sqlesc($image);
      mysql_query("insert into categories (name,image) values ($name,$image)");
      write_log("Kategorien", "Kategorie $name von '<a href=\"userdetails.php?id=$CURUSER[id]\">$CURUSER[username]</a>' hinzugefügt");
    }
    header("Location: $PHP_SELF?" . SID);
    die();
} 
if ($_POST['action'] == "catchg") {
    $res=mysql_query("select * from categories");
    while($r=mysql_fetch_object($res))
    {
      $name=trim($_POST["name".$r->id]);
      $image=trim($_POST["image".$r->id]);
      if($_POST["del".$r->id]=="on")
      {
        mysql_query("update torrents set category=".sqlesc($_POST["std"])." where category=".sqlesc($r->id));
        mysql_query("delete from categories where id=$r->id");
        write_log("Kategorien", "Kategorie $r->name/$r->image wurde gelöscht von '<a href=\"userdetails.php?id=$CURUSER[id]\">$CURUSER[username]</a>'");
      }
      else
        if($name!=$r->name||$image!=$r->image)
        {
          mysql_query("update categories set name=".sqlesc($name).", image=".sqlesc($image)." where id=$r->id");
          write_log("Kategorien", "Kategorie $r->name/$r->image wurde geändert ==> ".sqlesc($name)."/".sqlesc($image)."  von '<a href=\"userdetails.php?id=$CURUSER[id]\">$CURUSER[username]</a>'");
        }
    }
    mysql_query("update categories set std='J' where id=".$_POST["std"]);
    mysql_query("update categories set std='N' where id<>".$_POST["std"]);
    header("Location: $PHP_SELF?&" . SID);
    die();
} 

$res=mysql_query("select * from categories order by name");
$_a=0;
stdhead("Torrent-Kategorien");
?>


<table cellpadding="4" cellspacing="1" border="0" style="width:600px" class="tableinborder">
<tr class="tabletitle" width="100%">
<td colspan="10" width="100%"><span class="smallfont"><center>
<b>Kategorien</b> - Torrent-Kategorien
</center></span></td></tr><tr><td width="100%" class="tablea">


<form method=post action="<?=$PHP_SELF?>">
<table width="450"  border="0" cellspacing="1" cellpadding="3" align="center" class="tableinborder">
<tr align="center">
    <td colspan="3" class=tablecat>Kategorie hinzufügen</td>
  </tr>
  <tr><td class=tableb><b>Kategorie:</b></td><td class=tableb><b>Bild:</b></td></tr>
  <tr><td class=tablea><input name="name" type="text" size="20" maxlength="40" value=""></td>
  <td class=tablea><input name="image" type="text" size="20" maxlength="40" value=""></td></tr>
  <tr align="center">
    <td class=tablea colspan="2" style="text-align:center"><input type="hidden" name="action" value="catadd"><input type="submit" name="Submit" class="btn" style="height:23px" value="Anlegen"></td>
  </tr>
</table>
</form><br><br>
<form method=post action="<?=$PHP_SELF?>">
<table width="550"  border="0" cellspacing="1" cellpadding="3" align="center" class="tableinborder">
<tr align="center">
    <td colspan="5" class=tablecat>Kategorien verwalten</td>
  </tr>
  <?php
  while($r=mysql_fetch_object($res)) {
      if($_a==0) {
  ?>
  <tr>
  <tr><td class=tableb><b>Kategorie</b></td><td class=tableb><b>Bild</b></td><td class=tableb><b>Bilddatei</b></td><td class=tableb><b>Standard</b></td><td class=tableb><b>Löschen</b></td></tr>
  <?php $_a++; } ?>
  <tr valign="top"><td class=tablea><input name="name<?=$r->id?>" type="text" size="20" maxlength="40" value="<?=$r->name?>"></td>
    <td class=tablea><img src="<? echo $GLOBALS["PIC_BASE_URL"]."/".$r->image;?>" align="top"></td><td class=tablea><input name="image<?=$r->id?>" type="text" size="20" maxlength="40" value="<?=$r->image?>"></td><td class=tablea><input name="std" type="radio" value="<?=$r->id?>"<?php echo ($r->std=="J"?" checked":""); ?>></td><td class=tablea><input name="del<?=$r->id?>" type="checkbox"<?php echo ($r->std=="J"?" disabled":""); ?>></td>
  </tr>
  <?php } $_a=0;?>
  <tr align="center">
    <td class=tablea colspan="5" style="text-align:center"><input type="hidden" name="action" value="catchg"><input type="submit" name="Submit" class="btn" style="height:23px" value="Ändern"></td>
  </tr>

</table>
</form><br><br>
<table width="450"  border="0" cellspacing="1" cellpadding="3" align="center" class="tableinborder">
<tr align="center">
    <td colspan="2" class=tablecat>Vorhandene Kategorie-Bilder</td>
  </tr>
  <?php
  $dir=opendir($GLOBALS["PIC_BASE_URL"]);
  while(($file=readdir($dir))!=false) {
    if(substr($file,0,4)=="cat_") {
      if($_a==0) {
  ?>
  <tr><td class=tableb><b>Name</b></td><td class=tableb><b>Bild</b></td></tr>
  <?php $_a++; } ?>
  <tr><td class=tablea><?=$file?></td><td class=tablea><img src="<? echo $GLOBALS["PIC_BASE_URL"]."/".$file;?>"></td></tr>
  <?php  } } closedir($dir) ?>

</table>
</td></tr></table>
<br><br>
<?php

stdfoot();
?>