<?
require "include/bittorrent.php";
dbconn(false);
loggedinorreturn();
if (get_user_class() < UC_ADMINISTRATOR)
stderr("Sorry", "Zugrif verweigert.");
$action = (isset($_GET['action']) ? $_GET['action'] : 'showlist');
$id = (isset($_GET['id']) ? $_GET['id'] : '');
function check ($id) {    
    if (!is_valid_id($id))
        return stderr("Fehler","Ungültige ID");
    else
        return true;
}
function safe_query ($query,$id) {
    $query = sprintf("$query WHERE id ='%s'",
    mysql_real_escape_string($id));
    $result = mysql_query($query);
    if (!$result)
        return sqlerr(__FILE__,__LINE__);
    else
        redirect('maxlogin.php');
}
function redirect($url)
{
    if(!headers_sent())
         header("Location : $url");
    else
        echo "<script language=\"JavaScript\">window.location.href = '$url';</script>";
    exit;
} 
if ($action == 'showlist') {
stdhead ("Max. Anmeldung Zugriffe - Liste");
print("<table border=1 cellspacing=0 cellpadding=5 width=737>\n");
$res = mysql_query("SELECT * FROM  loginattempts ORDER BY added DESC") or sqlerr(__FILE__,__LINE__);
if (mysql_num_rows($res) == 0)
      print("<tr><td class=tableb colspan=1><b>Nichts Gefunden</b></td></tr>\n");
else
{ 
  print("<table cellpadding=4 cellspacing=1 border=0 style=width:80% class=tableinborder>\n");
  print("<tr  class=tabletitle width=80%>\n"); 
  print("<td colspan=10 width=80%><span class=normalfont><center><b>max. versuchte Login</b></center></span></td></tr>\n");
  print("<tr><td class=tabletitle>ID</td><td class=tabletitle align=left>Ip Addresse</td><td class=tabletitle align=left>Ereignis Zeit</td>".
    "<td class=tabletitle align=left>Versuche</td><td class=tabletitle align=left>Status</td></tr>\n");

  while ($arr = mysql_fetch_assoc($res))
  {
      $r2 = mysql_query("SELECT id,username FROM users WHERE ip=".sqlesc($arr[ip])) or sqlerr(__FILE__,__LINE__);
      $a2 = mysql_fetch_assoc($r2);    
       print("<tr><td class=tablea align=>$arr[id]</td><td class=tablea align=left>$arr[ip] " . ($a2[id] ? "<a href=userdetails.php?id=$a2[id]>" : "" ) . " " . ( $a2[username] ? "($a2[username])</a>" : "" ) . "</td><td class=tablea align=left>$arr[added]</td><td class=tablea align=left>$arr[attempts]</td><td class=tablea align=left>".($arr[banned] == "yes" ? "<font color=red><b>gebannt</b></font> <a href=maxlogin.php?action=unban&id=$arr[id]><font color=green>[<b>entbannen</b>]</font></a>" : "<font color=green><b>nicht gebannt</b></font> <a href=maxlogin.php?action=ban&id=$arr[id]><font color=red>[<b>bannen</b>]</font></a>")."  <a OnClick=\"return confirm('Möchtest du diesen Versuch wirklich löschen?');\" href=maxlogin.php?action=delete&id=$arr[id]>[<b>löschen</b>]</a></td></tr>\n");
  }
  
}
print("</table>\n");
print("</td></tr></table>\n");
}elseif ($action == 'ban') {
    check($id);
    stdhead ("Max. Anmeldung Zugriffe - BAN");    
    safe_query("UPDATE loginattempts SET banned = 'yes'",$id);
}elseif ($action == 'unban') {
    check($id);
    stdhead ("Max. Anmeldung Zugriffe - UNBAN");
    safe_query("UPDATE loginattempts SET banned = 'no'",$id);
}elseif ($action == 'delete') {
    check($id);
    stdhead ("Max. Anmeldung Zugriffe - DELETE");
    safe_query("DELETE FROM loginattempts",$id);
    
}else
    stderr("Fehler","Ungültige Action");
?>