PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/zafenio/search.php

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