PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/search.php

https://bitbucket.org/KamranMackey/mybb
PHP | 1593 lines | 1364 code | 158 blank | 71 comment | 252 complexity | c9e6569238e4871bcaa6a71a423ca456 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * MyBB 1.6
  4. * Copyright 2010 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://mybb.com
  7. * License: http://mybb.com/about/license
  8. *
  9. * $Id$
  10. */
  11. define("IN_MYBB", 1);
  12. define("IGNORE_CLEAN_VARS", "sid");
  13. define('THIS_SCRIPT', 'search.php');
  14. $templatelist = "search,forumdisplay_thread_gotounread,search_results_threads_thread,search_results_threads,search_results_posts,search_results_posts_post";
  15. $templatelist .= ",multipage_nextpage,multipage_page_current,multipage_page,multipage_start,multipage_end,multipage,forumdisplay_thread_multipage_more,forumdisplay_thread_multipage_page,forumdisplay_thread_multipage";
  16. $templatelist .= ",search_results_posts_inlinecheck,search_results_posts_nocheck,search_results_threads_inlinecheck,search_results_threads_nocheck,search_results_inlinemodcol,search_results_posts_inlinemoderation_custom_tool,search_results_posts_inlinemoderation_custom,search_results_posts_inlinemoderation,search_results_threads_inlinemoderation_custom_tool,search_results_threads_inlinemoderation_custom,search_results_threads_inlinemoderation,search_orderarrow,search_moderator_options";
  17. $templatelist .= ",forumdisplay_thread_attachment_count,forumdisplay_threadlist_inlineedit_js,search_threads_inlinemoderation_selectall";
  18. require_once "./global.php";
  19. require_once MYBB_ROOT."inc/functions_post.php";
  20. require_once MYBB_ROOT."inc/functions_search.php";
  21. require_once MYBB_ROOT."inc/class_parser.php";
  22. $parser = new postParser;
  23. // Load global language phrases
  24. $lang->load("search");
  25. add_breadcrumb($lang->nav_search, "search.php");
  26. switch($mybb->input['action'])
  27. {
  28. case "results":
  29. add_breadcrumb($lang->nav_results);
  30. break;
  31. default:
  32. break;
  33. }
  34. if($mybb->usergroup['cansearch'] == 0)
  35. {
  36. error_no_permission();
  37. }
  38. $now = TIME_NOW;
  39. $mybb->input['keywords'] = trim($mybb->input['keywords']);
  40. $limitsql = "";
  41. if(intval($mybb->settings['searchhardlimit']) > 0)
  42. {
  43. $limitsql = "ORDER BY t.dateline DESC LIMIT ".intval($mybb->settings['searchhardlimit']);
  44. }
  45. if($mybb->input['action'] == "results")
  46. {
  47. $sid = $db->escape_string($mybb->input['sid']);
  48. $query = $db->simple_select("searchlog", "*", "sid='$sid'");
  49. $search = $db->fetch_array($query);
  50. if(!$search['sid'])
  51. {
  52. error($lang->error_invalidsearch);
  53. }
  54. $plugins->run_hooks("search_results_start");
  55. // Decide on our sorting fields and sorting order.
  56. $order = my_strtolower(htmlspecialchars($mybb->input['order']));
  57. $sortby = my_strtolower(htmlspecialchars($mybb->input['sortby']));
  58. switch($sortby)
  59. {
  60. case "replies":
  61. $sortfield = "t.replies";
  62. break;
  63. case "views":
  64. $sortfield = "t.views";
  65. break;
  66. case "subject":
  67. if($search['resulttype'] == "threads")
  68. {
  69. $sortfield = "t.subject";
  70. }
  71. else
  72. {
  73. $sortfield = "p.subject";
  74. }
  75. break;
  76. case "forum":
  77. $sortfield = "t.fid";
  78. break;
  79. case "starter":
  80. if($search['resulttype'] == "threads")
  81. {
  82. $sortfield = "t.username";
  83. }
  84. else
  85. {
  86. $sortfield = "p.username";
  87. }
  88. break;
  89. case "lastpost":
  90. default:
  91. if($search['resulttype'] == "threads")
  92. {
  93. $sortfield = "t.lastpost";
  94. $sortby = "lastpost";
  95. }
  96. else
  97. {
  98. $sortfield = "p.dateline";
  99. $sortby = "dateline";
  100. }
  101. break;
  102. }
  103. if($order != "asc")
  104. {
  105. $order = "desc";
  106. $oppsortnext = "asc";
  107. $oppsort = $lang->asc;
  108. }
  109. else
  110. {
  111. $oppsortnext = "desc";
  112. $oppsort = $lang->desc;
  113. }
  114. if(!$mybb->settings['threadsperpage'])
  115. {
  116. $mybb->settings['threadsperpage'] = 20;
  117. }
  118. // Work out pagination, which page we're at, as well as the limits.
  119. $perpage = $mybb->settings['threadsperpage'];
  120. $page = intval($mybb->input['page']);
  121. if($page > 0)
  122. {
  123. $start = ($page-1) * $perpage;
  124. }
  125. else
  126. {
  127. $start = 0;
  128. $page = 1;
  129. }
  130. $end = $start + $perpage;
  131. $lower = $start+1;
  132. $upper = $end;
  133. // Work out if we have terms to highlight
  134. $highlight = "";
  135. if($search['keywords'])
  136. {
  137. if($mybb->settings['seourls'] == "yes" || ($mybb->settings['seourls'] == "auto" && $_SERVER['SEO_SUPPORT'] == 1))
  138. {
  139. $highlight = "?highlight=".urlencode($search['keywords']);
  140. }
  141. else
  142. {
  143. $highlight = "&amp;highlight=".urlencode($search['keywords']);
  144. }
  145. }
  146. $sorturl = "search.php?action=results&amp;sid={$sid}";
  147. $thread_url = "";
  148. $post_url = "";
  149. eval("\$orderarrow['$sortby'] = \"".$templates->get("search_orderarrow")."\";");
  150. // Read some caches we will be using
  151. $forumcache = $cache->read("forums");
  152. $icon_cache = $cache->read("posticons");
  153. $threads = array();
  154. if($mybb->user['uid'] == 0)
  155. {
  156. // Build a forum cache.
  157. $query = $db->query("
  158. SELECT fid
  159. FROM ".TABLE_PREFIX."forums
  160. WHERE active != 0
  161. ORDER BY pid, disporder
  162. ");
  163. $forumsread = my_unserialize($mybb->cookies['mybb']['forumread']);
  164. }
  165. else
  166. {
  167. // Build a forum cache.
  168. $query = $db->query("
  169. SELECT f.fid, fr.dateline AS lastread
  170. FROM ".TABLE_PREFIX."forums f
  171. LEFT JOIN ".TABLE_PREFIX."forumsread fr ON (fr.fid=f.fid AND fr.uid='{$mybb->user['uid']}')
  172. WHERE f.active != 0
  173. ORDER BY pid, disporder
  174. ");
  175. }
  176. while($forum = $db->fetch_array($query))
  177. {
  178. if($mybb->user['uid'] == 0)
  179. {
  180. if($forumsread[$forum['fid']])
  181. {
  182. $forum['lastread'] = $forumsread[$forum['fid']];
  183. }
  184. }
  185. $readforums[$forum['fid']] = $forum['lastread'];
  186. }
  187. $fpermissions = forum_permissions();
  188. // Inline Mod Column for moderators
  189. $inlinemodcol = $inlinecookie = '';
  190. $is_mod = $is_supermod = false;
  191. if($mybb->usergroup['issupermod'])
  192. {
  193. $is_supermod = true;
  194. }
  195. if($is_supermod || is_moderator())
  196. {
  197. eval("\$inlinemodcol = \"".$templates->get("search_results_inlinemodcol")."\";");
  198. $inlinecookie = "inlinemod_search".$sid;
  199. $inlinecount = 0;
  200. $is_mod = true;
  201. $return_url = 'search.php?'.htmlspecialchars_uni($_SERVER['QUERY_STRING']);
  202. }
  203. // Show search results as 'threads'
  204. if($search['resulttype'] == "threads")
  205. {
  206. $threadcount = 0;
  207. // Moderators can view unapproved threads
  208. $query = $db->simple_select("moderators", "fid", "(id='{$mybb->user['uid']}' AND isgroup='0') OR (id='{$mybb->user['usergroup']}' AND isgroup='1')");
  209. if($mybb->usergroup['issupermod'] == 1)
  210. {
  211. // Super moderators (and admins)
  212. $unapproved_where = "t.visible>-1";
  213. }
  214. elseif($db->num_rows($query))
  215. {
  216. // Normal moderators
  217. $moderated_forums = '0';
  218. while($forum = $db->fetch_array($query))
  219. {
  220. $moderated_forums .= ','.$forum['fid'];
  221. }
  222. $unapproved_where = "(t.visible>0 OR (t.visible=0 AND t.fid IN ({$moderated_forums})))";
  223. }
  224. else
  225. {
  226. // Normal users
  227. $unapproved_where = 't.visible>0';
  228. }
  229. // If we have saved WHERE conditions, execute them
  230. if($search['querycache'] != "")
  231. {
  232. $where_conditions = $search['querycache'];
  233. $query = $db->simple_select("threads t", "t.tid", $where_conditions. " AND {$unapproved_where} AND t.closed NOT LIKE 'moved|%' {$limitsql}");
  234. while($thread = $db->fetch_array($query))
  235. {
  236. $threads[$thread['tid']] = $thread['tid'];
  237. $threadcount++;
  238. }
  239. // Build our list of threads.
  240. if($threadcount > 0)
  241. {
  242. $search['threads'] = implode(",", $threads);
  243. }
  244. // No results.
  245. else
  246. {
  247. error($lang->error_nosearchresults);
  248. }
  249. $where_conditions = "t.tid IN (".$search['threads'].")";
  250. }
  251. // This search doesn't use a query cache, results stored in search table.
  252. else
  253. {
  254. $where_conditions = "t.tid IN (".$search['threads'].")";
  255. $query = $db->simple_select("threads t", "COUNT(t.tid) AS resultcount", $where_conditions. " AND {$unapproved_where} AND t.closed NOT LIKE 'moved|%' {$limitsql}");
  256. $count = $db->fetch_array($query);
  257. if(!$count['resultcount'])
  258. {
  259. error($lang->error_nosearchresults);
  260. }
  261. $threadcount = $count['resultcount'];
  262. }
  263. $permsql = "";
  264. $onlyusfids = array();
  265. // Check group permissions if we can't view threads not started by us
  266. $group_permissions = forum_permissions();
  267. foreach($group_permissions as $fid => $forum_permissions)
  268. {
  269. if($forum_permissions['canonlyviewownthreads'] == 1)
  270. {
  271. $onlyusfids[] = $fid;
  272. }
  273. }
  274. if(!empty($onlyusfids))
  275. {
  276. $permsql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
  277. }
  278. $unsearchforums = get_unsearchable_forums();
  279. if($unsearchforums)
  280. {
  281. $permsql .= " AND t.fid NOT IN ($unsearchforums)";
  282. }
  283. $inactiveforums = get_inactive_forums();
  284. if($inactiveforums)
  285. {
  286. $permsql .= " AND t.fid NOT IN ($inactiveforums)";
  287. }
  288. // Begin selecting matching threads, cache them.
  289. $sqlarray = array(
  290. 'order_by' => $sortfield,
  291. 'order_dir' => $order,
  292. 'limit_start' => $start,
  293. 'limit' => $perpage
  294. );
  295. $query = $db->query("
  296. SELECT t.*, u.username AS userusername, p.displaystyle AS threadprefix
  297. FROM ".TABLE_PREFIX."threads t
  298. LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=t.uid)
  299. LEFT JOIN ".TABLE_PREFIX."threadprefixes p ON (p.pid=t.prefix)
  300. WHERE $where_conditions AND {$unapproved_where} {$permsql} AND t.closed NOT LIKE 'moved|%'
  301. ORDER BY $sortfield $order
  302. LIMIT $start, $perpage
  303. ");
  304. $thread_cache = array();
  305. while($thread = $db->fetch_array($query))
  306. {
  307. $thread_cache[$thread['tid']] = $thread;
  308. }
  309. $thread_ids = implode(",", array_keys($thread_cache));
  310. if(empty($thread_ids))
  311. {
  312. error($lang->error_nosearchresults);
  313. }
  314. // Fetch dot icons if enabled
  315. if($mybb->settings['dotfolders'] != 0 && $mybb->user['uid'] && $thread_cache)
  316. {
  317. $query = $db->simple_select("posts", "DISTINCT tid,uid", "uid='".$mybb->user['uid']."' AND tid IN(".$thread_ids.")");
  318. while($thread = $db->fetch_array($query))
  319. {
  320. $thread_cache[$thread['tid']]['dot_icon'] = 1;
  321. }
  322. }
  323. // Fetch the read threads.
  324. if($mybb->user['uid'] && $mybb->settings['threadreadcut'] > 0)
  325. {
  326. $query = $db->simple_select("threadsread", "tid,dateline", "uid='".$mybb->user['uid']."' AND tid IN(".$thread_ids.")");
  327. while($readthread = $db->fetch_array($query))
  328. {
  329. $thread_cache[$readthread['tid']]['lastread'] = $readthread['dateline'];
  330. }
  331. }
  332. if(!$mybb->settings['maxmultipagelinks'])
  333. {
  334. $mybb->settings['maxmultipagelinks'] = 5;
  335. }
  336. foreach($thread_cache as $thread)
  337. {
  338. $bgcolor = alt_trow();
  339. $folder = '';
  340. $prefix = '';
  341. // Unapproved colour
  342. if(!$thread['visible'])
  343. {
  344. $bgcolor = 'trow_shaded';
  345. }
  346. if($thread['userusername'])
  347. {
  348. $thread['username'] = $thread['userusername'];
  349. }
  350. $thread['profilelink'] = build_profile_link($thread['username'], $thread['uid']);
  351. // If this thread has a prefix, insert a space between prefix and subject
  352. if($thread['prefix'] != 0)
  353. {
  354. $thread['threadprefix'] .= '&nbsp;';
  355. }
  356. $thread['subject'] = $parser->parse_badwords($thread['subject']);
  357. $thread['subject'] = htmlspecialchars_uni($thread['subject']);
  358. if($icon_cache[$thread['icon']])
  359. {
  360. $posticon = $icon_cache[$thread['icon']];
  361. $icon = "<img src=\"".$posticon['path']."\" alt=\"".$posticon['name']."\" />";
  362. }
  363. else
  364. {
  365. $icon = "&nbsp;";
  366. }
  367. if($thread['poll'])
  368. {
  369. $prefix = $lang->poll_prefix;
  370. }
  371. // Determine the folder
  372. $folder = '';
  373. $folder_label = '';
  374. if($thread['dot_icon'])
  375. {
  376. $folder = "dot_";
  377. $folder_label .= $lang->icon_dot;
  378. }
  379. $gotounread = '';
  380. $isnew = 0;
  381. $donenew = 0;
  382. $last_read = 0;
  383. if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
  384. {
  385. $forum_read = $readforums[$thread['fid']];
  386. $read_cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
  387. if($forum_read == 0 || $forum_read < $read_cutoff)
  388. {
  389. $forum_read = $read_cutoff;
  390. }
  391. }
  392. else
  393. {
  394. $forum_read = $forumsread[$thread['fid']];
  395. }
  396. if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'] && $thread['lastpost'] > $forum_read)
  397. {
  398. if($thread['lastread'])
  399. {
  400. $last_read = $thread['lastread'];
  401. }
  402. else
  403. {
  404. $last_read = $read_cutoff;
  405. }
  406. }
  407. else
  408. {
  409. $last_read = my_get_array_cookie("threadread", $thread['tid']);
  410. }
  411. if($forum_read > $last_read)
  412. {
  413. $last_read = $forum_read;
  414. }
  415. if($thread['lastpost'] > $last_read && $last_read)
  416. {
  417. $folder .= "new";
  418. $new_class = "subject_new";
  419. $folder_label .= $lang->icon_new;
  420. $thread['newpostlink'] = get_thread_link($thread['tid'], 0, "newpost").$highlight;
  421. eval("\$gotounread = \"".$templates->get("forumdisplay_thread_gotounread")."\";");
  422. $unreadpost = 1;
  423. }
  424. else
  425. {
  426. $new_class = 'subject_old';
  427. $folder_label .= $lang->icon_no_new;
  428. }
  429. if($thread['replies'] >= $mybb->settings['hottopic'] || $thread['views'] >= $mybb->settings['hottopicviews'])
  430. {
  431. $folder .= "hot";
  432. $folder_label .= $lang->icon_hot;
  433. }
  434. if($thread['closed'] == 1)
  435. {
  436. $folder .= "lock";
  437. $folder_label .= $lang->icon_lock;
  438. }
  439. $folder .= "folder";
  440. if(!$mybb->settings['postsperpage'])
  441. {
  442. $mybb->settings['postperpage'] = 20;
  443. }
  444. $thread['pages'] = 0;
  445. $thread['multipage'] = '';
  446. $threadpages = '';
  447. $morelink = '';
  448. $thread['posts'] = $thread['replies'] + 1;
  449. if(is_moderator($thread['fid']))
  450. {
  451. $thread['posts'] += $thread['unapprovedposts'];
  452. }
  453. if($thread['posts'] > $mybb->settings['postsperpage'])
  454. {
  455. $thread['pages'] = $thread['posts'] / $mybb->settings['postsperpage'];
  456. $thread['pages'] = ceil($thread['pages']);
  457. if($thread['pages'] > $mybb->settings['maxmultipagelinks'])
  458. {
  459. $pagesstop = $mybb->settings['maxmultipagelinks'] - 1;
  460. $page_link = get_thread_link($thread['tid'], $thread['pages']).$highlight;
  461. eval("\$morelink = \"".$templates->get("forumdisplay_thread_multipage_more")."\";");
  462. }
  463. else
  464. {
  465. $pagesstop = $thread['pages'];
  466. }
  467. for($i = 1; $i <= $pagesstop; ++$i)
  468. {
  469. $page_link = get_thread_link($thread['tid'], $i).$highlight;
  470. eval("\$threadpages .= \"".$templates->get("forumdisplay_thread_multipage_page")."\";");
  471. }
  472. eval("\$thread['multipage'] = \"".$templates->get("forumdisplay_thread_multipage")."\";");
  473. }
  474. else
  475. {
  476. $threadpages = '';
  477. $morelink = '';
  478. $thread['multipage'] = '';
  479. }
  480. $lastpostdate = my_date($mybb->settings['dateformat'], $thread['lastpost']);
  481. $lastposttime = my_date($mybb->settings['timeformat'], $thread['lastpost']);
  482. $lastposter = $thread['lastposter'];
  483. $thread['lastpostlink'] = get_thread_link($thread['tid'], 0, "lastpost");
  484. $lastposteruid = $thread['lastposteruid'];
  485. $thread_link = get_thread_link($thread['tid']);
  486. // Don't link to guest's profiles (they have no profile).
  487. if($lastposteruid == 0)
  488. {
  489. $lastposterlink = $lastposter;
  490. }
  491. else
  492. {
  493. $lastposterlink = build_profile_link($lastposter, $lastposteruid);
  494. }
  495. $thread['replies'] = my_number_format($thread['replies']);
  496. $thread['views'] = my_number_format($thread['views']);
  497. if($forumcache[$thread['fid']])
  498. {
  499. $thread['forumlink'] = "<a href=\"".get_forum_link($thread['fid'])."\">".$forumcache[$thread['fid']]['name']."</a>";
  500. }
  501. else
  502. {
  503. $thread['forumlink'] = "";
  504. }
  505. // If this user is the author of the thread and it is not closed or they are a moderator, they can edit
  506. if(($thread['uid'] == $mybb->user['uid'] && $thread['closed'] != 1 && $mybb->user['uid'] != 0 && $fpermissions[$thread['fid']]['caneditposts'] == 1) || is_moderator($thread['fid'], "caneditposts"))
  507. {
  508. $inline_edit_class = "subject_editable";
  509. }
  510. else
  511. {
  512. $inline_edit_class = "";
  513. }
  514. $load_inline_edit_js = 1;
  515. // If this thread has 1 or more attachments show the papperclip
  516. if($thread['attachmentcount'] > 0)
  517. {
  518. if($thread['attachmentcount'] > 1)
  519. {
  520. $attachment_count = $lang->sprintf($lang->attachment_count_multiple, $thread['attachmentcount']);
  521. }
  522. else
  523. {
  524. $attachment_count = $lang->attachment_count;
  525. }
  526. eval("\$attachment_count = \"".$templates->get("forumdisplay_thread_attachment_count")."\";");
  527. }
  528. else
  529. {
  530. $attachment_count = '';
  531. }
  532. $inline_edit_tid = $thread['tid'];
  533. // Inline thread moderation
  534. $inline_mod_checkbox = '';
  535. if($is_supermod || is_moderator($thread['fid']))
  536. {
  537. eval("\$inline_mod_checkbox = \"".$templates->get("search_results_threads_inlinecheck")."\";");
  538. }
  539. elseif($is_mod)
  540. {
  541. eval("\$inline_mod_checkbox = \"".$templates->get("search_results_threads_nocheck")."\";");
  542. }
  543. $plugins->run_hooks("search_results_thread");
  544. eval("\$results .= \"".$templates->get("search_results_threads_thread")."\";");
  545. }
  546. if(!$results)
  547. {
  548. error($lang->error_nosearchresults);
  549. }
  550. else
  551. {
  552. if($load_inline_edit_js == 1)
  553. {
  554. eval("\$inline_edit_js = \"".$templates->get("forumdisplay_threadlist_inlineedit_js")."\";");
  555. }
  556. }
  557. $multipage = multipage($threadcount, $perpage, $page, "search.php?action=results&amp;sid=$sid&amp;sortby=$sortby&amp;order=$order&amp;uid=".$mybb->input['uid']);
  558. if($upper > $threadcount)
  559. {
  560. $upper = $threadcount;
  561. }
  562. // Inline Thread Moderation Options
  563. if($is_mod)
  564. {
  565. // If user has moderation tools available, prepare the Select All feature
  566. $lang->page_selected = $lang->sprintf($lang->page_selected, count($thread_cache));
  567. $lang->all_selected = $lang->sprintf($lang->all_selected, intval($threadcount));
  568. $lang->select_all = $lang->sprintf($lang->select_all, intval($threadcount));
  569. eval("\$selectall = \"".$templates->get("search_threads_inlinemoderation_selectall")."\";");
  570. $customthreadtools = '';
  571. switch($db->type)
  572. {
  573. case "pgsql":
  574. case "sqlite":
  575. $query = $db->simple_select("modtools", "tid, name", "type='t' AND (','||forums||',' LIKE '%,-1,%' OR forums='')");
  576. break;
  577. default:
  578. $query = $db->simple_select("modtools", "tid, name", "type='t' AND (CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='')");
  579. }
  580. while($tool = $db->fetch_array($query))
  581. {
  582. eval("\$customthreadtools .= \"".$templates->get("search_results_threads_inlinemoderation_custom_tool")."\";");
  583. }
  584. // Build inline moderation dropdown
  585. if(!empty($customthreadtools))
  586. {
  587. eval("\$customthreadtools = \"".$templates->get("search_results_threads_inlinemoderation_custom")."\";");
  588. }
  589. eval("\$inlinemod = \"".$templates->get("search_results_threads_inlinemoderation")."\";");
  590. }
  591. $plugins->run_hooks("search_results_end");
  592. eval("\$searchresults = \"".$templates->get("search_results_threads")."\";");
  593. output_page($searchresults);
  594. }
  595. else // Displaying results as posts
  596. {
  597. if(!$search['posts'])
  598. {
  599. error($lang->error_nosearchresults);
  600. }
  601. $postcount = 0;
  602. // Moderators can view unapproved threads
  603. $query = $db->simple_select("moderators", "fid", "(id='{$mybb->user['uid']}' AND isgroup='0') OR (id='{$mybb->user['usergroup']}' AND isgroup='1')");
  604. if($mybb->usergroup['issupermod'] == 1)
  605. {
  606. // Super moderators (and admins)
  607. $p_unapproved_where = "visible >= 0";
  608. $t_unapproved_where = "visible < 0";
  609. }
  610. elseif($db->num_rows($query))
  611. {
  612. // Normal moderators
  613. $moderated_forums = '0';
  614. while($forum = $db->fetch_array($query))
  615. {
  616. $moderated_forums .= ','.$forum['fid'];
  617. $test_moderated_forums[$forum['fid']] = $forum['fid'];
  618. }
  619. $p_unapproved_where = "visible >= 0";
  620. $t_unapproved_where = "visible < 0 AND fid NOT IN ({$moderated_forums})";
  621. }
  622. else
  623. {
  624. // Normal users
  625. $p_unapproved_where = 'visible=1';
  626. $t_unapproved_where = 'visible < 1';
  627. }
  628. $post_cache_options = array();
  629. if(intval($mybb->settings['searchhardlimit']) > 0)
  630. {
  631. $post_cache_options['limit'] = intval($mybb->settings['searchhardlimit']);
  632. }
  633. if(strpos($sortfield, 'p.') !== false)
  634. {
  635. $post_cache_options['order_by'] = str_replace('p.', '', $sortfield);
  636. $post_cache_options['order_dir'] = $order;
  637. }
  638. $tids = array();
  639. $pids = array();
  640. // Make sure the posts we're viewing we have permission to view.
  641. $query = $db->simple_select("posts", "pid, tid", "pid IN(".$db->escape_string($search['posts']).") AND {$p_unapproved_where}", $post_cache_options);
  642. while($post = $db->fetch_array($query))
  643. {
  644. $pids[$post['pid']] = $post['tid'];
  645. $tids[$post['tid']][$post['pid']] = $post['pid'];
  646. }
  647. if(!empty($pids))
  648. {
  649. $temp_pids = array();
  650. // Check the thread records as well. If we don't have permissions, remove them from the listing.
  651. $query = $db->simple_select("threads", "tid", "tid IN(".$db->escape_string(implode(',', $pids)).") AND ({$t_unapproved_where} OR closed LIKE 'moved|%')");
  652. while($thread = $db->fetch_array($query))
  653. {
  654. if(array_key_exists($thread['tid'], $tids) != false)
  655. {
  656. $temp_pids = $tids[$thread['tid']];
  657. foreach($temp_pids as $pid)
  658. {
  659. unset($pids[$pid]);
  660. unset($tids[$thread['tid']]);
  661. }
  662. }
  663. }
  664. unset($temp_pids);
  665. }
  666. // Declare our post count
  667. $postcount = count($pids);
  668. if(!$postcount)
  669. {
  670. error($lang->error_nosearchresults);
  671. }
  672. // And now we have our sanatized post list
  673. $search['posts'] = implode(',', array_keys($pids));
  674. $tids = implode(",", array_keys($tids));
  675. // Read threads
  676. if($mybb->user['uid'] && $mybb->settings['threadreadcut'] > 0)
  677. {
  678. $query = $db->simple_select("threadsread", "tid, dateline", "uid='".$mybb->user['uid']."' AND tid IN(".$db->escape_string($tids).")");
  679. while($readthread = $db->fetch_array($query))
  680. {
  681. $readthreads[$readthread['tid']] = $readthread['dateline'];
  682. }
  683. }
  684. $dot_icon = array();
  685. if($mybb->settings['dotfolders'] != 0 && $mybb->user['uid'] != 0)
  686. {
  687. $query = $db->simple_select("posts", "DISTINCT tid,uid", "uid='".$mybb->user['uid']."' AND tid IN(".$db->escape_string($tids).")");
  688. while($post = $db->fetch_array($query))
  689. {
  690. $dot_icon[$post['tid']] = true;
  691. }
  692. }
  693. $query = $db->query("
  694. SELECT p.*, u.username AS userusername, t.subject AS thread_subject, t.replies AS thread_replies, t.views AS thread_views, t.lastpost AS thread_lastpost, t.closed AS thread_closed, t.uid as thread_uid
  695. FROM ".TABLE_PREFIX."posts p
  696. LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
  697. LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
  698. WHERE p.pid IN (".$db->escape_string($search['posts']).")
  699. ORDER BY $sortfield $order
  700. LIMIT $start, $perpage
  701. ");
  702. while($post = $db->fetch_array($query))
  703. {
  704. $bgcolor = alt_trow();
  705. if(!$post['visible'])
  706. {
  707. $bgcolor = 'trow_shaded';
  708. }
  709. if($post['userusername'])
  710. {
  711. $post['username'] = $post['userusername'];
  712. }
  713. $post['profilelink'] = build_profile_link($post['username'], $post['uid']);
  714. $post['subject'] = $parser->parse_badwords($post['subject']);
  715. $post['thread_subject'] = $parser->parse_badwords($post['thread_subject']);
  716. $post['thread_subject'] = htmlspecialchars_uni($post['thread_subject']);
  717. if($icon_cache[$post['icon']])
  718. {
  719. $posticon = $icon_cache[$post['icon']];
  720. $icon = "<img src=\"".$posticon['path']."\" alt=\"".$posticon['name']."\" />";
  721. }
  722. else
  723. {
  724. $icon = "&nbsp;";
  725. }
  726. if($forumcache[$thread['fid']])
  727. {
  728. $post['forumlink'] = "<a href=\"".get_forum_link($post['fid'])."\">".$forumcache[$post['fid']]['name']."</a>";
  729. }
  730. else
  731. {
  732. $post['forumlink'] = "";
  733. }
  734. // Determine the folder
  735. $folder = '';
  736. $folder_label = '';
  737. $gotounread = '';
  738. $isnew = 0;
  739. $donenew = 0;
  740. $last_read = 0;
  741. $post['thread_lastread'] = $readthreads[$post['tid']];
  742. if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
  743. {
  744. $forum_read = $readforums[$post['fid']];
  745. $read_cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
  746. if($forum_read == 0 || $forum_read < $read_cutoff)
  747. {
  748. $forum_read = $read_cutoff;
  749. }
  750. }
  751. else
  752. {
  753. $forum_read = $forumsread[$post['fid']];
  754. }
  755. if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'] && $post['thread_lastpost'] > $forum_read)
  756. {
  757. $cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
  758. if($post['thread_lastpost'] > $cutoff)
  759. {
  760. if($post['thread_lastread'])
  761. {
  762. $last_read = $post['thread_lastread'];
  763. }
  764. else
  765. {
  766. $last_read = 1;
  767. }
  768. }
  769. }
  770. if($dot_icon[$post['tid']])
  771. {
  772. $folder = "dot_";
  773. $folder_label .= $lang->icon_dot;
  774. }
  775. if(!$last_read)
  776. {
  777. $readcookie = $threadread = my_get_array_cookie("threadread", $post['tid']);
  778. if($readcookie > $forum_read)
  779. {
  780. $last_read = $readcookie;
  781. }
  782. elseif($forum_read > $mybb->user['lastvisit'])
  783. {
  784. $last_read = $forum_read;
  785. }
  786. else
  787. {
  788. $last_read = $mybb->user['lastvisit'];
  789. }
  790. }
  791. if($post['thread_lastpost'] > $last_read && $last_read)
  792. {
  793. $folder .= "new";
  794. $folder_label .= $lang->icon_new;
  795. eval("\$gotounread = \"".$templates->get("forumdisplay_thread_gotounread")."\";");
  796. $unreadpost = 1;
  797. }
  798. else
  799. {
  800. $folder_label .= $lang->icon_no_new;
  801. }
  802. if($post['thread_replies'] >= $mybb->settings['hottopic'] || $post['thread_views'] >= $mybb->settings['hottopicviews'])
  803. {
  804. $folder .= "hot";
  805. $folder_label .= $lang->icon_hot;
  806. }
  807. if($thread['thread_closed'] == 1)
  808. {
  809. $folder .= "lock";
  810. $folder_label .= $lang->icon_lock;
  811. }
  812. $folder .= "folder";
  813. $post['thread_replies'] = my_number_format($post['thread_replies']);
  814. $post['thread_views'] = my_number_format($post['thread_views']);
  815. if($forumcache[$post['fid']])
  816. {
  817. $post['forumlink'] = "<a href=\"".get_forum_link($post['fid'])."\">".$forumcache[$post['fid']]['name']."</a>";
  818. }
  819. else
  820. {
  821. $post['forumlink'] = "";
  822. }
  823. if(!$post['subject'])
  824. {
  825. $post['subject'] = $post['message'];
  826. }
  827. if(my_strlen($post['subject']) > 50)
  828. {
  829. $post['subject'] = htmlspecialchars_uni(my_substr($post['subject'], 0, 50)."...");
  830. }
  831. else
  832. {
  833. $post['subject'] = htmlspecialchars_uni($post['subject']);
  834. }
  835. // What we do here is parse the post using our post parser, then strip the tags from it
  836. $parser_options = array(
  837. 'allow_html' => 0,
  838. 'allow_mycode' => 1,
  839. 'allow_smilies' => 0,
  840. 'allow_imgcode' => 0,
  841. 'filter_badwords' => 1
  842. );
  843. $post['message'] = strip_tags($parser->parse_message($post['message'], $parser_options));
  844. if(my_strlen($post['message']) > 200)
  845. {
  846. $prev = my_substr($post['message'], 0, 200)."...";
  847. }
  848. else
  849. {
  850. $prev = $post['message'];
  851. }
  852. $posted = my_date($mybb->settings['dateformat'], $post['dateline']).", ".my_date($mybb->settings['timeformat'], $post['dateline']);
  853. $thread_url = get_thread_link($post['tid']);
  854. $post_url = get_post_link($post['pid'], $post['tid']);
  855. // Inline post moderation
  856. $inline_mod_checkbox = '';
  857. if($is_supermod || is_moderator($post['fid']))
  858. {
  859. eval("\$inline_mod_checkbox = \"".$templates->get("search_results_posts_inlinecheck")."\";");
  860. }
  861. elseif($is_mod)
  862. {
  863. eval("\$inline_mod_checkbox = \"".$templates->get("search_results_posts_nocheck")."\";");
  864. }
  865. $plugins->run_hooks("search_results_post");
  866. eval("\$results .= \"".$templates->get("search_results_posts_post")."\";");
  867. }
  868. if(!$results)
  869. {
  870. error($lang->error_nosearchresults);
  871. }
  872. $multipage = multipage($postcount, $perpage, $page, "search.php?action=results&amp;sid=".htmlspecialchars_uni($mybb->input['sid'])."&amp;sortby=$sortby&amp;order=$order&amp;uid=".$mybb->input['uid']);
  873. if($upper > $postcount)
  874. {
  875. $upper = $postcount;
  876. }
  877. // Inline Post Moderation Options
  878. if($is_mod)
  879. {
  880. // If user has moderation tools available, prepare the Select All feature
  881. $num_results = $db->num_rows($query);
  882. $lang->page_selected = $lang->sprintf($lang->page_selected, intval($num_results));
  883. $lang->select_all = $lang->sprintf($lang->select_all, intval($postcount));
  884. $lang->all_selected = $lang->sprintf($lang->page_selected, intval($postcount));
  885. eval("\$selectall = \"".$templates->get("search_posts_inlinemoderation_selectall")."\";");
  886. $customthreadtools = $customposttools = '';
  887. switch($db->type)
  888. {
  889. case "pgsql":
  890. case "sqlite":
  891. $query = $db->simple_select("modtools", "tid, name, type", "type='p' AND (','||forums||',' LIKE '%,-1,%' OR forums='')");
  892. break;
  893. default:
  894. $query = $db->simple_select("modtools", "tid, name, type", "type='p' AND (CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='')");
  895. }
  896. while($tool = $db->fetch_array($query))
  897. {
  898. eval("\$customposttools .= \"".$templates->get("search_results_posts_inlinemoderation_custom_tool")."\";");
  899. }
  900. // Build inline moderation dropdown
  901. if(!empty($customposttools))
  902. {
  903. eval("\$customposttools = \"".$templates->get("search_results_posts_inlinemoderation_custom")."\";");
  904. }
  905. eval("\$inlinemod = \"".$templates->get("search_results_posts_inlinemoderation")."\";");
  906. }
  907. $plugins->run_hooks("search_results_end");
  908. eval("\$searchresults = \"".$templates->get("search_results_posts")."\";");
  909. output_page($searchresults);
  910. }
  911. }
  912. elseif($mybb->input['action'] == "findguest")
  913. {
  914. $where_sql = "uid='0'";
  915. $unsearchforums = get_unsearchable_forums();
  916. if($unsearchforums)
  917. {
  918. $where_sql .= " AND fid NOT IN ($unsearchforums)";
  919. }
  920. $inactiveforums = get_inactive_forums();
  921. if($inactiveforums)
  922. {
  923. $where_sql .= " AND fid NOT IN ($inactiveforums)";
  924. }
  925. $permsql = "";
  926. $onlyusfids = array();
  927. // Check group permissions if we can't view threads not started by us
  928. $group_permissions = forum_permissions();
  929. foreach($group_permissions as $fid => $forum_permissions)
  930. {
  931. if($forum_permissions['canonlyviewownthreads'] == 1)
  932. {
  933. $onlyusfids[] = $fid;
  934. }
  935. }
  936. if(!empty($onlyusfids))
  937. {
  938. $where_sql .= " AND fid NOT IN(".implode(',', $onlyusfids).")";
  939. }
  940. $options = array(
  941. 'order_by' => 'dateline',
  942. 'order_dir' => 'desc'
  943. );
  944. // Do we have a hard search limit?
  945. if($mybb->settings['searchhardlimit'] > 0)
  946. {
  947. $options['limit'] = intval($mybb->settings['searchhardlimit']);
  948. }
  949. $pids = '';
  950. $comma = '';
  951. $query = $db->simple_select("posts", "pid", "{$where_sql}", $options);
  952. while($pid = $db->fetch_field($query, "pid"))
  953. {
  954. $pids .= $comma.$pid;
  955. $comma = ',';
  956. }
  957. $tids = '';
  958. $comma = '';
  959. $query = $db->simple_select("threads", "tid", $where_sql);
  960. while($tid = $db->fetch_field($query, "tid"))
  961. {
  962. $tids .= $comma.$tid;
  963. $comma = ',';
  964. }
  965. $sid = md5(uniqid(microtime(), 1));
  966. $searcharray = array(
  967. "sid" => $db->escape_string($sid),
  968. "uid" => $mybb->user['uid'],
  969. "dateline" => TIME_NOW,
  970. "ipaddress" => $db->escape_string($session->ipaddress),
  971. "threads" => $db->escape_string($tids),
  972. "posts" => $db->escape_string($pids),
  973. "resulttype" => "posts",
  974. "querycache" => '',
  975. "keywords" => ''
  976. );
  977. $plugins->run_hooks("search_do_search_process");
  978. $db->insert_query("searchlog", $searcharray);
  979. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  980. }
  981. elseif($mybb->input['action'] == "finduser")
  982. {
  983. $where_sql = "uid='".intval($mybb->input['uid'])."'";
  984. $unsearchforums = get_unsearchable_forums();
  985. if($unsearchforums)
  986. {
  987. $where_sql .= " AND fid NOT IN ($unsearchforums)";
  988. }
  989. $inactiveforums = get_inactive_forums();
  990. if($inactiveforums)
  991. {
  992. $where_sql .= " AND fid NOT IN ($inactiveforums)";
  993. }
  994. $permsql = "";
  995. $onlyusfids = array();
  996. // Check group permissions if we can't view threads not started by us
  997. $group_permissions = forum_permissions();
  998. foreach($group_permissions as $fid => $forum_permissions)
  999. {
  1000. if($forum_permissions['canonlyviewownthreads'] == 1)
  1001. {
  1002. $onlyusfids[] = $fid;
  1003. }
  1004. }
  1005. if(!empty($onlyusfids))
  1006. {
  1007. $where_sql .= "AND ((fid IN(".implode(',', $onlyusfids).") AND uid='{$mybb->user['uid']}') OR fid NOT IN(".implode(',', $onlyusfids)."))";
  1008. }
  1009. $options = array(
  1010. 'order_by' => 'dateline',
  1011. 'order_dir' => 'desc'
  1012. );
  1013. // Do we have a hard search limit?
  1014. if($mybb->settings['searchhardlimit'] > 0)
  1015. {
  1016. $options['limit'] = intval($mybb->settings['searchhardlimit']);
  1017. }
  1018. $pids = '';
  1019. $comma = '';
  1020. $query = $db->simple_select("posts", "pid", "{$where_sql}", $options);
  1021. while($pid = $db->fetch_field($query, "pid"))
  1022. {
  1023. $pids .= $comma.$pid;
  1024. $comma = ',';
  1025. }
  1026. $tids = '';
  1027. $comma = '';
  1028. $query = $db->simple_select("threads", "tid", $where_sql);
  1029. while($tid = $db->fetch_field($query, "tid"))
  1030. {
  1031. $tids .= $comma.$tid;
  1032. $comma = ',';
  1033. }
  1034. $sid = md5(uniqid(microtime(), 1));
  1035. $searcharray = array(
  1036. "sid" => $db->escape_string($sid),
  1037. "uid" => $mybb->user['uid'],
  1038. "dateline" => TIME_NOW,
  1039. "ipaddress" => $db->escape_string($session->ipaddress),
  1040. "threads" => $db->escape_string($tids),
  1041. "posts" => $db->escape_string($pids),
  1042. "resulttype" => "posts",
  1043. "querycache" => '',
  1044. "keywords" => ''
  1045. );
  1046. $plugins->run_hooks("search_do_search_process");
  1047. $db->insert_query("searchlog", $searcharray);
  1048. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  1049. }
  1050. elseif($mybb->input['action'] == "finduserthreads")
  1051. {
  1052. $where_sql = "t.uid='".intval($mybb->input['uid'])."'";
  1053. $unsearchforums = get_unsearchable_forums();
  1054. if($unsearchforums)
  1055. {
  1056. $where_sql .= " AND t.fid NOT IN ($unsearchforums)";
  1057. }
  1058. $inactiveforums = get_inactive_forums();
  1059. if($inactiveforums)
  1060. {
  1061. $where_sql .= " AND t.fid NOT IN ($inactiveforums)";
  1062. }
  1063. $permsql = "";
  1064. $onlyusfids = array();
  1065. // Check group permissions if we can't view threads not started by us
  1066. $group_permissions = forum_permissions();
  1067. foreach($group_permissions as $fid => $forum_permissions)
  1068. {
  1069. if($forum_permissions['canonlyviewownthreads'] == 1)
  1070. {
  1071. $onlyusfids[] = $fid;
  1072. }
  1073. }
  1074. if(!empty($onlyusfids))
  1075. {
  1076. $where_sql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
  1077. }
  1078. $sid = md5(uniqid(microtime(), 1));
  1079. $searcharray = array(
  1080. "sid" => $db->escape_string($sid),
  1081. "uid" => $mybb->user['uid'],
  1082. "dateline" => TIME_NOW,
  1083. "ipaddress" => $db->escape_string($session->ipaddress),
  1084. "threads" => '',
  1085. "posts" => '',
  1086. "resulttype" => "threads",
  1087. "querycache" => $db->escape_string($where_sql),
  1088. "keywords" => ''
  1089. );
  1090. $plugins->run_hooks("search_do_search_process");
  1091. $db->insert_query("searchlog", $searcharray);
  1092. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  1093. }
  1094. elseif($mybb->input['action'] == "getnew")
  1095. {
  1096. $where_sql = "t.lastpost >= '".$mybb->user['lastvisit']."'";
  1097. if($mybb->input['fid'])
  1098. {
  1099. $where_sql .= " AND t.fid='".intval($mybb->input['fid'])."'";
  1100. }
  1101. else if($mybb->input['fids'])
  1102. {
  1103. $fids = explode(',', $mybb->input['fids']);
  1104. foreach($fids as $key => $fid)
  1105. {
  1106. $fids[$key] = intval($fid);
  1107. }
  1108. if(!empty($fids))
  1109. {
  1110. $where_sql .= " AND t.fid IN (".implode(',', $fids).")";
  1111. }
  1112. }
  1113. $unsearchforums = get_unsearchable_forums();
  1114. if($unsearchforums)
  1115. {
  1116. $where_sql .= " AND t.fid NOT IN ($unsearchforums)";
  1117. }
  1118. $inactiveforums = get_inactive_forums();
  1119. if($inactiveforums)
  1120. {
  1121. $where_sql .= " AND t.fid NOT IN ($inactiveforums)";
  1122. }
  1123. $permsql = "";
  1124. $onlyusfids = array();
  1125. // Check group permissions if we can't view threads not started by us
  1126. $group_permissions = forum_permissions();
  1127. foreach($group_permissions as $fid => $forum_permissions)
  1128. {
  1129. if($forum_permissions['canonlyviewownthreads'] == 1)
  1130. {
  1131. $onlyusfids[] = $fid;
  1132. }
  1133. }
  1134. if(!empty($onlyusfids))
  1135. {
  1136. $where_sql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
  1137. }
  1138. $sid = md5(uniqid(microtime(), 1));
  1139. $searcharray = array(
  1140. "sid" => $db->escape_string($sid),
  1141. "uid" => $mybb->user['uid'],
  1142. "dateline" => TIME_NOW,
  1143. "ipaddress" => $db->escape_string($session->ipaddress),
  1144. "threads" => '',
  1145. "posts" => '',
  1146. "resulttype" => "threads",
  1147. "querycache" => $db->escape_string($where_sql),
  1148. "keywords" => ''
  1149. );
  1150. $plugins->run_hooks("search_do_search_process");
  1151. $db->insert_query("searchlog", $searcharray);
  1152. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  1153. }
  1154. elseif($mybb->input['action'] == "getdaily")
  1155. {
  1156. if($mybb->input['days'] < 1)
  1157. {
  1158. $days = 1;
  1159. }
  1160. else
  1161. {
  1162. $days = intval($mybb->input['days']);
  1163. }
  1164. $datecut = TIME_NOW-(86400*$days);
  1165. $where_sql = "t.lastpost >='".$datecut."'";
  1166. if($mybb->input['fid'])
  1167. {
  1168. $where_sql .= " AND t.fid='".intval($mybb->input['fid'])."'";
  1169. }
  1170. else if($mybb->input['fids'])
  1171. {
  1172. $fids = explode(',', $mybb->input['fids']);
  1173. foreach($fids as $key => $fid)
  1174. {
  1175. $fids[$key] = intval($fid);
  1176. }
  1177. if(!empty($fids))
  1178. {
  1179. $where_sql .= " AND t.fid IN (".implode(',', $fids).")";
  1180. }
  1181. }
  1182. $unsearchforums = get_unsearchable_forums();
  1183. if($unsearchforums)
  1184. {
  1185. $where_sql .= " AND t.fid NOT IN ($unsearchforums)";
  1186. }
  1187. $inactiveforums = get_inactive_forums();
  1188. if($inactiveforums)
  1189. {
  1190. $where_sql .= " AND t.fid NOT IN ($inactiveforums)";
  1191. }
  1192. $permsql = "";
  1193. $onlyusfids = array();
  1194. // Check group permissions if we can't view threads not started by us
  1195. $group_permissions = forum_permissions();
  1196. foreach($group_permissions as $fid => $forum_permissions)
  1197. {
  1198. if($forum_permissions['canonlyviewownthreads'] == 1)
  1199. {
  1200. $onlyusfids[] = $fid;
  1201. }
  1202. }
  1203. if(!empty($onlyusfids))
  1204. {
  1205. $where_sql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
  1206. }
  1207. $sid = md5(uniqid(microtime(), 1));
  1208. $searcharray = array(
  1209. "sid" => $db->escape_string($sid),
  1210. "uid" => $mybb->user['uid'],
  1211. "dateline" => TIME_NOW,
  1212. "ipaddress" => $db->escape_string($session->ipaddress),
  1213. "threads" => '',
  1214. "posts" => '',
  1215. "resulttype" => "threads",
  1216. "querycache" => $db->escape_string($where_sql),
  1217. "keywords" => ''
  1218. );
  1219. $plugins->run_hooks("search_do_search_process");
  1220. $db->insert_query("searchlog", $searcharray);
  1221. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  1222. }
  1223. elseif($mybb->input['action'] == "do_search" && $mybb->request_method == "post")
  1224. {
  1225. $plugins->run_hooks("search_do_search_start");
  1226. // Check if search flood checking is enabled and user is not admin
  1227. if($mybb->settings['searchfloodtime'] > 0 && $mybb->usergroup['cancp'] != 1)
  1228. {
  1229. // Fetch the time this user last searched
  1230. if($mybb->user['uid'])
  1231. {
  1232. $conditions = "uid='{$mybb->user['uid']}'";
  1233. }
  1234. else
  1235. {
  1236. $conditions = "uid='0' AND ipaddress='".$db->escape_string($session->ipaddress)."'";
  1237. }
  1238. $timecut = TIME_NOW-$mybb->settings['searchfloodtime'];
  1239. $query = $db->simple_select("searchlog", "*", "$conditions AND dateline > '$timecut'", array('order_by' => "dateline", 'order_dir' => "DESC"));
  1240. $last_search = $db->fetch_array($query);
  1241. // Users last search was within the flood time, show the error
  1242. if($last_search['sid'])
  1243. {
  1244. $remaining_time = $mybb->settings['searchfloodtime']-(TIME_NOW-$last_search['dateline']);
  1245. if($remaining_time == 1)
  1246. {
  1247. $lang->error_searchflooding = $lang->sprintf($lang->error_searchflooding_1, $mybb->settings['searchfloodtime']);
  1248. }
  1249. else
  1250. {
  1251. $lang->error_searchflooding = $lang->sprintf($lang->error_searchflooding, $mybb->settings['searchfloodtime'], $remaining_time);
  1252. }
  1253. error($lang->error_searchflooding);
  1254. }
  1255. }
  1256. if($mybb->input['showresults'] == "threads")
  1257. {
  1258. $resulttype = "threads";
  1259. }
  1260. else
  1261. {
  1262. $resulttype = "posts";
  1263. }
  1264. $search_data = array(
  1265. "keywords" => $mybb->input['keywords'],
  1266. "author" => $mybb->input['author'],
  1267. "postthread" => $mybb->input['postthread'],
  1268. "matchusername" => $mybb->input['matchusername'],
  1269. "postdate" => $mybb->input['postdate'],
  1270. "pddir" => $mybb->input['pddir'],
  1271. "forums" => $mybb->input['forums'],
  1272. "findthreadst" => $mybb->input['findthreadst'],
  1273. "numreplies" => $mybb->input['numreplies'],
  1274. "threadprefix" => $mybb->input['threadprefix']
  1275. );
  1276. if(is_moderator() && !empty($mybb->input['visible']))
  1277. {
  1278. if($mybb->input['visible'] == 1)
  1279. {
  1280. $search_data['visible'] = 1;
  1281. }
  1282. else
  1283. {
  1284. $search_data['visible'] = 0;
  1285. }
  1286. }
  1287. if($db->can_search == true)
  1288. {
  1289. if($mybb->settings['searchtype'] == "fulltext" && $db->supports_fulltext_boolean("posts") && $db->is_fulltext("posts"))
  1290. {
  1291. $search_results = perform_search_mysql_ft($search_data);
  1292. }
  1293. else
  1294. {
  1295. $search_results = perform_search_mysql($search_data);
  1296. }
  1297. }
  1298. else
  1299. {
  1300. error($lang->error_no_search_support);
  1301. }
  1302. $sid = md5(uniqid(microtime(), 1));
  1303. $searcharray = array(
  1304. "sid" => $db->escape_string($sid),
  1305. "uid" => $mybb->user['uid'],
  1306. "dateline" => $now,
  1307. "ipaddress" => $db->escape_string($session->ipaddress),
  1308. "threads" => $search_results['threads'],
  1309. "posts" => $search_results['posts'],
  1310. "resulttype" => $resulttype,
  1311. "querycache" => $search_results['querycache'],
  1312. "keywords" => $db->escape_string($mybb->input['keywords']),
  1313. );
  1314. $plugins->run_hooks("search_do_search_process");
  1315. $db->insert_query("searchlog", $searcharray);
  1316. if(my_strtolower($mybb->input['sortordr']) == "asc" || my_strtolower($mybb->input['sortordr'] == "desc"))
  1317. {
  1318. $sortorder = $mybb->input['sortordr'];
  1319. }
  1320. else
  1321. {
  1322. $sortorder = "desc";
  1323. }
  1324. $sortby = htmlspecialchars($mybb->input['sortby']);
  1325. $plugins->run_hooks("search_do_search_end");
  1326. redirect("search.php?action=results&sid=".$sid."&sortby=".$sortby."&order=".$sortorder, $lang->redirect_searchresults);
  1327. }
  1328. else if($mybb->input['action'] == "thread")
  1329. {
  1330. // Fetch thread info
  1331. $thread = get_thread($mybb->input['tid']);
  1332. if(!$thread['tid'] || (($thread['visible'] == 0 && !is_moderator($thread['fid'])) || $thread['visible'] < 0))
  1333. {
  1334. error($lang->error_invalidthread);
  1335. }
  1336. // Get forum info
  1337. $forum = get_forum($thread['fid']);
  1338. if(!$forum)
  1339. {
  1340. error($lang->error_invalidforum);
  1341. }
  1342. $forum_permissions = forum_permissions($forum['fid']);
  1343. if($forum['open'] == 0 || $forum['type'] != "f")
  1344. {
  1345. error($lang->error_closedinvalidforum);
  1346. }
  1347. if($forum_permissions['canview'] == 0 || $forum_permissions['canviewthreads'] != 1)
  1348. {
  1349. error_no_permission();
  1350. }
  1351. $plugins->run_hooks("search_thread_start");
  1352. // Check if search flood checking is enabled and user is not admin
  1353. if($mybb->settings['searchfloodtime'] > 0 && $mybb->usergroup['cancp'] != 1)
  1354. {
  1355. // Fetch the time this user last searched
  1356. if($mybb->user['uid'])
  1357. {
  1358. $conditions = "uid='{$mybb->user['uid']}'";
  1359. }
  1360. else
  1361. {
  1362. $conditions = "uid='0' AND ipaddress='".$db->escape_string($session->ipaddress)."'";
  1363. }
  1364. $timecut = TIME_NOW-$mybb->settings['searchfloodtime'];
  1365. $query = $db->simple_select("searchlog", "*", "$conditions AND dateline > '$timecut'", array('order_by' => "dateline", 'order_dir' => "DESC"));
  1366. $last_search = $db->fetch_array($query);
  1367. // We shouldn't show remaining time if time is 0 or under.
  1368. $remaining_time = $mybb->settings['searchfloodtime']-(TIME_NOW-$last_search['dateline']);
  1369. // Users last search was within the flood time, show the error.
  1370. if($last_search['sid'] && $remaining_time > 0)
  1371. {
  1372. if($remaining_time == 1)
  1373. {
  1374. $lang->error_searchflooding = $lang->sprintf($lang->error_searchflooding_1, $mybb->settings['searchfloodtime']);
  1375. }
  1376. else
  1377. {
  1378. $lang->error_searchflooding = $lang->sprintf($lang->error_searchflooding, $mybb->settings['searchfloodtime'], $remaining_time);
  1379. }
  1380. error($lang->error_searchflooding);
  1381. }
  1382. }
  1383. $search_data = array(
  1384. "keywords" => $mybb->input['keywords'],
  1385. "postthread" => 1,
  1386. "tid" => $mybb->input['tid']
  1387. );
  1388. if($db->can_search == true)
  1389. {
  1390. if($mybb->settings['searchtype'] == "fulltext" && $db->supports_fulltext_boolean("posts") && $db->is_fulltext("posts"))
  1391. {
  1392. $search_results = perform_search_mysql_ft($search_data);
  1393. }
  1394. else
  1395. {
  1396. $search_results = perform_search_mysql($search_data);
  1397. }
  1398. }
  1399. else
  1400. {
  1401. error($lang->error_no_search_support);
  1402. }
  1403. $sid = md5(uniqid(microtime(), 1));
  1404. $searcharray = array(
  1405. "sid" => $db->escape_string($sid),
  1406. "uid" => $mybb->user['uid'],
  1407. "dateline" => $now,
  1408. "ipaddress" => $db->escape_string($session->ipaddress),
  1409. "threads" => $search_results['threads'],
  1410. "posts" => $search_results['posts'],
  1411. "resulttype" => 'posts',
  1412. "querycache" => $search_results['querycache'],
  1413. "keywords" => $db->escape_string($mybb->input['keywords'])
  1414. );
  1415. $plugins->run_hooks("search_thread_process");
  1416. $db->insert_query("searchlog", $searcharray);
  1417. $plugins->run_hooks("search_do_search_end");
  1418. redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
  1419. }
  1420. else
  1421. {
  1422. $plugins->run_hooks("search_start");
  1423. $srchlist = make_searchable_forums("", $fid);
  1424. $prefixselect = build_prefix_select('all', 'any', 1);
  1425. $rowspan = 5;
  1426. if(is_moderator())
  1427. {
  1428. $rowspan += 2;
  1429. eval("\$moderator_options = \"".$templates->get("search_moderator_options")."\";");
  1430. }
  1431. $plugins->run_hooks("search_end");
  1432. eval("\$search = \"".$templates->get("search")."\";");
  1433. output_page($search);
  1434. }
  1435. ?>