PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/Forums.php

https://bitbucket.org/nexea/x00n
PHP | 1410 lines | 1327 code | 29 blank | 54 comment | 75 complexity | 58d061b08f24562d471758088ed0b886 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file