<?
require_once("include/bittorrent.php");

hit_start();


dbconn(false);

hit_count();

loggedinorreturn();

//if (get_user_class() == UC_USER)
//    stderr("Fehler", "Dir ist der Zugang zu dieser Seite nicht gestattet!");

stdhead("Uploader Bewerbung");


if ($_POST["form"]=="") {
 if($CURUSER["class"]<UC_MODERATOR)
   $CURUSER["uploadpos"] == 'no'?0:2;
else
  $form=10;
} else
$form=$_POST["form"];
if($form==0) {
 $res=mysql_query("SELECT * FROM uploadapp WHERE userid=".$CURUSER["id"]) or sqlerr(__FILE__, __LINE__);
 if(mysql_num_rows($res)) {
  $row=mysql_fetch_array($res);
  $form=4;
 }
}

$debug=0;
$upreq = 20;
$upreqn = $upreq * 1073741824;


begin_main_frame();
if($debug) {
begin_frame("Debug Box");
print("<table>");
 print("<form action=\"uploadapp.php\" method=\"post\" enctype=\"multipart/form-data\" name=\"debug\" id=\"uploadapp\">");
tr("User Class","&nbsp;&nbsp;".get_user_class_name($CURUSER["class"]),1);
tr("Variables",
 "form = " . $form. "<br>".
 "user = " . $_POST["user"]. "<br>".
 "groupacct = " . $_POST["groupacct"]. "<br>".
 "grouname = " . unesc($_POST["groupname"]). "<br>".
 "groupdes = " . unesc($_POST["groupdes"]). "<br>".
 "joined = " . unesc($_POST["joined"]). "<br>".
 "ratio = " . unesc($_POST["ratio"]). "<br>".
 "upk =". unesc($_POST["upk"]). "<br>".
 "rbseed = ".unesc($_POST["rbseed"]).
  "<br>rbrelease = ".unesc($_POST["rbrelease"]).
  "<br>rbstime = ".unesc($_POST["rbstime"]) . "<br>".
 "plans =". unesc($_POST["plans"]). "<br>".
 "comment =". unesc($_POST["comment"]). "<br>".
 "",1);
 tr("View forms","<input type=\"radio\" name=\"form\" value=\"0\" ". ($form==0?"checked":""). ">Upload App".
 "<input name=\"form\" type=\"radio\" value=\"3\" ".($form==3?"checked":"").">Moderator+ Page".
 "<input type=\"submit\" name=\"SubmitD\" value=\"Change Forms\">",1);
 print("</table> </form>");

end_frame();
}


if($form>=10 && $CURUSER["class"]<UC_MODERATOR) {
begin_frame("Fehlerhafte Anfrage");
end_frame();
}
else if($form==0) {
begin_frame("Uploader Bewerbung");
?>
 <form action="uploadapp.php" method="post" enctype="multipart/form-data" name="uploadapp" id="uploadapp">
 <table border="1" cellspacing="0" cellpadding="10">
 <table border="0">
<?

if ($CURUSER["downloaded"] > 0)
$ratio = $CURUSER['uploaded'] / $CURUSER['downloaded'];
else if ($CURUSER["uploaded"] > 0)
$ratio = 1;
else
$ratio = 0;

tr("User","&nbsp;&nbsp;<input name=\"user\" type=\"hidden\" value=\"". $CURUSER['id']."\">".$CURUSER['username'],1);
tr("Bist du in<br>einer Crew?","<input type=radio name=grupp value=yes>Ja <input type=radio name=grupp value=no>Nein",1);
tr("","<h4><center>Nur für Crew-Member!</center></h4>",1);
tr("Crew Name","<input name=\"groupname\" type=\"text\" id=\"groupname\" size=\"50\" maxlength=\"50\">",1);
tr("","<h4><center>Für alle Bewerber!</center></h4>",1);
tr("Registierungsdatum","&nbsp;&nbsp;<input name=\"joined\" type=\"hidden\" value=\"".$CURUSER['added']."\">".$CURUSER['added'],1);
tr("Meine Ratio ist 1.0 oder mehr!","&nbsp;&nbsp;<input name=\"ratio\" type=\"hidden\" value=\"".($ratio>=1?"ok":"not ok")."\">".($ratio>=1?"Ja":"Nein"),1);
$upreqm=$CURUSER['uploaded']>=$upreqn;
tr("Ich habe genau bzw. mehr als " . $upreq ." GB Upload Transfer","&nbsp;&nbsp;<input name=\"upk\" type=\"hidden\" value=\"".($upreqm?"yes":"no")."\">".($upreqm?"JA":"Nein"),1);
tr("Was planst du zu Uppen?","<textarea name=\"plans\" cols=\"100\" rows=\"2\" wrap=\"VIRTUAL\"></textarea>",1);
tr("Warum sollten wir dich zum Uploader befördern?","<textarea name=\"comment\" cols=\"100\" rows=\"4\" wrap=\"VIRTUAL\"></textarea>",1);
?>
</table>
<p>Ich weiß wie man seedet und ich kenne mich mit der Torrentserstellung aus etc...?<br>
<input type="radio" name="rbseed" value="1">
 Ja<br>
 <input name="rbseed" type="radio" value="0" checked>
 Nein</p>
<p>Ich habe die Regeln gelesen, weiß was verboten ist und was nicht und werde mich daran halten.<br>
<input type="radio" name="rbrelease" value="1">
 Ja<br>
 <input name="rbrelease" type="radio" value="0" checked>
 Nein</p>
<p>Ich habe verstanden, dass ich mindestens 24h seeden muss und/oder ich den seed erst verlassen darf wenn das file gut verteilt ist.<br>
<input type="radio" name="rbstime" value="1">
Ja<br>
<input name="rbstime" type="radio" value="0" checked>
Nein</p>
<br>
<input name="form" type="hidden" value="1">
<input type="submit" name="Submit" value="Bewerbung Abschicken"> </table> </form>
<?
end_frame();

} else if ($form==1) {

begin_frame("Uploader Bewerbungs Antrag");
$qry="INSERT INTO uploadapp (userid,applied,grpacct,grpname,grpdes,content,comment,seeding,othergrps,seedtime) ".
 "VALUES (". $_POST["user"].", ".
  implode(",",array_map("sqlesc",array(
    get_date_time(),
   $_POST["groupacct"],
   $_POST["groupname"],
   $_POST["groupdes"],
   $_POST["plans"],
   $_POST["comment"],
    $_POST["rbseed"],
   $_POST["rbrelease"],
   $_POST["rbstime"]))).")";

///////////////////////////////////////
$text = "[color=red][b]Neue UPLOADER-BEWERBUNG ist eingegangen [/b][/color] "; 
// $date = gmdate("Y-m-d H:i:s");
$date=time();
//  $query = "INSERT INTO shoutbox (id, userid, username, date, text) VALUES (NULL, '".$CURUSER['username']."', '".MakeSQLSafe($_POST['message'])."', '".$local_time."', '".$CURUSER['id']."')";

mysql_query("INSERT INTO shoutbox (id, userid, username, date, text) VALUES ('id'," . sqlesc('0') . ", " . sqlesc('System') . ", $date, " . sqlesc($text) . ")");
header("Location: $BASEURL/details.php?id=$id&uploaded=1");


hit_end();
//////////////////////////

$ret=mysql_query($qry);
if (!$ret) {
  if (mysql_errno() == 1062)
   print("Deine Bewerbung wurde bereits gesendet<br>");
  else
  print("mysql puked: ".mysql_error());

} else
 print("<h3>Deine Bewerbung wurde erfolgreich gesendet</h3>");




end_frame();
} else if($form==2) {
begin_frame("Uploader Bewerbung");
print("<h3>Du hast bereits Uploadrechte</h3>");
end_frame();
} else if($form==4) {
begin_frame("Deine Uploader Bewerbung");
  $votesyes=$votesno==0;
   if($row["votes"]!="") {
    $votes=explode(" ",$row["votes"]);
    for($i=0;$i<count($votes);$i++)
    {
     $votei=explode(":",$votes[$i]);
     $votei[1]?$votesyes++:$votesno++;
    }
   }
   print("Uploader Bewerbung: Ja = ".$votesyes." &nbsp;&nbsp; Nein = ".$votesno);
   print("<br>Abstimmung ist ".($row["active"]=="0"?"beendet":"offen").".");
end_frame();
} else if($form>=10) {
begin_frame("Team Abstimmung");
if($form==11) {
 $res=mysql_query("SELECT * FROM uploadapp WHERE id=".$_POST["pollid"]) or sqlerr(__FILE__, __LINE__);
 $row=mysql_fetch_array($res);
  $votesyes=$votesno=$voted=0;
   if($row["votes"]!="") {
    $votes=explode(" ",$row["votes"]);
    for($i=0;$i<count($votes);$i++)
    {
     $votei=explode(":",$votes[$i]);
     if($CURUSER["id"]==$votei[0]) $voted++;
     $votei[1]?$votesyes++:$votesno++;
    }
   }
 if($_POST["ballet"] && $voted==0) {
  $votes=($row["votes"]!=""?$row["votes"]." ":"").implode(":",array($CURUSER["id"],$_POST["ballet"]=="Yes"?1:0));
  mysql_query("UPDATE uploadapp SET votes='".$votes."' WHERE id=".$_POST["pollid"]);
  print("Vote for ".$_POST["pollid"]." recieved (".$_POST["ballet"].")<br>");
 } else if($_POST["closepoll"]) {
   print("Anfrage, die Abstimmung ".$_POST["pollid"]." zu schließen, erhalten<br>");
   if(count($votes)<5) {
     print("Anfrage abgelehnt, 5 Votes nötig um Abstimmung zu schließen");
   } else {
    mysql_query("UPDATE uploadapp SET active='0' WHERE id=".$_POST["pollid"]);
    $tvotes=$votesyes+$votesno;
    $votea=$votesyes>$votesno;
    $modcomment = gmdate("Y-m-d") . " - Uploader Bewerbung: ".($votea?"Angenommen":"Abgelehnt")." (Ja = ".$votesyes." Nein = ".$votesno." (".
     number_format((($votea?$votesyes:$votesno)/$tvotes)*100,3)."%)";
    print($modcomment."<br>");
    if($votea) {
     $mq="UPDATE users SET uploadpos='yes',class='".UC_UPLOADER."',modcomment=CONCAT(modcomment,".sqlesc($modcomment."\n").") WHERE id=".$row["userid"];
     mysql_query($mq);
     print("Updating User Records...<br>");
    $dt = sqlesc(get_date_time());
    $msg = sqlesc("Herzlichen Glückwunsch, deine Bewerbung wurde angenommen und du wirst unser neuer Uploader!.\n");
    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".$row["userid"].", $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
    } else {
     $mq="UPDATE users SET modcomment=CONCAT(modcomment,".sqlesc($modcomment."\n").") WHERE id=".$row["userid"];
     mysql_query($mq);
    $dt = sqlesc(get_date_time());
    $msg = sqlesc("sorry, Deine Uploader Bewerbung wurde abgelehnt.\n");
    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".$row["userid"].", $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
    }
  }
 } else if($_POST["removepoll"]) {
  mysql_query("DELETE FROM uploadapp where id=".$_POST["pollid"]);
  print("Abstimmung ".$_POST["pollid"]." aus der Datenbank gelöscht");
 } else if($_POST["addcomment"]) {
  print ("Kommentar: ".sqlesc($_POST["newcomments"])."<br>");
   if(($_POST["newcomments"])) {
    $un=sqlesc($CURUSER["username"].": ".$_POST["newcomments"]."\n");
   $mq="UPDATE uploadapp SET modcomments=CONCAT(modcomments,".$un.") WHERE id=".$_POST["pollid"];
   mysql_query($mq);
   print("Kommentar zur Abstimmung ".$_POST["pollid"]."hinzugefügt");
  }
 }

}
print("<h1>Neue Uploader Bewerbungen</h1>");
$res=mysql_query("SELECT * FROM uploadapp ORDER BY applied DESC") or sqlerr(__FILE__, __LINE__);
if(!mysql_num_rows($res)) print("<h3>Keine");
else {
?>
 <table>
 <tr><td>Abstimmung </td>
 <td>Username</td>
 <td><span title="Bewerbungs Datum &amp; Join Date " class="popup">Bewerbungsdatum</span></td>
 <td><span title="Angeschlossene Gruppe(Crew)" class="popup">Crew</span></td>
 <td><span title="Zu liefernder Inhalt" class="popup">Inhalt</span></td>
 <td><span title="Warum sollten wir diesen User zum Uploader machen?" class="popup">Warum gerade er?</span></td>
 <td><span title="Hat der User die erforderliche Ratio?" class="popup">Ratio</span></td>
 <td><span title="Hat der User den erforderlichen Gesamtupload" class="popup"><?=$upreq?>gb+</span></td>
 <td><span title="Weiß der User was Seeden bedeutet?" class="popup">Seeden</span></td>
 <td><span title="Hat der User die Regeln gelesen und weiß was erlaubt und verboten ist?" class="popup">Regeln</span></td>
 <td><span title="Kennt der User die Mindestseedzeit?" class=popup">Seeder<br>Zeit</span></td>
 <td>Voting</td>
 </tr>
<?
 while($row=mysql_fetch_array($res))
 {
   $resu=mysql_query("SELECT * FROM users where id = ".$row["userid"])  or sqlerr(__FILE__, __LINE__);
   $rowu=mysql_fetch_array($resu);
   $voted=$tvotes=$votesyes=$votesno=0;
   if($row["votes"]!="") {
    $votes=explode(" ",$row["votes"]);
    for($i=0;$i<count($votes);$i++)
    {
     $votei=explode(":",$votes[$i]);
     if($CURUSER["id"]==$votei[0]) $voted++;
     $votei[1]?$votesyes++:$votesno++;
     $tvotes++;
    }
   }
 if ($rowu["downloaded"] > 0)
  $ratio = $rowu['uploaded'] / $rowu['downloaded'];
 else if ($rowu["uploaded"] > 0)
  $ratio = 1;
 else
  $ratio = 0;
?>
 <tr>
   <form action="uploadapp.php" method="post" enctype="multipart/form-data" name="poll<?=$row["id"]?>" id="uploadapp">
   <input name="form" type="hidden" value="11">
 <input name="pollid" type="hidden" value="<?=$row["id"]?>">
 <td><?=$row["id"]?></td>
 <td><a href=userdetails.php?id=<?=$row["userid"]?>><?=$rowu["username"]?></a></td>
 <td><?=$row["applied"]?></td>
 <td <?=($row["grpacct"]?"bgcolor=\"#FFFF00\">(".unesc($row["grpdes"]).")&nbsp;".unesc($row["grpname"]):">N/A")?></td>
 <td><?=$row["content"]?></td>
 <td><?=$row["comment"]?></td>
 <td bgcolor="<?=($ratio>=1?"#00FF00":"#FF0000")?>"></td>
 <td bgcolor="<?=($rowu["uploaded"]>=$upreqn?"#00FF00":"#FF0000")?>"></td>
 <td bgcolor="<?=($row["seeding"]?"#00FF00":"#FF0000")?>"></td>
 <td bgcolor="<?=($row["othergrps"]?"#00FF00":"#FF0000")?>"></td>
  <td bgcolor="<?=($row["seedtime"]?"#00FF00":"#FF0000")?>"></td>
  <td rowspan="2"><?=($voted||!$row["active"]?$votesyes." Yes<br>".$votesno." No"
  :"Votes: ".$tvotes."<br>".
  "<input name=\"ballet\" type=\"submit\" value=\"Yes\">".
 "<input name=\"ballet\" type=\"submit\" value=\"No\">").
 (($CURUSER["class"]>=UC_CO_ADMIN&&$row["active"])?"<input name=\"closepoll\" type=\"submit\" value=\"Abstimmung schließen\">":
  "<br>".($row["active"]?"<br><font color=#00FF00>Poll Open</font>":"<br><font color=#FF0000>Abstimmung geschlossen</font>")).
 ($CURUSER["class"]>=UC_CO_ADMIN?"<br><input name=\"removepoll\" type=\"submit\" value=\"Abstimmung löschen\">":"")
 ?></td>  </form><tr></tr>
<?
  }
  print("</table>");
 }


end_frame();
}
end_main_frame();
stdfoot();
hit_end();
?>