phpBB

Code Changes

File: search.php

  Unmodified   Added   Modified   Removed
Line 46Line 46

$return_chars = request_var('ch', ($topic_id) ? -1 : 300);
$search_forum = request_var('fid', array(0));


$return_chars = request_var('ch', ($topic_id) ? -1 : 300);
$search_forum = request_var('fid', array(0));

 

// We put login boxes for the case if search_id is newposts, egosearch or unreadposts
// because a guest should be able to log in even if guests search is not permitted

switch ($search_id)
{
// Egosearch is an author search
case 'egosearch':
$author_id = $user->data['user_id'];
if ($user->data['user_id'] == ANONYMOUS)
{
login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
}
break;

// Search for unread posts needs to be allowed and user to be logged in if topics tracking for guests is disabled
case 'unreadposts':
if (!$config['load_unreads_search'])
{
$template->assign_var('S_NO_SEARCH', true);
trigger_error('NO_SEARCH_UNREADS');
}
else if (!$config['load_anon_lastread'] && !$user->data['is_registered'])
{
login_box('', $user->lang['LOGIN_EXPLAIN_UNREADSEARCH']);
}
break;

// The "new posts" search uses user_lastvisit which is user based, so it should require user to log in.
case 'newposts':
if ($user->data['user_id'] == ANONYMOUS)
{
login_box('', $user->lang['LOGIN_EXPLAIN_NEWPOSTS']);
}
break;

default:
// There's nothing to do here for now ;)
break;
}


// Is user able to search? Has search been disabled?
if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])


// Is user able to search? Has search been disabled?
if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])

Line 61Line 101
	trigger_error('NO_SEARCH_TIME');
}


	trigger_error('NO_SEARCH_TIME');
}


// Check flood limit ... if applicable


// It is applicable if the configuration setting is non-zero, and the user cannot
// ignore the flood setting, and the search is a keyword search.

$interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];

$interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];

if ($interval && !$auth->acl_get('u_ignoreflood'))

if ($interval && !in_array($search_id, array('unreadposts', 'unanswered', 'active_topics', 'egosearch')) && !$auth->acl_get('u_ignoreflood'))

{
if ($user->data['user_last_search'] > time() - $interval)
{

{
if ($user->data['user_last_search'] > time() - $interval)
{

Line 83Line 124
{
// clear arrays
$id_ary = array();

{
// clear arrays
$id_ary = array();


// egosearch is an author search
if ($search_id == 'egosearch')
{
$author_id = $user->data['user_id'];

if ($user->data['user_id'] == ANONYMOUS)
{
login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
}
}

 

// If we are looking for authors get their ids
$author_id_ary = array();


// If we are looking for authors get their ids
$author_id_ary = array();

 
	$sql_author_match = '';

	if ($author_id)
{
$author_id_ary[] = $author_id;

	if ($author_id)
{
$author_id_ary[] = $author_id;

Line 113Line 144
		$sql = 'SELECT user_id
FROM ' . USERS_TABLE . "
WHERE $sql_where

		$sql = 'SELECT user_id
FROM ' . USERS_TABLE . "
WHERE $sql_where

				AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';

				AND user_type <> " . USER_IGNORE;

		$result = $db->sql_query_limit($sql, 100);

while ($row = $db->sql_fetchrow($result))

		$result = $db->sql_query_limit($sql, 100);

while ($row = $db->sql_fetchrow($result))

Line 121Line 152
			$author_id_ary[] = (int) $row['user_id'];
}
$db->sql_freeresult($result);

			$author_id_ary[] = (int) $row['user_id'];
}
$db->sql_freeresult($result);

 

$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)) . "'";

$sql = 'SELECT 1 as guest_post
FROM ' . POSTS_TABLE . "
WHERE $sql_where
AND poster_id = " . ANONYMOUS;
$result = $db->sql_query_limit($sql, 1);
$found_guest_post = $db->sql_fetchfield('guest_post');
$db->sql_freeresult($result);

if ($found_guest_post)
{
$author_id_ary[] = ANONYMOUS;
$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)) . "'";
}


if (!sizeof($author_id_ary))
{


if (!sizeof($author_id_ary))
{

Line 155Line 202

$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'] . ')' : "";



$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'] . ')' : "";


	$sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id

	$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

		FROM ' . FORUMS_TABLE . ' f
LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')

		FROM ' . FORUMS_TABLE . ' f
LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')

Line 168Line 215
	while ($row = $db->sql_fetchrow($result))
{
if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])

	while ($row = $db->sql_fetchrow($result))
{
if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])

 
		{
$ex_fid_ary[] = (int) $row['forum_id'];
continue;
}

// Exclude forums from active topics
if (!($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && ($search_id == 'active_topics'))

		{
$ex_fid_ary[] = (int) $row['forum_id'];
continue;

		{
$ex_fid_ary[] = (int) $row['forum_id'];
continue;

Line 307Line 361
				{
$last_post_time = '';
}

				{
$last_post_time = '';
}



 

if ($sort_key == 'a')
{


if ($sort_key == 'a')
{

Line 339Line 392
						$sql_sort";
$field = 'topic_id';
}

						$sql_sort";
$field = 'topic_id';
}

 
			break;

case 'unreadposts':
$l_search_title = $user->lang['SEARCH_UNREAD'];
// force sorting
$show_results = 'topics';
$sort_key = 't';
$sort_by_sql['t'] = 't.topic_last_post_time';
$sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');

$sql_where = 'AND t.topic_moved_id = 0
' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '');

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);
$s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';

			break;

case 'newposts':

			break;

case 'newposts':

Line 372Line 441
							' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
$sql_sort";

							' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
$sql_sort";

 
/*
[Fix] queued replies missing from "view new posts" (Bug #42705 - Patch by Paul)
- Creates temporary table, query is far from optimized

$sql = 'SELECT t.topic_id
FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
WHERE p.post_time > ' . $user->data['user_lastvisit'] . '
AND t.topic_id = p.topic_id
AND t.topic_moved_id = 0
' . $m_approve_fid_sql . '
' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
GROUP BY t.topic_id
$sql_sort";
*/

					$field = 'topic_id';
}
break;

					$field = 'topic_id';
}
break;

Line 385Line 468
	// show_results should not change after this
$per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
$total_match_count = 0;

	// show_results should not change after this
$per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
$total_match_count = 0;

 

// Set limit for the $total_match_count to reduce server load
$total_matches_limit = 1000;
$found_more_search_matches = false;


if ($search_id)
{
if ($sql)
{


if ($search_id)
{
if ($sql)
{

			// only return up to 1000 ids (the last one will be removed later)
$result = $db->sql_query_limit($sql, 1001 - $start, $start);

			// Only return up to $total_matches_limit+1 ids (the last one will be removed later)
$result = $db->sql_query_limit($sql, $total_matches_limit + 1);


while ($row = $db->sql_fetchrow($result))
{


while ($row = $db->sql_fetchrow($result))
{

				$id_ary[] = $row[$field];

				$id_ary[] = (int) $row[$field];

			}
$db->sql_freeresult($result);

			}
$db->sql_freeresult($result);


$total_match_count = sizeof($id_ary) + $start;
$id_ary = array_slice($id_ary, 0, $per_page);



		}
else if ($search_id == 'unreadposts')
{
// Only return up to $total_matches_limit+1 ids (the last one will be removed later)
$id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, $total_matches_limit + 1));

		}
else
{
$search_id = '';

		}
else
{
$search_id = '';

 
		}

$total_match_count = sizeof($id_ary);
if ($total_match_count)
{
// Limit the number to $total_matches_limit for pre-made searches
if ($total_match_count > $total_matches_limit)
{
$found_more_search_matches = true;
$total_match_count = $total_matches_limit;
}

// Make sure $start is set to the last page if it exceeds the amount
if ($start < 0)
{
$start = 0;
}
else if ($start >= $total_match_count)
{
$start = floor(($total_match_count - 1) / $per_page) * $per_page;
}

$id_ary = array_slice($id_ary, $start, $per_page);
}
else
{
// Set $start to 0 if no matches were found
$start = 0;

		}
}


		}
}


Line 415Line 532

if (!empty($search->search_query))
{


if (!empty($search->search_query))
{

		$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, $id_ary, $start, $per_page);

		$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);

	}
else if (sizeof($author_id_ary))
{
$firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;

	}
else if (sizeof($author_id_ary))
{
$firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;

		$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, $id_ary, $start, $per_page);
}

// For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
if (!sizeof($id_ary) && !$search_id)
{
trigger_error('NO_SEARCH_RESULTS');

		$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);







	}

$sql_where = '';

	}

$sql_where = '';

Line 453Line 564
	$icons = $cache->obtain_icons();

// Output header

	$icons = $cache->obtain_icons();

// Output header

	if ($search_id && ($total_match_count > 1000))

	if ($found_more_search_matches)

	{

	{

		// limit the number to 1000 for pre-made searches
$total_match_count--;

 
		$l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
}
else

		$l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
}
else

Line 465Line 574
	}

// define some vars for urls

	}

// define some vars for urls

	$hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
// Do not allow *only* wildcard being used for hilight
$hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit;

	// A single wildcard will make the search results look ugly
$hilit = phpbb_clean_search_string(str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords));
$hilit = str_replace(' ', '|', $hilit);


$u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));


$u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));

	$u_show_results = ($show_results != 'posts') ? '&amp;sr=' . $show_results : '';

	$u_show_results = '&amp;sr=' . $show_results;

	$u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);

$u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
$u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';

	$u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);

$u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
$u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';

	$u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($search->search_query)) : '';

	$u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';

	$u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
$u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
$u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';

	$u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
$u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
$u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';

Line 488Line 597
	$template->assign_vars(array(
'SEARCH_TITLE' => $l_search_title,
'SEARCH_MATCHES' => $l_search_matches,

	$template->assign_vars(array(
'SEARCH_TITLE' => $l_search_title,
'SEARCH_MATCHES' => $l_search_matches,

		'SEARCH_WORDS'		=> $search->search_query,


		'SEARCH_WORDS'		=> $keywords,
'SEARCHED_QUERY' => $search->search_query,

		'IGNORED_WORDS'		=> (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),

		'IGNORED_WORDS'		=> (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),

Line 581Line 691
			$forums = $rowset = $shadow_topic_list = array();
while ($row = $db->sql_fetchrow($result))
{

			$forums = $rowset = $shadow_topic_list = array();
while ($row = $db->sql_fetchrow($result))
{

 
				$row['forum_id'] = (int) $row['forum_id'];
$row['topic_id'] = (int) $row['topic_id'];


				if ($row['topic_status'] == ITEM_MOVED)
{
$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];

				if ($row['topic_status'] == ITEM_MOVED)
{
$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];

Line 727Line 840
			$hilit_array = array_filter(explode('|', $hilit), 'strlen');
foreach ($hilit_array as $key => $value)
{

			$hilit_array = array_filter(explode('|', $hilit), 'strlen');
foreach ($hilit_array as $key => $value)
{

				$hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));


				$hilit_array[$key] = phpbb_clean_search_string($value);
$hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($hilit_array[$key], '#'));

				$hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
}
$hilit = implode('|', $hilit_array);

				$hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
}
$hilit = implode('|', $hilit_array);

Line 767Line 881
				$u_forum_id = $forum_id;
}


				$u_forum_id = $forum_id;
}


			$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : ''));


			$view_topic_url_params = "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : '');
$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);


$replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];



$replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];


Line 818Line 933

'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
'S_TOPIC_TYPE' => $row['topic_type'],


'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
'S_TOPIC_TYPE' => $row['topic_type'],

					'S_USER_POSTED'			=> (!empty($row['mark_type'])) ? true : false,

					'S_USER_POSTED'			=> (!empty($row['topic_posted'])) ? true : false,

					'S_UNREAD_TOPIC'		=> $unread_topic,

'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
'S_TOPIC_UNAPPROVED' => $topic_unapproved,
'S_POSTS_UNAPPROVED' => $posts_unapproved,


					'S_UNREAD_TOPIC'		=> $unread_topic,

'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
'S_TOPIC_UNAPPROVED' => $topic_unapproved,
'S_POSTS_UNAPPROVED' => $posts_unapproved,


					'U_LAST_POST'			=> $view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id'],

					'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'],

					'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),

					'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),

					'U_NEWEST_POST'			=> $view_topic_url . '&amp;view=unread#unread',

					'U_NEWEST_POST'			=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',

					'U_MCP_REPORT'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
'U_MCP_QUEUE' => $u_mcp_queue,
);

					'U_MCP_REPORT'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
'U_MCP_QUEUE' => $u_mcp_queue,
);

Line 929Line 1044

page_footer();
}


page_footer();
}



 

// Search forum
$s_forums = '';


// Search forum
$s_forums = '';

Line 1078Line 1192

case 'mssql':
case 'mssql_odbc':


case 'mssql':
case 'mssql_odbc':

 
		case 'mssqlnative':

			$sql = 'SELECT search_time, search_keywords
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE DATALENGTH(search_keywords) > 0

			$sql = 'SELECT search_time, search_keywords
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE DATALENGTH(search_keywords) > 0