search.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/search.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1214 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +define('IN_PHPBB', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +include($phpbb_root_path . 'common.' . $phpEx);
    1.21 +
    1.22 +// Start session management
    1.23 +$user->session_begin();
    1.24 +$auth->acl($user->data);
    1.25 +$user->setup('search');
    1.26 +
    1.27 +// Define initial vars
    1.28 +$mode			= request_var('mode', '');
    1.29 +$search_id		= request_var('search_id', '');
    1.30 +$start			= max(request_var('start', 0), 0);
    1.31 +$post_id		= request_var('p', 0);
    1.32 +$topic_id		= request_var('t', 0);
    1.33 +$view			= request_var('view', '');
    1.34 +
    1.35 +$submit			= request_var('submit', false);
    1.36 +$keywords		= utf8_normalize_nfc(request_var('keywords', '', true));
    1.37 +$add_keywords	= utf8_normalize_nfc(request_var('add_keywords', '', true));
    1.38 +$author			= request_var('author', '', true);
    1.39 +$author_id		= request_var('author_id', 0);
    1.40 +$show_results	= ($topic_id) ? 'posts' : request_var('sr', 'posts');
    1.41 +$show_results	= ($show_results == 'posts') ? 'posts' : 'topics';
    1.42 +$search_terms	= request_var('terms', 'all');
    1.43 +$search_fields	= request_var('sf', 'all');
    1.44 +$search_child	= request_var('sc', true);
    1.45 +
    1.46 +$sort_days		= request_var('st', 0);
    1.47 +$sort_key		= request_var('sk', 't');
    1.48 +$sort_dir		= request_var('sd', 'd');
    1.49 +
    1.50 +$return_chars	= request_var('ch', ($topic_id) ? -1 : 300);
    1.51 +$search_forum	= request_var('fid', array(0));
    1.52 +
    1.53 +// We put login boxes for the case if search_id is newposts, egosearch or unreadposts
    1.54 +// because a guest should be able to log in even if guests search is not permitted
    1.55 +
    1.56 +switch ($search_id)
    1.57 +{
    1.58 +	// Egosearch is an author search
    1.59 +	case 'egosearch':
    1.60 +		$author_id = $user->data['user_id'];
    1.61 +		if ($user->data['user_id'] == ANONYMOUS)
    1.62 +		{
    1.63 +			login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
    1.64 +		}
    1.65 +	break;
    1.66 +
    1.67 +	// Search for unread posts needs to be allowed and user to be logged in if topics tracking for guests is disabled
    1.68 +	case 'unreadposts':
    1.69 +		if (!$config['load_unreads_search'])
    1.70 +		{
    1.71 +			$template->assign_var('S_NO_SEARCH', true);
    1.72 +			trigger_error('NO_SEARCH_UNREADS');
    1.73 +		}
    1.74 +		else if (!$config['load_anon_lastread'] && !$user->data['is_registered'])
    1.75 +		{
    1.76 +			login_box('', $user->lang['LOGIN_EXPLAIN_UNREADSEARCH']);
    1.77 +		}
    1.78 +	break;
    1.79 +	
    1.80 +	// The "new posts" search uses user_lastvisit which is user based, so it should require user to log in.
    1.81 +	case 'newposts':
    1.82 +		if ($user->data['user_id'] == ANONYMOUS)
    1.83 +		{
    1.84 +			login_box('', $user->lang['LOGIN_EXPLAIN_NEWPOSTS']);
    1.85 +		}
    1.86 +	break;
    1.87 +	
    1.88 +	default:
    1.89 +		// There's nothing to do here for now ;)
    1.90 +	break;
    1.91 +}
    1.92 +
    1.93 +// Is user able to search? Has search been disabled?
    1.94 +if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
    1.95 +{
    1.96 +	$template->assign_var('S_NO_SEARCH', true);
    1.97 +	trigger_error('NO_SEARCH');
    1.98 +}
    1.99 +
   1.100 +// Check search load limit
   1.101 +if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load'])))
   1.102 +{
   1.103 +	$template->assign_var('S_NO_SEARCH', true);
   1.104 +	trigger_error('NO_SEARCH_TIME');
   1.105 +}
   1.106 +
   1.107 +// It is applicable if the configuration setting is non-zero, and the user cannot
   1.108 +// ignore the flood setting, and the search is a keyword search.
   1.109 +$interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];
   1.110 +if ($interval && !in_array($search_id, array('unreadposts', 'unanswered', 'active_topics', 'egosearch')) && !$auth->acl_get('u_ignoreflood'))
   1.111 +{
   1.112 +	if ($user->data['user_last_search'] > time() - $interval)
   1.113 +	{
   1.114 +		$template->assign_var('S_NO_SEARCH', true);
   1.115 +		trigger_error('NO_SEARCH_TIME');
   1.116 +	}
   1.117 +}
   1.118 +
   1.119 +// Define some vars
   1.120 +$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']);
   1.121 +$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']);
   1.122 +
   1.123 +$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
   1.124 +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);
   1.125 +
   1.126 +if ($keywords || $author || $author_id || $search_id || $submit)
   1.127 +{
   1.128 +	// clear arrays
   1.129 +	$id_ary = array();
   1.130 +
   1.131 +	// If we are looking for authors get their ids
   1.132 +	$author_id_ary = array();
   1.133 +	$sql_author_match = '';
   1.134 +	if ($author_id)
   1.135 +	{
   1.136 +		$author_id_ary[] = $author_id;
   1.137 +	}
   1.138 +	else if ($author)
   1.139 +	{
   1.140 +		if ((strpos($author, '*') !== false) && (utf8_strlen(str_replace(array('*', '%'), '', $author)) < $config['min_search_author_chars']))
   1.141 +		{
   1.142 +			trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars']));
   1.143 +		}
   1.144 +
   1.145 +		$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)) . "'";
   1.146 +
   1.147 +		$sql = 'SELECT user_id
   1.148 +			FROM ' . USERS_TABLE . "
   1.149 +			WHERE $sql_where
   1.150 +				AND user_type <> " . USER_IGNORE;
   1.151 +		$result = $db->sql_query_limit($sql, 100);
   1.152 +
   1.153 +		while ($row = $db->sql_fetchrow($result))
   1.154 +		{
   1.155 +			$author_id_ary[] = (int) $row['user_id'];
   1.156 +		}
   1.157 +		$db->sql_freeresult($result);
   1.158 +
   1.159 +		$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)) . "'";
   1.160 +
   1.161 +		$sql = 'SELECT 1 as guest_post
   1.162 +			FROM ' . POSTS_TABLE . "
   1.163 +			WHERE $sql_where
   1.164 +				AND poster_id = " . ANONYMOUS;
   1.165 +		$result = $db->sql_query_limit($sql, 1);
   1.166 +		$found_guest_post = $db->sql_fetchfield('guest_post');
   1.167 +		$db->sql_freeresult($result);
   1.168 +
   1.169 +		if ($found_guest_post)
   1.170 +		{
   1.171 +			$author_id_ary[] = ANONYMOUS;
   1.172 +			$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)) . "'";
   1.173 +		}
   1.174 +
   1.175 +		if (!sizeof($author_id_ary))
   1.176 +		{
   1.177 +			trigger_error('NO_SEARCH_RESULTS');
   1.178 +		}
   1.179 +	}
   1.180 +
   1.181 +	// if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode
   1.182 +	// so we can keep the old keywords in their old mode, but add the new ones as required words
   1.183 +	if ($add_keywords)
   1.184 +	{
   1.185 +		if ($search_terms == 'all')
   1.186 +		{
   1.187 +			$keywords .= ' ' . $add_keywords;
   1.188 +		}
   1.189 +		else
   1.190 +		{
   1.191 +			$search_terms = 'all';
   1.192 +			$keywords = implode(' |', explode(' ', preg_replace('#\s+#u', ' ', $keywords))) . ' ' .$add_keywords;
   1.193 +		}
   1.194 +	}
   1.195 +
   1.196 +	// Which forums should not be searched? Author searches are also carried out in unindexed forums
   1.197 +	if (empty($keywords) && sizeof($author_id_ary))
   1.198 +	{
   1.199 +		$ex_fid_ary = array_keys($auth->acl_getf('!f_read', true));
   1.200 +	}
   1.201 +	else
   1.202 +	{
   1.203 +		$ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true))));
   1.204 +	}
   1.205 +
   1.206 +	$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'] . ')' : "";
   1.207 +
   1.208 +	$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
   1.209 +		FROM ' . FORUMS_TABLE . ' f
   1.210 +		LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
   1.211 +			AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
   1.212 +		$not_in_fid
   1.213 +		ORDER BY f.left_id";
   1.214 +	$result = $db->sql_query($sql);
   1.215 +
   1.216 +	$right_id = 0;
   1.217 +	$reset_search_forum = true;
   1.218 +	while ($row = $db->sql_fetchrow($result))
   1.219 +	{
   1.220 +		if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
   1.221 +		{
   1.222 +			$ex_fid_ary[] = (int) $row['forum_id'];
   1.223 +			continue;
   1.224 +		}
   1.225 +
   1.226 +		// Exclude forums from active topics
   1.227 +		if (!($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && ($search_id == 'active_topics'))
   1.228 +		{
   1.229 +			$ex_fid_ary[] = (int) $row['forum_id'];
   1.230 +			continue;
   1.231 +		}
   1.232 +
   1.233 +		if (sizeof($search_forum))
   1.234 +		{
   1.235 +			if ($search_child)
   1.236 +			{
   1.237 +				if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)
   1.238 +				{
   1.239 +					$right_id = (int) $row['right_id'];
   1.240 +				}
   1.241 +				else if ($row['right_id'] < $right_id)
   1.242 +				{
   1.243 +					continue;
   1.244 +				}
   1.245 +			}
   1.246 +
   1.247 +			if (!in_array($row['forum_id'], $search_forum))
   1.248 +			{
   1.249 +				$ex_fid_ary[] = (int) $row['forum_id'];
   1.250 +				$reset_search_forum = false;
   1.251 +			}
   1.252 +		}
   1.253 +	}
   1.254 +	$db->sql_freeresult($result);
   1.255 +
   1.256 +	// find out in which forums the user is allowed to view approved posts
   1.257 +	if ($auth->acl_get('m_approve'))
   1.258 +	{
   1.259 +		$m_approve_fid_ary = array(-1);
   1.260 +		$m_approve_fid_sql = '';
   1.261 +	}
   1.262 +	else if ($auth->acl_getf_global('m_approve'))
   1.263 +	{
   1.264 +		$m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary);
   1.265 +		$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) : '') . ')';
   1.266 +	}
   1.267 +	else
   1.268 +	{
   1.269 +		$m_approve_fid_ary = array();
   1.270 +		$m_approve_fid_sql = ' AND p.post_approved = 1';
   1.271 +	}
   1.272 +
   1.273 +	if ($reset_search_forum)
   1.274 +	{
   1.275 +		$search_forum = array();
   1.276 +	}
   1.277 +
   1.278 +	// Select which method we'll use to obtain the post_id or topic_id information
   1.279 +	$search_type = basename($config['search_type']);
   1.280 +
   1.281 +	if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
   1.282 +	{
   1.283 +		trigger_error('NO_SUCH_SEARCH_MODULE');
   1.284 +	}
   1.285 +
   1.286 +	require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
   1.287 +
   1.288 +	// We do some additional checks in the module to ensure it can actually be utilised
   1.289 +	$error = false;
   1.290 +	$search = new $search_type($error);
   1.291 +
   1.292 +	if ($error)
   1.293 +	{
   1.294 +		trigger_error($error);
   1.295 +	}
   1.296 +
   1.297 +	// let the search module split up the keywords
   1.298 +	if ($keywords)
   1.299 +	{
   1.300 +		$correct_query = $search->split_keywords($keywords, $search_terms);
   1.301 +		if (!$correct_query || (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id))
   1.302 +		{
   1.303 +			$ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], implode(' ', $search->common_words)) . '<br />' : '';
   1.304 +			trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max']));
   1.305 +		}
   1.306 +	}
   1.307 +
   1.308 +	if (!$keywords && sizeof($author_id_ary))
   1.309 +	{
   1.310 +		// if it is an author search we want to show topics by default
   1.311 +		$show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts');
   1.312 +		$show_results = ($show_results == 'posts') ? 'posts' : 'topics';
   1.313 +	}
   1.314 +
   1.315 +	// define some variables needed for retrieving post_id/topic_id information
   1.316 +	$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'));
   1.317 +
   1.318 +	// pre-made searches
   1.319 +	$sql = $field = $l_search_title = '';
   1.320 +	if ($search_id)
   1.321 +	{
   1.322 +		switch ($search_id)
   1.323 +		{
   1.324 +			// Oh holy Bob, bring us some activity...
   1.325 +			case 'active_topics':
   1.326 +				$l_search_title = $user->lang['SEARCH_ACTIVE_TOPICS'];
   1.327 +				$show_results = 'topics';
   1.328 +				$sort_key = 't';
   1.329 +				$sort_dir = 'd';
   1.330 +				$sort_days = request_var('st', 7);
   1.331 +				$sort_by_sql['t'] = 't.topic_last_post_time';
   1.332 +
   1.333 +				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);
   1.334 +				$s_sort_key = $s_sort_dir = '';
   1.335 +
   1.336 +				$last_post_time_sql = ($sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($sort_days * 24 * 3600)) : '';
   1.337 +
   1.338 +				$sql = 'SELECT t.topic_last_post_time, t.topic_id
   1.339 +					FROM ' . TOPICS_TABLE . " t
   1.340 +					WHERE t.topic_moved_id = 0
   1.341 +						$last_post_time_sql
   1.342 +						" . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
   1.343 +						' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . '
   1.344 +					ORDER BY t.topic_last_post_time DESC';
   1.345 +				$field = 'topic_id';
   1.346 +			break;
   1.347 +
   1.348 +			case 'unanswered':
   1.349 +				$l_search_title = $user->lang['SEARCH_UNANSWERED'];
   1.350 +				$show_results = request_var('sr', 'topics');
   1.351 +				$show_results = ($show_results == 'posts') ? 'posts' : 'topics';
   1.352 +				$sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
   1.353 +				$sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title';
   1.354 +				$sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
   1.355 +
   1.356 +				$sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : '';
   1.357 +				$sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort;
   1.358 +
   1.359 +				if ($sort_days)
   1.360 +				{
   1.361 +					$last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600));
   1.362 +				}
   1.363 +				else
   1.364 +				{
   1.365 +					$last_post_time = '';
   1.366 +				}
   1.367 +
   1.368 +				if ($sort_key == 'a')
   1.369 +				{
   1.370 +					$sort_join = USERS_TABLE . ' u, ';
   1.371 +					$sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort;
   1.372 +				}
   1.373 +				if ($show_results == 'posts')
   1.374 +				{
   1.375 +					$sql = "SELECT p.post_id
   1.376 +						FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
   1.377 +						WHERE t.topic_replies = 0
   1.378 +							AND p.topic_id = t.topic_id
   1.379 +							$last_post_time
   1.380 +							$m_approve_fid_sql
   1.381 +							" . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
   1.382 +							$sql_sort";
   1.383 +					$field = 'post_id';
   1.384 +				}
   1.385 +				else
   1.386 +				{
   1.387 +					$sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id
   1.388 +						FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
   1.389 +						WHERE t.topic_replies = 0
   1.390 +							AND t.topic_moved_id = 0
   1.391 +							AND p.topic_id = t.topic_id
   1.392 +							$last_post_time
   1.393 +							$m_approve_fid_sql
   1.394 +							" . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
   1.395 +						$sql_sort";
   1.396 +					$field = 'topic_id';
   1.397 +				}
   1.398 +			break;
   1.399 +
   1.400 +			case 'unreadposts':
   1.401 +				$l_search_title = $user->lang['SEARCH_UNREAD'];
   1.402 +				// force sorting
   1.403 +				$show_results = 'topics';
   1.404 +				$sort_key = 't';
   1.405 +				$sort_by_sql['t'] = 't.topic_last_post_time';
   1.406 +				$sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
   1.407 +
   1.408 +				$sql_where = 'AND t.topic_moved_id = 0
   1.409 +					' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
   1.410 +					' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '');
   1.411 +
   1.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);
   1.413 +				$s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
   1.414 +			break;
   1.415 +
   1.416 +			case 'newposts':
   1.417 +				$l_search_title = $user->lang['SEARCH_NEW'];
   1.418 +				// force sorting
   1.419 +				$show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics';
   1.420 +				$sort_key = 't';
   1.421 +				$sort_dir = 'd';
   1.422 +				$sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
   1.423 +				$sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
   1.424 +
   1.425 +				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);
   1.426 +				$s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
   1.427 +
   1.428 +				if ($show_results == 'posts')
   1.429 +				{
   1.430 +					$sql = 'SELECT p.post_id
   1.431 +						FROM ' . POSTS_TABLE . ' p
   1.432 +						WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
   1.433 +							$m_approve_fid_sql
   1.434 +							" . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
   1.435 +						$sql_sort";
   1.436 +					$field = 'post_id';
   1.437 +				}
   1.438 +				else
   1.439 +				{
   1.440 +					$sql = 'SELECT t.topic_id
   1.441 +						FROM ' . TOPICS_TABLE . ' t
   1.442 +						WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . '
   1.443 +							AND t.topic_moved_id = 0
   1.444 +							' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
   1.445 +							' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
   1.446 +						$sql_sort";
   1.447 +/*
   1.448 +		[Fix] queued replies missing from "view new posts" (Bug #42705 - Patch by Paul)
   1.449 +		- Creates temporary table, query is far from optimized
   1.450 +
   1.451 +					$sql = 'SELECT t.topic_id
   1.452 +						FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
   1.453 +						WHERE p.post_time > ' . $user->data['user_lastvisit'] . '
   1.454 +							AND t.topic_id = p.topic_id
   1.455 +							AND t.topic_moved_id = 0
   1.456 +							' . $m_approve_fid_sql . '
   1.457 +							' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
   1.458 +						GROUP BY t.topic_id
   1.459 +						$sql_sort";
   1.460 +*/
   1.461 +					$field = 'topic_id';
   1.462 +				}
   1.463 +			break;
   1.464 +
   1.465 +			case 'egosearch':
   1.466 +				$l_search_title = $user->lang['SEARCH_SELF'];
   1.467 +			break;
   1.468 +		}
   1.469 +	}
   1.470 +
   1.471 +	// show_results should not change after this
   1.472 +	$per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
   1.473 +	$total_match_count = 0;
   1.474 +
   1.475 +	if ($search_id)
   1.476 +	{
   1.477 +		if ($sql)
   1.478 +		{
   1.479 +			// only return up to 1000 ids (the last one will be removed later)
   1.480 +			$result = $db->sql_query_limit($sql, 1001 - $start, $start);
   1.481 +
   1.482 +			while ($row = $db->sql_fetchrow($result))
   1.483 +			{
   1.484 +				$id_ary[] = (int) $row[$field];
   1.485 +			}
   1.486 +			$db->sql_freeresult($result);
   1.487 +
   1.488 +			$total_match_count = sizeof($id_ary) + $start;
   1.489 +			$id_ary = array_slice($id_ary, 0, $per_page);
   1.490 +		}
   1.491 +		else if ($search_id == 'unreadposts')
   1.492 +		{
   1.493 +			$id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, 1001 - $start, $start));
   1.494 +
   1.495 +			$total_match_count = sizeof($id_ary) + $start;
   1.496 +			$id_ary = array_slice($id_ary, 0, $per_page);
   1.497 +		}
   1.498 +		else
   1.499 +		{
   1.500 +			$search_id = '';
   1.501 +		}
   1.502 +	}
   1.503 +
   1.504 +	// make sure that some arrays are always in the same order
   1.505 +	sort($ex_fid_ary);
   1.506 +	sort($m_approve_fid_ary);
   1.507 +	sort($author_id_ary);
   1.508 +
   1.509 +	if (!empty($search->search_query))
   1.510 +	{
   1.511 +		$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);
   1.512 +	}
   1.513 +	else if (sizeof($author_id_ary))
   1.514 +	{
   1.515 +		$firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;
   1.516 +		$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);
   1.517 +	}
   1.518 +
   1.519 +	// For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
   1.520 +	if (!sizeof($id_ary) && !$search_id)
   1.521 +	{
   1.522 +		trigger_error('NO_SEARCH_RESULTS');
   1.523 +	}
   1.524 +
   1.525 +	$sql_where = '';
   1.526 +
   1.527 +	if (sizeof($id_ary))
   1.528 +	{
   1.529 +		$sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
   1.530 +		$sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
   1.531 +		$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);
   1.532 +	}
   1.533 +
   1.534 +	if ($show_results == 'posts')
   1.535 +	{
   1.536 +		include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
   1.537 +	}
   1.538 +	else
   1.539 +	{
   1.540 +		include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
   1.541 +	}
   1.542 +
   1.543 +	$user->add_lang('viewtopic');
   1.544 +
   1.545 +	// Grab icons
   1.546 +	$icons = $cache->obtain_icons();
   1.547 +
   1.548 +	// Output header
   1.549 +	if ($search_id && ($total_match_count > 1000))
   1.550 +	{
   1.551 +		// limit the number to 1000 for pre-made searches
   1.552 +		$total_match_count--;
   1.553 +		$l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
   1.554 +	}
   1.555 +	else
   1.556 +	{
   1.557 +		$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);
   1.558 +	}
   1.559 +
   1.560 +	// define some vars for urls
   1.561 +	$hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
   1.562 +	// Do not allow *only* wildcard being used for hilight
   1.563 +	$hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit;
   1.564 +
   1.565 +	$u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
   1.566 +	$u_show_results = '&amp;sr=' . $show_results;
   1.567 +	$u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
   1.568 +
   1.569 +	$u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
   1.570 +	$u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
   1.571 +	$u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
   1.572 +	$u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
   1.573 +	$u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
   1.574 +	$u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
   1.575 +	$u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
   1.576 +	$u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
   1.577 +	$u_search .= (!$search_child) ? '&amp;sc=0' : '';
   1.578 +	$u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
   1.579 +	$u_search .= ($return_chars != 300) ? '&amp;ch=' . $return_chars : '';
   1.580 +
   1.581 +	$template->assign_vars(array(
   1.582 +		'SEARCH_TITLE'		=> $l_search_title,
   1.583 +		'SEARCH_MATCHES'	=> $l_search_matches,
   1.584 +		'SEARCH_WORDS'		=> $search->search_query,
   1.585 +		'IGNORED_WORDS'		=> (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
   1.586 +		'PAGINATION'		=> generate_pagination($u_search, $total_match_count, $per_page, $start),
   1.587 +		'PAGE_NUMBER'		=> on_page($total_match_count, $per_page, $start),
   1.588 +		'TOTAL_MATCHES'		=> $total_match_count,
   1.589 +		'SEARCH_IN_RESULTS'	=> ($search_id) ? false : true,
   1.590 +
   1.591 +		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
   1.592 +		'S_SELECT_SORT_KEY'		=> $s_sort_key,
   1.593 +		'S_SELECT_SORT_DAYS'	=> $s_limit_days,
   1.594 +		'S_SEARCH_ACTION'		=> $u_search,
   1.595 +		'S_SHOW_TOPICS'			=> ($show_results == 'posts') ? false : true,
   1.596 +
   1.597 +		'GOTO_PAGE_IMG'		=> $user->img('icon_post_target', 'GOTO_PAGE'),
   1.598 +		'NEWEST_POST_IMG'	=> $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
   1.599 +		'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
   1.600 +		'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
   1.601 +		'LAST_POST_IMG'		=> $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
   1.602 +
   1.603 +		'U_SEARCH_WORDS'	=> $u_search,
   1.604 +	));
   1.605 +
   1.606 +	if ($sql_where)
   1.607 +	{
   1.608 +		if ($show_results == 'posts')
   1.609 +		{
   1.610 +			// @todo Joining this query to the one below?
   1.611 +			$sql = 'SELECT zebra_id, friend, foe
   1.612 +				FROM ' . ZEBRA_TABLE . '
   1.613 +				WHERE user_id = ' . $user->data['user_id'];
   1.614 +			$result = $db->sql_query($sql);
   1.615 +
   1.616 +			$zebra = array();
   1.617 +			while ($row = $db->sql_fetchrow($result))
   1.618 +			{
   1.619 +				$zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
   1.620 +			}
   1.621 +			$db->sql_freeresult($result);
   1.622 +
   1.623 +			$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
   1.624 +				FROM ' . POSTS_TABLE . ' p
   1.625 +					LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
   1.626 +					LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
   1.627 +					LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
   1.628 +				WHERE $sql_where";
   1.629 +		}
   1.630 +		else
   1.631 +		{
   1.632 +			$sql_from = TOPICS_TABLE . ' t
   1.633 +				LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
   1.634 +				' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
   1.635 +			$sql_select = 't.*, f.forum_id, f.forum_name';
   1.636 +
   1.637 +			if ($user->data['is_registered'])
   1.638 +			{
   1.639 +				if ($config['load_db_track'] && $author_id !== $user->data['user_id'])
   1.640 +				{
   1.641 +					$sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
   1.642 +						AND t.topic_id = tp.topic_id)';
   1.643 +					$sql_select .= ', tp.topic_posted';
   1.644 +				}
   1.645 +
   1.646 +				if ($config['load_db_lastread'])
   1.647 +				{
   1.648 +					$sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
   1.649 +							AND t.topic_id = tt.topic_id)
   1.650 +						LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
   1.651 +							AND ft.forum_id = f.forum_id)';
   1.652 +					$sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
   1.653 +				}
   1.654 +			}
   1.655 +
   1.656 +			if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
   1.657 +			{
   1.658 +				$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
   1.659 +				$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
   1.660 +			}
   1.661 +
   1.662 +			$sql = "SELECT $sql_select
   1.663 +				FROM $sql_from
   1.664 +				WHERE $sql_where";
   1.665 +		}
   1.666 +		$sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
   1.667 +		$result = $db->sql_query($sql);
   1.668 +		$result_topic_id = 0;
   1.669 +
   1.670 +		$rowset = array();
   1.671 +
   1.672 +		if ($show_results == 'topics')
   1.673 +		{
   1.674 +			$forums = $rowset = $shadow_topic_list = array();
   1.675 +			while ($row = $db->sql_fetchrow($result))
   1.676 +			{
   1.677 +				$row['forum_id'] = (int) $row['forum_id'];
   1.678 +				$row['topic_id'] = (int) $row['topic_id'];
   1.679 +
   1.680 +				if ($row['topic_status'] == ITEM_MOVED)
   1.681 +				{
   1.682 +					$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
   1.683 +				}
   1.684 +
   1.685 +				$rowset[$row['topic_id']] = $row;
   1.686 +
   1.687 +				if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
   1.688 +				{
   1.689 +					$forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
   1.690 +				}
   1.691 +				$forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
   1.692 +				$forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
   1.693 +			}
   1.694 +			$db->sql_freeresult($result);
   1.695 +
   1.696 +			// If we have some shadow topics, update the rowset to reflect their topic information
   1.697 +			if (sizeof($shadow_topic_list))
   1.698 +			{
   1.699 +				$sql = 'SELECT *
   1.700 +					FROM ' . TOPICS_TABLE . '
   1.701 +					WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
   1.702 +				$result = $db->sql_query($sql);
   1.703 +
   1.704 +				while ($row = $db->sql_fetchrow($result))
   1.705 +				{
   1.706 +					$orig_topic_id = $shadow_topic_list[$row['topic_id']];
   1.707 +
   1.708 +					// We want to retain some values
   1.709 +					$row = array_merge($row, array(
   1.710 +						'topic_moved_id'	=> $rowset[$orig_topic_id]['topic_moved_id'],
   1.711 +						'topic_status'		=> $rowset[$orig_topic_id]['topic_status'],
   1.712 +						'forum_name'		=> $rowset[$orig_topic_id]['forum_name'])
   1.713 +					);
   1.714 +
   1.715 +					$rowset[$orig_topic_id] = $row;
   1.716 +				}
   1.717 +				$db->sql_freeresult($result);
   1.718 +			}
   1.719 +			unset($shadow_topic_list);
   1.720 +
   1.721 +			foreach ($forums as $forum_id => $forum)
   1.722 +			{
   1.723 +				if ($user->data['is_registered'] && $config['load_db_lastread'])
   1.724 +				{
   1.725 +					$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']);
   1.726 +				}
   1.727 +				else if ($config['load_anon_lastread'] || $user->data['is_registered'])
   1.728 +				{
   1.729 +					$topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
   1.730 +
   1.731 +					if (!$user->data['is_registered'])
   1.732 +					{
   1.733 +						$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
   1.734 +					}
   1.735 +				}
   1.736 +			}
   1.737 +			unset($forums);
   1.738 +		}
   1.739 +		else
   1.740 +		{
   1.741 +			$bbcode_bitfield = $text_only_message = '';
   1.742 +			$attach_list = array();
   1.743 +
   1.744 +			while ($row = $db->sql_fetchrow($result))
   1.745 +			{
   1.746 +				// We pre-process some variables here for later usage
   1.747 +				$row['post_text'] = censor_text($row['post_text']);
   1.748 +
   1.749 +				$text_only_message = $row['post_text'];
   1.750 +				// make list items visible as such
   1.751 +				if ($row['bbcode_uid'])
   1.752 +				{
   1.753 +					$text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
   1.754 +					// no BBCode in text only message
   1.755 +					strip_bbcode($text_only_message, $row['bbcode_uid']);
   1.756 +				}
   1.757 +
   1.758 +				if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
   1.759 +				{
   1.760 +					$row['display_text_only'] = false;
   1.761 +					$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
   1.762 +
   1.763 +					// Does this post have an attachment? If so, add it to the list
   1.764 +					if ($row['post_attachment'] && $config['allow_attachments'])
   1.765 +					{
   1.766 +						$attach_list[$row['forum_id']][] = $row['post_id'];
   1.767 +					}
   1.768 +				}
   1.769 +				else
   1.770 +				{
   1.771 +					$row['post_text'] = $text_only_message;
   1.772 +					$row['display_text_only'] = true;
   1.773 +				}
   1.774 +
   1.775 +				$rowset[] = $row;
   1.776 +			}
   1.777 +			$db->sql_freeresult($result);
   1.778 +
   1.779 +			unset($text_only_message);
   1.780 +
   1.781 +			// Instantiate BBCode if needed
   1.782 +			if ($bbcode_bitfield !== '')
   1.783 +			{
   1.784 +				include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
   1.785 +				$bbcode = new bbcode(base64_encode($bbcode_bitfield));
   1.786 +			}
   1.787 +
   1.788 +			// Pull attachment data
   1.789 +			if (sizeof($attach_list))
   1.790 +			{
   1.791 +				$use_attach_list = $attach_list;
   1.792 +				$attach_list = array();
   1.793 +
   1.794 +				foreach ($use_attach_list as $forum_id => $_list)
   1.795 +				{
   1.796 +					if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
   1.797 +					{
   1.798 +						$attach_list = array_merge($attach_list, $_list);
   1.799 +					}
   1.800 +				}
   1.801 +			}
   1.802 +
   1.803 +			if (sizeof($attach_list))
   1.804 +			{
   1.805 +				$sql = 'SELECT *
   1.806 +					FROM ' . ATTACHMENTS_TABLE . '
   1.807 +					WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
   1.808 +						AND in_message = 0
   1.809 +					ORDER BY filetime DESC, post_msg_id ASC';
   1.810 +				$result = $db->sql_query($sql);
   1.811 +
   1.812 +				while ($row = $db->sql_fetchrow($result))
   1.813 +				{
   1.814 +					$attachments[$row['post_msg_id']][] = $row;
   1.815 +				}
   1.816 +				$db->sql_freeresult($result);
   1.817 +			}
   1.818 +		}
   1.819 +
   1.820 +		if ($hilit)
   1.821 +		{
   1.822 +			// Remove bad highlights
   1.823 +			$hilit_array = array_filter(explode('|', $hilit), 'strlen');
   1.824 +			foreach ($hilit_array as $key => $value)
   1.825 +			{
   1.826 +				$hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
   1.827 +				$hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
   1.828 +			}
   1.829 +			$hilit = implode('|', $hilit_array);
   1.830 +		}
   1.831 +
   1.832 +		foreach ($rowset as $row)
   1.833 +		{
   1.834 +			$forum_id = $row['forum_id'];
   1.835 +			$result_topic_id = $row['topic_id'];
   1.836 +			$topic_title = censor_text($row['topic_title']);
   1.837 +
   1.838 +			// we need to select a forum id for this global topic
   1.839 +			if (!$forum_id)
   1.840 +			{
   1.841 +				if (!isset($g_forum_id))
   1.842 +				{
   1.843 +					// Get a list of forums the user cannot read
   1.844 +					$forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
   1.845 +
   1.846 +					// Determine first forum the user is able to read (must not be a category)
   1.847 +					$sql = 'SELECT forum_id
   1.848 +						FROM ' . FORUMS_TABLE . '
   1.849 +						WHERE forum_type = ' . FORUM_POST;
   1.850 +
   1.851 +					if (sizeof($forum_ary))
   1.852 +					{
   1.853 +						$sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
   1.854 +					}
   1.855 +
   1.856 +					$result = $db->sql_query_limit($sql, 1);
   1.857 +					$g_forum_id = (int) $db->sql_fetchfield('forum_id');
   1.858 +				}
   1.859 +				$u_forum_id = $g_forum_id;
   1.860 +			}
   1.861 +			else
   1.862 +			{
   1.863 +				$u_forum_id = $forum_id;
   1.864 +			}
   1.865 +
   1.866 +			$view_topic_url_params = "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : '');
   1.867 +			$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
   1.868 +
   1.869 +			$replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
   1.870 +
   1.871 +			if ($show_results == 'topics')
   1.872 +			{
   1.873 +				if ($config['load_db_track'] && $author_id === $user->data['user_id'])
   1.874 +				{
   1.875 +					$row['topic_posted'] = 1;
   1.876 +				}
   1.877 +
   1.878 +				$folder_img = $folder_alt = $topic_type = '';
   1.879 +				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);
   1.880 +
   1.881 +				$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;
   1.882 +
   1.883 +				$topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
   1.884 +				$posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
   1.885 +				$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) : '';
   1.886 +
   1.887 +				$row['topic_title'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['topic_title']);
   1.888 +
   1.889 +				$tpl_ary = array(
   1.890 +					'TOPIC_AUTHOR'				=> get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.891 +					'TOPIC_AUTHOR_COLOUR'		=> get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.892 +					'TOPIC_AUTHOR_FULL'			=> get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.893 +					'FIRST_POST_TIME'			=> $user->format_date($row['topic_time']),
   1.894 +					'LAST_POST_SUBJECT'			=> $row['topic_last_post_subject'],
   1.895 +					'LAST_POST_TIME'			=> $user->format_date($row['topic_last_post_time']),
   1.896 +					'LAST_VIEW_TIME'			=> $user->format_date($row['topic_last_view_time']),
   1.897 +					'LAST_POST_AUTHOR'			=> get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.898 +					'LAST_POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.899 +					'LAST_POST_AUTHOR_FULL'		=> get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.900 +
   1.901 +					'PAGINATION'		=> topic_generate_pagination($replies, $view_topic_url),
   1.902 +					'TOPIC_TYPE'		=> $topic_type,
   1.903 +
   1.904 +					'TOPIC_FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
   1.905 +					'TOPIC_FOLDER_IMG_SRC'	=> $user->img($folder_img, $folder_alt, false, '', 'src'),
   1.906 +					'TOPIC_FOLDER_IMG_ALT'	=> $user->lang[$folder_alt],
   1.907 +					'TOPIC_FOLDER_IMG_WIDTH'=> $user->img($folder_img, '', false, '', 'width'),
   1.908 +					'TOPIC_FOLDER_IMG_HEIGHT'	=> $user->img($folder_img, '', false, '', 'height'),
   1.909 +
   1.910 +					'TOPIC_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
   1.911 +					'TOPIC_ICON_IMG_WIDTH'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
   1.912 +					'TOPIC_ICON_IMG_HEIGHT'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
   1.913 +					'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']) : '',
   1.914 +					'UNAPPROVED_IMG'		=> ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
   1.915 +
   1.916 +					'S_TOPIC_GLOBAL'		=> (!$forum_id) ? true : false,
   1.917 +					'S_TOPIC_TYPE'			=> $row['topic_type'],
   1.918 +					'S_USER_POSTED'			=> (!empty($row['topic_posted'])) ? true : false,
   1.919 +					'S_UNREAD_TOPIC'		=> $unread_topic,
   1.920 +
   1.921 +					'S_TOPIC_REPORTED'		=> (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
   1.922 +					'S_TOPIC_UNAPPROVED'	=> $topic_unapproved,
   1.923 +					'S_POSTS_UNAPPROVED'	=> $posts_unapproved,
   1.924 +
   1.925 +					'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'],
   1.926 +					'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.927 +					'U_TOPIC_AUTHOR'		=> get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.928 +					'U_NEWEST_POST'			=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
   1.929 +					'U_MCP_REPORT'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
   1.930 +					'U_MCP_QUEUE'			=> $u_mcp_queue,
   1.931 +				);
   1.932 +			}
   1.933 +			else
   1.934 +			{
   1.935 +				if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
   1.936 +				{
   1.937 +					$template->assign_block_vars('searchresults', array(
   1.938 +						'S_IGNORE_POST' => true,
   1.939 +
   1.940 +						'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>'))
   1.941 +					);
   1.942 +
   1.943 +					continue;
   1.944 +				}
   1.945 +
   1.946 +				// Replace naughty words such as farty pants
   1.947 +				$row['post_subject'] = censor_text($row['post_subject']);
   1.948 +
   1.949 +				if ($row['display_text_only'])
   1.950 +				{
   1.951 +					// now find context for the searched words
   1.952 +					$row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
   1.953 +					$row['post_text'] = bbcode_nl2br($row['post_text']);
   1.954 +				}
   1.955 +				else
   1.956 +				{
   1.957 +					// Second parse bbcode here
   1.958 +					if ($row['bbcode_bitfield'])
   1.959 +					{
   1.960 +						$bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
   1.961 +					}
   1.962 +
   1.963 +					$row['post_text'] = bbcode_nl2br($row['post_text']);
   1.964 +					$row['post_text'] = smiley_text($row['post_text']);
   1.965 +
   1.966 +					if (!empty($attachments[$row['post_id']]))
   1.967 +					{
   1.968 +						parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
   1.969 +
   1.970 +						// we only display inline attachments
   1.971 +						unset($attachments[$row['post_id']]);
   1.972 +					}
   1.973 +				}
   1.974 +
   1.975 +				if ($hilit)
   1.976 +				{
   1.977 +					// post highlighting
   1.978 +					$row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
   1.979 +					$row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_subject']);
   1.980 +				}
   1.981 +
   1.982 +				$tpl_ary = array(
   1.983 +					'POST_AUTHOR_FULL'		=> get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.984 +					'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.985 +					'POST_AUTHOR'			=> get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.986 +					'U_POST_AUTHOR'			=> get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.987 +
   1.988 +					'POST_SUBJECT'		=> $row['post_subject'],
   1.989 +					'POST_DATE'			=> (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
   1.990 +					'MESSAGE'			=> $row['post_text']
   1.991 +				);
   1.992 +			}
   1.993 +
   1.994 +			$template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
   1.995 +				'FORUM_ID'			=> $forum_id,
   1.996 +				'TOPIC_ID'			=> $result_topic_id,
   1.997 +				'POST_ID'			=> ($show_results == 'posts') ? $row['post_id'] : false,
   1.998 +
   1.999 +				'FORUM_TITLE'		=> $row['forum_name'],
  1.1000 +				'TOPIC_TITLE'		=> $topic_title,
  1.1001 +				'TOPIC_REPLIES'		=> $replies,
  1.1002 +				'TOPIC_VIEWS'		=> $row['topic_views'],
  1.1003 +
  1.1004 +				'U_VIEW_TOPIC'		=> $view_topic_url,
  1.1005 +				'U_VIEW_FORUM'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  1.1006 +				'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'] : '')
  1.1007 +			));
  1.1008 +		}
  1.1009 +
  1.1010 +		if ($topic_id && ($topic_id == $result_topic_id))
  1.1011 +		{
  1.1012 +			$template->assign_vars(array(
  1.1013 +				'SEARCH_TOPIC'		=> $topic_title,
  1.1014 +				'U_SEARCH_TOPIC'	=> $view_topic_url
  1.1015 +			));
  1.1016 +		}
  1.1017 +	}
  1.1018 +	unset($rowset);
  1.1019 +
  1.1020 +	page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
  1.1021 +
  1.1022 +	$template->set_filenames(array(
  1.1023 +		'body' => 'search_results.html')
  1.1024 +	);
  1.1025 +	make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  1.1026 +
  1.1027 +	page_footer();
  1.1028 +}
  1.1029 +
  1.1030 +// Search forum
  1.1031 +$s_forums = '';
  1.1032 +$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
  1.1033 +	FROM ' . FORUMS_TABLE . ' f
  1.1034 +	LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
  1.1035 +		AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
  1.1036 +	ORDER BY f.left_id ASC";
  1.1037 +$result = $db->sql_query($sql);
  1.1038 +
  1.1039 +$right = $cat_right = $padding_inc = 0;
  1.1040 +$padding = $forum_list = $holding = '';
  1.1041 +$pad_store = array('0' => '');
  1.1042 +
  1.1043 +while ($row = $db->sql_fetchrow($result))
  1.1044 +{
  1.1045 +	if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
  1.1046 +	{
  1.1047 +		// Non-postable forum with no subforums, don't display
  1.1048 +		continue;
  1.1049 +	}
  1.1050 +
  1.1051 +	if ($row['forum_type'] == FORUM_POST && ($row['left_id'] + 1 == $row['right_id']) && !$row['enable_indexing'])
  1.1052 +	{
  1.1053 +		// Postable forum with no subforums and indexing disabled, don't display
  1.1054 +		continue;
  1.1055 +	}
  1.1056 +
  1.1057 +	if ($row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
  1.1058 +	{
  1.1059 +		// if this forum is a link or password protected (user has not entered the password yet) then skip to the next branch
  1.1060 +		continue;
  1.1061 +	}
  1.1062 +
  1.1063 +	if ($row['left_id'] < $right)
  1.1064 +	{
  1.1065 +		$padding .= '&nbsp; &nbsp;';
  1.1066 +		$pad_store[$row['parent_id']] = $padding;
  1.1067 +	}
  1.1068 +	else if ($row['left_id'] > $right + 1)
  1.1069 +	{
  1.1070 +		if (isset($pad_store[$row['parent_id']]))
  1.1071 +		{
  1.1072 +			$padding = $pad_store[$row['parent_id']];
  1.1073 +		}
  1.1074 +		else
  1.1075 +		{
  1.1076 +			continue;
  1.1077 +		}
  1.1078 +	}
  1.1079 +
  1.1080 +	$right = $row['right_id'];
  1.1081 +
  1.1082 +	if ($auth->acl_gets('!f_search', '!f_list', $row['forum_id']))
  1.1083 +	{
  1.1084 +		// if the user does not have permissions to search or see this forum skip only this forum/category
  1.1085 +		continue;
  1.1086 +	}
  1.1087 +
  1.1088 +	$selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
  1.1089 +
  1.1090 +	if ($row['left_id'] > $cat_right)
  1.1091 +	{
  1.1092 +		// make sure we don't forget anything
  1.1093 +		$s_forums .= $holding;
  1.1094 +		$holding = '';
  1.1095 +	}
  1.1096 +
  1.1097 +	if ($row['right_id'] - $row['left_id'] > 1)
  1.1098 +	{
  1.1099 +		$cat_right = max($cat_right, $row['right_id']);
  1.1100 +
  1.1101 +		$holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  1.1102 +	}
  1.1103 +	else
  1.1104 +	{
  1.1105 +		$s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
  1.1106 +		$holding = '';
  1.1107 +	}
  1.1108 +}
  1.1109 +
  1.1110 +if ($holding)
  1.1111 +{
  1.1112 +	$s_forums .= $holding;
  1.1113 +}
  1.1114 +
  1.1115 +$db->sql_freeresult($result);
  1.1116 +unset($pad_store);
  1.1117 +
  1.1118 +if (!$s_forums)
  1.1119 +{
  1.1120 +	trigger_error('NO_SEARCH');
  1.1121 +}
  1.1122 +
  1.1123 +// Number of chars returned
  1.1124 +$s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
  1.1125 +$s_characters .= '<option value="0">0</option>';
  1.1126 +$s_characters .= '<option value="25">25</option>';
  1.1127 +$s_characters .= '<option value="50">50</option>';
  1.1128 +
  1.1129 +for ($i = 100; $i <= 1000 ; $i += 100)
  1.1130 +{
  1.1131 +	$selected = ($i == 300) ? ' selected="selected"' : '';
  1.1132 +	$s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
  1.1133 +}
  1.1134 +
  1.1135 +$s_hidden_fields = array('t' => $topic_id);
  1.1136 +
  1.1137 +if ($_SID)
  1.1138 +{
  1.1139 +	$s_hidden_fields['sid'] = $_SID;
  1.1140 +}
  1.1141 +
  1.1142 +if (!empty($_EXTRA_URL))
  1.1143 +{
  1.1144 +	foreach ($_EXTRA_URL as $url_param)
  1.1145 +	{
  1.1146 +		$url_param = explode('=', $url_param, 2);
  1.1147 +		$s_hidden_fields[$url_param[0]] = $url_param[1];
  1.1148 +	}
  1.1149 +}
  1.1150 +
  1.1151 +$template->assign_vars(array(
  1.1152 +	'S_SEARCH_ACTION'		=> append_sid("{$phpbb_root_path}search.$phpEx", false, true, 0), // We force no ?sid= appending by using 0
  1.1153 +	'S_HIDDEN_FIELDS'		=> build_hidden_fields($s_hidden_fields),
  1.1154 +	'S_CHARACTER_OPTIONS'	=> $s_characters,
  1.1155 +	'S_FORUM_OPTIONS'		=> $s_forums,
  1.1156 +	'S_SELECT_SORT_DIR'		=> $s_sort_dir,
  1.1157 +	'S_SELECT_SORT_KEY'		=> $s_sort_key,
  1.1158 +	'S_SELECT_SORT_DAYS'	=> $s_limit_days,
  1.1159 +	'S_IN_SEARCH'			=> true,
  1.1160 +));
  1.1161 +
  1.1162 +// only show recent searches to search administrators
  1.1163 +if ($auth->acl_get('a_search'))
  1.1164 +{
  1.1165 +	// Handle large objects differently for Oracle and MSSQL
  1.1166 +	switch ($db->sql_layer)
  1.1167 +	{
  1.1168 +		case 'oracle':
  1.1169 +			$sql = 'SELECT search_time, search_keywords
  1.1170 +				FROM ' . SEARCH_RESULTS_TABLE . '
  1.1171 +				WHERE dbms_lob.getlength(search_keywords) > 0
  1.1172 +				ORDER BY search_time DESC';
  1.1173 +		break;
  1.1174 +
  1.1175 +		case 'mssql':
  1.1176 +		case 'mssql_odbc':
  1.1177 +		case 'mssqlnative':
  1.1178 +			$sql = 'SELECT search_time, search_keywords
  1.1179 +				FROM ' . SEARCH_RESULTS_TABLE . '
  1.1180 +				WHERE DATALENGTH(search_keywords) > 0
  1.1181 +				ORDER BY search_time DESC';
  1.1182 +		break;
  1.1183 +
  1.1184 +		default:
  1.1185 +			$sql = 'SELECT search_time, search_keywords
  1.1186 +				FROM ' . SEARCH_RESULTS_TABLE . '
  1.1187 +				WHERE search_keywords <> \'\'
  1.1188 +				ORDER BY search_time DESC';
  1.1189 +		break;
  1.1190 +	}
  1.1191 +	$result = $db->sql_query_limit($sql, 5);
  1.1192 +
  1.1193 +	while ($row = $db->sql_fetchrow($result))
  1.1194 +	{
  1.1195 +		$keywords = $row['search_keywords'];
  1.1196 +
  1.1197 +		$template->assign_block_vars('recentsearch', array(
  1.1198 +			'KEYWORDS'	=> $keywords,
  1.1199 +			'TIME'		=> $user->format_date($row['search_time']),
  1.1200 +
  1.1201 +			'U_KEYWORDS'	=> append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
  1.1202 +		));
  1.1203 +	}
  1.1204 +	$db->sql_freeresult($result);
  1.1205 +}
  1.1206 +
  1.1207 +// Output the basic page
  1.1208 +page_header($user->lang['SEARCH']);
  1.1209 +
  1.1210 +$template->set_filenames(array(
  1.1211 +	'body' => 'search_body.html')
  1.1212 +);
  1.1213 +make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
  1.1214 +
  1.1215 +page_footer();
  1.1216 +
  1.1217 +?>
  1.1218 \ No newline at end of file