PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/search.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 1331 lines | 1106 code | 127 blank | 98 comment | 208 complexity | 2f485b4a3e239edafbece4c4e6b6648f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file