PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/dead/forums.php

https://bitbucket.org/nexea/x00n
PHP | 1589 lines | 1357 code | 199 blank | 33 comment | 75 complexity | 64f11b6c08ac6ccbb9c198d45a7419d3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?
  2. ob_start("ob_gzhandler");
  3. require "include/bittorrent.php";
  4. dbconn(false);
  5. loggedinorreturn();
  6. $action = $HTTP_GET_VARS["action"];
  7. function catch_up()
  8. {
  9. /*
  10. die("This feature is currently unavailable.");
  11. */
  12. global $CURUSER;
  13. $userid = $CURUSER["id"];
  14. $res = mysql_query("SELECT id, lastpost FROM topics") or sqlerr(__FILE__, __LINE__);
  15. while ($arr = mysql_fetch_assoc($res))
  16. {
  17. $topicid = $arr["id"];
  18. $postid = $arr["lastpost"];
  19. $r = mysql_query("SELECT id,lastpostread FROM readposts WHERE userid=$userid and topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  20. if (mysql_num_rows($r) == 0)
  21. mysql_query("INSERT INTO readposts (userid, topicid, lastpostread) VALUES($userid, $topicid, $postid)") or sqlerr(__FILE__, __LINE__);
  22. else
  23. {
  24. $a = mysql_fetch_assoc($r);
  25. if ($a["lastpostread"] < $postid)
  26. mysql_query("UPDATE readposts SET lastpostread=$postid WHERE id=" . $a["id"]) or sqlerr(__FILE__, __LINE__);
  27. }
  28. }
  29. }
  30. //-------- Returns the minimum read/write class levels of a forum
  31. function get_forum_access_levels($forumid)
  32. {
  33. $res = mysql_query("SELECT minclassread, minclasswrite, minclasscreate FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
  34. if (mysql_num_rows($res) != 1)
  35. return false;
  36. $arr = mysql_fetch_assoc($res);
  37. return array("read" => $arr["minclassread"], "write" => $arr["minclasswrite"], "create" => $arr["minclasscreate"]);
  38. }
  39. //-------- Returns the forum ID of a topic, or false on error
  40. function get_topic_forum($topicid)
  41. {
  42. $res = mysql_query("SELECT forumid FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  43. if (mysql_num_rows($res) != 1)
  44. return false;
  45. $arr = mysql_fetch_row($res);
  46. return $arr[0];
  47. }
  48. //-------- Returns the ID of the last post of a forum
  49. function update_topic_last_post($topicid)
  50. {
  51. $res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
  52. $arr = mysql_fetch_row($res) or die("No post found");
  53. $postid = $arr[0];
  54. mysql_query("UPDATE topics SET lastpost=$postid WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  55. }
  56. function get_forum_last_post($forumid)
  57. {
  58. $res = mysql_query("SELECT lastpost FROM topics WHERE forumid=$forumid ORDER BY lastpost DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
  59. $arr = mysql_fetch_row($res);
  60. $postid = $arr[0];
  61. if ($postid)
  62. return $postid;
  63. else
  64. return 0;
  65. }
  66. //-------- Inserts a quick jump menu
  67. function insert_quick_jump_menu($currentforum = 0)
  68. {
  69. print("<p class=\"txtCenter\"><form method=\"get\" action=\"?\" name=\"jump\">\n");
  70. print("<input type=\"hidden\" name=\"action\" value=\"viewforum\">\n");
  71. print("Quick jump: ");
  72. print("<select name=\"forumid\" onchange=\"if(this.options[this.selectedIndex].value != -1){ forms['jump'].submit() }\">\n");
  73. $res = mysql_query("SELECT * FROM forums ORDER BY name") or sqlerr(__FILE__, __LINE__);
  74. while ($arr = mysql_fetch_assoc($res))
  75. {
  76. if (get_user_class() >= $arr["minclassread"])
  77. print("<option value=\"" . $arr["id"] . ($currentforum == $arr["id"] ? " selected\">" : ">") . $arr["name"] . "\n");
  78. }
  79. print("</select>\n");
  80. print("<input type=\"submit\" value=\"Go!\">\n");
  81. print("</form>\n</p>");
  82. }
  83. //-------- Inserts a compose frame
  84. function insert_compose_frame($id, $newtopic = true, $quote = false)
  85. {
  86. global $maxsubjectlength, $CURUSER;
  87. if ($newtopic)
  88. {
  89. $res = mysql_query("SELECT name FROM forums WHERE id=$id") or sqlerr(__FILE__, __LINE__);
  90. $arr = mysql_fetch_assoc($res) or die("Bad forum id");
  91. $forumname = $arr["name"];
  92. print("<p class=\"txtCenter\">New topic in <a href=\"?action=viewforum&amp;forumid=$id\">$forumname</a> forum</p>\n");
  93. }
  94. else
  95. {
  96. $res = mysql_query("SELECT * FROM topics WHERE id=$id") or sqlerr(__FILE__, __LINE__);
  97. $arr = mysql_fetch_assoc($res) or stderr("Forum error", "Topic not found.");
  98. $subject = $arr["subject"];
  99. print("<p class=\"txtCenter\">Reply to topic: <a href=\"?action=viewtopic&amp;topicid=$id\">$subject</a></p>");
  100. }
  101. begin_frame("Compose", true);
  102. print("<form method=\"post\" action=\"?action=post\">\n");
  103. if ($newtopic)
  104. print("<input type=\"hidden\" name=\"forumid\" value=\"$id\">\n");
  105. else
  106. print("<input type=\"hidden\" name=\"topicid\" value=\"$id\">\n");
  107. begin_table();
  108. if ($newtopic)
  109. print("<tr><td class=\"rowhead\">Subject</td>" .
  110. "<td align=\"left\" style=\"padding: 0px\"><input type=\"text\" size=\"100\" maxlength=\"$maxsubjectlength\" name=\"subject\" " .
  111. "style=\"border: 0px; height: 19px\"></td></tr>\n");
  112. if ($quote)
  113. {
  114. $postid = $_GET["postid"];
  115. if (!is_valid_id($postid))
  116. die;
  117. $res = mysql_query("SELECT posts.*, users.username FROM posts JOIN users ON posts.userid = users.id WHERE posts.id=$postid") or sqlerr(__FILE__, __LINE__);
  118. if (mysql_num_rows($res) != 1)
  119. stderr("Error", "No post with ID $postid.");
  120. $arr = mysql_fetch_assoc($res);
  121. }
  122. print("<tr><td class=\"rowhead\">Body</td><td align=\"left\" style=\"padding: 0px\">" .
  123. "<textarea name=\"body\" cols=\"100\" rows=\"20\" style=\"border: 0px\">".
  124. ($quote?(("[quote=".htmlspecialchars($arr["username"])."]".htmlspecialchars($arr["body"])."[/quote]")):"").
  125. "</textarea></td></tr>\n");
  126. print("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" class=\"btn\" value=\"Submit\"></td></tr>\n");
  127. end_table();
  128. print("</form>\n");
  129. print("<p class=\"txtCenter\"><a href=\"tags\" target=\"_blank\">Tags</a> | <a href=\"smilies\" target=\"_blank\">Smilies</a></p>\n");
  130. end_frame();
  131. //------ Get 10 last posts if this is a reply
  132. if (!$newtopic)
  133. {
  134. $postres = mysql_query("SELECT * FROM posts WHERE topicid=$id ORDER BY id DESC LIMIT 10") or sqlerr(__FILE__, __LINE__);
  135. begin_frame("10 last posts, in reverse order");
  136. while ($post = mysql_fetch_assoc($postres))
  137. {
  138. //-- Get poster details
  139. $userres = mysql_query("SELECT * FROM users WHERE id=" . $post["userid"] . " LIMIT 1") or sqlerr(__FILE__, __LINE__);
  140. $user = mysql_fetch_assoc($userres);
  141. $avatar = ($CURUSER["avatars"] == "yes" ? htmlspecialchars($user["avatar"]) : "");
  142. // $avatar = $user["avatar"];
  143. if (!$avatar)
  144. $avatar = "images/default_avatar.gif";
  145. print("<p class=sub>#" . $post["id"] . " by " . $user["username"] . " at " . $post["added"] . " GMT</p>");
  146. begin_table(true);
  147. print("<tr valign=\"top\"><td width=\"150\" align=\"center\" style=\"padding: 0px\">" . ($avatar ? "<img width=\"150\" src=\"$avatar\">" : "").
  148. "</td><td class=\"comment\">" . format_comment($post["body"]) . "</td></tr>\n");
  149. end_table();
  150. }
  151. end_frame();
  152. }
  153. insert_quick_jump_menu();
  154. }
  155. //-------- Global variables
  156. $maxsubjectlength = 40;
  157. $postsperpage = $CURUSER["postsperpage"];
  158. if (!$postsperpage) $postsperpage = 25;
  159. //-------- Action: New topic
  160. if ($action == "newtopic")
  161. {
  162. $forumid = $_GET["forumid"];
  163. if (!is_valid_id($forumid))
  164. die;
  165. stdhead("New topic");
  166. begin_main_frame();
  167. insert_compose_frame($forumid);
  168. end_main_frame();
  169. stdfoot();
  170. die;
  171. }
  172. //-------- Action: Post
  173. if ($action == "post")
  174. {
  175. $forumid = 0 + $_POST["forumid"];
  176. $topicid = 0 + $_POST["topicid"];
  177. if (!is_valid_id($forumid) && !is_valid_id($topicid))
  178. stderr("Error", "Bad forum or topic ID.");
  179. $newtopic = $forumid > 0;
  180. $subject = $_POST["subject"];
  181. if ($newtopic)
  182. {
  183. $subject = trim($subject);
  184. if (!$subject)
  185. stderr("Error", "You must enter a subject.");
  186. if (strlen($subject) > $maxsubjectlength)
  187. stderr("Error", "Subject is limited to $maxsubjectlength characters.");
  188. }
  189. else
  190. $forumid = get_topic_forum($topicid) or die("Bad topic ID");
  191. //------ Make sure sure user has write access in forum
  192. $arr = get_forum_access_levels($forumid) or die("Bad forum ID");
  193. if (get_user_class() < $arr["write"] || ($newtopic && get_user_class() < $arr["create"]))
  194. stderr("Error", "Permission denied.");
  195. $body = trim($_POST["body"]);
  196. if ($body == "")
  197. stderr("Error", "No body text.");
  198. $userid = $CURUSER["id"];
  199. if ($newtopic)
  200. {
  201. //---- Create topic
  202. $subject = sqlesc($subject);
  203. mysql_query("INSERT INTO topics (userid, forumid, subject) VALUES($userid, $forumid, $subject)") or sqlerr(__FILE__, __LINE__);
  204. $topicid = mysql_insert_id() or stderr("Error", "No topic ID returned");
  205. }
  206. else
  207. {
  208. //---- Make sure topic exists and is unlocked
  209. $res = mysql_query("SELECT * FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  210. $arr = mysql_fetch_assoc($res) or die("Topic id n/a");
  211. if ($arr["locked"] == 'yes' && get_user_class() < UC_MODERATOR)
  212. stderr("Error", "This topic is locked.");
  213. //---- Get forum ID
  214. $forumid = $arr["forumid"];
  215. }
  216. //------ Insert post
  217. $added = "'" . get_date_time() . "'";
  218. $body = sqlesc($body);
  219. mysql_query("INSERT INTO posts (topicid, userid, added, body) " .
  220. "VALUES($topicid, $userid, $added, $body)") or sqlerr(__FILE__, __LINE__);
  221. $postid = mysql_insert_id() or die("Post id n/a");
  222. //------ Update topic last post
  223. update_topic_last_post($topicid);
  224. //------ All done, redirect user to the post
  225. $headerstr = "Location: $DEFAULTBASEURL/forums?action=viewtopic&amp;topicid=$topicid&amp;page=last";
  226. if ($newtopic)
  227. header($headerstr);
  228. else
  229. header("$headerstr#$postid");
  230. die;
  231. }
  232. //-------- Action: View topic
  233. if ($action == "viewtopic")
  234. {
  235. $topicid = $_GET["topicid"];
  236. $page = $_GET["page"];
  237. if (!is_valid_id($topicid))
  238. die;
  239. $userid = $CURUSER["id"];
  240. //------ Get topic info
  241. $res = mysql_query("SELECT * FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  242. $arr = mysql_fetch_assoc($res) or stderr("Forum error", "Topic not found");
  243. $locked = ($arr["locked"] == 'yes');
  244. $subject = $arr["subject"];
  245. $sticky = $arr["sticky"] == "yes";
  246. $forumid = $arr["forumid"];
  247. //------ Update hits column
  248. mysql_query("UPDATE topics SET views = views + 1 WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  249. //------ Get forum
  250. $res = mysql_query("SELECT * FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
  251. $arr = mysql_fetch_assoc($res) or die("Forum = NULL");
  252. $forum = $arr["name"];
  253. if ($CURUSER["class"] < $arr["minclassread"])
  254. stderr("Error", "You are not permitted to view this topic.");
  255. //------ Get post count
  256. $res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  257. $arr = mysql_fetch_row($res);
  258. $postcount = $arr[0];
  259. //------ Make page menu
  260. $pagemenu = "<p>\n";
  261. $perpage = $postsperpage;
  262. $pages = ceil($postcount / $perpage);
  263. if ($page[0] == "p")
  264. {
  265. $findpost = substr($page, 1);
  266. $res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid ORDER BY added") or sqlerr(__FILE__, __LINE__);
  267. $i = 1;
  268. while ($arr = mysql_fetch_row($res))
  269. {
  270. if ($arr[0] == $findpost)
  271. break;
  272. ++$i;
  273. }
  274. $page = ceil($i / $perpage);
  275. }
  276. if ($page == "last")
  277. $page = $pages;
  278. else
  279. {
  280. if($page < 1)
  281. $page = 1;
  282. elseif ($page > $pages)
  283. $page = $pages;
  284. }
  285. $offset = $page * $perpage - $perpage;
  286. for ($i = 1; $i <= $pages; ++$i)
  287. {
  288. if ($i == $page)
  289. $pagemenu .= "<span><b>$i</b></span>\n";
  290. else
  291. $pagemenu .= "<a href=\"?action=viewtopic&amp;topicid=$topicid&amp;page=$i\"><b>$i</b></a>\n";
  292. }
  293. if ($page == 1)
  294. $pagemenu .= "<br /><span><b>&lt;&lt; Prev</b></span>";
  295. else
  296. $pagemenu .= "<br /><a href=\"?action=viewtopic&amp;topicid=$topicid&amp;page=" . ($page - 1) .
  297. "\"><b>&lt;&lt; Prev</b></a>";
  298. $pagemenu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  299. if ($page == $pages)
  300. $pagemenu .= "<span><b>Next &gt;&gt;</b></span></p>\n";
  301. else
  302. $pagemenu .= "<a href=\"?action=viewtopic&amp;topicid=$topicid&amp;page=" . ($page + 1) .
  303. "\"><b>Next &gt;&gt;</b></a></p>\n";
  304. //------ Get posts
  305. $res = mysql_query("SELECT * FROM posts WHERE topicid=$topicid ORDER BY id LIMIT $offset,$perpage") or sqlerr(__FILE__, __LINE__);
  306. stdhead("View topic");
  307. print("<a name=\"top\"><h1><a href=\"?action=viewforum&amp;forumid=$forumid\">$forum</a> &gt; $subject</h1>\n");
  308. print($pagemenu);
  309. //------ Print table
  310. begin_main_frame();
  311. begin_frame();
  312. $pc = mysql_num_rows($res);
  313. $pn = 0;
  314. $r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=" . $CURUSER["id"] . " AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  315. $a = mysql_fetch_row($r);
  316. $lpr = $a[0];
  317. if (!$lpr)
  318. mysql_query("INSERT INTO readposts (userid, topicid) VALUES($userid, $topicid)") or sqlerr(__FILE__, __LINE__);
  319. while ($arr = mysql_fetch_assoc($res))
  320. {
  321. ++$pn;
  322. $postid = $arr["id"];
  323. $posterid = $arr["userid"];
  324. $added = $arr["added"] . " GMT (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($arr["added"]))) . " ago)";
  325. //---- Get poster details
  326. $res2 = mysql_query("SELECT username,class,avatar,donor,title,enabled,warned FROM users WHERE id=$posterid") or sqlerr(__FILE__, __LINE__);
  327. $arr2 = mysql_fetch_assoc($res2);
  328. $postername = $arr2["username"];
  329. if ($postername == "")
  330. {
  331. $by = "unknown[$posterid]";
  332. $avatar = "";
  333. }
  334. else
  335. {
  336. // if ($arr2["enabled"] == "yes")
  337. $avatar = ($CURUSER["avatars"] == "yes" ? htmlspecialchars($arr2["avatar"]) : "");
  338. // else
  339. // $avatar = "images/disabled_avatar.gif";
  340. $title = $arr2["title"];
  341. if (!$title)
  342. $title = get_user_class_name($arr2["class"]);
  343. $by = "<a href=\"userdetails?id=$posterid\"><b>$postername</b></a>" . ($arr2["donor"] == "yes" ? "<img src=\"".
  344. "images/star.gif\" alt=\"Donor\">" : "") . ($arr2["enabled"] == "no" ? "<img src=".
  345. "images/disabled.gif\" alt=\"This account is disabled\" style=\"margin-left: 2px\">" : ($arr2["warned"] == "yes" ? "<a href=rules#warning class=altlink><img src=images/warned.gif alt=\"Warned\" border=0></a>" : "")) . " ($title)";
  346. }
  347. if (!$avatar)
  348. $avatar = "images/default_avatar.gif";
  349. print("<a name=\"$postid\">\n");
  350. if ($pn == $pc)
  351. {
  352. print("<a name=\"last\">\n");
  353. if ($postid > $lpr)
  354. mysql_query("UPDATE readposts SET lastpostread=$postid WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  355. }
  356. print("<p class=\"sub\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"embedded\" width=\"99%\">#$postid by $by at $added");
  357. if (!$locked || get_user_class() >= UC_MODERATOR)
  358. print(" - [<a href=\"?action=quotepost&amp;topicid=$topicid&amp;postid=$postid\"><b>Quote</b></a>]");
  359. if (($CURUSER["id"] == $posterid && !$locked) || get_user_class() >= UC_MODERATOR)
  360. print(" - [<a href=\"?action=editpost&amp;postid=$postid\"><b>Edit</b></a>]");
  361. if (get_user_class() >= UC_MODERATOR)
  362. print(" - [<a href=\"?action=deletepost&amp;postid=$postid\"><b>Delete</b></a>]");
  363. print("</td><td class=\"embedded\" width=\"1%\"><a href=#top><img src=\"images/top.gif\" border=\"0\" alt=\"Top\"></a></td></tr>");
  364. print("</table></p>\n");
  365. begin_table(true);
  366. $body = format_comment($arr["body"]);
  367. if (is_valid_id($arr['editedby']))
  368. {
  369. $res2 = mysql_query("SELECT username FROM users WHERE id=$arr[editedby]");
  370. if (mysql_num_rows($res2) == 1)
  371. {
  372. $arr2 = mysql_fetch_assoc($res2);
  373. $body .= "<p><span>Last edited by <a href=\"userdetails?id=$arr[editedby]\"><b>$arr2[username]</b></a> at $arr[editedat] GMT</span></p>\n";
  374. }
  375. }
  376. print("<tr valign=\"top\"><td width=\"150\" align=\"center\" style=\"padding: 0px\">" .
  377. ($avatar ? "<img width=\"150\" src=\"$avatar\">" : ""). "</td><td class=\"comment\">$body</td></tr>\n");
  378. end_table();
  379. }
  380. //------ Mod options
  381. if (get_user_class() >= UC_MODERATOR)
  382. {
  383. attach_frame();
  384. $res = mysql_query("SELECT id,name,minclasswrite FROM forums ORDER BY name") or sqlerr(__FILE__, __LINE__);
  385. print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
  386. print("<form method=\"post\" action=\"?action=setsticky\">\n");
  387. print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n");
  388. print("<input type=\"hidden\" name=\"returnto\" value=\"$HTTP_SERVER_VARS[REQUEST_URI]\">\n");
  389. print("<tr><td class=\"embedded\" align=\"right\">Sticky:</td>\n");
  390. print("<td class=\"embedded\"><input type=\"radio\" name=\"sticky\" value='yes' " . ($sticky ? " checked" : "") . "> Yes <input type=\"radio\" name=\"sticky\" value='no' " . (!$sticky ? " checked" : "") . "> No\n");
  391. print("<input type=\"submit\" value=\"Set\"></td></tr>");
  392. print("</form>\n");
  393. print("<form method=\"post\" action=\"?action=setlocked\">\n");
  394. print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n");
  395. print("<input type=\"hidden\" name=\"returnto\" value=\"$HTTP_SERVER_VARS[REQUEST_URI]\">\n");
  396. print("<tr><td class=\"embedded\" align=\"right\">Locked:</td>\n");
  397. print("<td class=\"embedded\"><input type=\"radio\" name=\"locked\" value='yes' " . ($locked ? " checked" : "") . "> Yes <input type=\"radio\" name=\"locked\" value='no' " . (!$locked ? " checked" : "") . "> No\n");
  398. print("<input type=submit value='Set'></td></tr>");
  399. print("</form>\n");
  400. print("<form method=\"post\" action=\"?action=renametopic\">\n");
  401. print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n");
  402. print("<input type=\"hidden\" name=\"returnto\" value=\"$DEFAULTBASEURL$HTTP_SERVER_VARS[REQUEST_URI]\">\n");
  403. print("<tr><td class=\"embedded\" align=\"right\">Rename topic:</td><td class=embedded><input type=text name=subject size=60 maxlength=$maxsubjectlength value=\"" . htmlspecialchars($subject) . "\">\n");
  404. print("<input type=\"submit\" value=\"Okay\"></td></tr>");
  405. print("</form>\n");
  406. print("<form method=\"post\" action=\"?action=movetopic&amp;topicid=$topicid\">\n");
  407. print("<tr><td class=\"embedded\">Move this thread to:&nbsp;</td><td class=\"embedded\"><select name=\"forumid\">");
  408. while ($arr = mysql_fetch_assoc($res))
  409. if ($arr["id"] != $forumid && get_user_class() >= $arr["minclasswrite"])
  410. print("<option value=" . $arr["id"] . ">" . $arr["name"] . "\n");
  411. print("</select> <input type=\"submit\" value=\"Okay\"></form></td></tr>\n");
  412. print("<tr><td class=\"embedded\">Delete topic</td><td class=\"embedded\">\n");
  413. print("<form method=\"get\" action=\"/forums\">\n");
  414. print("<input type=\"hidden\" name=\"action\" value=\"deletetopic\">\n");
  415. print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n");
  416. print("<input type=\"hidden\" name=\"forumid\" value=\"$forumid\">\n");
  417. print("<input type=\"checkbox\" name=\"sure\" value=\"1\">I'm sure\n");
  418. print("<input type=\"submit\" value=\"Okay\">\n");
  419. print("</form>\n");
  420. print("</td></tr>\n");
  421. print("</table>\n");
  422. }
  423. end_frame();
  424. end_main_frame();
  425. print($pagemenu);
  426. if ($locked && get_user_class() < UC_MODERATOR)
  427. print("<p>This topic is locked; no new posts are allowed.</p>\n");
  428. else
  429. {
  430. $arr = get_forum_access_levels($forumid) or die;
  431. if (get_user_class() < $arr["write"])
  432. print("<p><i>You are not permitted to post in this forum.</i></p>\n");
  433. else
  434. $maypost = true;
  435. }
  436. //------ "View unread" / "Add reply" buttons
  437. print("<p><table class=\"main\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>\n");
  438. print("<td class=\"embedded\"><form method=\"get\" action=\"?\" />\n");
  439. print("<input type=\"hidden\" name=\"action\" value=\"viewunread\" />\n");
  440. print("<fieldset><input type=\"submit\" value=\"View Unread\" /></fieldset>\n");
  441. print("</form></td>\n");
  442. if ($maypost)
  443. {
  444. print("<td class=\"embedded\" style=\"padding-left: 10px\"><form method=\"get\" action=\"?\">\n");
  445. print("<input type=\"hidden\" name=\"action\" value=\"reply\">\n");
  446. print("<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n");
  447. print("<input type=\"submit\" value=\"Add Reply\" class=\"btn\">\n");
  448. print("</form></td>\n");
  449. }
  450. print("</tr></table></p>\n");
  451. //------ Forum quick jump drop-down
  452. insert_quick_jump_menu($forumid);
  453. stdfoot();
  454. die;
  455. }
  456. //-------- Action: Quote
  457. if ($action == "quotepost")
  458. {
  459. $topicid = $_GET["topicid"];
  460. if (!is_valid_id($topicid))
  461. stderr("Error", "Invalid topic ID $topicid.");
  462. stdhead("Post reply");
  463. begin_main_frame();
  464. insert_compose_frame($topicid, false, true);
  465. end_main_frame();
  466. stdfoot();
  467. die;
  468. }
  469. //-------- Action: Reply
  470. if ($action == "reply")
  471. {
  472. $topicid = $_GET["topicid"];
  473. if (!is_valid_id($topicid))
  474. die;
  475. stdhead("Post reply");
  476. begin_main_frame();
  477. insert_compose_frame($topicid, false);
  478. end_main_frame();
  479. stdfoot();
  480. die;
  481. }
  482. //-------- Action: Move topic
  483. if ($action == "movetopic")
  484. {
  485. $forumid = $_POST["forumid"];
  486. $topicid = $_GET["topicid"];
  487. if (!is_valid_id($forumid) || !is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
  488. die;
  489. // Make sure topic and forum is valid
  490. $res = @mysql_query("SELECT minclasswrite FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
  491. if (mysql_num_rows($res) != 1)
  492. stderr("Error", "Forum not found.");
  493. $arr = mysql_fetch_row($res);
  494. if (get_user_class() < $arr[0])
  495. die;
  496. $res = @mysql_query("SELECT subject,forumid FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  497. if (mysql_num_rows($res) != 1)
  498. stderr("Error", "Topic not found.");
  499. $arr = mysql_fetch_assoc($res);
  500. if ($arr["forumid"] != $forumid)
  501. @mysql_query("UPDATE topics SET forumid=$forumid WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  502. // Redirect to forum page
  503. header("Location: $DEFAULTBASEURL/forums?action=viewforum&forumid=$forumid");
  504. die;
  505. }
  506. //-------- Action: Delete topic
  507. if ($action == "deletetopic")
  508. {
  509. $topicid = $_GET["topicid"];
  510. $forumid = $_GET["forumid"];
  511. if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
  512. die;
  513. $sure = $_GET["sure"];
  514. if (!$sure)
  515. {
  516. stderr("Delete topic", "Sanity check: You are about to delete a topic. Click\n" .
  517. "<a href=?action=deletetopic&amp;topicid=$topicid&amp;sure=1>here</a> if you are sure.");
  518. }
  519. mysql_query("DELETE FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  520. mysql_query("DELETE FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  521. header("Location: $DEFAULTBASEURL/forums?action=viewforum&forumid=$forumid");
  522. die;
  523. }
  524. //-------- Action: Edit post
  525. if ($action == "editpost")
  526. {
  527. $postid = $HTTP_GET_VARS["postid"];
  528. if (!is_valid_id($postid))
  529. die;
  530. $res = mysql_query("SELECT * FROM posts WHERE id=$postid") or sqlerr(__FILE__, __LINE__);
  531. if (mysql_num_rows($res) != 1)
  532. stderr("Error", "No post with ID $postid.");
  533. $arr = mysql_fetch_assoc($res);
  534. $res2 = mysql_query("SELECT locked FROM topics WHERE id = " . $arr["topicid"]) or sqlerr(__FILE__, __LINE__);
  535. $arr2 = mysql_fetch_assoc($res2);
  536. if (mysql_num_rows($res) != 1)
  537. stderr("Error", "No topic associated with post ID $postid.");
  538. $locked = ($arr2["locked"] == 'yes');
  539. if (($CURUSER["id"] != $arr["userid"] || $locked) && get_user_class() < UC_MODERATOR)
  540. stderr("Error", "Denied!");
  541. if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'POST')
  542. {
  543. $body = $HTTP_POST_VARS['body'];
  544. if ($body == "")
  545. stderr("Error", "Body cannot be empty!");
  546. $body = sqlesc($body);
  547. $editedat = sqlesc(get_date_time());
  548. mysql_query("UPDATE posts SET body = '$body', editedat = '$editedat', editedby = '$CURUSER[id]' WHERE id = '$postid'") or sqlerr(__FILE__, __LINE__);
  549. $returnto = $HTTP_POST_VARS["returnto"];
  550. if ($returnto != "")
  551. {
  552. $returnto .= "&amp;page=p$postid#$postid";
  553. header("Location: $returnto");
  554. }
  555. else
  556. stderr("Success", "Post was edited successfully.");
  557. }
  558. stdhead();
  559. print("<h1>Edit Post</h1>\n");
  560. print("<form method=\"post\" action=\"?action=editpost&amp;postid=$postid\">\n");
  561. print("<input type=\"hidden\" name=\"returnto\" value=\"" . htmlspecialchars($HTTP_SERVER_VARS["HTTP_REFERER"]) . "\">\n");
  562. print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n");
  563. print("<tr><td style=\"padding: 0px\"><textarea name=\"body\" cols=\"100\" rows=\"20\" style=\"border: 0px\">" . htmlspecialchars($arr["body"]) . "</textarea></td></tr>\n");
  564. print("<tr><td align=\"center\"><input type=\"submit\" value=\"Okay\" class=\"btn\"></td></tr>\n");
  565. print("</table>\n");
  566. print("</form>\n");
  567. stdfoot();
  568. die;
  569. }
  570. //-------- Action: Delete post
  571. if ($action == "deletepost")
  572. {
  573. $postid = $_GET["postid"];
  574. $sure = $_GET["sure"];
  575. if (get_user_class() < UC_MODERATOR || !is_valid_id($postid))
  576. die;
  577. //------- Get topic id
  578. $res = mysql_query("SELECT topicid FROM posts WHERE id=$postid") or sqlerr(__FILE__, __LINE__);
  579. $arr = mysql_fetch_row($res) or stderr("Error", "Post not found");
  580. $topicid = $arr[0];
  581. //------- We can not delete the post if it is the only one of the topic
  582. $res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  583. $arr = mysql_fetch_row($res);
  584. if ($arr[0] < 2)
  585. stderr("Error", "Can't delete post; it is the only post of the topic. You should\n" .
  586. "<a href=\"?action=deletetopic&amp;topicid=$topicid&amp;sure=1\">delete the topic</a> instead.\n");
  587. //------- Get the id of the last post before the one we're deleting
  588. $res = mysql_query("SELECT id FROM posts WHERE topicid=$topicid AND id < $postid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
  589. if (mysql_num_rows($res) == 0)
  590. $redirtopost = "";
  591. else
  592. {
  593. $arr = mysql_fetch_row($res);
  594. $redirtopost = "&amp;page=p$arr[0]#$arr[0]";
  595. }
  596. //------- Make sure we know what we do :-)
  597. if (!$sure)
  598. {
  599. stderr("Delete post", "Sanity check: You are about to delete a post. Click\n" .
  600. "<a href=\"?action=deletepost&amp;postid=$postid&amp;sure=1\">here</a> if you are sure.");
  601. }
  602. //------- Delete post
  603. mysql_query("DELETE FROM posts WHERE id=$postid") or sqlerr(__FILE__, __LINE__);
  604. //------- Update topic
  605. update_topic_last_post($topicid);
  606. header("Location: $DEFAULTBASEURL/forums?action=viewtopic&amp;topicid=$topicid$redirtopost");
  607. die;
  608. }
  609. //-------- Action: Lock topic
  610. if ($action == "locktopic")
  611. {
  612. $forumid = $_GET["forumid"];
  613. $topicid = $_GET["topicid"];
  614. $page = $_GET["page"];
  615. if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
  616. die;
  617. mysql_query("UPDATE topics SET locked='yes' WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  618. header("Location: $DEFAULTBASEURL/forums?action=viewforum&forumid=$forumid&page=$page");
  619. die;
  620. }
  621. //-------- Action: Unlock topic
  622. if ($action == "unlocktopic")
  623. {
  624. $forumid = $_GET["forumid"];
  625. $topicid = $_GET["topicid"];
  626. $page = $_GET["page"];
  627. if (!is_valid_id($topicid) || get_user_class() < UC_MODERATOR)
  628. die;
  629. mysql_query("UPDATE topics SET locked='no' WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  630. header("Location: $DEFAULTBASEURL/forums?action=viewforum&forumid=$forumid&page=$page");
  631. die;
  632. }
  633. //-------- Action: Set locked on/off
  634. if ($action == "setlocked")
  635. {
  636. $topicid = 0 + $HTTP_POST_VARS["topicid"];
  637. if (!$topicid || get_user_class() < UC_MODERATOR)
  638. die;
  639. $locked = sqlesc($HTTP_POST_VARS["locked"]);
  640. mysql_query("UPDATE topics SET locked=$locked WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  641. header("Location: $HTTP_POST_VARS[returnto]");
  642. die;
  643. }
  644. //-------- Action: Set sticky on/off
  645. if ($action == "setsticky")
  646. {
  647. $topicid = 0 + $HTTP_POST_VARS["topicid"];
  648. if (!topicid || get_user_class() < UC_MODERATOR)
  649. die;
  650. $sticky = sqlesc($HTTP_POST_VARS["sticky"]);
  651. mysql_query("UPDATE topics SET sticky=$sticky WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
  652. header("Location: $HTTP_POST_VARS[returnto]");
  653. die;
  654. }
  655. //-------- Action: Rename topic
  656. if ($action == 'renametopic')
  657. {
  658. if (get_user_class() < UC_MODERATOR)
  659. die;
  660. $topicid = $HTTP_POST_VARS['topicid'];
  661. if (!is_valid_id($topicid))
  662. die;
  663. $subject = $HTTP_POST_VARS['subject'];
  664. if ($subject == '')
  665. stderr('Error', 'You must enter a new title!');
  666. $subject = sqlesc($subject);
  667. mysql_query("UPDATE topics SET subject=$subject WHERE id=$topicid") or sqlerr();
  668. $returnto = $HTTP_POST_VARS['returnto'];
  669. if ($returnto)
  670. header("Location: $returnto");
  671. die;
  672. }
  673. //-------- Action: View forum
  674. if ($action == "viewforum")
  675. {
  676. $forumid = $_GET["forumid"];
  677. if (!is_valid_id($forumid))
  678. die;
  679. $page = $_GET["page"];
  680. $userid = $CURUSER["id"];
  681. //------ Get forum name
  682. $res = mysql_query("SELECT name, minclassread FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
  683. $arr = mysql_fetch_assoc($res) or die;
  684. $forumname = $arr["name"];
  685. if (get_user_class() < $arr["minclassread"])
  686. die("Not permitted");
  687. //------ Page links
  688. //------ Get topic count
  689. $perpage = $CURUSER["topicsperpage"];
  690. if (!$perpage) $perpage = 20;
  691. $res = mysql_query("SELECT COUNT(*) FROM topics WHERE forumid=$forumid") or sqlerr(__FILE__, __LINE__);
  692. $arr = mysql_fetch_row($res);
  693. $num = $arr[0];
  694. if ($page == 0)
  695. $page = 1;
  696. $first = ($page * $perpage) - $perpage + 1;
  697. $last = $first + $perpage - 1;
  698. if ($last > $num)
  699. $last = $num;
  700. $pages = floor($num / $perpage);
  701. if ($perpage * $pages < $num)
  702. ++$pages;
  703. //------ Build menu
  704. $menu = "<p class=\"txtCenter\"><b>\n";
  705. $lastspace = false;
  706. for ($i = 1; $i <= $pages; ++$i)
  707. {
  708. if ($i == $page)
  709. $menu .= "<span>$i</span>\n";
  710. elseif ($i > 3 && ($i < $pages - 2) && ($page - $i > 3 || $i - $page > 3))
  711. {
  712. if ($lastspace)
  713. continue;
  714. $menu .= "... \n";
  715. $lastspace = true;
  716. }
  717. else
  718. {
  719. $menu .= "<a href=\"?action=viewforum&amp;forumid=$forumid&amp;page=$i\">$i</a>\n";
  720. $lastspace = false;
  721. }
  722. if ($i < $pages)
  723. $menu .= "</b>|<b>\n";
  724. }
  725. $menu .= "<br />\n";
  726. if ($page == 1)
  727. $menu .= "<span>&lt;&lt; Prev</span>";
  728. else
  729. $menu .= "<a href=\"?action=viewforum&amp;forumid=$forumid&amp;page=" . ($page - 1) . "\">&lt;&lt; Prev</a>";
  730. $menu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  731. if ($last == $num)
  732. $menu .= "<span>Next &gt;&gt;</span>";
  733. else
  734. $menu .= "<a href=\"?action=viewforum&amp;forumid=$forumid&amp;page=" . ($page + 1) . "\">Next &gt;&gt;</a>";
  735. $menu .= "</b></p>\n";
  736. $offset = $first - 1;
  737. //------ Get topics data
  738. $topicsres = mysql_query("SELECT * FROM topics WHERE forumid=$forumid ORDER BY sticky, lastpost DESC LIMIT $offset,$perpage") or
  739. stderr("SQL Error", mysql_error());
  740. stdhead("Forum");
  741. $numtopics = mysql_num_rows($topicsres);
  742. print("<h1>$forumname</h1>\n");
  743. if ($numtopics > 0)
  744. {
  745. print($menu);
  746. print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">");
  747. print("<tr><td class=\"colhead\" align=\"left\">Topic</td><td class=\"colhead\">Replies</td><td class=\"colhead\">Views</td>\n" .
  748. "<td class=\"colhead\" align=\"left\">Author</td><td class=\"colhead\" align=\"left\">Last&nbsp;post</td>\n");
  749. print("</tr>\n");
  750. while ($topicarr = mysql_fetch_assoc($topicsres))
  751. {
  752. $topicid = $topicarr["id"];
  753. $topic_userid = $topicarr["userid"];
  754. $topic_views = $topicarr["views"];
  755. $views = number_format($topic_views);
  756. $locked = $topicarr["locked"] == "yes";
  757. $sticky = $topicarr["sticky"] == "yes";
  758. //---- Get reply count
  759. $res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  760. $arr = mysql_fetch_row($res);
  761. $posts = $arr[0];
  762. $replies = max(0, $posts - 1);
  763. $tpages = floor($posts / $postsperpage);
  764. if ($tpages * $postsperpage != $posts)
  765. ++$tpages;
  766. if ($tpages > 1)
  767. {
  768. $topicpages = " (<img src=\"images/multipage.gif\">";
  769. for ($i = 1; $i <= $tpages; ++$i)
  770. $topicpages .= " <a href=\"?action=viewtopic&amp;topicid=$topicid&amp;page=$i\">$i</a>";
  771. $topicpages .= ")";
  772. }
  773. else
  774. $topicpages = "";
  775. //---- Get userID and date of last post
  776. $res = mysql_query("SELECT * FROM posts WHERE topicid=$topicid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
  777. $arr = mysql_fetch_assoc($res);
  778. $lppostid = 0 + $arr["id"];
  779. $lpuserid = 0 + $arr["userid"];
  780. $lpadded = "" . $arr["added"] . "";
  781. //------ Get name of last poster
  782. $res = mysql_query("SELECT * FROM users WHERE id=$lpuserid") or sqlerr(__FILE__, __LINE__);
  783. if (mysql_num_rows($res) == 1)
  784. {
  785. $arr = mysql_fetch_assoc($res);
  786. $lpusername = "<a href=\"userdetails?id=$lpuserid\"><b>$arr[username]</b></a>";
  787. }
  788. else
  789. $lpusername = "unknown[$topic_userid]";
  790. //------ Get author
  791. $res = mysql_query("SELECT username FROM users WHERE id=$topic_userid") or sqlerr(__FILE__, __LINE__);
  792. if (mysql_num_rows($res) == 1)
  793. {
  794. $arr = mysql_fetch_assoc($res);
  795. $lpauthor = "<a href=\"userdetails?id=$topic_userid\"><b>$arr[username]</b></a>";
  796. }
  797. else
  798. $lpauthor = "unknown[$topic_userid]";
  799. //---- Print row
  800. $r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  801. $a = mysql_fetch_row($r);
  802. $new = !$a || $lppostid > $a[0];
  803. $topicpic = ($locked ? ($new ? "lockednew" : "locked") : ($new ? "unlockednew" : "unlocked"));
  804. $subject = ($sticky ? "Sticky: " : "") . "<a href=\"?action=viewtopic&amp;topicid=$topicid\"><b>" .
  805. encodehtml($topicarr["subject"]) . "</b></a>$topicpages";
  806. print("<tr><td align=\"left\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>" .
  807. "<td class=\"embedded\" style=\"padding-right: 5px\"><img src=\"images/$topicpic.gif\" alt=\"$topicpic\"/>" .
  808. "</td><td class=\"embedded\" align=\"left\">\n" .
  809. "$subject</td></tr></table></td><td align=\"right\">$replies</td>\n" .
  810. "<td align=\"right\">$views</td><td align=\"left\">$lpauthor</td>\n" .
  811. "<td align=\"left\">$lpadded<br />by&nbsp;$lpusername</td>\n");
  812. print("</tr>\n");
  813. } // while
  814. print("</table>\n");
  815. print($menu);
  816. } // if
  817. else
  818. print("<p class=\"txtCenter\">No topics found</p>\n");
  819. print("<table class=\"main\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"middle\">\n");
  820. print("<td class=\"embedded\"><img src=\"images/unlockednew.gif\" style=\"margin-right: 5px\" alt=\"New Posts\" /></td><td class=\"embedded\">New posts</td>\n");
  821. print("<td class=\"embedded\"><img src=\"images/locked.gif\" alt=\"Locked\" style=\"margin-left: 10px; margin-right: 5px\" />" .
  822. "</td><td class=\"embedded\">Locked topic</td>\n");
  823. print("</tr></table>\n");
  824. $arr = get_forum_access_levels($forumid) or die;
  825. $maypost = get_user_class() >= $arr["write"] && get_user_class() >= $arr["create"];
  826. if (!$maypost)
  827. print("<p><i>You are not permitted to start new topics in this forum.</i></p>\n");
  828. print("<table border=\"0\" class=\"main\" cellspacing=\"0\" cellpadding=\"0\"><tr>\n");
  829. print("<td class=\"embedded\"><form method=\"get\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\"viewunread\"><input type=\"submit\" value=\"View unread\" class=\"btn\"></form></td>\n");
  830. if ($maypost)
  831. print("<td class=\"embedded\"><form method=\"get\" action=\"?\">
  832. <input type=\"hidden\" name=\"action\" value=\"newtopic\">
  833. <input type=\"hidden\" name=\"forumid\" value=\"$forumid\">
  834. <input type=\"submit\" value=\"New topic\" class=\"btn\" style=\"margin-left: 10px\"></form></td>\n");
  835. print("</tr></table>\n");
  836. insert_quick_jump_menu($forumid);
  837. stdfoot();
  838. die;
  839. }
  840. //-------- Action: View unread posts
  841. if ($action == "viewunread")
  842. {
  843. $userid = $CURUSER['id'];
  844. $maxresults = 25;
  845. $res = mysql_query("SELECT id, forumid, subject, lastpost FROM topics ORDER BY lastpost") or sqlerr(__FILE__, __LINE__);
  846. stdhead();
  847. print("<h2>Topics with unread posts</h2>\n");
  848. $n = 0;
  849. $uc = get_user_class();
  850. while ($arr = mysql_fetch_assoc($res))
  851. {
  852. $topicid = $arr['id'];
  853. $forumid = $arr['forumid'];
  854. //---- Check if post is read
  855. $r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$userid AND topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  856. $a = mysql_fetch_row($r);
  857. if ($a && $a[0] == $arr['lastpost'])
  858. continue;
  859. //---- Check access & get forum name
  860. $r = mysql_query("SELECT name, minclassread FROM forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
  861. $a = mysql_fetch_assoc($r);
  862. if ($uc < $a['minclassread'])
  863. continue;
  864. ++$n;
  865. if ($n > $maxresults)
  866. break;
  867. $forumname = $a['name'];
  868. if ($n == 1)
  869. {
  870. ?><table border="1" cellspacing="0" cellpadding="5">
  871. <tr><td class="colhead" align="left">Topic</td><td class="colhead" align="left">Forum</td></tr>
  872. <?php
  873. }
  874. ?>
  875. <tr><td align="left"><table border="0" cellspacing="0" cellpadding="0"><tr><td class="embedded">
  876. <img src="images/unlockednew.gif" style="margin-right: 5px" alt="Unlocked Topic"/></td><td class="embedded">
  877. <a href="?action=viewtopic&amp;topicid=<?=$topicid?>&amp;page=last#last"><b><?=htmlspecialchars($arr["subject"])?>
  878. </b></a></td></tr></table></td><td align="left"><a href="?action=viewforum&amp;forumid=<?=$forumid?>"><b><?=$forumname?></b></a></td></tr>
  879. <?php
  880. }
  881. if ($n > 0)
  882. {
  883. print("</table>\n");
  884. if ($n > $maxresults)
  885. print("<p>More than $maxresults items found, displaying first $maxresults.</p>\n");
  886. print("<p><a href=\"?catchup\"><b>Catch up</b></a></p>\n");
  887. }
  888. else
  889. print("<b>Nothing found</b>");
  890. stdfoot();
  891. die;
  892. }
  893. if ($action == "search")
  894. {
  895. stdhead("Forum Search");
  896. print("<h2>Forum Search (<span class=\"red\">BETA</span>)</h2>\n");
  897. $keywords = trim($HTTP_GET_VARS["keywords"]);
  898. if ($keywords != "")
  899. {
  900. $perpage = 50;
  901. $page = max(1, 0 + $HTTP_GET_VARS["page"]);
  902. $ekeywords = sqlesc($keywords);
  903. print("<p><b>Searched for \"" . htmlspecialchars($keywords) . "\"</b></p>\n");
  904. $res = mysql_query("SELECT COUNT(*) FROM posts WHERE MATCH (body) AGAINST ($ekeywords)") or sqlerr(__FILE__, __LINE__);
  905. $arr = mysql_fetch_row($res);
  906. $hits = 0 + $arr[0];
  907. if ($hits == 0)
  908. print("<p><b>Sorry, nothing found!</b></p>");
  909. else
  910. {
  911. $pages = 0 + ceil($hits / $perpage);
  912. if ($page > $pages) $page = $pages;
  913. for ($i = 1; $i <= $pages; ++$i)
  914. if ($page == $i)
  915. $pagemenu1 .= "<span><b>$i</b></span>\n";
  916. else
  917. $pagemenu1 .= "<a href=\"/forums?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=$i\"><b>$i</b></a>\n";
  918. if ($page == 1)
  919. $pagemenu2 = "<span><b>&lt;&lt; Prev</b></span>\n";
  920. else
  921. $pagemenu2 = "<a href=\"/forums?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=" . ($page - 1) . "\"><b>&lt;&lt; Prev</b></a>\n";
  922. $pagemenu2 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
  923. if ($page == $pages)
  924. $pagemenu2 .= "<span><b>Next &gt;&gt;</b></span>\n";
  925. else
  926. $pagemenu2 .= "<a href=\"/forums?action=search&amp;keywords=" . htmlspecialchars($keywords) . "&amp;page=" . ($page + 1) . "\"><b>Next &gt;&gt;</b></a>\n";
  927. $offset = ($page * $perpage) - $perpage;
  928. $res = mysql_query("SELECT id, topicid,userid,added FROM posts WHERE MATCH (body) AGAINST ($ekeywords) LIMIT $offset,$perpage") or sqlerr(__FILE__, __LINE__);
  929. $num = mysql_num_rows($res);
  930. print("<p>$pagemenu1<br />$pagemenu2</p>");
  931. print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n");
  932. print("<tr><td class=\"colhead\">Post</td><td class=\"colhead\" align=\"left\">Topic</td><td class=\"colhead\" align=\"left\">Forum</td><td class=\"colhead\" align=\"left\">Posted by</td></tr>\n");
  933. for ($i = 0; $i < $num; ++$i)
  934. {
  935. $post = mysql_fetch_assoc($res);
  936. $res2 = mysql_query("SELECT forumid, subject FROM topics WHERE id=$post[topicid]") or
  937. sqlerr(__FILE__, __LINE__);
  938. $topic = mysql_fetch_assoc($res2);
  939. $res2 = mysql_query("SELECT name,minclassread FROM forums WHERE id=$topic[forumid]") or
  940. sqlerr(__FILE__, __LINE__);
  941. $forum = mysql_fetch_assoc($res2);
  942. if ($forum["name"] == "" || $forum["minclassread"] > $CURUSER["class"])
  943. {
  944. --$hits;
  945. continue;
  946. }
  947. $res2 = mysql_query("SELECT username FROM users WHERE id=$post[userid]") or
  948. sqlerr(__FILE__, __LINE__);
  949. $user = mysql_fetch_assoc($res2);
  950. if ($user["username"] == "")
  951. $user["username"] = "[$post[userid]]";
  952. print("<tr><td>$post[id]</td><td align=\"left\"><a href=\"?action=viewtopic&amp;topicid=$post[topicid]&amp;page=p$post[id]#$post[id]\"><b>" . htmlspecialchars($topic["subject"]) . "</b></a></td><td align=\"left\"><a href=\"?action=viewforum&amp;forumid=$topic[forumid]\"><b>" . htmlspecialchars($forum["name"]) . "</b></a><td align=\"left\"><a href=\"userdetails?id=$post[userid]\"><b>$user[username]</b></a><br />at $post[added]</tr>\n");
  953. }
  954. print("</table>\n");
  955. print("<p>$pagemenu2<br />$pagemenu1</p>");
  956. print("<p>Found $hits post" . ($hits != 1 ? "s" : "") . ".</p>");
  957. print("<p><b>Search again</b></p>\n");
  958. }
  959. }
  960. print("<form method=\"get action=\"/forums\">\n");
  961. print("<input type=\"hidden\" name=\"action\" value=\"search\">\n");
  962. print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n");
  963. print("<tr><td class=\"rowhead\">Key words</td><td align=\"left\"><input type=\"text\" size=\"55\" name=\"keywords\" value=\"" . htmlspecialchars($keywords) .
  964. "\"><br />\n" .
  965. "<span class=\"small\" size=\"-1\">Enter one or more words to search for.<br />Very common words and words with less than 3 characters are ignored.</span></td></tr>\n");
  966. print("<tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\"Search\"></td></tr>\n");
  967. print("</table>\n</form>\n");
  968. stdfoot();
  969. die;
  970. }
  971. //-------- Handle unknown action
  972. if ($action != "")
  973. stderr("Forum Error", "Unknown action '$action'.");
  974. //-------- Default action: View forums
  975. if (isset($_GET["catchup"]))
  976. catch_up();
  977. //-------- Get forums
  978. $forums_res = mysql_query("SELECT * FROM forums ORDER BY sort, name") or sqlerr(__FILE__, __LINE__);
  979. stdhead("Forums");
  980. print("<h2>Forums</h2>\n");
  981. print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n");
  982. print("<tr><td class=\"colhead\" align=\"left\">Forum</td><td class=\"colhead\" align=\"right\">Topics</td>" .
  983. "<td class=\"colhead\" align=\"right\">Posts</td>" .
  984. "<td class=\"colhead\" align=\"left\">Last post</td></tr>\n");
  985. while ($forums_arr = mysql_fetch_assoc($forums_res))
  986. {
  987. if (get_user_class() < $forums_arr["minclassread"])
  988. continue;
  989. $forumid = $forums_arr["id"];
  990. $forumname = htmlspecialchars($forums_arr["name"]);
  991. $forumdescription = htmlspecialchars($forums_arr["description"]);
  992. $topiccount = number_format($forums_arr["topiccount"]);
  993. $postcount = number_format($forums_arr["postcount"]);
  994. /*
  995. while ($topicids_arr = mysql_fetch_assoc($topicids_res))
  996. {
  997. $topicid = $topicids_arr['id'];
  998. $postcount_res = mysql_query("SELECT COUNT(*) FROM posts WHERE topicid=$topicid") or sqlerr(__FILE__, __LINE__);
  999. $postcount_arr = mysql_fetch_row($postcount_res);
  1000. $postcount += $postcount_arr[0];
  1001. }
  1002. */
  1003. $postcount = number_format($postcount);
  1004. // Find last post ID
  1005. $lastpostid = get_forum_last_post($forumid);
  1006. // Get last post info
  1007. $post_res = mysql_query("SELECT added,topicid,userid FROM posts WHERE id=$lastpostid") or sqlerr(__FILE__, __LINE__);
  1008. if (mysql_num_rows($post_res) == 1)
  1009. {
  1010. $post_arr = mysql_fetch_assoc($post_res) or die("Bad forum last_post");
  1011. $lastposterid = $post_arr["userid"];
  1012. $lastpostdate = $post_arr["added"];
  1013. $lasttopicid = $post_arr["topicid"];
  1014. $user_res = mysql_query("SELECT username FROM users WHERE id=$lastposterid") or sqlerr(__FILE__, __LINE__);
  1015. $user_arr = mysql_fetch_assoc($user_res);
  1016. $lastposter = htmlspecialchars($user_arr['username']);
  1017. $topic_res = mysql_query("SELECT subject FROM topics WHERE id=$lasttopicid") or sqlerr(__FILE__, __LINE__);
  1018. $topic_arr = mysql_fetch_assoc($topic_res);
  1019. $lasttopic = htmlspecialchars($topic_arr['subject']);
  1020. $lastpost = "$lastpostdate<br />" .
  1021. "by <a href=\"userdetails?id=$lastposterid\"><b>$lastposter</b></a><br />" .
  1022. "in <a href=\"?action=viewtopic&amp;topicid=$lasttopicid&amp;page=p$lastpostid#$lastpostid\"><b>$lasttopic</b></a>";
  1023. $r = mysql_query("SELECT lastpostread FROM readposts WHERE userid=$CURUSER[id] AND topicid=$lasttopicid") or sqlerr(__FILE__, __LINE__);
  1024. $a = mysql_fetch_row($r);
  1025. if ($a && $a[0] >= $lastpostid)
  1026. $img = "unlocked";
  1027. else
  1028. $img = "unlockednew";
  1029. }
  1030. else
  1031. {
  1032. $lastpost = "N/A";
  1033. $img = "unlocked";
  1034. }
  1035. print("<tr>
  1036. <td align=\"left\">
  1037. <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  1038. <tr>
  1039. <td class=\"embedded\" style=\"padding-right: 5px\">
  1040. <img src=\"images/$img.gif\" alt=\"$img\"/>
  1041. </td>
  1042. <td class=\"embedded\">
  1043. <a href=\"?action=viewforum&amp;forumid=$forumid\"><b>$forumname</b></a><br />$forumdescription
  1044. </td>
  1045. </tr>
  1046. </table>
  1047. </td>
  1048. <td align=\"right\">
  1049. $topiccount
  1050. </td>
  1051. <td align=\"right\">$postcount</td>" .
  1052. "<td align=\"left\">$lastpost</td></tr>\n");
  1053. }
  1054. print("</table>\n");
  1055. print("<p class=\"txtCenter\"><a href=\"?action=search\"><b>Search</b></a> | <a href=\"?action=viewunread\"><b>View unread</b></a> | <a href=\"?catchup\"><b>Catch up</b></a></p>");
  1056. stdfoot();
  1057. ?>