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