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

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