<?php

/*
// +--------------------------------------------------------------------------+
// | Torrent-Requests auflisten/eintragen/voten/loeschen                      |
// | by truenoir                                                              |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
 */

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

$mod=(get_user_class()>=UC_MODERATOR);

if ($_POST["action"] == "add") {
  $cat=0+$_POST["cat"];
  $titel=trim($_POST["titel"]);
  $info=trim($_POST["info"]);
  if($titel!=""&&$info!="")
  {
    mysql_query(sprintf("insert into requests (user,kategorie,titel,info,added) values (%d,%d,%s,%s,NOW())",$CURUSER["id"],$cat,sqlesc($titel),sqlesc($info)));
    mysql_query("insert into votes (what,user,voteid) values ('requests',$CURUSER[id],".mysql_insert_id().")");
  }
  else
    stderr("FEHLER", "Alle Felder m&uuml;ssen ausgef&uuml;llt werden");
} 
if ($_POST["action"] == "closed") {
  $tid=0+$_POST["tid"];
  $id=0+$_POST["id"];
  if($tid>0)
  {
$res=mysql_query("select * from torrents where id=".sqlesc($tid)); // Changed for PN
    if(mysql_num_rows($res)===1)
    {
      mysql_query("update requests set closed=".sqlesc($tid)." where id=$id");
      // BEGIN: Send PN to requester
      $torrent=mysql_fetch_assoc($res);
      $r=mysql_fetch_assoc(mysql_query("select * from requests where id=$id"));
      $destid=$r["user"];
      sendPersonalMessage(0, $destid, "Request erfüllt", "Der User [URL=userdetails.php?id=$CURUSER[id]]$CURUSER[username][/URL] hat Dein Request [b]$r[titel][/b] mit dem Torrent [URL=details.php?id=$tid][b]$torrent[name][/b][/URL] erfüllt.");
      // END: Send PN to requester
    }
    else
      stderr("FEHLER","Ein Torrent mit der ID $tid gibt es nicht");
  }
  else
    stderr("FEHLER","Du musst die ID des Torrents angeben (steht bei den Torrent-Details in der Browser-Zeile!)");
}
if ($_POST["action"] == "closed") {
  $tid = intval($_POST["tid"]);
  $id = intval($_POST["id"]);
  if($tid>0)
  {
    $res=mysql_query("select * from torrents where id=".$tid);
    if(mysql_num_rows($res)===1)

    {
      // BEGIN: Upload gutschreiben
      $torrent = mysql_fetch_assoc($res);
      mysql_query("UPDATE users SET uploaded=uploaded+'1073741824' WHERE id=".$torrent['owner']);
      // END: Upload gutschreiben
    }
  }
} 
if ($_GET["action"] == "vote") {
  $voteid=0+$_GET["id"];
  $res=mysql_query("select id from votes where what='requests' and voteid=$voteid and user=$CURUSER[id]");
  if(mysql_num_rows($res)===0)
    mysql_query("insert into votes (what,user,voteid) values ('requests',$CURUSER[id],$voteid)");
}
if ($_GET["action"]=="del") {
  if($mod) {
    $voteid=0+$_GET["id"];
    mysql_query("delete from votes where what='requests' and voteid=$voteid");
    mysql_query("delete from requests where id=$voteid");
  }
  else
    stderr("Fehler","Du hast hier nix zu suchen. Verschwinde!");
} 
if ($_GET["action"]=="info") {
  $infoid=0+$_GET["id"];
} 

$res=mysql_query("select * from categories order by name");
$cats="<select name=cat>\n";
while($r=mysql_fetch_assoc($res))
  $cats.="<option value=$r[id]>$r[name]</option>\n";
$cats.="</select>\n";
mysql_free_result($res);
$res=mysql_query("select requests.*,count(votes.id) as votes,categories.name,categories.image,users.username from requests,votes,categories,users where votes.what='requests' and votes.voteid=requests.id and categories.id=requests.kategorie and users.id=requests.user group by requests.id order by requests.added desc limit 50");
$_a=0;
stdhead("Torrent-Requests");
?>


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


<table width="95%"  border="0" cellspacing="1" cellpadding="3" align="center" class="tableinborder">
<tr align="center"><td colspan=<?=($mod?7:6)?> class=tabletitle>Vorhandene Requests</td></tr>
<?php
echo "<tr><td class=tablecat><b>Kategorie</b></td><td class=tablecat><b>Titel</b></td><td class=tablecat><b>Von</b></td><td class=tablecat><b>Vom</b></td><td class=tablecat><b>Votes</b></td><td class=tablecat><b>Erf&uuml;llt</b></td>".($mod?"<td class=tablecat><b>L&ouml;schen</b></td>":"")."</tr>";
if(mysql_num_rows($res)<1)
  echo "<tr><td class=tableb colspan=".($mod?7:6).">Es sind keine Requests vorhanden</td></tr>\n";
else
  while($r=mysql_fetch_assoc($res))
  {
    echo "<tr><td class=tablea><img src=\"$GLOBALS[PIC_BASE_URL]/$r[image]\"/></td><td class=tableb>".($infoid!=$r["id"]?"<a href=\"$PHP_SELF?action=info&id=$r[id]&".SID."\">$r[titel]</a>":"<b>$r[titel]</b><p>".format_comment($r["info"]))."</td>";
    echo "<td class=tablea><a href=\"userdetails.php?id=$r[user]&".SID."\">$r[username]</a></td><td class=tableb>$r[added]</td>";
    echo "<td class=tablea style=\"text-align:center\"><a href=\"$PHP_SELF?action=vote&id=$r[id]&".SID."\"><b>$r[votes]</b></a></td>";

    if($r["closed"]<1)
    {
      echo "<td class=tableb style=\"text-align:center\">";
      echo "<form action=\"$PHP_SELF\" method=post style=\"display: inline;\"><input type=hidden name=action value=closed /><input type=hidden name=id value=$r[id] />ID:&nbsp;<input type=text name=tid size=5 /></form>";
      echo "</td>";
    }
    else
    {
      echo "<td class=tableb style=\"text-align:center\"><a href=\"details.php?id=$r[closed]&".SID."\"><b>Ja</b></a></td>";
    }

    echo ($mod?"<td class=tablea style=\"text-align:center\"><a href=\"$PHP_SELF?action=del&id=$r[id]&".SID."\"><img src=\"$GLOBALS[PIC_BASE_URL]/buttons/button_none.gif\" border=0 /></a></td>":"")."</tr>\n";
  }
?>
</table>
<br><br>

<?php if (get_user_class() >= UC_USER) { ?>
<form method=post action="<?=$PHP_SELF?>">
<input type=hidden name=action value=add />
<table width="450"  border="0" cellspacing="1" cellpadding="3" align="center" class="tableinborder">
<tr align="center"><td colspan="2" class=tabletitle>Request eintragen</td></tr>
<tr><td class=tableb>Kategorie</td><td class=tablea><?=$cats?></td></tr>
<tr><td class=tableb>Titel</td><td class=tablea><input type=text name=titel size=40 maxlength=250 /></td></tr>
<tr><td class=tableb>Info</td><td class=tablea><textarea name=info cols=50 rows=5></textarea></td></tr>
<tr><td class=tablea colspan="2" style="text-align:center"><input type=submit value=Eintragen /></td></tr>
</table>
<br><br>
</form>
<?php } ?>
</td></tr></table>
<br><br>
<?php

stdfoot();
?>