PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/search.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 1214 lines | 1009 code | 129 blank | 76 comment | 182 complexity | 14b528df97c41076aa0ff60a51ce9604 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. define('IN_PHPBB', true);
  14. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  15. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  16. include($phpbb_root_path . 'common.' . $phpEx);
  17. // Start session management
  18. $user->session_begin();
  19. $auth->acl($user->data);
  20. $user->setup('search');
  21. // Define initial vars
  22. $mode = request_var('mode', '');
  23. $search_id = request_var('search_id', '');
  24. $start = max(request_var('start', 0), 0);
  25. $post_id = request_var('p', 0);
  26. $topic_id = request_var('t', 0);
  27. $view = request_var('view', '');
  28. $submit = request_var('submit', false);
  29. $keywords = utf8_normalize_nfc(request_var('keywords', '', true));
  30. $add_keywords = utf8_normalize_nfc(request_var('add_keywords', '', true));
  31. $author = request_var('author', '', true);
  32. $author_id = request_var('author_id', 0);
  33. $show_results = ($topic_id) ? 'posts' : request_var('sr', 'posts');
  34. $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
  35. $search_terms = request_var('terms', 'all');
  36. $search_fields = request_var('sf', 'all');
  37. $search_child = request_var('sc', true);
  38. $sort_days = request_var('st', 0);
  39. $sort_key = request_var('sk', 't');
  40. $sort_dir = request_var('sd', 'd');
  41. $return_chars = request_var('ch', ($topic_id) ? -1 : 300);
  42. $search_forum = request_var('fid', array(0));
  43. // We put login boxes for the case if search_id is newposts, egosearch or unreadposts
  44. // because a guest should be able to log in even if guests search is not permitted
  45. switch ($search_id)
  46. {
  47. // Egosearch is an author search
  48. case 'egosearch':
  49. $author_id = $user->data['user_id'];
  50. if ($user->data['user_id'] == ANONYMOUS)
  51. {
  52. login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
  53. }
  54. break;
  55. // Search for unread posts needs to be allowed and user to be logged in if topics tracking for guests is disabled
  56. case 'unreadposts':
  57. if (!$config['load_unreads_search'])
  58. {
  59. $template->assign_var('S_NO_SEARCH', true);
  60. trigger_error('NO_SEARCH_UNREADS');
  61. }
  62. else if (!$config['load_anon_lastread'] && !$user->data['is_registered'])
  63. {
  64. login_box('', $user->lang['LOGIN_EXPLAIN_UNREADSEARCH']);
  65. }
  66. break;
  67. // The "new posts" search uses user_lastvisit which is user based, so it should require user to log in.
  68. case 'newposts':
  69. if ($user->data['user_id'] == ANONYMOUS)
  70. {
  71. login_box('', $user->lang['LOGIN_EXPLAIN_NEWPOSTS']);
  72. }
  73. break;
  74. default:
  75. // There's nothing to do here for now ;)
  76. break;
  77. }
  78. // Is user able to search? Has search been disabled?
  79. if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
  80. {
  81. $template->assign_var('S_NO_SEARCH', true);
  82. trigger_error('NO_SEARCH');
  83. }
  84. // Check search load limit
  85. if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load'])))
  86. {
  87. $template->assign_var('S_NO_SEARCH', true);
  88. trigger_error('NO_SEARCH_TIME');
  89. }
  90. // It is applicable if the configuration setting is non-zero, and the user cannot
  91. // ignore the flood setting, and the search is a keyword search.
  92. $interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];
  93. if ($interval && !in_array($search_id, array('unreadposts', 'unanswered', 'active_topics', 'egosearch')) && !$auth->acl_get('u_ignoreflood'))
  94. {
  95. if ($user->data['user_last_search'] > time() - $interval)
  96. {
  97. $template->assign_var('S_NO_SEARCH', true);
  98. trigger_error('NO_SEARCH_TIME');
  99. }
  100. }
  101. // Define some vars
  102. $limit_days = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  103. $sort_by_text = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']);
  104. $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
  105. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
  106. if ($keywords || $author || $author_id || $search_id || $submit)
  107. {
  108. // clear arrays
  109. $id_ary = array();
  110. // If we are looking for authors get their ids
  111. $author_id_ary = array();
  112. $sql_author_match = '';
  113. if ($author_id)
  114. {
  115. $author_id_ary[] = $author_id;
  116. }
  117. else if ($author)
  118. {
  119. if ((strpos($author, '*') !== false) && (utf8_strlen(str_replace(array('*', '%'), '', $author)) < $config['min_search_author_chars']))
  120. {
  121. trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars']));
  122. }
  123. $sql_where = (strpos($author, '*') !== false) ? ' username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " username_clean = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
  124. $sql = 'SELECT user_id
  125. FROM ' . USERS_TABLE . "
  126. WHERE $sql_where
  127. AND user_type <> " . USER_IGNORE;
  128. $result = $db->sql_query_limit($sql, 100);
  129. while ($row = $db->sql_fetchrow($result))
  130. {
  131. $author_id_ary[] = (int) $row['user_id'];
  132. }
  133. $db->sql_freeresult($result);
  134. $sql_where = (strpos($author, '*') !== false) ? ' post_username ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " post_username = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
  135. $sql = 'SELECT 1 as guest_post
  136. FROM ' . POSTS_TABLE . "
  137. WHERE $sql_where
  138. AND poster_id = " . ANONYMOUS;
  139. $result = $db->sql_query_limit($sql, 1);
  140. $found_guest_post = $db->sql_fetchfield('guest_post');
  141. $db->sql_freeresult($result);
  142. if ($found_guest_post)
  143. {
  144. $author_id_ary[] = ANONYMOUS;
  145. $sql_author_match = (strpos($author, '*') !== false) ? ' ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
  146. }
  147. if (!sizeof($author_id_ary))
  148. {
  149. trigger_error('NO_SEARCH_RESULTS');
  150. }
  151. }
  152. // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode
  153. // so we can keep the old keywords in their old mode, but add the new ones as required words
  154. if ($add_keywords)
  155. {
  156. if ($search_terms == 'all')
  157. {
  158. $keywords .= ' ' . $add_keywords;
  159. }
  160. else
  161. {
  162. $search_terms = 'all';
  163. $keywords = implode(' |', explode(' ', preg_replace('#\s+#u', ' ', $keywords))) . ' ' .$add_keywords;
  164. }
  165. }
  166. // Which forums should not be searched? Author searches are also carried out in unindexed forums
  167. if (empty($keywords) && sizeof($author_id_ary))
  168. {
  169. $ex_fid_ary = array_keys($auth->acl_getf('!f_read', true));
  170. }
  171. else
  172. {
  173. $ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true))));
  174. }
  175. $not_in_fid = (sizeof($ex_fid_ary)) ? 'WHERE ' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . " OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ')' : "";
  176. $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, f.forum_flags, fa.user_id
  177. FROM ' . FORUMS_TABLE . ' f
  178. LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
  179. AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
  180. $not_in_fid
  181. ORDER BY f.left_id";
  182. $result = $db->sql_query($sql);
  183. $right_id = 0;
  184. $reset_search_forum = true;
  185. while ($row = $db->sql_fetchrow($result))
  186. {
  187. if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
  188. {
  189. $ex_fid_ary[] = (int) $row['forum_id'];
  190. continue;
  191. }
  192. // Exclude forums from active topics
  193. if (!($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && ($search_id == 'active_topics'))
  194. {
  195. $ex_fid_ary[] = (int) $row['forum_id'];
  196. continue;
  197. }
  198. if (sizeof($search_forum))
  199. {
  200. if ($search_child)
  201. {
  202. if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)
  203. {
  204. $right_id = (int) $row['right_id'];
  205. }
  206. else if ($row['right_id'] < $right_id)
  207. {
  208. continue;
  209. }
  210. }
  211. if (!in_array($row['forum_id'], $search_forum))
  212. {
  213. $ex_fid_ary[] = (int) $row['forum_id'];
  214. $reset_search_forum = false;
  215. }
  216. }
  217. }
  218. $db->sql_freeresult($result);
  219. // find out in which forums the user is allowed to view approved posts
  220. if ($auth->acl_get('m_approve'))
  221. {
  222. $m_approve_fid_ary = array(-1);
  223. $m_approve_fid_sql = '';
  224. }
  225. else if ($auth->acl_getf_global('m_approve'))
  226. {
  227. $m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary);
  228. $m_approve_fid_sql = ' AND (p.post_approved = 1' . ((sizeof($m_approve_fid_ary)) ? ' OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) : '') . ')';
  229. }
  230. else
  231. {
  232. $m_approve_fid_ary = array();
  233. $m_approve_fid_sql = ' AND p.post_approved = 1';
  234. }
  235. if ($reset_search_forum)
  236. {
  237. $search_forum = array();
  238. }
  239. // Select which method we'll use to obtain the post_id or topic_id information
  240. $search_type = basename($config['search_type']);
  241. if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
  242. {
  243. trigger_error('NO_SUCH_SEARCH_MODULE');
  244. }
  245. require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
  246. // We do some additional checks in the module to ensure it can actually be utilised
  247. $error = false;
  248. $search = new $search_type($error);
  249. if ($error)
  250. {
  251. trigger_error($error);
  252. }
  253. // let the search module split up the keywords
  254. if ($keywords)
  255. {
  256. $correct_query = $search->split_keywords($keywords, $search_terms);
  257. if (!$correct_query || (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id))
  258. {
  259. $ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], implode(' ', $search->common_words)) . '<br />' : '';
  260. trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max']));
  261. }
  262. }
  263. if (!$keywords && sizeof($author_id_ary))
  264. {
  265. // if it is an author search we want to show topics by default
  266. $show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts');
  267. $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
  268. }
  269. // define some variables needed for retrieving post_id/topic_id information
  270. $sort_by_sql = array('a' => 'u.username_clean', 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'p.post_subject' : 't.topic_title'));
  271. // pre-made searches
  272. $sql = $field = $l_search_title = '';
  273. if ($search_id)
  274. {
  275. switch ($search_id)
  276. {
  277. // Oh holy Bob, bring us some activity...
  278. case 'active_topics':
  279. $l_search_title = $user->lang['SEARCH_ACTIVE_TOPICS'];
  280. $show_results = 'topics';
  281. $sort_key = 't';
  282. $sort_dir = 'd';
  283. $sort_days = request_var('st', 7);
  284. $sort_by_sql['t'] = 't.topic_last_post_time';
  285. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
  286. $s_sort_key = $s_sort_dir = '';
  287. $last_post_time_sql = ($sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($sort_days * 24 * 3600)) : '';
  288. $sql = 'SELECT t.topic_last_post_time, t.topic_id
  289. FROM ' . TOPICS_TABLE . " t
  290. WHERE t.topic_moved_id = 0
  291. $last_post_time_sql
  292. " . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
  293. ' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . '
  294. ORDER BY t.topic_last_post_time DESC';
  295. $field = 'topic_id';
  296. break;
  297. case 'unanswered':
  298. $l_search_title = $user->lang['SEARCH_UNANSWERED'];
  299. $show_results = request_var('sr', 'topics');
  300. $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
  301. $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
  302. $sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title';
  303. $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
  304. $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : '';
  305. $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort;
  306. if ($sort_days)
  307. {
  308. $last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600));
  309. }
  310. else
  311. {
  312. $last_post_time = '';
  313. }
  314. if ($sort_key == 'a')
  315. {
  316. $sort_join = USERS_TABLE . ' u, ';
  317. $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort;
  318. }
  319. if ($show_results == 'posts')
  320. {
  321. $sql = "SELECT p.post_id
  322. FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
  323. WHERE t.topic_replies = 0
  324. AND p.topic_id = t.topic_id
  325. $last_post_time
  326. $m_approve_fid_sql
  327. " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
  328. $sql_sort";
  329. $field = 'post_id';
  330. }
  331. else
  332. {
  333. $sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id
  334. FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
  335. WHERE t.topic_replies = 0
  336. AND t.topic_moved_id = 0
  337. AND p.topic_id = t.topic_id
  338. $last_post_time
  339. $m_approve_fid_sql
  340. " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
  341. $sql_sort";
  342. $field = 'topic_id';
  343. }
  344. break;
  345. case 'unreadposts':
  346. $l_search_title = $user->lang['SEARCH_UNREAD'];
  347. // force sorting
  348. $show_results = 'topics';
  349. $sort_key = 't';
  350. $sort_by_sql['t'] = 't.topic_last_post_time';
  351. $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
  352. $sql_where = 'AND t.topic_moved_id = 0
  353. ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
  354. ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '');
  355. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
  356. $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
  357. break;
  358. case 'newposts':
  359. $l_search_title = $user->lang['SEARCH_NEW'];
  360. // force sorting
  361. $show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics';
  362. $sort_key = 't';
  363. $sort_dir = 'd';
  364. $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
  365. $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
  366. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
  367. $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
  368. if ($show_results == 'posts')
  369. {
  370. $sql = 'SELECT p.post_id
  371. FROM ' . POSTS_TABLE . ' p
  372. WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
  373. $m_approve_fid_sql
  374. " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
  375. $sql_sort";
  376. $field = 'post_id';
  377. }
  378. else
  379. {
  380. $sql = 'SELECT t.topic_id
  381. FROM ' . TOPICS_TABLE . ' t
  382. WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . '
  383. AND t.topic_moved_id = 0
  384. ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
  385. ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
  386. $sql_sort";
  387. /*
  388. [Fix] queued replies missing from "view new posts" (Bug #42705 - Patch by Paul)
  389. - Creates temporary table, query is far from optimized
  390. $sql = 'SELECT t.topic_id
  391. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
  392. WHERE p.post_time > ' . $user->data['user_lastvisit'] . '
  393. AND t.topic_id = p.topic_id
  394. AND t.topic_moved_id = 0
  395. ' . $m_approve_fid_sql . '
  396. ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
  397. GROUP BY t.topic_id
  398. $sql_sort";
  399. */
  400. $field = 'topic_id';
  401. }
  402. break;
  403. case 'egosearch':
  404. $l_search_title = $user->lang['SEARCH_SELF'];
  405. break;
  406. }
  407. }
  408. // show_results should not change after this
  409. $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
  410. $total_match_count = 0;
  411. if ($search_id)
  412. {
  413. if ($sql)
  414. {
  415. // only return up to 1000 ids (the last one will be removed later)
  416. $result = $db->sql_query_limit($sql, 1001 - $start, $start);
  417. while ($row = $db->sql_fetchrow($result))
  418. {
  419. $id_ary[] = (int) $row[$field];
  420. }
  421. $db->sql_freeresult($result);
  422. $total_match_count = sizeof($id_ary) + $start;
  423. $id_ary = array_slice($id_ary, 0, $per_page);
  424. }
  425. else if ($search_id == 'unreadposts')
  426. {
  427. $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, 1001 - $start, $start));
  428. $total_match_count = sizeof($id_ary) + $start;
  429. $id_ary = array_slice($id_ary, 0, $per_page);
  430. }
  431. else
  432. {
  433. $search_id = '';
  434. }
  435. }
  436. // make sure that some arrays are always in the same order
  437. sort($ex_fid_ary);
  438. sort($m_approve_fid_ary);
  439. sort($author_id_ary);
  440. if (!empty($search->search_query))
  441. {
  442. $total_match_count = $search->keyword_search($show_results, $search_fields, $search_terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $sql_author_match, $id_ary, $start, $per_page);
  443. }
  444. else if (sizeof($author_id_ary))
  445. {
  446. $firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;
  447. $total_match_count = $search->author_search($show_results, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $sql_author_match, $id_ary, $start, $per_page);
  448. }
  449. // For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
  450. if (!sizeof($id_ary) && !$search_id)
  451. {
  452. trigger_error('NO_SEARCH_RESULTS');
  453. }
  454. $sql_where = '';
  455. if (sizeof($id_ary))
  456. {
  457. $sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
  458. $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
  459. $sql_where .= ($show_results == 'posts') ? $m_approve_fid_sql : str_replace(array('p.post_approved', 'p.forum_id'), array('t.topic_approved', 't.forum_id'), $m_approve_fid_sql);
  460. }
  461. if ($show_results == 'posts')
  462. {
  463. include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
  464. }
  465. else
  466. {
  467. include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  468. }
  469. $user->add_lang('viewtopic');
  470. // Grab icons
  471. $icons = $cache->obtain_icons();
  472. // Output header
  473. if ($search_id && ($total_match_count > 1000))
  474. {
  475. // limit the number to 1000 for pre-made searches
  476. $total_match_count--;
  477. $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
  478. }
  479. else
  480. {
  481. $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count);
  482. }
  483. // define some vars for urls
  484. $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
  485. // Do not allow *only* wildcard being used for hilight
  486. $hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit;
  487. $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
  488. $u_show_results = '&amp;sr=' . $show_results;
  489. $u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
  490. $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
  491. $u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
  492. $u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
  493. $u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
  494. $u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
  495. $u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
  496. $u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
  497. $u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
  498. $u_search .= (!$search_child) ? '&amp;sc=0' : '';
  499. $u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
  500. $u_search .= ($return_chars != 300) ? '&amp;ch=' . $return_chars : '';
  501. $template->assign_vars(array(
  502. 'SEARCH_TITLE' => $l_search_title,
  503. 'SEARCH_MATCHES' => $l_search_matches,
  504. 'SEARCH_WORDS' => $search->search_query,
  505. 'IGNORED_WORDS' => (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
  506. 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
  507. 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),
  508. 'TOTAL_MATCHES' => $total_match_count,
  509. 'SEARCH_IN_RESULTS' => ($search_id) ? false : true,
  510. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  511. 'S_SELECT_SORT_KEY' => $s_sort_key,
  512. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  513. 'S_SEARCH_ACTION' => $u_search,
  514. 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true,
  515. 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'),
  516. 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
  517. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
  518. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
  519. 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
  520. 'U_SEARCH_WORDS' => $u_search,
  521. ));
  522. if ($sql_where)
  523. {
  524. if ($show_results == 'posts')
  525. {
  526. // @todo Joining this query to the one below?
  527. $sql = 'SELECT zebra_id, friend, foe
  528. FROM ' . ZEBRA_TABLE . '
  529. WHERE user_id = ' . $user->data['user_id'];
  530. $result = $db->sql_query($sql);
  531. $zebra = array();
  532. while ($row = $db->sql_fetchrow($result))
  533. {
  534. $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
  535. }
  536. $db->sql_freeresult($result);
  537. $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.username_clean, u.user_sig, u.user_sig_bbcode_uid, u.user_colour
  538. FROM ' . POSTS_TABLE . ' p
  539. LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
  540. LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
  541. LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
  542. WHERE $sql_where";
  543. }
  544. else
  545. {
  546. $sql_from = TOPICS_TABLE . ' t
  547. LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
  548. ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
  549. $sql_select = 't.*, f.forum_id, f.forum_name';
  550. if ($user->data['is_registered'])
  551. {
  552. if ($config['load_db_track'] && $author_id !== $user->data['user_id'])
  553. {
  554. $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
  555. AND t.topic_id = tp.topic_id)';
  556. $sql_select .= ', tp.topic_posted';
  557. }
  558. if ($config['load_db_lastread'])
  559. {
  560. $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
  561. AND t.topic_id = tt.topic_id)
  562. LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
  563. AND ft.forum_id = f.forum_id)';
  564. $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
  565. }
  566. }
  567. if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
  568. {
  569. $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
  570. $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
  571. }
  572. $sql = "SELECT $sql_select
  573. FROM $sql_from
  574. WHERE $sql_where";
  575. }
  576. $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
  577. $result = $db->sql_query($sql);
  578. $result_topic_id = 0;
  579. $rowset = array();
  580. if ($show_results == 'topics')
  581. {
  582. $forums = $rowset = $shadow_topic_list = array();
  583. while ($row = $db->sql_fetchrow($result))
  584. {
  585. $row['forum_id'] = (int) $row['forum_id'];
  586. $row['topic_id'] = (int) $row['topic_id'];
  587. if ($row['topic_status'] == ITEM_MOVED)
  588. {
  589. $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
  590. }
  591. $rowset[$row['topic_id']] = $row;
  592. if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
  593. {
  594. $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
  595. }
  596. $forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
  597. $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
  598. }
  599. $db->sql_freeresult($result);
  600. // If we have some shadow topics, update the rowset to reflect their topic information
  601. if (sizeof($shadow_topic_list))
  602. {
  603. $sql = 'SELECT *
  604. FROM ' . TOPICS_TABLE . '
  605. WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
  606. $result = $db->sql_query($sql);
  607. while ($row = $db->sql_fetchrow($result))
  608. {
  609. $orig_topic_id = $shadow_topic_list[$row['topic_id']];
  610. // We want to retain some values
  611. $row = array_merge($row, array(
  612. 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
  613. 'topic_status' => $rowset[$orig_topic_id]['topic_status'],
  614. 'forum_name' => $rowset[$orig_topic_id]['forum_name'])
  615. );
  616. $rowset[$orig_topic_id] = $row;
  617. }
  618. $db->sql_freeresult($result);
  619. }
  620. unset($shadow_topic_list);
  621. foreach ($forums as $forum_id => $forum)
  622. {
  623. if ($user->data['is_registered'] && $config['load_db_lastread'])
  624. {
  625. $topic_tracking_info[$forum_id] = get_topic_tracking($forum_id, $forum['topic_list'], $forum['rowset'], array($forum_id => $forum['mark_time']), ($forum_id) ? false : $forum['topic_list']);
  626. }
  627. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  628. {
  629. $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
  630. if (!$user->data['is_registered'])
  631. {
  632. $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
  633. }
  634. }
  635. }
  636. unset($forums);
  637. }
  638. else
  639. {
  640. $bbcode_bitfield = $text_only_message = '';
  641. $attach_list = array();
  642. while ($row = $db->sql_fetchrow($result))
  643. {
  644. // We pre-process some variables here for later usage
  645. $row['post_text'] = censor_text($row['post_text']);
  646. $text_only_message = $row['post_text'];
  647. // make list items visible as such
  648. if ($row['bbcode_uid'])
  649. {
  650. $text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
  651. // no BBCode in text only message
  652. strip_bbcode($text_only_message, $row['bbcode_uid']);
  653. }
  654. if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
  655. {
  656. $row['display_text_only'] = false;
  657. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  658. // Does this post have an attachment? If so, add it to the list
  659. if ($row['post_attachment'] && $config['allow_attachments'])
  660. {
  661. $attach_list[$row['forum_id']][] = $row['post_id'];
  662. }
  663. }
  664. else
  665. {
  666. $row['post_text'] = $text_only_message;
  667. $row['display_text_only'] = true;
  668. }
  669. $rowset[] = $row;
  670. }
  671. $db->sql_freeresult($result);
  672. unset($text_only_message);
  673. // Instantiate BBCode if needed
  674. if ($bbcode_bitfield !== '')
  675. {
  676. include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  677. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  678. }
  679. // Pull attachment data
  680. if (sizeof($attach_list))
  681. {
  682. $use_attach_list = $attach_list;
  683. $attach_list = array();
  684. foreach ($use_attach_list as $forum_id => $_list)
  685. {
  686. if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  687. {
  688. $attach_list = array_merge($attach_list, $_list);
  689. }
  690. }
  691. }
  692. if (sizeof($attach_list))
  693. {
  694. $sql = 'SELECT *
  695. FROM ' . ATTACHMENTS_TABLE . '
  696. WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  697. AND in_message = 0
  698. ORDER BY filetime DESC, post_msg_id ASC';
  699. $result = $db->sql_query($sql);
  700. while ($row = $db->sql_fetchrow($result))
  701. {
  702. $attachments[$row['post_msg_id']][] = $row;
  703. }
  704. $db->sql_freeresult($result);
  705. }
  706. }
  707. if ($hilit)
  708. {
  709. // Remove bad highlights
  710. $hilit_array = array_filter(explode('|', $hilit), 'strlen');
  711. foreach ($hilit_array as $key => $value)
  712. {
  713. $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
  714. $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
  715. }
  716. $hilit = implode('|', $hilit_array);
  717. }
  718. foreach ($rowset as $row)
  719. {
  720. $forum_id = $row['forum_id'];
  721. $result_topic_id = $row['topic_id'];
  722. $topic_title = censor_text($row['topic_title']);
  723. // we need to select a forum id for this global topic
  724. if (!$forum_id)
  725. {
  726. if (!isset($g_forum_id))
  727. {
  728. // Get a list of forums the user cannot read
  729. $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
  730. // Determine first forum the user is able to read (must not be a category)
  731. $sql = 'SELECT forum_id
  732. FROM ' . FORUMS_TABLE . '
  733. WHERE forum_type = ' . FORUM_POST;
  734. if (sizeof($forum_ary))
  735. {
  736. $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
  737. }
  738. $result = $db->sql_query_limit($sql, 1);
  739. $g_forum_id = (int) $db->sql_fetchfield('forum_id');
  740. }
  741. $u_forum_id = $g_forum_id;
  742. }
  743. else
  744. {
  745. $u_forum_id = $forum_id;
  746. }
  747. $view_topic_url_params = "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : '');
  748. $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
  749. $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
  750. if ($show_results == 'topics')
  751. {
  752. if ($config['load_db_track'] && $author_id === $user->data['user_id'])
  753. {
  754. $row['topic_posted'] = 1;
  755. }
  756. $folder_img = $folder_alt = $topic_type = '';
  757. topic_status($row, $replies, (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false, $folder_img, $folder_alt, $topic_type);
  758. $unread_topic = (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false;
  759. $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
  760. $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
  761. $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$result_topic_id", true, $user->session_id) : '';
  762. $row['topic_title'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['topic_title']);
  763. $tpl_ary = array(
  764. 'TOPIC_AUTHOR' => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  765. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  766. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  767. 'FIRST_POST_TIME' => $user->format_date($row['topic_time']),
  768. 'LAST_POST_SUBJECT' => $row['topic_last_post_subject'],
  769. 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']),
  770. 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']),
  771. 'LAST_POST_AUTHOR' => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  772. 'LAST_POST_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  773. 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  774. 'PAGINATION' => topic_generate_pagination($replies, $view_topic_url),
  775. 'TOPIC_TYPE' => $topic_type,
  776. 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt),
  777. 'TOPIC_FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'),
  778. 'TOPIC_FOLDER_IMG_ALT' => $user->lang[$folder_alt],
  779. 'TOPIC_FOLDER_IMG_WIDTH'=> $user->img($folder_img, '', false, '', 'width'),
  780. 'TOPIC_FOLDER_IMG_HEIGHT' => $user->img($folder_img, '', false, '', 'height'),
  781. 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
  782. 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
  783. 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
  784. 'ATTACH_ICON_IMG' => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
  785. 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
  786. 'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
  787. 'S_TOPIC_TYPE' => $row['topic_type'],
  788. 'S_USER_POSTED' => (!empty($row['topic_posted'])) ? true : false,
  789. 'S_UNREAD_TOPIC' => $unread_topic,
  790. 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
  791. 'S_TOPIC_UNAPPROVED' => $topic_unapproved,
  792. 'S_POSTS_UNAPPROVED' => $posts_unapproved,
  793. 'U_LAST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;p=' . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
  794. 'U_LAST_POST_AUTHOR' => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  795. 'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  796. 'U_NEWEST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
  797. 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
  798. 'U_MCP_QUEUE' => $u_mcp_queue,
  799. );
  800. }
  801. else
  802. {
  803. if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
  804. {
  805. $template->assign_block_vars('searchresults', array(
  806. 'S_IGNORE_POST' => true,
  807. 'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"$u_search&amp;start=$start&amp;p=" . $row['post_id'] . '&amp;view=show#p' . $row['post_id'] . '">', '</a>'))
  808. );
  809. continue;
  810. }
  811. // Replace naughty words such as farty pants
  812. $row['post_subject'] = censor_text($row['post_subject']);
  813. if ($row['display_text_only'])
  814. {
  815. // now find context for the searched words
  816. $row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
  817. $row['post_text'] = bbcode_nl2br($row['post_text']);
  818. }
  819. else
  820. {
  821. // Second parse bbcode here
  822. if ($row['bbcode_bitfield'])
  823. {
  824. $bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
  825. }
  826. $row['post_text'] = bbcode_nl2br($row['post_text']);
  827. $row['post_text'] = smiley_text($row['post_text']);
  828. if (!empty($attachments[$row['post_id']]))
  829. {
  830. parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
  831. // we only display inline attachments
  832. unset($attachments[$row['post_id']]);
  833. }
  834. }
  835. if ($hilit)
  836. {
  837. // post highlighting
  838. $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
  839. $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_subject']);
  840. }
  841. $tpl_ary = array(
  842. 'POST_AUTHOR_FULL' => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  843. 'POST_AUTHOR_COLOUR' => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  844. 'POST_AUTHOR' => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  845. 'U_POST_AUTHOR' => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  846. 'POST_SUBJECT' => $row['post_subject'],
  847. 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
  848. 'MESSAGE' => $row['post_text']
  849. );
  850. }
  851. $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
  852. 'FORUM_ID' => $forum_id,
  853. 'TOPIC_ID' => $result_topic_id,
  854. 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false,
  855. 'FORUM_TITLE' => $row['forum_name'],
  856. 'TOPIC_TITLE' => $topic_title,
  857. 'TOPIC_REPLIES' => $replies,
  858. 'TOPIC_VIEWS' => $row['topic_views'],
  859. 'U_VIEW_TOPIC' => $view_topic_url,
  860. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  861. 'U_VIEW_POST' => (!empty($row['post_id'])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=" . $row['topic_id'] . '&amp;p=' . $row['post_id'] . (($u_hilit) ? '&amp;hilit=' . $u_hilit : '')) . '#p' . $row['post_id'] : '')
  862. ));
  863. }
  864. if ($topic_id && ($topic_id == $result_topic_id))
  865. {
  866. $template->assign_vars(array(
  867. 'SEARCH_TOPIC' => $topic_title,
  868. 'U_SEARCH_TOPIC' => $view_topic_url
  869. ));
  870. }
  871. }
  872. unset($rowset);
  873. page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
  874. $template->set_filenames(array(
  875. 'body' => 'search_results.html')
  876. );
  877. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  878. page_footer();
  879. }
  880. // Search forum
  881. $s_forums = '';
  882. $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, f.enable_indexing, fa.user_id
  883. FROM ' . FORUMS_TABLE . ' f
  884. LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
  885. AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
  886. ORDER BY f.left_id ASC";
  887. $result = $db->sql_query($sql);
  888. $right = $cat_right = $padding_inc = 0;
  889. $padding = $forum_list = $holding = '';
  890. $pad_store = array('0' => '');
  891. while ($row = $db->sql_fetchrow($result))
  892. {
  893. if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
  894. {
  895. // Non-postable forum with no subforums, don't display
  896. continue;
  897. }
  898. if ($row['forum_type'] == FORUM_POST && ($row['left_id'] + 1 == $row['right_id']) && !$row['enable_indexing'])
  899. {
  900. // Postable forum with no subforums and indexing disabled, don't display
  901. continue;
  902. }
  903. if ($row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
  904. {
  905. // if this forum is a link or password protected (user has not entered the password yet) then skip to the next branch
  906. continue;
  907. }
  908. if ($row['left_id'] < $right)
  909. {
  910. $padding .= '&nbsp; &nbsp;';
  911. $pad_store[$row['parent_id']] = $padding;
  912. }
  913. else if ($row['left_id'] > $right + 1)
  914. {
  915. if (isset($pad_store[$row['parent_id']]))
  916. {
  917. $padding = $pad_store[$row['parent_id']];
  918. }
  919. else
  920. {
  921. continue;
  922. }
  923. }
  924. $right = $row['right_id'];
  925. if ($auth->acl_gets('!f_search', '!f_list', $row['forum_id']))
  926. {
  927. // if the user does not have permissions to search or see this forum skip only this forum/category
  928. continue;
  929. }
  930. $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
  931. if ($row['left_id'] > $cat_right)
  932. {
  933. // make sure we don't forget anything
  934. $s_forums .= $holding;
  935. $holding = '';
  936. }
  937. if ($row['right_id'] - $row['left_id'] > 1)
  938. {
  939. $cat_right = max($cat_right, $row['right_id']);
  940. $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  941. }
  942. else
  943. {
  944. $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  945. $holding = '';
  946. }
  947. }
  948. if ($holding)
  949. {
  950. $s_forums .= $holding;
  951. }
  952. $db->sql_freeresult($result);
  953. unset($pad_store);
  954. if (!$s_forums)
  955. {
  956. trigger_error('NO_SEARCH');
  957. }
  958. // Number of chars returned
  959. $s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
  960. $s_characters .= '<option value="0">0</option>';
  961. $s_characters .= '<option value="25">25</option>';
  962. $s_characters .= '<option value="50">50</option>';
  963. for ($i = 100; $i <= 1000 ; $i += 100)
  964. {
  965. $selected = ($i == 300) ? ' selected="selected"' : '';
  966. $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
  967. }
  968. $s_hidden_fields = array('t' => $topic_id);
  969. if ($_SID)
  970. {
  971. $s_hidden_fields['sid'] = $_SID;
  972. }
  973. if (!empty($_EXTRA_URL))
  974. {
  975. foreach ($_EXTRA_URL as $url_param)
  976. {
  977. $url_param = explode('=', $url_param, 2);
  978. $s_hidden_fields[$url_param[0]] = $url_param[1];
  979. }
  980. }
  981. $template->assign_vars(array(
  982. 'S_SEARCH_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx", false, true, 0), // We force no ?sid= appending by using 0
  983. 'S_HIDDEN_FIELDS' => build_hidden_fields($s_hidden_fields),
  984. 'S_CHARACTER_OPTIONS' => $s_characters,
  985. 'S_FORUM_OPTIONS' => $s_forums,
  986. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  987. 'S_SELECT_SORT_KEY' => $s_sort_key,
  988. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  989. 'S_IN_SEARCH' => true,
  990. ));
  991. // only show recent searches to search administrators
  992. if ($auth->acl_get('a_search'))
  993. {
  994. // Handle large objects differently for Oracle and MSSQL
  995. switch ($db->sql_layer)
  996. {
  997. case 'oracle':
  998. $sql = 'SELECT search_time, search_keywords
  999. FROM ' . SEARCH_RESULTS_TABLE . '
  1000. WHERE dbms_lob.getlength(search_keywords) > 0
  1001. ORDER BY search_time DESC';
  1002. break;
  1003. case 'mssql':
  1004. case 'mssql_odbc':
  1005. case 'mssqlnative':
  1006. $sql = 'SELECT search_time, search_keywords
  1007. FROM ' . SEARCH_RESULTS_TABLE . '
  1008. WHERE DATALENGTH(search_keywords) > 0
  1009. ORDER BY search_time DESC';
  1010. break;
  1011. default:
  1012. $sql = 'SELECT search_time, search_keywords
  1013. FROM ' . SEARCH_RESULTS_TABLE . '
  1014. WHERE search_keywords <> \'\'
  1015. ORDER BY search_time DESC';
  1016. break;
  1017. }
  1018. $result = $db->sql_query_limit($sql, 5);
  1019. while ($row = $db->sql_fetchrow($result))
  1020. {
  1021. $keywords = $row['search_keywords'];
  1022. $template->assign_block_vars('recentsearch', array(
  1023. 'KEYWORDS' => $keywords,
  1024. 'TIME' => $user->format_date($row['search_time']),
  1025. 'U_KEYWORDS' => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
  1026. ));
  1027. }
  1028. $db->sql_freeresult($result);
  1029. }
  1030. // Output the basic page
  1031. page_header($user->lang['SEARCH']);
  1032. $template->set_filenames(array(
  1033. 'body' => 'search_body.html')
  1034. );
  1035. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  1036. page_footer();
  1037. ?>