<?php

require "include/bittorrent.php";

dbconn();
loggedinorreturn();



$action = $_GET["action"];

// Delete Announce Item //////////////////////////////////////////////////////

if ($action == 'delete')
{

if (get_user_class() < UC_ADMINISTRATOR)
die(Error);

$ann_id = $_GET["ann_id"];
if (!is_valid_id($ann_id))
stderr("Error","Invalid Announcement item ID - Code 1.");

$returnto = $_GET["returnto"];

$sure = $_GET["sure"];
if (!$sure)
stderr("Ankündigung löschen","Willst du diese Ankündigung? Klicke\n" .
"<a href=?action=delete&ann_id=$ann_id&returnto=$returnto&sure=1>hier</a> wenn du dir sicher bist.");

mysql_query("DELETE FROM announcement WHERE id=$ann_id") or sqlerr(__FILE__, __LINE__);

if ($returnto != "")
header("Location: $returnto");
else
$warning = "Ankündigung erfolgreich gelöscht.";
}

// Add Announcement Item /////////////////////////////////////////////////////////

if ($action == 'add')
{
if (get_user_class() < UC_SYSOP)
die(Error);

$body = $_POST["body"];
if (!$body)
stderr("Error","Die Ankündigung darf nicht leer sein!");

$title = $_POST["title"];
if (!$title)
stderr("Error","Der Titel darf nicht leer sein!");

$added = $_POST["added"];
if (!$added)
$added = sqlesc(get_date_time());

mysql_query("INSERT INTO announcement (userid, added, body ,title) VALUES (".
$CURUSER['id'] . ", $added, " . sqlesc($body) . ", " . sqlesc($title) . ")") or sqlerr(__FILE__, __LINE__);
if (mysql_affected_rows() == 1)
{
mysql_query("UPDATE users SET announce = 'yes' WHERE announce='no'");
$warning = "Ankündigung erfolgreich hinzugefügt.";
}
else
stderr("Error","Etwas Seltsames ist passiert.");
}

// Edit Announcement ////////////////////////////////////////////////////////

if ($action == 'edit')
{
if (get_user_class() < UC_ADMINISTRATOR)
die(Error);

$ann_id = $_GET["ann_id"];

if (!is_valid_id($ann_id))
stderr("Error","Invalid Announcement item ID - Code 2.");

$res = mysql_query("SELECT * FROM announcement WHERE id=$ann_id") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
stderr("Error", "No Announcement item with ID $ann_id.");

$arr = mysql_fetch_array($res);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$body = $_POST['body'];
if ($body == "")
stderr("Error", "Body darf nicht leer sein!");
$body = sqlesc($body);
mysql_query("UPDATE announcement SET body=$body WHERE id=$ann_id") or sqlerr(__FILE__, __LINE__);

$title = $_POST['title'];
if ($title == "")
stderr("Error", "Titel darf nicht leer sein!");
$title = sqlesc($title);
mysql_query("UPDATE announcement SET title=$title WHERE id=$ann_id") or sqlerr(__FILE__, __LINE__);


$editedat = sqlesc(get_date_time());



$returnto = $_POST['returnto'];

if ($returnto != "")
header("Location: $returnto");
else
$warning = "Ankündigung erfolgreich editiert.";
}
else
{
$returnto = $_GET['returnto'];
stdhead();
begin_frame("Ankündigungen bearbeiten", true, "70%");
begin_table();
print("<form method=post action=?action=edit&ann_id=$ann_id>\n");
print("<table border=1 cellspacing=0 cellpadding=5>\n");
print("<tr><td><input type=hidden name=returnto value=$returnto></td></tr>\n");
print("<tr><td><b>Title &nbsp </b><input type=text size=100 maxlength=mega_shok.gif name=title value=".htmlspecialchars($arr['title'])."></td></tr>\n");
print("<tr><td style='padding: 0px'><textarea name=body cols=145 rows=5 style='border: 0px'>" . htmlspecialchars($arr["body"]) . "</textarea></td></tr>\n");
print("<tr><td align=center><input type=submit value='Edit' class=btn></td></tr>\n");
print("</table>\n");
print("</form>\n");
end_table();
end_frame();
stdfoot();
die;
}
}

// Other Actions and followup ////////////////////////////////////////////


// Set announce to YES for all users //////////////////////////////////////////////////////

if ($action == 'show_all')
{

if (get_user_class() < UC_ADMINISTRATOR)
die(Error);

$sure = $_GET["sure"];
if (!$sure)
stderr("Ankündigung für alle User Sichtbar ?","Diese Aktion setzt die Ankündigung für alle auf Ja. Klick\n" . "<a href=?action=show_all&returnto=$returnto&sure=1>hier</a> wenn du dir sicher bist.");
mysql_query("UPDATE users SET announce = 'yes' WHERE announce='no'");

if ($returnto != "")
header("Location: $returnto");
else
$warning = "Ankündigung ist für alle User auf Ja";
}

// Set announce to NO for all users //////////////////////////////////////////////////////

if ($action == 'hide_all')
{
if (get_user_class() < UC_ADMINISTRATOR)
die(Error);

$sure = $_GET["sure"];
if (!$sure)
stderr("Ankündigung für alle User Verbergen ?","Diese Aktion setzt die Ankündigung für alle auf Nein. Klick\n" .
"<a href=?action=hide_all&returnto=$returnto&sure=1>hier</a> wenn du dir sicher bist.");
mysql_query("UPDATE users SET announce = 'no' WHERE announce='yes'");

if ($returnto != "")
header("Location: $returnto");
else
$warning = "Ankündigung ist für alle User auf Nein";
}
////////////////////////////////////////////////////////////////////////////////////////////////


stdhead("Announcement");
if (get_user_class() > UC_ADMINISTRATOR){
begin_frame("Ankündigungen erstellen", true, "70%");
begin_table();
if ($warning)
print("<p><font size=-3>($warning)</font></p>");
print("<form method=post action=?action=add>\n");
print("<table border=1 cellspacing=0 cellpadding=5>\n");
print("<tr><td style='padding: 10px'><b>Title &nbsp </b><input type=text size=140 maxlength=mega_shok.gif name=title " . "style='border: 0px; height: 19px'></input>\n");
print("<tr><td style='padding: 10px'><textarea name=body cols=145 rows=5 style='border: 0px'></textarea>\n");
print("<br><br><div align=center><input type=submit value='Ankündigen' class=btn></div></td></tr>\n");
print("</table></form><br><br>\n");
end_frame();
}
$res = mysql_query("SELECT * FROM announcement ORDER BY added DESC") or sqlerr(__FILE__, __LINE__);


if (get_user_class() >= UC_ADMINISTRATOR){
print("[<a href=?action=show_all><b>Ankündigung für alle Sichtbar</b></a>]");
print(" - [<a href=?action=hide_all><b>Ankündigung für alle nicht Sichtbar</b></a>]");
}

print("<h1>.: Ankündigungen :.</h1>");

if (mysql_num_rows($res) > 0)
{


begin_main_frame();
begin_frame();





while ($arr = mysql_fetch_array($res))
{
$ann_id = $arr["id"];
$body = $arr["body"];
$title = $arr["title"];
$userid = $arr["userid"];
$added = $arr["added"] . " GMT (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($arr["added"]))) . " ago)";

$res2 = mysql_query("SELECT username, donor FROM users WHERE id = $userid") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_array($res2);

$postername = $arr2["username"];
print("<p class=sub><table border=0 cellspacing=0 cellpadding=0><tr><td class=embedded>");
print("$added&nbsp;");
if (get_user_class() >= UC_ADMINISTRATOR){
if ($postername == "")
$by = "unknown[$userid]";
else
$by = "<a href=userdetails.php?id=$userid><b>$postername</b></a>" .
($arr2["donor"] == "yes" ? "<img src=pic/star.gif alt='Donor'>" : "");
print(" ---&nbsp;by&nbsp$by");
print(" - [<a href=?action=edit&ann_id=$ann_id><b>Bearbeiten</b></a>]");
print(" - [<a href=?action=delete&ann_id=$ann_id><b>Löschen</b></a>]");

}


print("</td></tr></table></p>\n");
begin_table(true);
print("<td padding: 0px height=5 background=/pic/ann_bg.jpg><b><font size=1>$title</font></b></td>\n");
print("<tr valign=top><td class=comment>".format_comment($body)."</td></tr>\n");
end_table();
}
end_frame();
end_main_frame();
}
else
stdmsg("Sorry", "Keine Ankündigungen!");

mysql_query("UPDATE users SET announce = 'no' WHERE id=".$CURUSER['id']);
stdfoot();
die;
?>