BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. // @todo Clean up this fucking mess
  3. /*
  4. Forums search result page
  5. */
  6. list($Page, $Limit) = Format::page_limit(POSTS_PER_PAGE);
  7. if (isset($_GET['type']) && $_GET['type'] === 'body') {
  8. $Type = 'body';
  9. } else {
  10. $Type = 'title';
  11. }
  12. // What are we looking for? Let's make sure it isn't dangerous.
  13. if (isset($_GET['search'])) {
  14. $Search = trim($_GET['search']);
  15. } else {
  16. $Search = '';
  17. }
  18. $ThreadAfterDate = db_string($_GET['thread_created_after']);
  19. $ThreadBeforeDate = db_string($_GET['thread_created_before']);
  20. if ((!empty($ThreadAfterDate) && !is_valid_date($ThreadAfterDate)) || (!empty($ThreadBeforeDate) && !is_valid_date($ThreadBeforeDate))) {
  21. error("Incorrect topic created date");
  22. }
  23. $PostAfterDate = db_string($_GET['post_created_after']);
  24. $PostBeforeDate = db_string($_GET['post_created_before']);
  25. if ((!empty($PostAfterDate) && !is_valid_date($PostAfterDate)) || (!empty($PostBeforeDate) && !is_valid_date($PostBeforeDate))) {
  26. error("Incorrect post created date");
  27. }
  28. // Searching for posts by a specific user
  29. if (!empty($_GET['user'])) {
  30. $User = trim($_GET['user']);
  31. $DB->query("
  32. SELECT ID
  33. FROM users_main
  34. WHERE Username = '".db_string($User)."'");
  35. list($AuthorID) = $DB->next_record();
  36. if ($AuthorID === null) {
  37. $AuthorID = 0;
  38. // This will cause the search to return 0 results
  39. // Workaround in line 276 to display that the username was wrong
  40. }
  41. } else {
  42. $User = '';
  43. }
  44. // Are we looking in individual forums?
  45. if (isset($_GET['forums']) && is_array($_GET['forums'])) {
  46. $ForumArray = [];
  47. foreach ($_GET['forums'] as $Forum) {
  48. if (is_number($Forum)) {
  49. $ForumArray[] = $Forum;
  50. }
  51. }
  52. if (count($ForumArray) > 0) {
  53. $SearchForums = implode(', ', $ForumArray);
  54. }
  55. }
  56. // Searching for posts in a specific thread
  57. if (!empty($_GET['threadid']) && is_number($_GET['threadid'])) {
  58. $ThreadID = $_GET['threadid'];
  59. $Type = 'body';
  60. $SQL = "
  61. SELECT
  62. Title
  63. FROM forums_topics AS t
  64. JOIN forums AS f ON f.ID = t.ForumID
  65. WHERE t.ID = $ThreadID
  66. AND " . Forums::user_forums_sql();
  67. $DB->query($SQL);
  68. if (list($Title) = $DB->next_record()) {
  69. $Title = " &gt; <a href=\"forums.php?action=viewthread&amp;threadid=$ThreadID\">$Title</a>";
  70. } else {
  71. error(404);
  72. }
  73. } else {
  74. $ThreadID = '';
  75. }
  76. // Let's hope we got some results - start printing out the content
  77. View::show_header('Forums &gt; Search', 'bbcode,forum_search');
  78. ?>
  79. <div class="thin">
  80. <div class="header">
  81. <h2><a href="forums.php">Forums</a> &gt; Search<?=$Title?></h2>
  82. </div>
  83. <form class="search_form" name="forums" action="" method="get">
  84. <input type="hidden" name="action" value="search" />
  85. <table cellpadding="6" cellspacing="1" border="0" class="layout border" width="100%">
  86. <tr>
  87. <td><strong>Search Terms</strong></td>
  88. <td>
  89. <input type="search" name="search" size="70" value="<?=display_str($Search)?>" />
  90. </td>
  91. </tr>
  92. <tr>
  93. <td><strong>Posted By</strong></td>
  94. <td>
  95. <input type="search" name="user" placeholder="Username" size="70" value="<?=display_str($User)?>" />
  96. </td>
  97. </tr>
  98. <tr>
  99. <td><strong>Topic Created</strong></td>
  100. <td>
  101. After
  102. <input type="text" name="thread_created_after" id="thread_created_after" placeholder="YYYY-MM-DD" pattern="[1-2][0-9]{3}-[0-9]{2}-[0-9]{2}" value="<?=$ThreadAfterDate?>" />&nbsp;&nbsp;
  103. Before
  104. <input type="text" name="thread_created_before" id="thread_created_before" placeholder="YYYY-MM-DD" pattern="[1-2][0-9]{3}-[0-9]{2}-[0-9]{2}" value="<?=$ThreadBeforeDate?>" />
  105. </td>
  106. </tr>
  107. <?php
  108. if (empty($ThreadID)) {
  109. ?>
  110. <tr>
  111. <td><strong>Search In</strong></td>
  112. <td>
  113. <input type="radio" name="type" id="type_title" value="title"<?php if ($Type == 'title') {
  114. echo ' checked="checked"';
  115. } ?> />
  116. <label for="type_title">Titles</label>
  117. <input type="radio" name="type" id="type_body" value="body"<?php if ($Type == 'body') {
  118. echo ' checked="checked"';
  119. } ?> />&nbsp;&nbsp;
  120. <label for="type_body">Body</label>
  121. </td>
  122. </tr>
  123. <tr id="post_created_row" <?php if ($Type == 'title') {
  124. echo "class='hidden'";
  125. } ?>>
  126. <td><strong>Post created:</strong></td>
  127. <td>
  128. After:
  129. <input type="text" class="date_picker" name="post_created_after" id="post_created_after" value="<?=$PostAfterDate?>" />
  130. Before:
  131. <input type="text" class="date_picker" name="post_created_before" id="post_created_before" value="<?=$PostBeforeDate?>" />
  132. </td>
  133. </tr>
  134. <tr>
  135. <td><strong>Forums</strong></td>
  136. <td>
  137. <table id="forum_search_cat_list" class="cat_list layout">
  138. <?php
  139. // List of forums
  140. $Open = false;
  141. $LastCategoryID = -1;
  142. $Columns = 0;
  143. $i = 0;
  144. foreach ($Forums as $Forum) {
  145. if (!Forums::check_forumperm($Forum['ID'])) {
  146. continue;
  147. }
  148. $Columns++;
  149. if ($Forum['CategoryID'] != $LastCategoryID) {
  150. $LastCategoryID = $Forum['CategoryID'];
  151. if ($Open) {
  152. if ($Columns % 5) { ?>
  153. <td colspan="<?=(5 - ($Columns % 5))?>"></td>
  154. <?php
  155. } ?>
  156. </tr>
  157. <?php
  158. }
  159. $Columns = 0;
  160. $Open = true;
  161. $i++; ?>
  162. <tr>
  163. <td colspan="5" class="forum_cat">
  164. <strong><?=$ForumCats[$Forum['CategoryID']]?></strong>
  165. <a href="#" class="brackets forum_category" id="forum_category_<?=$i?>">Check all</a>
  166. </td>
  167. </tr>
  168. <tr>
  169. <?php
  170. } elseif ($Columns % 5 == 0) { ?>
  171. </tr>
  172. <tr>
  173. <?php } ?>
  174. <td>
  175. <input type="checkbox" name="forums[]" value="<?=$Forum['ID']?>" data-category="forum_category_<?=$i?>" id="forum_<?=$Forum['ID']?>"<?php if (isset($_GET['forums']) && in_array($Forum['ID'], $_GET['forums'])) {
  176. echo ' checked="checked"';
  177. } ?> />
  178. <label for="forum_<?=$Forum['ID']?>"><?=htmlspecialchars($Forum['Name'])?></label>
  179. </td>
  180. <?php
  181. }
  182. if ($Columns % 5) { ?>
  183. <td colspan="<?=(5 - ($Columns % 5))?>"></td>
  184. <?php } ?>
  185. </tr>
  186. </table>
  187. <?php
  188. } else { ?>
  189. <input type="hidden" name="threadid" value="<?=$ThreadID?>" />
  190. <?php } ?>
  191. </td>
  192. </tr>
  193. <tr>
  194. <td colspan="2" class="center">
  195. <input type="submit" value="Search" />
  196. </td>
  197. </tr>
  198. </table>
  199. </form>
  200. <div class="linkbox">
  201. <?php
  202. // Break search string down into individual words
  203. $Words = explode(' ', db_string($Search));
  204. if ($Type == 'body') {
  205. $SQL = "
  206. SELECT
  207. SQL_CALC_FOUND_ROWS
  208. t.ID,
  209. ".(!empty($ThreadID) ? "SUBSTRING_INDEX(p.Body, ' ', 40)" : 't.Title').",
  210. t.ForumID,
  211. f.Name,
  212. p.AddedTime,
  213. p.ID,
  214. p.Body,
  215. t.CreatedTime
  216. FROM forums_posts AS p
  217. JOIN forums_topics AS t ON t.ID = p.TopicID
  218. JOIN forums AS f ON f.ID = t.ForumID
  219. WHERE " . Forums::user_forums_sql() . ' AND ';
  220. // In tests, this is significantly faster than LOCATE
  221. $SQL .= "p.Body LIKE '%";
  222. $SQL .= implode("%' AND p.Body LIKE '%", $Words);
  223. $SQL .= "%' ";
  224. //$SQL .= "LOCATE('";
  225. //$SQL .= implode("', p.Body) AND LOCATE('", $Words);
  226. //$SQL .= "', p.Body) ";
  227. if (isset($SearchForums)) {
  228. $SQL .= " AND f.ID IN ($SearchForums)";
  229. }
  230. if (isset($AuthorID)) {
  231. $SQL .= " AND p.AuthorID = '$AuthorID' ";
  232. }
  233. if (!empty($ThreadID)) {
  234. $SQL .= " AND t.ID = '$ThreadID' ";
  235. }
  236. if (!empty($ThreadAfterDate)) {
  237. $SQL .= " AND t.CreatedTime >= '$ThreadAfterDate'";
  238. }
  239. if (!empty($ThreadBeforeDate)) {
  240. $SQL .= " AND t.CreatedTime <= '$ThreadBeforeDate'";
  241. }
  242. if (!empty($PostAfterDate)) {
  243. $SQL .= " AND p.AddedTime >= '$PostAfterDate'";
  244. }
  245. if (!empty($PostBeforeDate)) {
  246. $SQL .= " AND p.AddedTime <= '$PostBeforeDate'";
  247. }
  248. $SQL .= "
  249. ORDER BY p.AddedTime DESC
  250. LIMIT $Limit";
  251. } else {
  252. $SQL = "
  253. SELECT
  254. SQL_CALC_FOUND_ROWS
  255. t.ID,
  256. t.Title,
  257. t.ForumID,
  258. f.Name,
  259. t.LastPostTime,
  260. '',
  261. '',
  262. t.CreatedTime
  263. FROM forums_topics AS t
  264. JOIN forums AS f ON f.ID = t.ForumID
  265. WHERE " . Forums::user_forums_sql() . ' AND ';
  266. $SQL .= "t.Title LIKE '%";
  267. $SQL .= implode("%' AND t.Title LIKE '%", $Words);
  268. $SQL .= "%' ";
  269. if (isset($SearchForums)) {
  270. $SQL .= " AND f.ID IN ($SearchForums)";
  271. }
  272. if (isset($AuthorID)) {
  273. $SQL .= " AND t.AuthorID = '$AuthorID' ";
  274. }
  275. if (!empty($ThreadAfterDate)) {
  276. $SQL .= " AND t.CreatedTime >= '$ThreadAfterDate'";
  277. }
  278. if (!empty($ThreadBeforeDate)) {
  279. $SQL .= " AND t.CreatedTime <= '$ThreadBeforeDate'";
  280. }
  281. $SQL .= "
  282. ORDER BY t.LastPostTime DESC
  283. LIMIT $Limit";
  284. }
  285. // Perform the query
  286. $Records = $DB->query($SQL);
  287. $DB->query('SELECT FOUND_ROWS()');
  288. list($Results) = $DB->next_record();
  289. $DB->set_query_id($Records);
  290. $Pages = Format::get_pages($Page, $Results, POSTS_PER_PAGE, 9);
  291. echo $Pages;
  292. ?>
  293. </div>
  294. <table cellpadding="6" cellspacing="1" border="0" class="forum_list border" width="100%">
  295. <tr class="colhead">
  296. <td>Forum</td>
  297. <td><?=((!empty($ThreadID)) ? 'Post begins' : 'Topic')?></td>
  298. <td>Topic creation time</td>
  299. <td>Last post time</td>
  300. </tr>
  301. <?php if (!$DB->has_results()) { ?>
  302. <tr><td colspan="4">Nothing found<?=((isset($AuthorID) && $AuthorID == 0) ? ' (unknown username)' : '')?>!</td></tr>
  303. <?php }
  304. while (list($ID, $Title, $ForumID, $ForumName, $LastTime, $PostID, $Body, $ThreadCreatedTime) = $DB->next_record()) {
  305. // Print results?>
  306. <tr class="row">
  307. <td>
  308. <a href="forums.php?action=viewforum&amp;forumid=<?=$ForumID?>"><?=$ForumName?></a>
  309. </td>
  310. <td>
  311. <?php if (empty($ThreadID)) { ?>
  312. <a href="forums.php?action=viewthread&amp;threadid=<?=$ID?>"><?=Format::cut_string($Title, 80); ?></a>
  313. <?php } else { ?>
  314. <?=Format::cut_string($Title, 80); ?>
  315. <?php
  316. }
  317. if ($Type == 'body') { ?>
  318. <a data-toggle-target="#post_<?=$PostID?>_text">(Show)</a> <span class="float_right tooltip last_read" title="Jump to post"><a href="forums.php?action=viewthread&amp;threadid=<?=$ID?><?php if (!empty($PostID)) {
  319. echo "&amp;postid=$PostID#post$PostID";
  320. } ?>"></a></span>
  321. <?php } ?>
  322. </td>
  323. <td>
  324. <?=time_diff($ThreadCreatedTime)?>
  325. </td>
  326. <td>
  327. <?=time_diff($LastTime)?>
  328. </td>
  329. </tr>
  330. <?php if ($Type == 'body') { ?>
  331. <tr class="row hidden" id="post_<?=$PostID?>_text">
  332. <td colspan="4"><?=Text::full_format($Body)?></td>
  333. </tr>
  334. <?php }
  335. }
  336. ?>
  337. </table>
  338. <div class="linkbox">
  339. <?=$Pages?>
  340. </div>
  341. </div>
  342. <?php View::show_footer(); ?>