<?php

/*
// +--------------------------------------------------------------------------+
// | Project:    NVTracker - NetVision BitTorrent Tracker                     |
// +--------------------------------------------------------------------------+
// | NVTracker is free software; you can redistribute it and/or modify        |
// | it under the terms of the GNU General Public License as published by     |
// | the Free Software Foundation; either version 2 of the License, or        |
// | (at your option) any later version.                                      |
// |                                                                          |
// | NVTracker is distributed in the hope that it will be useful,             |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
// | GNU General Public License for more details.                             |
// |                                                                          |
// | You should have received a copy of the GNU General Public License        |
// | along with NVTracker; if not, write to the Free Software Foundation,     |
// | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
//
// erstellt von Cerberus - für e-Xtreme-Tracker -- 26.01.2008
*/

if (is_dir($_SERVER["DOCUMENT_ROOT"] . "include")) $root = $_SERVER["DOCUMENT_ROOT"];
else $root = $_SERVER["DOCUMENT_ROOT"] . "/";

require_once($root . "include/bittorrent.php");

dbconn();
loggedinorreturn();

if (get_user_class() < UC_SYSOP)
{
  stderr("Error", "Du bist es nicht wert, diese Seite zu betrachten. Geh nach Hause zu deiner Mama!!!");
  exit;
}

if ($_POST["a"] == "") $action = "view";
else $action = htmlentities($_POST["a"]);

stdhead("Message-Cleaner v1.0");
?>
<script type="text/javascript" language="JavaScript">
 function alle_auswaehlen(field)
 {
   for(i=0; i<field.length; i++)
     field[i].checked = true;
 }
</script>

<?php
/////////////////////////////////////////////////////////
//       markierte Einträge löschen (tote User)        //
/////////////////////////////////////////////////////////
if ($action == "delete")
{
  $x = implode(", ", $_POST[delmsg]);
  $query = "DELETE FROM messages WHERE receiver IN (" . $x . ")";
  mysql_query($query);
  mysql_query("OPTIMIZE TABLE messages");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("  <td width=\"100%\" class=\"tabletitle\"><center><b><a href=\"" . $_SERVER[PHP_SELF] . "\">weiter &rArr;</a></b></center></td>\n");
  print(" </tr>\n");
  print("</table>\n");
}

/////////////////////////////////////////////////////////
//       markierte Einträge löschen (gelöschte)        //
/////////////////////////////////////////////////////////
if ($action == "totaldeleted")
{
  $x = implode(", ", $_POST[deletedmsg]);
  $query = "DELETE FROM messages WHERE folder_in=0 AND receiver IN (" . $x . ")";
  mysql_query($query);
  mysql_query("OPTIMIZE TABLE messages");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("  <td width=\"100%\" class=\"tabletitle\"><center><b><a href=\"" . $_SERVER[PHP_SELF] . "\">weiter &rArr;</a></b></center></td>\n");
  print(" </tr>\n");
  print("</table>\n");
}

////////////////////////////////////////////////////
//       alle gelöschten Einträge anzeigen        //
////////////////////////////////////////////////////
if ($action == "delmsg")
{
  $anz_deleted = 0;
  $anz_user = 0;
  print("<form action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\">\n");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("   <td class=\"tablea\" colspan=\"3\">\n");
  print("    <center><input type=\"hidden\" value=\"view\" name=\"a\">\n");
  print("    <input type=\"submit\" value=\"&uuml;berfl&uuml;ssige Nachrichten\" name=\"go\"></center>\n");
  print("   </td>\n");
  print(" </tr>\n");
  print("</table>\n");
  print("</form>\n");
  print("<br>\n");
  print("<form action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\" name=\"formular\">\n");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\" align=\"right\">\n");
  print("    <input type=\"button\" value=\"alles markieren\" name=\"go\" onclick=\"alle_auswaehlen(document.formular['deletedmsg[]'])\">&nbsp;&nbsp;\n");
  print("    <input type=\"hidden\" value=\"totaldeleted\" name=\"a\">\n");
  print("    <input type=\"submit\" value=\"endg&uuml;ltig L&ouml;schen\" name=\"go\">\n");
  print("   </td>\n");
  print(" </tr>\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" width=\"80\"><b>ID</b></td>\n");
  print("   <td class=\"tabletitle\"><b>Anz. Nachrichten</b></td>\n");
  print("   <td class=\"tabletitle\" width=\"80\"><b>L&ouml;schen</b></td>\n");
  print(" </tr>\n");
  $res = mysql_query("SELECT id, receiver, folder_in FROM messages WHERE folder_in=0 GROUP BY receiver");
  while($daten = mysql_fetch_array($res))
  {
    $id = $daten['receiver'];
    $res_user = mysql_query("SELECT id, username FROM users WHERE id='$id'");
    $daten_usr = mysql_fetch_array($res_user);
    if ($daten["folder_in"] == 0)
    {
      $res_msg = mysql_query("SELECT COUNT(*) AS anz FROM messages WHERE folder_in=0 AND receiver='$id'");
      $anz_msg = mysql_fetch_array($res_msg);
      $anz_deleted += intval($anz_msg[anz]);
      $anz_user ++;
      print(" <tr>\n");
      print("   <td class=\"tablea\">".$id."</td>\n");
      print("   <td class=\"tablea\">Der User ".$daten_usr["username"] ." hat noch <font color=\"red\"><b>".$anz_msg[anz]."</b></font> gel&ouml;schte Nachrichten</td>\n");
      print("   <td bgcolor=\"red\"><center><input type=\"checkbox\" name=\"deletedmsg[]\" value=\"" . $id . "\" /></center></td>\n");
      print(" </tr>\n");
    }
  }
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\"><center>gefundene Nachrichten: <b>".$anz_deleted."</b> von ".$anz_user." Benutzern</center></td>\n");
  print(" </tr>\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\"><center><font color=red><b>.:DB-Optimierung findet automatisch im Anschlu&szlig; statt :.</b></font></center></td>\n");
  print(" </tr>\n");
  print("</table>\n");
  print("</form>\n");
}

/////////////////////////////////////////
//       alle Einträge anzeigen        //
/////////////////////////////////////////
if ($action == "view")
{
  $anz_zuviel = 0;
  $fail_user = 0;
  print("<form action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\">\n");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("   <td class=\"tablea\" colspan=\"3\">\n");
  print("    <center><input type=\"hidden\" value=\"delmsg\" name=\"a\">\n");
  print("    <input type=\"submit\" value=\"gel&ouml;schte Nachrichten\" name=\"go\"></center>\n");
  print("   </td>\n");
  print(" </tr>\n");
  print("</table>\n");
  print("</form>\n");
  print("<br>\n");
  print("<form action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\" name=\"formular\">\n");
  print("<table style=\"width: 750px;\" class=\"tableinborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\" align=\"right\">\n");
  print("    <input type=\"button\" value=\"alles markieren\" name=\"go\" onclick=\"alle_auswaehlen(document.formular['delmsg[]'])\">&nbsp;&nbsp;\n");
  print("    <input type=\"hidden\" value=\"delete\" name=\"a\">\n");
  print("    <input type=\"submit\" value=\"L&ouml;schen\" name=\"go\">\n");
  print("   </td>\n");
  print(" </tr>\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" width=\"80\"><b>ID</b></td>\n");
  print("   <td class=\"tabletitle\"><b>Anz. Nachrichten</b></td>\n");
  print("   <td class=\"tabletitle\" width=\"80\"><b>L&ouml;schen</b></td>\n");
  print(" </tr>\n");

  $res = mysql_query("SELECT id, receiver FROM messages GROUP BY receiver");
  while($daten = mysql_fetch_array($res))
  {
    $id = $daten['receiver'];
    $res_user = mysql_query("SELECT id, username FROM users WHERE id='$id'");
    $daten_usr = mysql_fetch_array($res_user);
    if ($daten_usr['username'] == '')
    {
      $res_msg = mysql_query("SELECT COUNT(*) AS anz FROM messages WHERE receiver='$id'");
      $anz_msg = mysql_fetch_array($res_msg);
      $anz_zuviel += intval($anz_msg[anz]);
      $fail_user ++;
      print(" <tr>\n");
      print("   <td class=\"tablea\">".$id."</td>\n");
      print("   <td class=\"tablea\">Der User wurde nicht gefunden und hat noch <font color=\"red\"><b>".$anz_msg[anz]."</b></font> Nachrichten</td>\n");
      print("   <td bgcolor=\"red\"><center><input type=\"checkbox\" name=\"delmsg[]\" value=\"" . $id . "\" /></center></td>\n");
      print(" </tr>\n");
    }
  }

  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\"><center>gefundene Nachrichten: <b>".$anz_zuviel."</b> von ".$fail_user." nichtgefundenen Benutzern</center></td>\n");
  print(" </tr>\n");
  print(" <tr>\n");
  print("   <td class=\"tabletitle\" colspan=\"3\"><center><font color=red><b>.:DB-Optimierung findet automatisch im Anschlu&szlig; statt :.</b></font></center></td>\n");
  print(" </tr>\n");
  print("</table>\n");
  print("</form>\n");
}
stdfoot(true);
?> 