<?

/*
// +--------------------------------------------------------------------------+
// | Project:    NVTracker - NetVision BitTorrent Tracker                     |
// +--------------------------------------------------------------------------+
// | This file is part of NVTracker. NVTracker is based on BTSource,          |
// | originally by RedBeard of TorrentBits, extensively modified by           |
// | Gartenzwerg.                                                             |
// |                                                                          |
// | 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! |
// +--------------------------------------------------------------------------+
 */

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

$action = $_GET["action"];

global $CURUSER;
$userid = $CURUSER["id"];
$res = mysql_query("SELECT id, lastpost FROM topics") or sqlerr(__FILE__, __LINE__);

while ($arr = mysql_fetch_assoc($res)) {
$topicid = $arr["id"];
$postid = $arr["lastpost"];

$r = mysql_query("SELECT id,lastpostread FROM readposts WHERE userid=$userid and topicid=$topicid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($r) == 0)
mysql_query("INSERT INTO readposts (userid, topicid, lastpostread) VALUES($userid, $topicid, $postid)") or sqlerr(__FILE__, __LINE__);

else {
$a = mysql_fetch_assoc($r);

if ($a["lastpostread"] < $postid)
mysql_query("UPDATE readposts SET lastpostread=$postid WHERE id=" . $a["id"]) or sqlerr(__FILE__, __LINE__);
 }
}

//-------- Returns the minimum read/write class levels of a forum

function get_forum_access_levels($forumid) {

$res = mysql_query("SELECT minclassread, minclasswrite, minclasscreate FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
return false;

$arr = mysql_fetch_assoc($res);
return array("read" => $arr["minclassread"], "write" => $arr["minclasswrite"], "create" => $arr["minclasscreate"]);
}

//-------- Returns the forum ID of a topic, or false on error

function get_topic_forum($topicid) {
$res = mysql_query("SELECT forumid FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
return false;

$arr = mysql_fetch_row($res);
return $arr[0];
}

//-------- Returns the ID of the last post of a forum

function update_topic_last_post($topicid) {
$res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res) or die("Es sind keine Postings vorhanden.");
$postid = $arr[0];

mysql_query("UPDATE topics SET lastpost=$postid WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
}

function get_forum_last_post($forumid) {
$res = mysql_query("SELECT lastpost FROM topics WHERE forumid=$forumid ORDER BY lastpost DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);
$postid = $arr[0];

if ($postid)
return $postid;

else
return 0;
}

//-------- Inserts a quick jump menu

function insert_quick_jump_menu($currentforum = 0) {

print("<br><center><form method=\"get\" action=\"?\" name=\"jump\">");
print("<input type=\"hidden\" name=\"action\" value=\"viewforum\">");
print("<select name=\"forumid\" onchange=\"if(this.options[this.selectedIndex].value != -1){ forms['jump'].submit() }\">");

$res = mysql_query("SELECT * FROM forums ORDER BY name") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res)) {

if (get_user_class() >= $arr["minclassread"])
print("<option value=" . $arr["id"] . ($currentforum == $arr["id"] ? " selected>" : ">") . $arr["name"] . "");
}

print("</select>");
print("&nbsp;<input type=\"submit\" value=\"Und ab!\">");
print("</form></center><br>");
}

//-------- Inserts a compose frame

function insert_compose_frame($id, $newtopic = true, $quote = false) {
global $maxsubjectlength, $CURUSER;

if ($newtopic) {
$res = mysql_query("SELECT name FROM forums WHERE id=$id") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or stderr("Forum Fehler", "<b><p>Falsche Foren ID.</p></b>");
$forumname = $arr["name"];

begin_frame("<span class=normalfont><b>Neues Thema im Forum: <a href=?action=viewforum&forumid=$id><i>\"$forumname\"</i></a> erstellen</b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"770\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"770\" class=\"tableinborder\">
<tr><td align=\"center\" colspan=\"2\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Thema erstellen</b></span></td></tr>");

} else {
$res = mysql_query("SELECT * FROM topics WHERE id=$id") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or stderr("Forum Fehler", "<b><p>Das Thema konnte nicht gefunden werden.</p></b>");
$subject = $arr["subject"];

begin_frame("<span class=normalfont><b>Zurück zum Thema: <a href=?action=viewtopic&topicid=$id>$subject</a></b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"770\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"770\" class=\"tableinborder\">
<tr><td align=\"center\" colspan=\"2\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Antwort erstellen</b></span></td></tr>");

}
print("<form action=\"?action=post\" method=\"post\" name=\"body\" enctype=\"multipart/form-data\">");

if ($newtopic)
print("<input type=\"hidden\" name=\"forumid\" value=\"$id\">");

else
print("<input type=\"hidden\" name=\"topicid\" value=\"$id\">");


if ($newtopic)
print("<tr><td class=\"tableb\"><b>Thema:</b></td><td class=\"inposttable\"><input type=\"text\" size=\"110\" maxlength=\"$maxsubjectlength\" name=\"subject\" border=\"0\" height=\"15\"></td></tr>");

if ($quote) {
$postid = $_GET["postid"];
if (!is_valid_id($postid))
die;

$res = mysql_query("SELECT posts.*, users.username FROM posts JOIN users ON posts.userid = users.id WHERE posts.id=$postid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
stderr(" Forum Fehler", "<b><p>Es ist kein Thema mit der ID $postid vorhanden.</p></b>");

$arr = mysql_fetch_assoc($res);
}

$body[text] = ($quote?(("[quote=".htmlspecialchars($arr["username"])."]".htmlspecialchars($arr["body"])."[/quote]")):"");
?>
<tr><td class="tableb"><b>Text:</b></td><td align="left" class="tablea" input type="text" style="padding: 0px"><? textbbcode("body","body","$body[text]")?></td></tr>
<?
print("<tr><td align=\"center\" colspan=\"2\" class=\"tableb\"><input type=\"submit\" class=\"btn\" value=\"Und ab!\"></td></tr>");
print("</form>");
print("</table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"770\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");


  //------ Get 10 last posts if this is a reply

if (!$newtopic) {
$postres = mysql_query("SELECT * FROM posts WHERE topicid=$id ORDER BY id DESC LIMIT 10") or sqlerr(__FILE__, __LINE__);

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");


print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td align=\"center\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Die 10 letzten Posts in umgekehrter Reihenfolge</b></span></td></tr>
<tr><td class=\"tablea\">");

while ($post = mysql_fetch_assoc($postres)) {

  //-- Get poster details

$userres = mysql_query("SELECT * FROM users WHERE id=" . $post["userid"] . " LIMIT 1") or sqlerr(__FILE__, __LINE__);
$user = mysql_fetch_assoc($userres);

if ($user["avatar"]) {
$avatar = ("<img style=\"max-width:100px;\" src=\"".htmlspecialchars($user["avatar"])."\">");

} else {
$avatar = ("<img style=\"max-width:100px;\" src=\"".$GLOBALS["PIC_BASE_URL"]."default_avatar.gif\">");
}

print("<br><table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"700\" class=\"tableinborder\">
<tr><td colspan=\"2\" width=\"100%\" class=\"tablecat\"><span class=\"normalfont\"><b>Vom:</b> " . $post["added"] . "</span></td></tr>");

print("<tr><td align=\"center\" valign=\"top\" class=\"tableb\" width=\"100\">");
print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\" width=\"100\">
<tr><td align=\"center\" valign=\"top\" class=\"tabled\" width=\"100\">$avatar</td></tr>");
print("<tr><td class=\"tabled\" width=\"100\">
<font class=\"".get_class_color($user["class"])."\"><b>".$user["username"]."</b></font>
</td></tr></table>");

print("</td><td class=\"tablea\" valign=\"top\" width=\"600\">" . format_comment($post["body"]) . "</td></tr>");
print("</table>");
}

print("<br></td></tr></table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");
}

insert_quick_jump_menu();

end_frame();
}

  //-------- Global variables

$maxsubjectlength = 40;
$postsperpage = $CURUSER["postsperpage"];
if (!$postsperpage) $postsperpage = 25;

  //-------- Action: New topic

if ($action == "newtopic") {
$forumid = $_GET["forumid"];

if (!is_valid_id($forumid))
die;

stdhead("Tracker Forum");

insert_compose_frame($forumid);

end_frame();
stdfoot();
die;
}

  //-------- Action: Post

if ($action == "post") {
$forumid = 0 + $_POST["forumid"];
$topicid = 0 + $_POST["topicid"];

if (!is_valid_id($forumid) && !is_valid_id($topicid))
   stderr("Forum Fehler", "<b><p>Falsche Foren oder Themen ID.</p></b>");

$newtopic = $forumid > 0;
$subject = $_POST["subject"];

if ($newtopic) {
$subject = trim($subject);

if (!$subject)
   stderr("Forum Fehler", "<b><p>Du musst eine Überschrift eingeben.</p></b>");

if (strlen($subject) > $maxsubjectlength)
    stderr("Forum Fehler", "<b><p>Die Überschrift hat die maxlänge von $maxsubjectlength Buchstaben erreicht.</p></b>");
}

else
$forumid = get_topic_forum($topicid) or stderr("Forum Fehler", "<b><p>Die Themen ID ist falsch.</p></b>");

  //------ Make sure sure user has write access in forum

$arr = get_forum_access_levels($forumid) or stderr("Forum Fehler", "<b><p>Die Themen ID ist falsch.</p></b>");

if (get_user_class() < $arr["write"] || ($newtopic && get_user_class() < $arr["create"]))
   stderr("Forum Fehler", "<b><p>Dir fehlen die Rechte um hier etwas schreiben zu können.</p></b>");

$body = trim($_POST["body"]);

if ($body == "")
   stderr("Forum Fehler", "<b><p>Der Inhalt darf nicht leer sein.</p></b>");

$userid = $CURUSER["id"];

if ($newtopic) {

  //---- Create topic

$subject = sqlesc($subject);

mysql_query("INSERT INTO topics (userid, forumid, subject) VALUES($userid, $forumid, $subject)") or sqlerr(__FILE__, __LINE__);

$topicid = mysql_insert_id() or stderr("Forum Fehler", "<b><p>Die Themen ID konnte nicht gefunden werden.</p></b>");

  //------ Insert post

$added = "'" . get_date_time() . "'";
$body = sqlesc($body);

mysql_query("INSERT INTO posts (topicid, userid, added, body) VALUES($topicid, $userid, $added, $body)") or sqlerr(__FILE__, __LINE__);

$postid = mysql_insert_id() or stderr("Forum Fehler", "<b><p>Kommentar ID nicht gefunden!</p></b>");

mysql_query("UPDATE forums SET postcount = postcount + 1 WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE forums SET topiccount = topiccount + 1 WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
} else {

  //---- Make sure topic exists and is unlocked

$res = mysql_query("SELECT * FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or stderr("Forum Fehler", "<b><p>Die Themen ID konnte nicht gefunden werden.</p></b>");

if ($arr["locked"] == 'yes' && get_user_class() < UC_MODERATOR)
   stderr("Forum Fehler", "<b><p>Dieses Thema ist geschlossen.</p></b>");

  //---- Get forum ID

$forumid = $arr["forumid"];

  //------ Insert post

$added = "'" . get_date_time() . "'";
$body = sqlesc($body);

mysql_query("INSERT INTO posts (topicid, userid, added, body) VALUES($topicid, $userid, $added, $body)") or sqlerr(__FILE__, __LINE__);

$postid = mysql_insert_id() or stderr("Forum Fehler", "<b><p>Kommentar ID nicht gefunden!</p></b>");

mysql_query("UPDATE forums SET postcount = postcount + 1 WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
}

  //------ Update topic last post

update_topic_last_post($topicid);

  //------ All done, redirect user to the post

$headerstr = "Location: $BASEURL/forums.php?action=viewtopic&topicid=$topicid&page=last";

if ($newtopic)
header($headerstr);

else
header("$headerstr#$postid");
die;
}

  //-------- Action: View topic

if ($action == "viewtopic") {
$topicid = $_GET["topicid"];
$page = $_GET["page"];

if (!is_valid_id($topicid))
die;

$userid = $CURUSER["id"];

  //------ Get topic info

$res = mysql_query("SELECT * FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or stderr("Forum Fehler", "<b><p>Das Thema konnte nicht gefunden werden.</p></b>");

$locked = ($arr["locked"] == 'yes');
$subject = $arr["subject"];
$sticky = $arr["sticky"] == "yes";
$forumid = $arr["forumid"];

  //------ Update hits column

mysql_query("UPDATE topics SET views = views + 1 WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);

  //------ Get forum

$res = mysql_query("SELECT * FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or die("Forum = NULL");

$forum = $arr["name"];

if ($CURUSER["class"] < $arr["minclassread"])
   stderr("Forum Fehler", "<b><p>Du hast keine Leserechte in diesem Forum.</p></b>");

  //------ Get post count

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);

$postcount = $arr[0];

  //------ Make page menu

$pagemenu = "<p>\n";
$perpage = $postsperpage;
$pages = ceil($postcount / $perpage);

if ($page[0] == "p") {
$findpost = substr($page, 1);
$res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid ORDER BY added") or sqlerr(__FILE__, __LINE__);
$i = 1;

while ($arr = mysql_fetch_row($res)) {
if ($arr[0] == $findpost)
break;
++$i;
}
$page = ceil($i / $perpage);
}

if ($page == "last")
$page = $pages;

else {
if($page < 1)
$page = 1;

elseif ($page > $pages)
$page = $pages;
}

$offset = $page * $perpage - $perpage;

for ($i = 1; $i <= $pages; ++$i) {
if ($i == $page)
$pagemenu .= "<center><font class=gray><b>$i</b></font><center>";

else
$pagemenu .= "<a href=?action=viewtopic&topicid=$topicid&page=$i><b>$i</b></a>";
}

if ($page == 1)
$pagemenu .= "<br><font class=gray><b>&lt;&lt; Zurück</b></font>";

else
$pagemenu .= "<br><a href=?action=viewtopic&topicid=$topicid&page=".($page - 1)."><b>&lt;&lt; Zurück</b></a>";
$pagemenu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if ($page == $pages)
$pagemenu .= "<font class=gray><b>Nächste &gt;&gt;</b></font></p>\n";

else
$pagemenu .= "<a href=?action=viewtopic&topicid=$topicid&page=".($page + 1)."><b>Nächste &gt;&gt;</b></a></p>";

  //------ Get posts

$res = mysql_query("SELECT * FROM posts WHERE topicid=$topicid ORDER BY id LIMIT $offset,$perpage") or sqlerr(__FILE__, __LINE__);

stdhead("Tracker Forum");
begin_frame("<span class=normalfont><b>Tracker Forum</b></span>");


  //------ Print table

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<a name=top><table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td width=\"100%\" class=\"tabletitle\"><span class=\"smalfont\"><a href=\"forums.php\"><b>Tracker Forum</b></a> - <a href=\"forums.php?action=viewforum&forumid=$forumid\"><b>$forum</b></a> - <b><i>\"$subject\"</i></b></span></td></tr>
<tr><td class=\"tablea\"><br>");

$pc = mysql_num_rows($res);
$pn = 0;
$r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=" . $CURUSER["id"] . " AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_row($r);
$lpr = $a[0];

if (!$lpr)
mysql_query("INSERT INTO readposts (userid, topicid) VALUES($userid, $topicid)") or sqlerr(__FILE__, __LINE__);

if ($locked && get_user_class() < UC_MODERATOR){
$access = ("&nbsp;<input type=\"submit\" value=\"Thema geschlossen\" class=\"btn\">");

} else {
$arr = get_forum_access_levels($forumid);
if (get_user_class() < $arr["write"]) {
$access = ("&nbsp;<input type=\"submit\" value=\"Keine Schreibrechte\" class=\"btn\">");

} else {
$maypost = true;
$access = ("&nbsp;<a href=?action=reply&topicid=$topicid><input type=\"submit\" value=\"Antworten\" class=\"btn\"></a>");
 }
}

while ($arr = mysql_fetch_assoc($res)) {
++$pn;
$postid = $arr["id"];
$posterid = $arr["userid"];
$added = $arr["added"];
$body = $arr["body"];

  //---- Get poster details

$res2 = mysql_query("SELECT * FROM users WHERE id=$posterid") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);
$anon = $arr2[anon];

$postername = ("<font class=\"".get_class_color($arr2['class'])."\">$arr2[username]</font>");

if ($postername == "") {
$by = "Gelöscht";

} else {
if ($anon && (($posterid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$by = "<a href=userdetails.php?id=$posterid><b>$postername</b></a>" . ($arr2["donor"] == "yes" ? " <img src=\"".
$GLOBALS["PIC_BASE_URL"]."star.gif\" title=\"Dieses Mitglied hat gespendet\">" : "") . ($arr2["enabled"] == "no" ? "<img src=\"".
$GLOBALS["PIC_BASE_URL"]."disabled.gif\" title=\"Dieses Mitglied wurde gesperrt\" style='margin-left: 2px'>" : ($arr2["warned"] == "yes" ? "<a href=rules.php#warning class=altlink><img src=\"".$GLOBALS["PIC_BASE_URL"]."warned.gif\" title=\"Dieses Mitglied wurde verwarnd\" border=\"0\"></a>" : "")) . "";

} else {
$by = "<b>$postername</b>";
 }
}

print("<a name=$postid>");

if ($pn == $pc) {
print("<a name=last>");
if ($postid > $lpr)
mysql_query("UPDATE readposts SET lastpostread=$postid WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
}

if ($arr2["avatar"]) {
$avatar = ("<img style=\"max-width:120px;\" src=\"".htmlspecialchars($arr2["avatar"])."\">");

} else {
$avatar = ("<img style=\"max-width:120px;\" src=\"".$GLOBALS["PIC_BASE_URL"]."default_avatar.gif\">");
}

$res3 = mysql_query("SELECT COUNT(*) FROM posts WHERE userid=$posterid") or sqlerr(__FILE__, __LINE__);
$arr3 = mysql_fetch_row($res3);
$forumposts = $arr3[0];


print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"770\" class=\"tableinborder\">
<tr><td align=\"center\" class=\"tablecat\"><form method=\"post\" action=\"report.php?postid=$postid\"><input type=\"submit\" value=\"Melden\"></form></td><td align=\"right\" class=\"tablecat\">");

print("$access");

if (!$locked && $maypost) {
print("&nbsp;<a href=?action=quotepost&topicid=$topicid&postid=$postid><input type=\"submit\" value=\"Zitieren\" class=\"btn\"></a>");
}
if (($CURUSER["id"] == $posterid && !$locked) || get_user_class() >= UC_MODERATOR) {
print("&nbsp;<a href=?action=editpost&postid=$postid><input type=\"submit\" value=\"Bearbeiten\" class=\"btn\"></a>");
}
if (get_user_class() >= UC_MODERATOR) {
print("&nbsp;<a href=?action=deletepost&postid=$postid&forumid=$forumid><input type=\"submit\" value=\"Löschen\" class=\"btn\"></a>");
}
print("&nbsp;<a href=\"#top\"><img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."top.gif\" border=0 alt='Top'></a></td></tr>");

print("<tr><td align=\"center\" valign=\"top\" class=\"tableb\" width=\"120\">");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\" width=\"100\">
<tr><td align=\"center\" valign=\"top\" class=\"tabled\" width=\"120\">$avatar</td></tr>");
print("<tr><td class=\"tabled\" width=\"120\">
$by
<br>");

if ($forumposts && (($posterid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
print("Beiträge: <a href=\"userhistory.php?action=viewposts&id=$posterid\">$forumposts</a>");

} else {
print("Beiträge: $forumposts");
}
print("</td></tr></table>");

$body = "<div style=\"width:100%;min-height:100px;overflow:auto;\">" . format_comment($arr["body"]) . "</div>";
if (is_valid_id($arr['editedby'])) {
$res4 = mysql_query("SELECT id, username, class, anon FROM users WHERE id=$arr[editedby]");
if (mysql_num_rows($res4) == 1) {
$arr4 = mysql_fetch_assoc($res4);
if ($arr4[anon] == no  || (($arr4[id] == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$body .= "<p><font class=\"smallfont\">Zuletzt bearbeitet von <a href=\"userdetails.php?id=$arr[editedby]\"><b><font class=\"".get_class_color($arr4["class"])."\">$arr4[username]</font></b></a> am $arr[editedat]</font>.</p>";

} else {
$body .= "<p><font class=\"smallfont\">Zuletzt bearbeitet von <b><font class=\"".get_class_color($arr4["class"])."\">$arr4[username]</font></b> am $arr[editedat]</font>.</p>";
  }
 }
}

print("</td><td class=\"tablea\" valign=\"top\" width=\"650\">$body</td></tr>");
print("<tr><td align=\"center\" class=\"tableb\">");
print("<span class=\"smalfont\">$added</span>");
print("</td><td class=\"tableb\">");

if (($CURUSER[id]) != $posterid || get_user_class() >= UC_MODERATOR) {
print("&nbsp;<a href=messages.php?action=send&receiver=$posterid><input type=\"submit\" value=\"PN\" class=\"btn\"></a>");
print("&nbsp;<form method=\"get\" action=\"friends.php\" style=\"display:inline\"><input type=\"hidden\" name=\"action\" value=\"add\"><input type=\"hidden\" name=\"type\" value=\"friend\"><input type=\"hidden\" name=\"targetid\" value=\"$posterid\"><input type=\"submit\" value=\"Freund\"></form>");
print("&nbsp;<form method=\"get\" action=\"friends.php\" style=\"display:inline\"><input type=\"hidden\" name=\"action\" value=\"add\"><input type=\"hidden\" name=\"type\" value=\"block\"><input type=\"hidden\" name=\"targetid\" value=\"$posterid\"><input type=\"submit\" value=\"Blocken\"</form>&nbsp;");
}
if ($arr2[anon] == no  || (($posterid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
if ($arr2["icq"]) {
print("&nbsp;<img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."/messenger/icq.gif\" alt=\"icq\" title=\"icq\" border=\"0\"> ".htmlspecialchars($arr2["icq"])."");
}
if ($arr2["msn"]) {
print("&nbsp;<img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."/messenger/msn.gif\" alt=\"msn\" title=\"msn\" border=\"0\"> ".htmlspecialchars($arr2["msn"])."");
}
if ($arr2["aim"]) {
print("&nbsp;<img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."/messenger/aim.gif\" alt=\"aim\" title=\"aim\" border=\"0\"> ".htmlspecialchars($arr2["aim"])."");
}
if ($arr2["yahoo"]) {
print("&nbsp;<img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."/messenger/yahoo.gif\" alt=\"yahoo\" title=\"yahoo\" border=\"0\"> ".htmlspecialchars($arr2["yahoo"])."");
}
if ($arr2["skype"]) {
print("&nbsp;<img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."/messenger/skype.gif\" alt-\"skype\" title=\"skype\" border=\"0\"> ".htmlspecialchars($arr2["skype"])."");
 }
}

print("</td></tr></table><br>");
}

print("</td></tr></table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table>");

print($pagemenu);

  //------ Forum quick jump drop-down

insert_quick_jump_menu();

  //------ Mod options

if (get_user_class() >= UC_MODERATOR) {
$res = mysql_query("SELECT id, name, minclasswrite FROM forums ORDER BY name") or sqlerr(__FILE__, __LINE__);

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"700\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"700\" class=\"tableinborder\">
<tr><td align=\"center\" colspan=\"2\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Mod-Panel</b></span></td></tr>");

print("<form method=\"post\" action=\"?action=setsticky\">");
print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">");
print("<input type=\"hidden\" name=\"returnto\" value=\"$BASEURL$_SERVER[REQUEST_URI]\">");
print("<tr><td class=\"tableb\" align=\"right\"><b>Sticky:</b>&nbsp;</td>");
print("<td class=\"tablea\"><input type=\"radio\" name=\"sticky\" value=\"yes\" ".($sticky ? " checked" : "")."> Ja <input type=\"radio\" name=\"sticky\" value=\"no\" ".(!$sticky ? " checked" : "")."> Nein&nbsp;");
print("<input type=\"submit\" value=\"Und ab!\"></td></tr>");
print("</form>");

print("<form method=\"post\" action=\"?action=setlocked\">");
print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">");
print("<input type=\"hidden\" name=\"returnto\" value=\"$BASEURL$_SERVER[REQUEST_URI]\">");
print("<tr><td class=\"tableb\" align=\"right\"><b>Schliessen:</b>&nbsp;</td>");
print("<td class=\"tablea\"><input type=\"radio\" name=\"locked\" value=\"yes\" ".($locked ? " checked" : "")."> Ja <input type=\"radio\" name=\"locked\" value=\"no\" ".(!$locked ? " checked" : "")."> Nein&nbsp;");
print("<input type=\"submit\" value=\"Und ab!\"></td></tr>");
print("</form>");

print("<form method=\"post\" action=\"?action=renametopic\">");
print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">");
print("<input type=\"hidden\" name=\"returnto\" value=\"$BASEURL$_SERVER[REQUEST_URI]\">");
print("<tr><td class=\"tableb\" align=\"right\"><b>Titel Editieren:</b>&nbsp;</td>");
print("<td class=\"tablea\"><input type=\"text\" name=\"subject\" size=\"60\" maxlength=\"$maxsubjectlength\" value=\"".htmlspecialchars($subject)."\">&nbsp;");
print("<input type=\"submit\" value=\"Und ab!\"></td></tr>");
print("</form>");

print("<form method=\"post\" action=\"?action=movetopic&topicid=$topicid&\">");
print("<input type=\"hidden\" name=\"forumid2\" value=\"$forumid\">");
print("<tr><td align=\"right\" class=\"tableb\"><b>Verschieben nach:</b>&nbsp;</td>");
print("<td class=\"tablea\"><select name=\"forumid1\">");

while ($arr = mysql_fetch_assoc($res))
if ($arr["id"] != $forumid && get_user_class() >= $arr["minclasswrite"])
print("<option value=\"".$arr["id"]."\">".$arr["name"]."");

print("</select> <input type=\"submit\" value=\"Und ab!\"></td></tr>");
print("</form>");

print("<form method=\"get\" action=\"forums.php\">");
print("<input type=\"hidden\" name=\"action\" value=\"deletetopic\">");
print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">");
print("<input type=\"hidden\" name=\"forumid\" value=\"$forumid\">");
print("<tr><td align=\"right\" class=\"tableb\"><b>Thread löschen:</b>&nbsp;");
print("</td><td class=\"tablea\"><input type=\"checkbox\" name=\"sure\" value=\"2\" style=\"vertical-align: middle;\">&nbsp;");
print("<input type=\"submit\" value=\"Und ab!\"></td></tr>");
print("</form>");
print("</table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"700\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");
}

end_frame();
stdfoot();
die;
}

  //-------- Action: Quote

if ($action == "quotepost") {
$topicid = $_GET["topicid"];

if (!is_valid_id($topicid))
   stderr("Forum Fehler", "<b><p>Falsche Themen ID $topicid.</p></b>");

stdhead("Tracker Forum");

insert_compose_frame($topicid, false, true);

stdfoot();
die;
}

  //-------- Action: Reply

if ($action == "reply") {
$topicid = $_GET["topicid"];

if (!is_valid_id($topicid))
die;

stdhead("Tracker Forum");
insert_compose_frame($topicid, false);

stdfoot();
die;
}

  //-------- Action: Move topic

if ($action == "movetopic") {
$forumid1 = $_POST["forumid1"];
$topicid = $_GET["topicid"];
$forumid2 = $_POST["forumid2"];

if (!is_valid_id($forumid1) || !is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
die;

  // Make sure topic and forum is valid

$res = @mysql_query("SELECT minclasswrite FROM forums WHERE id=$forumid1") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
   stderr("Forum Fehler", "<b><p>Das Forum konnte nicht gefunden werden.</p></b>");

$arr = mysql_fetch_row($res);

if (get_user_class() < $arr[0])
die;

$res = @mysql_query("SELECT subject,forumid FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) != 1)
   stderr("Forum Fehler", "<b><p>Das Thema konnte nicht gefunden werden.</p></b>");

$arr = mysql_fetch_assoc($res);

if ($arr["forumid"] != $forumid1)
@mysql_query("UPDATE topics SET forumid=$forumid1 WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);

$postcount = $arr[0];

 mysql_query("UPDATE forums SET postcount = postcount + $postcount WHERE id=$forumid1") or sqlerr(__FILE__, __LINE__);
 mysql_query("UPDATE forums SET topiccount = topiccount + 1 WHERE id=$forumid1") or sqlerr(__FILE__, __LINE__);
 mysql_query("UPDATE forums SET postcount = postcount - $postcount WHERE id=$forumid2") or sqlerr(__FILE__, __LINE__);
 mysql_query("UPDATE forums SET topiccount = topiccount - 1 WHERE id=$forumid2") or sqlerr(__FILE__, __LINE__);

  // Redirect to forum page

header("Location: $BASEURL/forums.php?action=viewforum&forumid=$forumid1");
die;
}

  //-------- Action: Delete topic

if ($action == "deletetopic") {
$topicid = $_GET["topicid"];
$forumid = $_GET["forumid"];

if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
die;

$sure = $_GET["sure"];

if ($sure == "") {
    stderr("Thema löschen", "<b><p>Du hast kein Thema zum löschen ausgewählt!</p></b>");
}

if ($sure == "2") {
    stderr("Thema löschen", "<b><p>Du bist im Begriff dieses Thema zu löschen?  Klicke <a href=?action=deletetopic&topicid=$topicid&forumid=$forumid&sure=1>HIER</a> wenn du dir sicher bist!</p></b>");
}

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);

$postcount = $arr[0];

mysql_query("DELETE FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
mysql_query("DELETE FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
mysql_query("DELETE FROM readposts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE forums SET postcount = postcount - $postcount WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE forums SET topiccount = topiccount - 1 WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);

header("Location: $BASEURL/forums.php?action=viewforum&forumid=$forumid");
die;
}

  //-------- Action: Edit post

if ($action == "editpost") {
$postid = $_GET["postid"];

if (!is_valid_id($postid))
die;

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

if (mysql_num_rows($res) != 1)
   stderr("Forum Fehler", "<b><p>Es ist kein Beitrag mit der ID $postid vorhanden.</p></b>");

$arr = mysql_fetch_assoc($res);
$res2 = mysql_query("SELECT locked, subject FROM topics WHERE id = " . $arr["topicid"]) or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);

if (mysql_num_rows($res) != 1)
   stderr("Forum Fehler", "<b><p>Es ist kein Thema mit der ID $postid vorhanden.</p></b>");

$locked = ($arr2["locked"] == 'yes');

if (($CURUSER["id"] != $arr["userid"] || $locked) && get_user_class() < UC_MODERATOR)
    stderr("Forum Fehler", "<b><p>Du hast keine Berechtigung um diesen Beitrag zu ändern!</p></b>");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$subject = $_POST['subject'];

if ($subject == "")
    stderr("Forum Fehler", "<b><p>Das Thema darf nicht leer sein!</p></b>");

$subject = sqlesc($subject);

mysql_query("UPDATE topics SET subject=$subject WHERE id = " . $arr["topicid"]) or sqlerr(__FILE__, __LINE__);


$body = $_POST['body'];

if ($body == "")
    stderr("Forum Fehler", "<b><p>Der Inhalt darf nicht leer sein!</p></b>");

$body = sqlesc($body);
$editedat = sqlesc(get_date_time());

mysql_query("UPDATE posts SET body=$body, editedat=$editedat, editedby=$CURUSER[id] WHERE id=$postid") or sqlerr(__FILE__, __LINE__);

$returnto = $_POST["returnto"];

if ($returnto != "") {
$returnto .= "&page=p$postid#$postid";
header("Location: $returnto");

} else
stderr("Beitrag bearbeiten", "<b><p>Der Beitrag wurde erfolgreich bearbeitet!</p></b>");
}

stdhead("Tracker Forum");
begin_frame("<span class=normalfont><b>Beitrag bearbeiten</b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"770\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"770\" class=\"tableinborder\">
<tr><td align=\"center\" colspan=\"2\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Posting aus dem Tread <i>\"".$arr2[subject]."\"</i> bearbeiten</b></span></td></tr>");

print("<form action=\"?action=editpost&postid=$postid\" method=\"post\" name=\"body\" enctype=\"multipart/form-data\">");
print("<input type=hidden name=returnto value=\"" . htmlspecialchars($_SERVER["HTTP_REFERER"]) . "\">");
?>
<tr><td class="tableb"><b>Thema:</b></td><td class="tablea"><input type="text" name="subject" size="100" maxlength="255" value="<?=htmlspecialchars($arr2["subject"])?>"></td></tr>
<tr><td class="tableb"><b>Text:</b></td><td class="tablea" align="left" input type="text" style="padding: 0px"><? textbbcode("body","body","$arr[body]")?></td></tr>
<?
print("<tr><td align=\"center\" colspan=\"2\" class=\"tableb\"><input type=\"submit\" value=\"Und ab!\" class=\"btn\"></td></tr>");
print("</table></form>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"770\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

end_frame();
stdfoot();
die;
}

  //-------- Action: Delete post

if ($action == "deletepost") {
$forumid = $_GET["forumid"];
$postid = $_GET["postid"];
$sure = $_GET["sure"];

if (get_user_class() < UC_MODERATOR || !is_valid_id($postid))
die;

  //------- Get topic id

$res = mysql_query("SELECT topicid FROM posts WHERE id=$postid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res) or stderr("Forum Fehler", "<b><p>Der Beitrag wurde nicht gefunden!</p></b>");

$topicid = $arr[0];

  //------- We can not delete the post if it is the only one of the topic

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);

if ($arr[0] < 2)
   stderr("Forum Fehler", "<b><p>Der Beitrag kann nicht gelöscht werden, es ist der erste Beitrag dieses Themas. Klicke <a href=?action=deletetopic&topicid=$topicid&forumid=$forumid&sure=1>HIER</a> um das Thema zu löschen.</p></b>");

  //------- Get the id of the last post before the one we're deleting

$res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid AND id < $postid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) == 0)
$redirtopost = "";

else {
$arr = mysql_fetch_row($res);
$redirtopost = "&page=p$arr[0]#$arr[0]";
}

  //------- Make sure we know what we do :-)

if (!$sure) {
   stderr("Beitrag löschen", "<b><p>Bist du dir sicher das du diesen Beitrag löschen willst? Klicke <a href=?action=deletepost&postid=$postid&forumid=$forumid&sure=1>HIER</a> wenn du dir sicher bist.</p></b>");
}

  //------- Delete post

$forumid = $_GET["forumid"];
mysql_query("DELETE FROM posts WHERE id=$postid") or sqlerr(__FILE__, __LINE__);
mysql_query("DELETE FROM readposts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE forums SET postcount = postcount - 1 WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);

  //------- Update topic

update_topic_last_post($topicid);
header("Location: $BASEURL/forums.php?action=viewtopic&topicid=$topicid$redirtopost");
die;
}

  //-------- Action: Lock topic

if ($action == "locktopic") {
$forumid = $_GET["forumid"];
$topicid = $_GET["topicid"];
$page = $_GET["page"];

if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
die;

mysql_query("UPDATE topics SET locked='yes' WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
header("Location: $BASEURL/forums.php?action=viewforum&forumid=$forumid&page=$page");
die;
}

  //-------- Action: Unlock topic

if ($action == "unlocktopic") {
$forumid = $_GET["forumid"];
$topicid = $_GET["topicid"];
$page = $_GET["page"];

if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
die;

mysql_query("UPDATE topics SET locked='no' WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
header("Location: $BASEURL/forums.php?action=viewforum&forumid=$forumid&page=$page");
die;
}

  //-------- Action: Set locked on/off

if ($action == "setlocked") {
$topicid = 0 + $_POST["topicid"];

if (!$topicid || get_user_class() < UC_MODERATOR)
die;

$locked = sqlesc($_POST["locked"]);
mysql_query("UPDATE topics SET locked=$locked WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
header("Location: $_POST[returnto]");
die;
}

  //-------- Action: Set sticky on/off

if ($action == "setsticky") {
$topicid = 0 + $_POST["topicid"];

if (!topicid || get_user_class() < UC_MODERATOR)
die;

$sticky = sqlesc($_POST["sticky"]);
mysql_query("UPDATE topics SET sticky=$sticky WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
header("Location: $_POST[returnto]");
die;
}

  //-------- Action: Rename topic

if ($action == 'renametopic') {

if (get_user_class() < UC_MODERATOR)
die;

$topicid = $_POST['topicid'];

if (!is_valid_id($topicid))
die;

$subject = $_POST['subject'];

if ($subject == '')
   stderr("Forum Fehler", "<b><p>Du musst einen neuen Titel eintragen!</p></b>");

$subject = sqlesc($subject);
mysql_query("UPDATE topics SET subject=$subject WHERE id=$topicid") or sqlerr();
$returnto = $_POST['returnto'];

if ($returnto)
header("Location: $returnto");
die;
}

  //-------- Action: View forum

if ($action == "viewforum") {
$forumid = $_GET["forumid"];

if (!is_valid_id($forumid))
die;

$page = $_GET["page"];
$userid = $CURUSER["id"];

  //------ Get forum name

$res = mysql_query("SELECT name, minclassread FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or die;
$forumname = $arr["name"];

if (get_user_class() < $arr["minclassread"])
die("Kein Zugriff");

  //------ Page links

  //------ Get topic count

$perpage = $CURUSER["topicsperpage"];

if (!$perpage) $perpage = 20;
$res = mysql_query("SELECT COUNT(*) FROM topics WHERE forumid=$forumid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);
$num = $arr[0];

if ($page == 0)
$page = 1;
$first = ($page * $perpage) - $perpage + 1;
$last = $first + $perpage - 1;

if ($last > $num)
$last = $num;
$pages = floor($num / $perpage);

if ($perpage * $pages < $num)
++$pages;

  //------ Build menu

$menu = "<center><b>\n";
$lastspace = false;

for ($i = 1; $i <= $pages; ++$i) {

if ($i == $page)
$menu .= "<font class=gray>$i</font>\n";

elseif ($i > 3 && ($i < $pages - 2) && ($page - $i > 3 || $i - $page > 3)) {

if ($lastspace)
continue;
$menu .= "... \n";
$lastspace = true;

} else {
$menu .= "<center><a href=?action=viewforum&forumid=$forumid&page=$i>$i</a></center>\n";
$lastspace = false;
}

if ($i < $pages)
$menu .= "</b>|<b>\n";
}

$menu .= "<br>\n";

if ($page == 1)
$menu .= "<font class=gray>&lt;&lt; Zurück</font>";

else
$menu .= "<a href=?action=viewforum&forumid=$forumid&page=" . ($page - 1) . ">&lt;&lt; Zurück</a>";
$menu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if ($last == $num)
$menu .= "<font class=gray>Nächste &gt;&gt;</font>";

else
$menu .= "<a href=?action=viewforum&forumid=$forumid&page=" . ($page + 1) . ">Nächste &gt;&gt;</a>";
$menu .= "</b></center>";
$offset = $first - 1;

  //------ Get topics data

$topicsres = mysql_query("SELECT * FROM topics WHERE forumid=$forumid ORDER BY sticky, lastpost DESC LIMIT $offset,$perpage") or stderr("SQL Fehler", mysql_error());

stdhead("Tracker Forum");
$numtopics = mysql_num_rows($topicsres);
begin_frame("<span class=normalfont><b>Tracker Forum</b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td width=\"100%\" class=\"tabletitle\"><span class=\"smalfont\"><a href=\"forums.php\"><b>Tracker Forum</b></a> - <b><i>\"$forumname\"</i></b></span></td></tr>
<tr><td class=\"tablea\">");

if ($numtopics > 0) {
print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"tableinborder\">");
print("<tr><td class=\"tablecat\" width=\"40%\"><b>Themen</b></td><td align=\"center\" class=\"tablecat\" width=\"10%\"><b>Antworten</b></td><td align=\"center\" class=\"tablecat\" width=\"10%\"><b>Klicks</b></td><td class=\"tablecat\" width=\"15%\"><b>Themenstarter</b></td><td class=\"tablecat\" width=\"25%\"><b>Letzte Antwort</b></td></tr>");

while ($topicarr = mysql_fetch_assoc($topicsres)) {
$topicid = $topicarr["id"];
$topic_userid = $topicarr["userid"];
$topic_views = $topicarr["views"];
$views = number_format($topic_views);
$locked = $topicarr["locked"] == "yes";
$sticky = $topicarr["sticky"] == "yes";

  //---- Get reply count

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);
$posts = $arr[0];
$replies = max(0, $posts - 1);
$tpages = floor($posts / $postsperpage);

if ($tpages * $postsperpage != $posts)
++$tpages;

if ($tpages > 1) {
$topicpages = " (<img src=\"".$GLOBALS["PIC_BASE_URL"]."multipage.gif\">";

for ($i = 1; $i <= $tpages; ++$i)
$topicpages .= " <a href=?action=viewtopic&topicid=$topicid&page=$i>$i</a>";
$topicpages .= ")";
}

else
$topicpages = "";

  //---- Get userID and date of last post

$res = mysql_query("SELECT * FROM posts WHERE topicid=$topicid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res);
$lppostid = 0 + $arr["id"];
$lpuserid = 0 + $arr["userid"];
$lpadded = "<nobr>" . $arr["added"] . "</nobr>";

  //------ Get name of last poster

$res1 = mysql_query("SELECT * FROM users WHERE id=$lpuserid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res) == 1) {
$arr1 = mysql_fetch_assoc($res1);

if ($arr1[anon] == no  || (($lpuserid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$lpusername = ("<a href=userdetails.php?id=$lpuserid><font class=".get_class_color($arr1["class"])."><b>$arr1[username]</b></font></a>");

} else {
$lpusername = ("<font class=".get_class_color($arr1["class"])."><b>$arr1[username]</b></font>");
 }
}

else
$lpusername = "Gelöscht";

  //------ Get author
$res2 = mysql_query("SELECT username, class, anon FROM users WHERE id=$topic_userid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($res2) == 1) {
$arr2 = mysql_fetch_assoc($res2);

if ($arr2[anon] == no  || (($topic_userid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$lpauthor = ("<a href=userdetails.php?id=$topic_userid><font class=".get_class_color($arr2["class"])."><b>$arr2[username]</b></a>");

} else {
$lpauthor = ("<font class=".get_class_color($arr2["class"])."><b>$arr2[username]</b>");
 }
}

else
$lpauthor = "Gelöscht";

  //---- Print row

$r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_row($r);
$new = !$a || $lppostid > $a[0];
$topicpic = ($locked ? ($new ? "lockednew" : "locked") : ($new ? "unlockednew" : "unlocked"));
$subject = ($sticky ? "Sticky: " : "") . "<a href=?action=viewtopic&topicid=$topicid><b>" . encodehtml($topicarr["subject"]) . "</b></a>$topicpages";

$arr = get_forum_access_levels($forumid) or die;
$maypost = get_user_class() >= $arr["write"] && get_user_class() >= $arr["create"];

print("<tr><td class=\"tablea\"><img style=\"vertical-align: middle;\" src=\"".$GLOBALS["PIC_BASE_URL"]."".$topicpic.".gif\"> $subject</td>
<td align=\"center\" class=\"tablea\">$replies</td>
<td align=\"center\" class=\"tablea\">$views</td>
<td class=tablea>$lpauthor</td>
<td class=\"tablea\"><b>Von:</b> $lpusername <br><b>Am: </b>$lpadded</td></tr>");
}
print("<tr><td colspan=\"5\" class=\"tableb\">
<table cellpadding=\"2\" cellspacing=\"o\" border=\"0\"><tr><td class=\"tabled\"><form method=\"get\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\"viewunread\"><input type=\"submit\" value=\"Ungelesene Antworten\" class=\"btn\"></form></td>");

if (!$maypost)
print("<td class=\"tabled\"><input type=\"submit\" value=\"Keine Schreibrechte\" class=\"btn\"></td></tr></table>");

if ($maypost)
print("<td class=\"tabled\"><form method=\"get\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\"newtopic\"><input type=\"hidden\" name=\"forumid\" value=\"$forumid\"><input type=\"submit\" value=\"Neues Thema\" class=\"btn\"></form></td></tr></table>");

print("</td></tr></table>");

print("<br>&nbsp;<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlockednew.gif\">&nbsp;<b>Neue Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlocked.gif\">&nbsp;<b>Gelesene Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."lockednew.gif\">&nbsp;<b>Neue Geschlossene Themen</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."locked.gif\">&nbsp;<b>Gelesene Geschlossene Themen</b>
");

print($menu);

} else {
$arr = get_forum_access_levels($forumid) or die;
$maypost = get_user_class() >= $arr["write"] && get_user_class() >= $arr["create"];

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"tableinborder\">");
print("<tr><td class=\"tablecat\" width=\"40%\"><b>Themen</b></td><td align=\"center\" class=\"tablecat\" width=\"10%\"><b>Antworten</b></td><td align=\"center\" class=\"tablecat\" width=\"10%\"><b>Klicks</b></td><td class=\"tablecat\" width=\"15%\"><b>Themenstarter</b></td><td class=\"tablecat\" width=\"25%\"><b>Letzte Antwort</b></td></tr>");
print("<tr><td colspan=\"5\" class=\"tablea\"><b><p>Es wurden keine Themen gefunden.</p></b></td></tr>");
print("<tr><td colspan=\"5\" class=\"tableb\">
<table cellpadding=\"2\" cellspacing=\"o\" border=\"0\"><tr><td class=\"tabled\"><form method=\"get\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\"viewunread\"><input type=\"submit\" value=\"Ungelesene Antworten\" class=\"btn\"></form></td>");

if (!$maypost)
print("<td class=\"tabled\"><input type=\"submit\" value=\"Keine Schreibrechte\" class=\"btn\"></td></tr></table>");

if ($maypost)
print("<td class=\"tabled\"><form method=\"get\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\"newtopic\"><input type=\"hidden\" name=\"forumid\" value=\"$forumid\"><input type=\"submit\" value=\"Neues Thema\" class=\"btn\"></form></td></tr></table>");

print("</td></tr></table>");

print("<br>&nbsp;<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlockednew.gif\">&nbsp;<b>Neue Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlocked.gif\">&nbsp;<b>Gelesene Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."lockednew.gif\">&nbsp;<b>Neue Geschlossene Themen</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."locked.gif\">&nbsp;<b>Gelesene Geschlossene Themen</b><br>
");
}

insert_quick_jump_menu();

print("</td></tr></table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

end_frame();
stdfoot();
die;
}

  //-------- Action: View unread posts

if ($action == "viewunread") {
$userid = $CURUSER['id'];
$maxresults = 10;

$res = mysql_query("SELECT id, userid, forumid, subject, lastpost FROM topics ORDER BY lastpost") or sqlerr(__FILE__, __LINE__);

stdhead("Tracker Forum");
begin_frame("<span class=normalfont><b>Tracker Forum</b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td width=\"100%\" class=\"tabletitle\"><span class=\"smalfont\"><a href=\"forums.php\"><b>Tracker Forum</b></a> - <b><i>\"Ungelesene Themen\"</i></b></span></td></tr>
<tr><td class=\"tablea\">");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"tableinborder\">");
print("<tr><td class=\"tablecat\" width=\"25%\"><b>Forum</b></td><td class=\"tablecat\" width=\"50%\"><b>Thema</b></td><td class=\"tablecat\" width=\"25%\"><b>Letzer Beitrag</b></td></tr>");


$n = 0;
$uc = get_user_class();

while ($arr = mysql_fetch_assoc($res)) {
$topicid = $arr['id'];
$forumid = $arr['forumid'];

  // Find last post ID

$lastpostid = get_forum_last_post($forumid);

  // Get last post info

$post_res = mysql_query("SELECT added, topicid, userid FROM posts WHERE id=$lastpostid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($post_res) == 1) {
$post_arr = mysql_fetch_assoc($post_res) or die("Falsche Foren-ID last_post");
$lastposterid = $post_arr["userid"];
$lastpostdate = $post_arr["added"];

$user_res = mysql_query("SELECT username, class, anon FROM users WHERE id=$lastposterid") or sqlerr(__FILE__, __LINE__);
$user_arr = mysql_fetch_assoc($user_res);
$lastposter = ("<font class=".get_class_color($user_arr["class"])."><b>".htmlspecialchars($user_arr["username"])."</b></font>");

if ($user_arr[anon] == no  || (($lastposterid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$lastpost = "<b>Von:</b> <a href=\"userdetails.php?id=$lastposterid\">$lastposter</a><br><b>Am:</b> $lastpostdate";

} else {
$lastpost = "<b>Von:</b> $lastposter<br><b>Am:</b> $lastpostdate";
 }
}

  //---- Check if post is read

$r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_row($r);

if ($a && $a[0] == $arr['lastpost'])
continue;

  //---- Check access & get forum name

$r = mysql_query("SELECT name, minclassread FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_assoc($r);

if ($uc < $a['minclassread'])
continue;
++$n;

if ($n > $maxresults)
break;

$forumname = $a['name'];

print("<tr><td class=\"tablea\"><img src=\"".$GLOBALS["PIC_BASE_URL"]."unlockednew.gif\"> <a href=\"?action=viewforum&amp;forumid=$forumid\"><b>$forumname</b></a></a></td>
<td class=\"tablea\"><a href=\"?action=viewtopic&topicid=$topicid&page=last#last\"><b>".htmlspecialchars($arr["subject"])."</b></a></td><td class=\"tablea\">$lastpost</td></tr>");
}

if ($n == 0) {
print("<tr><td colspan=\"3\" class=\"tablea\"><b><p>Es wurden keine ungelesenen Themen gefunden.</p></b></td></tr>");
}

print("<tr><td colspan=\"3\" class=\"tableb\"><a href=\"?action=search\"><input type=\"submit\" value=\"Suche\" class=\"btn\"></a></td></tr>");
print("</table>");

print("<br>&nbsp;<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlockednew.gif\">&nbsp;<b>Neue Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlocked.gif\">&nbsp;<b>Gelesene Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."lockednew.gif\">&nbsp;<b>Neue Geschlossene Themen</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."locked.gif\">&nbsp;<b>Gelesene Geschlossene Themen</b><br>
");
if ($n > $maxresults) {
print("<b><p>Es wurden mehr als $maxresults ungelesene Threads gefunden, die ersten $maxresults werden angezeigt.</p></b>");
}

insert_quick_jump_menu();

print("</table>");
print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

end_frame();
stdfoot();
die;
}

if ($action == "search") {
stdhead("Tracker Forum");
begin_frame("<span class=normalfont><b>Tracker Forum</b></span>");

$keywords = trim($_GET["keywords"]);

if ($keywords != "") {
$perpage = 50;
$page = max(1, 0 + $_GET["page"]);
$ekeywords = sqlesc($keywords);

$res = mysql_query("SELECT COUNT(*) FROM posts WHERE MATCH (body) AGAINST ($ekeywords)") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_row($res);
$hits = 0 + $arr[0];

if ($hits == 0) {
print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"500\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"500\" class=\"tableinborder\">
<tr><td align=\"center\" width=\"100%\" class=\"tabletitle\"><span class=\"normalfont\"><b>Suche nach: <i>\"".htmlspecialchars($keywords)."\"</i></b></span></td></tr>");
print("<tr><td width=\"100%\" class=\"tablea\"><b><p>Es wurden keine passenden Einträge gefunden!</p></b></td></tr>");
print("<tr><td colspan=\"4\" class=\"tableb\"><a href=\"?action=search\"><input type=\"submit\" value=\"Suche\" class=\"btn\"></a></td></tr>");

print("</table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"500\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

} else {
print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td class=\"tabletitle\"><span class=\"smalfont\"><a href=\"forums.php\"><b>Tracker Forum</b></a> - <b><i>\"Forum Suche\"</i></b></span></td></tr>
<tr><td class=\"tablea\">");

$pages = 0 + ceil($hits / $perpage);

if ($page > $pages) $page = $pages;
for ($i = 1; $i <= $pages; ++$i)
if ($page == $i)
$pagemenu1 .= "<center><b>$i</b><center>";

else
$pagemenu1 .= "<a href=\"/forums.php?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=$i\"><b>$i</b></a>";

if ($page == 1)
$pagemenu2 = "<b>&lt;&lt; Zurück</b>";

else
$pagemenu2 = "<a href=\"/forums.php?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=" . ($page - 1) . "\"><b>&lt;&lt; Zurück</b></a>";
$pagemenu2 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";

if ($page == $pages)
$pagemenu2 .= "<b>Nächste &gt;&gt;</b>";

else
$pagemenu2 .= "<a href=\"/forums.php?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=" . ($page + 1) . "\"><b>Nächste &gt;&gt;</b></a>";
$offset = ($page * $perpage) - $perpage;

$res = mysql_query("SELECT id, topicid,userid,added FROM posts WHERE MATCH (body) AGAINST ($ekeywords) LIMIT $offset,$perpage") or sqlerr(__FILE__, __LINE__);
$num = mysql_num_rows($res);

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"tableinborder\">");
print("<tr><td class=\"tablecat\" width=\"30%\"><b>Forum</b></td><td class=\"tablecat\" width=\"35%\"><b>Thema</b></td><td align=\"center\" class=\"tablecat\" width=\"10%\"><b>Beiträge</b></td><td class=\"tablecat\" width=\"25%\"><b>Geschrieben von</b></td></tr>");

for ($i = 0; $i < $num; ++$i) {
$post = mysql_fetch_assoc($res);
$res2 = mysql_query("SELECT forumid, subject FROM topics WHERE id=$post[topicid]") or sqlerr(__FILE__, __LINE__);
$topic = mysql_fetch_assoc($res2);

$res2 = mysql_query("SELECT name, minclassread FROM forums WHERE id=$topic[forumid]") or sqlerr(__FILE__, __LINE__);
$forum = mysql_fetch_assoc($res2);

if ($forum["name"] == "" || $forum["minclassread"] > $CURUSER["class"]) {
--$hits;
continue;
}

$res2 = mysql_query("SELECT username, class, anon FROM users WHERE id=$post[userid]") or sqlerr(__FILE__, __LINE__);
$user = mysql_fetch_assoc($res2);
$username = ("<font class=\"".get_class_color($user["class"])."\"><b>$user[username]</b></font>");

if ($user["username"] == "")
$lastpost = "Gelöscht";

if ($user[anon] == no  || (($post[userid] == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$lastpost = "<b>Von:</b> <a href=\"userdetails.php?id=$post[userid]\">$username</a><br><b>Am:</b> $post[added]";

} else {
$lastpost = "<b>Von:</b> $username<br><b>Am:</b> $post[added]";
}

print("<tr><td class=\"tablea\"><a href=\"?action=viewforum&amp;forumid=$topic[forumid]\"><b>" . htmlspecialchars($forum["name"]) . "</b></a></td><td class=\"tablea\"><a href=\"?action=viewtopic&amp;topicid=$post[topicid]&amp;page=p$post[id]#$post[id]\"><b>" . htmlspecialchars($topic["subject"]) . "</b></a></td><td align=\"center\" class=\"tablea\">$post[id]</td><td class=\"tablea\">$lastpost</td></tr>");
}

print("<tr><td colspan=\"4\" class=\"tableb\"><a href=\"?action=viewunread\"><input type=\"submit\" value=\"Ungelesene Themen\" class=\"btn\"></a></td></tr>");

print("</table>");
print("<b><p>Es wurden $hits post".($hits != 1 ? "s" : "")." gefunden</p></b>");
print("<center><p>$pagemenu2<br>$pagemenu1</p></center>");

insert_quick_jump_menu();

print("</td></tr></table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");
 }
}

print("<form method=\"get\" action=\"/forums.php?\"><input type=\"hidden\" name=\"action\" value=\"search\">");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"500\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"500\" class=\"tableinborder\">
<tr><td align=\"center\" colspan=\"2\" class=\"tablecat\"><span class=\"normalfont\"><b>Forum Suche</b></span></td></tr>");
print("<tr><td class=\"tableb\"><b>Suchbegriff</b></td><td class=\"tablea\"><input type=\"text\" size=\"55\" name=\"keywords\" value=\"" . htmlspecialchars($keywords) ."\"><br><font class=\"small\" size=\"-1\">Gib einen oder mehrere Suchbegriffe ein.<br>Sehr lange oder kurze Begriffe werden automatisch ignoriert.</font></td></tr>");

print("<tr><td align=\"center\" colspan=\"2\" class=\"tableb\"><input type=\"submit\" value=\"Und ab!\" class=\"btn\"></td></tr>");

print("</table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"500\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

end_frame();
stdfoot();
die;
}

  //-------- Handle unknown action

if ($action != "")
stderr("Forum Fehler", "<b><p>Unbekannte Action '$action'.</p></b>");

  //-------- Get forums

$forums_res = mysql_query("SELECT * FROM forums ORDER BY sort, name") or sqlerr(__FILE__, __LINE__);

stdhead("Tracker Forum");
begin_frame("<span class=normalfont><b>Tracker Forum</b></span>");

print("<br><table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"obenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/obenrechts.gif\"></td>
</tr></table>");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"790\" class=\"tableinborder\">
<tr><td width=\"100%\" class=\"tabletitle\"><span class=\"smalfont\"><b><i>Tracker Forum</i></b></span></td></tr>
<tr><td class=\"tablea\">");

print("<table align=\"center\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"tableinborder\">");
print("<tr><td class=\"tablecat\"><b>Foren</b></td><td align=\"center\" class=\"tablecat\"><b>Themen</b></td><td align=\"center\" class=\"tablecat\"><b>Beiträge</b></td><td class=\"tablecat\"><b>Letzten Beiträge</b></td></tr>");

while ($forums_arr = mysql_fetch_assoc($forums_res)) {
if (get_user_class() < $forums_arr["minclassread"])
continue;

$forumid = $forums_arr["id"];
$forumname = htmlspecialchars($forums_arr["name"]);
$forumdescription = htmlspecialchars($forums_arr["description"]);
$topiccount = number_format($forums_arr["topiccount"]);
$postcount = number_format($forums_arr["postcount"]);

  // Find last post ID

$lastpostid = get_forum_last_post($forumid);

  // Get last post info

$post_res = mysql_query("SELECT added, topicid, userid FROM posts WHERE id=$lastpostid") or sqlerr(__FILE__, __LINE__);

if (mysql_num_rows($post_res) == 1) {
$post_arr = mysql_fetch_assoc($post_res) or die("Bad forum last_post");
$lastposterid = $post_arr["userid"];
$lastpostdate = $post_arr["added"];
$lasttopicid = $post_arr["topicid"];
$user_res = mysql_query("SELECT username, class, anon FROM users WHERE id=$lastposterid") or sqlerr(__FILE__, __LINE__);
$user_arr = mysql_fetch_assoc($user_res);
$lastposter = ("<font class=".get_class_color($user_arr["class"])."><b>".htmlspecialchars($user_arr["username"])."</b></font>");
$topic_res = mysql_query("SELECT subject FROM topics WHERE id=$lasttopicid") or sqlerr(__FILE__, __LINE__);
$topic_arr = mysql_fetch_assoc($topic_res);
$lasttopic1 = htmlspecialchars($topic_arr['subject']);

if (strlen($topic_arr['subject']) > 25) 
$lasttopic = substr($topic_arr['subject'], 0, 25) . "..."; 

else 
$lasttopic = $topic_arr['subject']; 

if ($user_arr[anon] == no  || (($posterid == $CURUSER[id]) || get_user_class() >= UC_MODERATOR)) {
$lastpost = "<b>Am:</b> $lastpostdate<br><b>Von:</b> <a href=userdetails.php?id=$lastposterid>$lastposter</a><br><b>In:</b> <a href=?action=viewtopic&topicid=$lasttopicid&amp;page=p$lastpostid#$lastpostid>$lasttopic</a>";

} else {
$lastpost = "<b>Am:</b> $lastpostdate<br><b>Von:</b> $lastposter<br><b>In:</b> <a href=?action=viewtopic&topicid=$lasttopicid&amp;page=p$lastpostid#$lastpostid>$lasttopic</a>";
}

$r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$CURUSER[id] AND topicid=$lasttopicid") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_row($r);

if ($a && $a[0] >= $lastpostid)
$img = "unlocked";

else
$img = "unlockednew";

} else {
$lastpost = "Keine Themen vorhanden.";
$img = "unlocked";
}

print("<tr><td class=\"tablea\" width=\"60%\"><img src=\"".$GLOBALS["PIC_BASE_URL"].$img.".gif\"> <a href=\"?action=viewforum&forumid=$forumid\"><b>$forumname</b></a><br>$forumdescription</td>
<td align=\"center\" class=\"tablea\" width=\"5%\">$topiccount</td>
<td align=\"center\" class=\"tablea\" width=\"10%\">$postcount</td>
<td class=\"tablea\" width=\"25%\">$lastpost</td></tr>");
}

print("<tr><td colspan=\"4\" class=\"tableb\">
<table cellpadding=\"2\" cellspacing=\"o\" border=\"0\"><tr><td class=\"tabled\"><a href=\"?action=viewunread\"><input type=\"submit\" value=\"Ungelesene Themen\" class=\"btn\"></a></td><td class=\"tabled\"><a href=\"?action=search\"><input type=\"submit\" value=\"Suche\" class=\"btn\"></a></td></tr></table>
</td></tr>");
print("</table>");

print("<br>&nbsp;<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlockednew.gif\">&nbsp;<b>Neue Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."unlocked.gif\">&nbsp;<b>Gelesene Antworten</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."lockednew.gif\">&nbsp;<b>Neue Geschlossene Themen</b>&nbsp;&nbsp;&nbsp;
<img src=\"".$GLOBALS["PIC_BASE_URL"]."locked.gif\">&nbsp;<b>Gelesene Geschlossene Themen</b><br>
");

insert_quick_jump_menu();

print("</td></tr></table>");

print("<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"790\"><tr>
<td align=\"left\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenlinks.gif\"></td>
<td align=\"center\" width=\"100%\" class=\"untenmitte\"></td>
<td align=\"right\"><img src=\"".$GLOBALS["PIC_BASE_URL"] . $GLOBALS["ss_uri"]."/untenrechts.gif\"></td>
</tr></table><br>");

end_frame();
stdfoot();
?>