PageRenderTime 72ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/search.php

https://bitbucket.org/jablonski/yebood
PHP | 1239 lines | 1028 code | 131 blank | 80 comment | 186 complexity | f34307850ecea5442d5c87fa1f253a96 MD5 | raw file
Possible License(s): AGPL-1.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. // Set limit for the $total_match_count to reduce server load
  412. $total_matches_limit = 1000;
  413. $found_more_search_matches = false;
  414. if ($search_id)
  415. {
  416. if ($sql)
  417. {
  418. // Only return up to $total_matches_limit+1 ids (the last one will be removed later)
  419. $result = $db->sql_query_limit($sql, $total_matches_limit + 1);
  420. while ($row = $db->sql_fetchrow($result))
  421. {
  422. $id_ary[] = (int) $row[$field];
  423. }
  424. $db->sql_freeresult($result);
  425. }
  426. else if ($search_id == 'unreadposts')
  427. {
  428. // Only return up to $total_matches_limit+1 ids (the last one will be removed later)
  429. $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, $total_matches_limit + 1));
  430. }
  431. else
  432. {
  433. $search_id = '';
  434. }
  435. $total_match_count = sizeof($id_ary);
  436. if ($total_match_count)
  437. {
  438. // Limit the number to $total_matches_limit for pre-made searches
  439. if ($total_match_count > $total_matches_limit)
  440. {
  441. $found_more_search_matches = true;
  442. $total_match_count = $total_matches_limit;
  443. }
  444. // Make sure $start is set to the last page if it exceeds the amount
  445. if ($start < 0)
  446. {
  447. $start = 0;
  448. }
  449. else if ($start >= $total_match_count)
  450. {
  451. $start = floor(($total_match_count - 1) / $per_page) * $per_page;
  452. }
  453. $id_ary = array_slice($id_ary, $start, $per_page);
  454. }
  455. else
  456. {
  457. // Set $start to 0 if no matches were found
  458. $start = 0;
  459. }
  460. }
  461. // make sure that some arrays are always in the same order
  462. sort($ex_fid_ary);
  463. sort($m_approve_fid_ary);
  464. sort($author_id_ary);
  465. if (!empty($search->search_query))
  466. {
  467. $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);
  468. }
  469. else if (sizeof($author_id_ary))
  470. {
  471. $firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;
  472. $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);
  473. }
  474. // For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
  475. if (!sizeof($id_ary) && !$search_id)
  476. {
  477. trigger_error('NO_SEARCH_RESULTS');
  478. }
  479. $sql_where = '';
  480. if (sizeof($id_ary))
  481. {
  482. $sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
  483. $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
  484. $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);
  485. }
  486. if ($show_results == 'posts')
  487. {
  488. include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
  489. }
  490. else
  491. {
  492. include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  493. }
  494. $user->add_lang('viewtopic');
  495. // Grab icons
  496. $icons = $cache->obtain_icons();
  497. // Output header
  498. if ($found_more_search_matches)
  499. {
  500. $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
  501. }
  502. else
  503. {
  504. $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);
  505. }
  506. // define some vars for urls
  507. $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
  508. // Do not allow *only* wildcard being used for hilight
  509. $hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit;
  510. $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
  511. $u_show_results = '&amp;sr=' . $show_results;
  512. $u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
  513. $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
  514. $u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
  515. $u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
  516. $u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
  517. $u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
  518. $u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
  519. $u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
  520. $u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
  521. $u_search .= (!$search_child) ? '&amp;sc=0' : '';
  522. $u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
  523. $u_search .= ($return_chars != 300) ? '&amp;ch=' . $return_chars : '';
  524. $template->assign_vars(array(
  525. 'SEARCH_TITLE' => $l_search_title,
  526. 'SEARCH_MATCHES' => $l_search_matches,
  527. 'SEARCH_WORDS' => $search->search_query,
  528. 'IGNORED_WORDS' => (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
  529. 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
  530. 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),
  531. 'TOTAL_MATCHES' => $total_match_count,
  532. 'SEARCH_IN_RESULTS' => ($search_id) ? false : true,
  533. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  534. 'S_SELECT_SORT_KEY' => $s_sort_key,
  535. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  536. 'S_SEARCH_ACTION' => $u_search,
  537. 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true,
  538. 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'),
  539. 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
  540. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
  541. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
  542. 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
  543. 'U_SEARCH_WORDS' => $u_search,
  544. ));
  545. if ($sql_where)
  546. {
  547. if ($show_results == 'posts')
  548. {
  549. // @todo Joining this query to the one below?
  550. $sql = 'SELECT zebra_id, friend, foe
  551. FROM ' . ZEBRA_TABLE . '
  552. WHERE user_id = ' . $user->data['user_id'];
  553. $result = $db->sql_query($sql);
  554. $zebra = array();
  555. while ($row = $db->sql_fetchrow($result))
  556. {
  557. $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
  558. }
  559. $db->sql_freeresult($result);
  560. $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
  561. FROM ' . POSTS_TABLE . ' p
  562. LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
  563. LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
  564. LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
  565. WHERE $sql_where";
  566. }
  567. else
  568. {
  569. $sql_from = TOPICS_TABLE . ' t
  570. LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
  571. ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
  572. $sql_select = 't.*, f.forum_id, f.forum_name';
  573. if ($user->data['is_registered'])
  574. {
  575. if ($config['load_db_track'] && $author_id !== $user->data['user_id'])
  576. {
  577. $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
  578. AND t.topic_id = tp.topic_id)';
  579. $sql_select .= ', tp.topic_posted';
  580. }
  581. if ($config['load_db_lastread'])
  582. {
  583. $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
  584. AND t.topic_id = tt.topic_id)
  585. LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
  586. AND ft.forum_id = f.forum_id)';
  587. $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
  588. }
  589. }
  590. if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
  591. {
  592. $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
  593. $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
  594. }
  595. $sql = "SELECT $sql_select
  596. FROM $sql_from
  597. WHERE $sql_where";
  598. }
  599. $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
  600. $result = $db->sql_query($sql);
  601. $result_topic_id = 0;
  602. $rowset = array();
  603. if ($show_results == 'topics')
  604. {
  605. $forums = $rowset = $shadow_topic_list = array();
  606. while ($row = $db->sql_fetchrow($result))
  607. {
  608. $row['forum_id'] = (int) $row['forum_id'];
  609. $row['topic_id'] = (int) $row['topic_id'];
  610. if ($row['topic_status'] == ITEM_MOVED)
  611. {
  612. $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
  613. }
  614. $rowset[$row['topic_id']] = $row;
  615. if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
  616. {
  617. $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
  618. }
  619. $forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
  620. $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
  621. }
  622. $db->sql_freeresult($result);
  623. // If we have some shadow topics, update the rowset to reflect their topic information
  624. if (sizeof($shadow_topic_list))
  625. {
  626. $sql = 'SELECT *
  627. FROM ' . TOPICS_TABLE . '
  628. WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
  629. $result = $db->sql_query($sql);
  630. while ($row = $db->sql_fetchrow($result))
  631. {
  632. $orig_topic_id = $shadow_topic_list[$row['topic_id']];
  633. // We want to retain some values
  634. $row = array_merge($row, array(
  635. 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
  636. 'topic_status' => $rowset[$orig_topic_id]['topic_status'],
  637. 'forum_name' => $rowset[$orig_topic_id]['forum_name'])
  638. );
  639. $rowset[$orig_topic_id] = $row;
  640. }
  641. $db->sql_freeresult($result);
  642. }
  643. unset($shadow_topic_list);
  644. foreach ($forums as $forum_id => $forum)
  645. {
  646. if ($user->data['is_registered'] && $config['load_db_lastread'])
  647. {
  648. $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']);
  649. }
  650. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  651. {
  652. $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
  653. if (!$user->data['is_registered'])
  654. {
  655. $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
  656. }
  657. }
  658. }
  659. unset($forums);
  660. }
  661. else
  662. {
  663. $bbcode_bitfield = $text_only_message = '';
  664. $attach_list = array();
  665. while ($row = $db->sql_fetchrow($result))
  666. {
  667. // We pre-process some variables here for later usage
  668. $row['post_text'] = censor_text($row['post_text']);
  669. $text_only_message = $row['post_text'];
  670. // make list items visible as such
  671. if ($row['bbcode_uid'])
  672. {
  673. $text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
  674. // no BBCode in text only message
  675. strip_bbcode($text_only_message, $row['bbcode_uid']);
  676. }
  677. if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
  678. {
  679. $row['display_text_only'] = false;
  680. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  681. // Does this post have an attachment? If so, add it to the list
  682. if ($row['post_attachment'] && $config['allow_attachments'])
  683. {
  684. $attach_list[$row['forum_id']][] = $row['post_id'];
  685. }
  686. }
  687. else
  688. {
  689. $row['post_text'] = $text_only_message;
  690. $row['display_text_only'] = true;
  691. }
  692. $rowset[] = $row;
  693. }
  694. $db->sql_freeresult($result);
  695. unset($text_only_message);
  696. // Instantiate BBCode if needed
  697. if ($bbcode_bitfield !== '')
  698. {
  699. include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  700. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  701. }
  702. // Pull attachment data
  703. if (sizeof($attach_list))
  704. {
  705. $use_attach_list = $attach_list;
  706. $attach_list = array();
  707. foreach ($use_attach_list as $forum_id => $_list)
  708. {
  709. if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  710. {
  711. $attach_list = array_merge($attach_list, $_list);
  712. }
  713. }
  714. }
  715. if (sizeof($attach_list))
  716. {
  717. $sql = 'SELECT *
  718. FROM ' . ATTACHMENTS_TABLE . '
  719. WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  720. AND in_message = 0
  721. ORDER BY filetime DESC, post_msg_id ASC';
  722. $result = $db->sql_query($sql);
  723. while ($row = $db->sql_fetchrow($result))
  724. {
  725. $attachments[$row['post_msg_id']][] = $row;
  726. }
  727. $db->sql_freeresult($result);
  728. }
  729. }
  730. if ($hilit)
  731. {
  732. // Remove bad highlights
  733. $hilit_array = array_filter(explode('|', $hilit), 'strlen');
  734. foreach ($hilit_array as $key => $value)
  735. {
  736. $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
  737. $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
  738. }
  739. $hilit = implode('|', $hilit_array);
  740. }
  741. foreach ($rowset as $row)
  742. {
  743. $forum_id = $row['forum_id'];
  744. $result_topic_id = $row['topic_id'];
  745. $topic_title = censor_text($row['topic_title']);
  746. // we need to select a forum id for this global topic
  747. if (!$forum_id)
  748. {
  749. if (!isset($g_forum_id))
  750. {
  751. // Get a list of forums the user cannot read
  752. $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
  753. // Determine first forum the user is able to read (must not be a category)
  754. $sql = 'SELECT forum_id
  755. FROM ' . FORUMS_TABLE . '
  756. WHERE forum_type = ' . FORUM_POST;
  757. if (sizeof($forum_ary))
  758. {
  759. $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
  760. }
  761. $result = $db->sql_query_limit($sql, 1);
  762. $g_forum_id = (int) $db->sql_fetchfield('forum_id');
  763. }
  764. $u_forum_id = $g_forum_id;
  765. }
  766. else
  767. {
  768. $u_forum_id = $forum_id;
  769. }
  770. $view_topic_url_params = "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : '');
  771. $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
  772. $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
  773. if ($show_results == 'topics')
  774. {
  775. if ($config['load_db_track'] && $author_id === $user->data['user_id'])
  776. {
  777. $row['topic_posted'] = 1;
  778. }
  779. $folder_img = $folder_alt = $topic_type = '';
  780. 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);
  781. $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;
  782. $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
  783. $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
  784. $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) : '';
  785. $row['topic_title'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['topic_title']);
  786. $tpl_ary = array(
  787. 'TOPIC_AUTHOR' => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  788. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  789. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  790. 'FIRST_POST_TIME' => $user->format_date($row['topic_time']),
  791. 'LAST_POST_SUBJECT' => $row['topic_last_post_subject'],
  792. 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']),
  793. 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']),
  794. 'LAST_POST_AUTHOR' => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  795. 'LAST_POST_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  796. 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  797. 'PAGINATION' => topic_generate_pagination($replies, $view_topic_url),
  798. 'TOPIC_TYPE' => $topic_type,
  799. 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt),
  800. 'TOPIC_FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'),
  801. 'TOPIC_FOLDER_IMG_ALT' => $user->lang[$folder_alt],
  802. 'TOPIC_FOLDER_IMG_WIDTH'=> $user->img($folder_img, '', false, '', 'width'),
  803. 'TOPIC_FOLDER_IMG_HEIGHT' => $user->img($folder_img, '', false, '', 'height'),
  804. 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
  805. 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
  806. 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
  807. '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']) : '',
  808. 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
  809. 'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
  810. 'S_TOPIC_TYPE' => $row['topic_type'],
  811. 'S_USER_POSTED' => (!empty($row['topic_posted'])) ? true : false,
  812. 'S_UNREAD_TOPIC' => $unread_topic,
  813. 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
  814. 'S_TOPIC_UNAPPROVED' => $topic_unapproved,
  815. 'S_POSTS_UNAPPROVED' => $posts_unapproved,
  816. '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'],
  817. 'U_LAST_POST_AUTHOR' => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  818. 'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  819. 'U_NEWEST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
  820. 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
  821. 'U_MCP_QUEUE' => $u_mcp_queue,
  822. );
  823. }
  824. else
  825. {
  826. if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
  827. {
  828. $template->assign_block_vars('searchresults', array(
  829. 'S_IGNORE_POST' => true,
  830. '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>'))
  831. );
  832. continue;
  833. }
  834. // Replace naughty words such as farty pants
  835. $row['post_subject'] = censor_text($row['post_subject']);
  836. if ($row['display_text_only'])
  837. {
  838. // now find context for the searched words
  839. $row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
  840. $row['post_text'] = bbcode_nl2br($row['post_text']);
  841. }
  842. else
  843. {
  844. // Second parse bbcode here
  845. if ($row['bbcode_bitfield'])
  846. {
  847. $bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
  848. }
  849. $row['post_text'] = bbcode_nl2br($row['post_text']);
  850. $row['post_text'] = smiley_text($row['post_text']);
  851. if (!empty($attachments[$row['post_id']]))
  852. {
  853. parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
  854. // we only display inline attachments
  855. unset($attachments[$row['post_id']]);
  856. }
  857. }
  858. if ($hilit)
  859. {
  860. // post highlighting
  861. $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
  862. $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_subject']);
  863. }
  864. $tpl_ary = array(
  865. 'POST_AUTHOR_FULL' => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  866. 'POST_AUTHOR_COLOUR' => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  867. 'POST_AUTHOR' => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  868. 'U_POST_AUTHOR' => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
  869. 'POST_SUBJECT' => $row['post_subject'],
  870. 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
  871. 'MESSAGE' => $row['post_text']
  872. );
  873. }
  874. $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
  875. 'FORUM_ID' => $forum_id,
  876. 'TOPIC_ID' => $result_topic_id,
  877. 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false,
  878. 'FORUM_TITLE' => $row['forum_name'],
  879. 'TOPIC_TITLE' => $topic_title,
  880. 'TOPIC_REPLIES' => $replies,
  881. 'TOPIC_VIEWS' => $row['topic_views'],
  882. 'U_VIEW_TOPIC' => $view_topic_url,
  883. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  884. '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'] : '')
  885. ));
  886. }
  887. if ($topic_id && ($topic_id == $result_topic_id))
  888. {
  889. $template->assign_vars(array(
  890. 'SEARCH_TOPIC' => $topic_title,
  891. 'U_SEARCH_TOPIC' => $view_topic_url
  892. ));
  893. }
  894. }
  895. unset($rowset);
  896. page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
  897. $template->set_filenames(array(
  898. 'body' => 'search_results.html')
  899. );
  900. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  901. page_footer();
  902. }
  903. // Search forum
  904. $s_forums = '';
  905. $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
  906. FROM ' . FORUMS_TABLE . ' f
  907. LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
  908. AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
  909. ORDER BY f.left_id ASC";
  910. $result = $db->sql_query($sql);
  911. $right = $cat_right = $padding_inc = 0;
  912. $padding = $forum_list = $holding = '';
  913. $pad_store = array('0' => '');
  914. while ($row = $db->sql_fetchrow($result))
  915. {
  916. if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
  917. {
  918. // Non-postable forum with no subforums, don't display
  919. continue;
  920. }
  921. if ($row['forum_type'] == FORUM_POST && ($row['left_id'] + 1 == $row['right_id']) && !$row['enable_indexing'])
  922. {
  923. // Postable forum with no subforums and indexing disabled, don't display
  924. continue;
  925. }
  926. if ($row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
  927. {
  928. // if this forum is a link or password protected (user has not entered the password yet) then skip to the next branch
  929. continue;
  930. }
  931. if ($row['left_id'] < $right)
  932. {
  933. $padding .= '&nbsp; &nbsp;';
  934. $pad_store[$row['parent_id']] = $padding;
  935. }
  936. else if ($row['left_id'] > $right + 1)
  937. {
  938. if (isset($pad_store[$row['parent_id']]))
  939. {
  940. $padding = $pad_store[$row['parent_id']];
  941. }
  942. else
  943. {
  944. continue;
  945. }
  946. }
  947. $right = $row['right_id'];
  948. if ($auth->acl_gets('!f_search', '!f_list', $row['forum_id']))
  949. {
  950. // if the user does not have permissions to search or see this forum skip only this forum/category
  951. continue;
  952. }
  953. $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
  954. if ($row['left_id'] > $cat_right)
  955. {
  956. // make sure we don't forget anything
  957. $s_forums .= $holding;
  958. $holding = '';
  959. }
  960. if ($row['right_id'] - $row['left_id'] > 1)
  961. {
  962. $cat_right = max($cat_right, $row['right_id']);
  963. $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  964. }
  965. else
  966. {
  967. $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  968. $holding = '';
  969. }
  970. }
  971. if ($holding)
  972. {
  973. $s_forums .= $holding;
  974. }
  975. $db->sql_freeresult($result);
  976. unset($pad_store);
  977. if (!$s_forums)
  978. {
  979. trigger_error('NO_SEARCH');
  980. }
  981. // Number of chars returned
  982. $s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
  983. $s_characters .= '<option value="0">0</option>';
  984. $s_characters .= '<option value="25">25</option>';
  985. $s_characters .= '<option value="50">50</option>';
  986. for ($i = 100; $i <= 1000 ; $i += 100)
  987. {
  988. $selected = ($i == 300) ? ' selected="selected"' : '';
  989. $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
  990. }
  991. $s_hidden_fields = array('t' => $topic_id);
  992. if ($_SID)
  993. {
  994. $s_hidden_fields['sid'] = $_SID;
  995. }
  996. if (!empty($_EXTRA_URL))
  997. {
  998. foreach ($_EXTRA_URL as $url_param)
  999. {
  1000. $url_param = explode('=', $url_param, 2);
  1001. $s_hidden_fields[$url_param[0]] = $url_param[1];
  1002. }
  1003. }
  1004. $template->assign_vars(array(
  1005. 'S_SEARCH_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx", false, true, 0), // We force no ?sid= appending by using 0
  1006. 'S_HIDDEN_FIELDS' => build_hidden_fields($s_hidden_fields),
  1007. 'S_CHARACTER_OPTIONS' => $s_characters,
  1008. 'S_FORUM_OPTIONS' => $s_forums,
  1009. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  1010. 'S_SELECT_SORT_KEY' => $s_sort_key,
  1011. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  1012. 'S_IN_SEARCH' => true,
  1013. ));
  1014. // only show recent searches to search administrators
  1015. if ($auth->acl_get('a_search'))
  1016. {
  1017. // Handle large objects differently for Oracle and MSSQL
  1018. switch ($db->sql_layer)
  1019. {
  1020. case 'oracle':
  1021. $sql = 'SELECT search_time, search_keywords
  1022. FROM ' . SEARCH_RESULTS_TABLE . '
  1023. WHERE dbms_lob.getlength(search_keywords) > 0
  1024. ORDER BY search_time DESC';
  1025. break;
  1026. case 'mssql':
  1027. case 'mssql_odbc':
  1028. case 'mssqlnative':
  1029. $sql = 'SELECT search_time, search_keywords
  1030. FROM ' . SEARCH_RESULTS_TABLE . '
  1031. WHERE DATALENGTH(search_keywords) > 0
  1032. ORDER BY search_time DESC';
  1033. break;
  1034. default:
  1035. $sql = 'SELECT search_time, search_keywords
  1036. FROM ' . SEARCH_RESULTS_TABLE . '
  1037. WHERE search_keywords <> \'\'
  1038. ORDER BY search_time DESC';
  1039. break;
  1040. }
  1041. $result = $db->sql_query_limit($sql, 5);
  1042. while ($row = $db->sql_fetchrow($result))
  1043. {
  1044. $keywords = $row['search_keywords'];
  1045. $template->assign_block_vars('recentsearch', array(
  1046. 'KEYWORDS' => $keywords,
  1047. 'TIME' => $user->format_date($row['search_time']),
  1048. 'U_KEYWORDS' => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
  1049. ));
  1050. }
  1051. $db->sql_freeresult($result);
  1052. }
  1053. // Output the basic page
  1054. page_header($user->lang['SEARCH']);
  1055. $template->set_filenames(array(
  1056. 'body' => 'search_body.html')
  1057. );
  1058. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  1059. page_footer();
  1060. ?>