0) ? $userdata['user_topics_per_page'] : '25';
$user_posts_per_page = ($userdata['user_posts_per_page'] > 0) ? $userdata['user_posts_per_page'] : '15';
$no_password_forum = ($userdata['user_level'] != ADMIN) ? "AND f.password = ''" : '';
$page_title = $lang['Search'];
if ( $board_config['login_require'] && !$userdata['session_logged_in'] )
{
$message = $lang['login_require'] . '
' . sprintf($lang['login_require_register'], '', '');
message_die(GENERAL_MESSAGE, $message);
}
//
// Define initial vars
//
if ( isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']) )
{
$mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
}
else
{
$mode = '';
}
$per_page = $from_sql = $where_sql = '';
if ( isset($HTTP_POST_VARS['search_keywords']) || isset($HTTP_GET_VARS['search_keywords']) )
{
$search_keywords = ( isset($HTTP_POST_VARS['search_keywords']) ) ? $HTTP_POST_VARS['search_keywords'] : $HTTP_GET_VARS['search_keywords'];
}
else
{
$search_keywords = '';
}
if ( isset($HTTP_POST_VARS['search_author']) || isset($HTTP_GET_VARS['search_author']))
{
$search_author = ( isset($HTTP_POST_VARS['search_author']) ) ? $HTTP_POST_VARS['search_author'] : $HTTP_GET_VARS['search_author'];
$search_author = phpbb_clean_username($search_author);
$search_author = str_replace(array('%', '_'), array('\%', '\_'), $search_author);
}
else
{
$search_author = '';
}
$search_id = ( isset($HTTP_GET_VARS['search_id']) ) ? $HTTP_GET_VARS['search_id'] : '';
$show_results = ( isset($HTTP_POST_VARS['show_results']) ) ? $HTTP_POST_VARS['show_results'] : 'posts';
$show_results = ($show_results == 'topics') ? 'topics' : 'posts';
if ( isset($HTTP_POST_VARS['search_terms']) )
{
$search_terms = ( $HTTP_POST_VARS['search_terms'] == 'all' ) ? 1 : 0;
}
else
{
$search_terms = 0;
}
$search_fields = $HTTP_POST_VARS['search_fields'];
$return_chars = (isset($HTTP_POST_VARS['return_chars'])) ? intval($HTTP_POST_VARS['return_chars']) : -1;
$search_where = (isset($HTTP_POST_VARS['search_where'])) ? $HTTP_POST_VARS['search_where'] : 'Root';
$sort_by = (isset($HTTP_POST_VARS['sort_by'])) ? intval($HTTP_POST_VARS['sort_by']) : 0;
if ( isset($HTTP_POST_VARS['sort_dir']) )
{
$sort_dir = ($HTTP_POST_VARS['sort_dir'] == 'DESC') ? 'DESC' : 'ASC';
}
else
{
$sort_dir = 'DESC';
}
if ( !empty($HTTP_POST_VARS['search_time']) || !empty($HTTP_GET_VARS['search_time']))
{
$search_time = CR_TIME - ( ( ( !empty($HTTP_POST_VARS['search_time']) ) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']) ) * 60 );
$topic_days = (!empty($HTTP_POST_VARS['search_time'])) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']);
}
else
{
$search_time = 0;
$topic_days = 0;
}
$start = (isset($HTTP_GET_VARS['start'])) ? intval($HTTP_GET_VARS['start']) : 0;
if ( isset($HTTP_POST_VARS['start']) )
{
$start = intval($HTTP_POST_VARS['start']);
}
$sort_by_types = array($lang['Sort_Time'], $lang['Topic_important'], $lang['Post_subject'], $lang['Sort_Topic_Title'], $lang['Author'], $lang['Forum']);
// encoding match for workaround
//$multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
// Begin core code
if ( $mode == 'searchuser' )
{
// This handles the simple windowed user search functions called from various other scripts
if ( isset($HTTP_POST_VARS['search_username']) )
{
username_search($HTTP_POST_VARS['search_username']);
}
else
{
username_search('');
}
exit;
}
else if ( $search_keywords != '' || $search_author != '' || $search_id )
{
$store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'sort_dir', 'show_results', 'return_chars');
$search_results = '';
//
// Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
$limiter = 5000;
$unread_topics_ids = array();
if ( $userdata['user_id'] != ANONYMOUS )
{
include($phpbb_root_path . 'includes/read_history.'.$phpEx);
$userdata = user_unread_posts();
foreach($userdata['unread_data'] as $forum_id => $topics)
{
$unread_topics_ids = array_merge($unread_topics_ids, array_keys($topics));
}
}
// Cycle through options ...
if ( $search_id == 'newposts' || $search_id == 'lastvisit' || $search_id == 'egosearch' || $search_id == 'unanswered' || $search_keywords != '' || $search_author != '' )
{
if ( $search_id == 'newposts' || $search_id == 'lastvisit' || $search_id == 'egosearch' || ( $search_author != '' && $search_keywords == '' ) )
{
if ( ($search_id == 'newposts' || $search_id == 'lastvisit') && $userdata['user_id'] != ANONYMOUS )
{
if ( $userdata['user_id'] != ANONYMOUS )
{
if ( $search_id == 'newposts' )
{
$sql = "SELECT post_id
FROM " . POSTS_TABLE . "
WHERE post_time >= " . $userdata['user_lastvisit'] . "
AND poster_id <> " . $userdata['user_id'];
}
else
{
$sql = "SELECT post_id
FROM " . READ_HIST_TABLE . "
WHERE user_id = " . $userdata['user_id'];
}
$show_results = 'topics';
$sort_by = 0;
$sort_dir = 'DESC';
}
else
{
redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=newposts", true));
}
$show_results = 'topics';
$sort_by = 0;
$sort_dir = 'DESC';
}
else if ( $search_id == 'egosearch' )
{
if ( $userdata['user_id'] != ANONYMOUS )
{
$sql = "SELECT post_id
FROM " . POSTS_TABLE . "
WHERE poster_id = " . $userdata['user_id'];
}
else
{
redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=egosearch", true));
}
$show_results = 'topics';
$sort_by = 0;
$sort_dir = 'DESC';
}
else
{
if ( $search_author != '*' && (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1}$#', str_replace(array('*', '%'), '', trim($search_author)))) )
{
$search_author = '';
}
$search_author = str_replace('*', '%', trim($search_author));
$sql = "SELECT user_id
FROM " . USERS_TABLE . "
WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Couldn\'t obtain list of matching users, searching for: ' . $search_author, '', __LINE__, __FILE__, $sql);
}
$matching_userids = '';
if ( $row = $db->sql_fetchrow($result) )
{
do
{
$matching_userids .= (($matching_userids != '') ? ', ' : '') . $row['user_id'];
}
while( $row = $db->sql_fetchrow($result) );
}
else
{
message_die(GENERAL_MESSAGE, $lang['No_search_match']);
}
if ( $HTTP_GET_VARS['gh'] == 'helped' )
{
$sql = "SELECT post_id
FROM " . POSTS_TABLE . "
WHERE poster_id IN ($matching_userids)
AND post_marked = 'y'";
}
else
{
$sql = "SELECT post_id
FROM " . POSTS_TABLE . "
WHERE poster_id IN ($matching_userids)";
}
if ($search_time)
{
$sql .= " AND post_time >= " . $search_time;
}
}
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
}
$search_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
$search_ids[] = $row['post_id'];
}
$db->sql_freeresult($result);
$total_match_count = count($search_ids);
}
else if ( $search_keywords != '' )
{
$stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt');
$synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt');
$split_search = array();
$stripped_keywords = stripslashes($search_keywords);
// $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ? split_words(clean_words('search', $stripped_keywords, $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords);
$split_search = split_words(clean_words('search', $stripped_keywords, $stopword_array, $synonym_array), 'search');
unset($stripped_keywords);
$word_count = 0;
$current_match_type = 'or';
$word_match = array();
$result_list = array();
for($i = 0; $i < count($split_search); $i++)
{
if (preg_match('#^[\*%]+$#', trim($split_search[$i])) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($split_search[$i]))))
{
$split_search[$i] = '';
continue;
}
switch ( $split_search[$i] )
{
case 'and':
$current_match_type = 'and';
break;
case 'or':
$current_match_type = 'or';
break;
case 'not':
$current_match_type = 'not';
break;
default:
if ( !empty($search_terms) )
{
$current_match_type = 'and';
}
// Searching by phrase but from posts text table it can load server
$search_phrase = str_replace('\\', '', $search_keywords);
if ( substr($search_phrase, -1) == '"' && substr($search_phrase, 0, 1) == '"' )
{
$phrase = stripslashes($search_keywords);
switch ( $search_fields )
{
case 'msgonly':
$search_match = "WHERE p.post_text LIKE '%" . str_replace('"', '', str_replace("\'", "''", $phrase)) . "%'";
break;
case 'titleonly':
$search_match = "WHERE p.post_subject LIKE '%" . str_replace('"', '', str_replace("\'", "''", $phrase)) . "%'";
break;
case 'title_e_only':
$search_match = ", " . TOPICS_TABLE . " t WHERE t.topic_title_e LIKE '%" . str_replace('"', '', str_replace("\'", "''", $phrase)) . "%' AND t.topic_first_post_id = p.post_id";
break;
default: // Disable search in post_subject and topic explain to no kill the MySQL server
$search_match = "WHERE p.post_text LIKE '%" . str_replace('"', '', str_replace("\'", "''", $phrase)) . "%'";
break;
}
$sql = "SELECT p.post_id
FROM " . POSTS_TEXT_TABLE . " p
$search_match";
}
// else if ( !strstr($multibyte_charset, $lang['ENCODING']) )
else
{
$match_word = str_replace('*', '%', $split_search[$i]);
switch ( $search_fields )
{
case 'msgonly':
$search_match = "AND m.title_match = 0";
break;
case 'titleonly':
$search_match = "AND m.title_match = 1";
break;
case 'title_e_only':
$search_match = "AND m.title_match = 2";
break;
default:
$search_match = "";
break;
}
$sql = "SELECT m.post_id
FROM (" . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m)
WHERE w.word_text LIKE '$match_word'
AND m.word_id = w.word_id
AND w.word_common <> 1
$search_match";
}
/*
else
{
$match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
switch ( $search_fields )
{
case 'msgonly':
$search_match = "post_text LIKE '$match_word'";
break;
case 'titleonly':
$search_match = "post_subject LIKE '$match_word'";
break;
// case 'title_e_only':
// $search_match = "AND m.title_match = 2";
// break;
default:
$search_match = "post_text LIKE '$match_word' OR post_subject LIKE '$match_word'";
break;
}
$sql = "SELECT post_id
FROM " . POSTS_TEXT_TABLE . "
WHERE " . $search_match;
}
*/
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_MESSAGE, $lang['No_search_match']);
}
$row = array();
while( $temp_row = $db->sql_fetchrow($result) )
{
$row[$temp_row['post_id']] = 1;
if ( !$word_count )
{
$result_list[$temp_row['post_id']] = 1;
}
else if ( $current_match_type == 'or' )
{
$result_list[$temp_row['post_id']] = 1;
}
else if ( $current_match_type == 'not' )
{
$result_list[$temp_row['post_id']] = 0;
}
}
if ( $current_match_type == 'and' && $word_count )
{
@reset($result_list);
while( list($post_id, $match_count) = @each($result_list) )
{
if ( !$row[$post_id] )
{
$result_list[$post_id] = 0;
}
}
}
$word_count++;
$db->sql_freeresult($result);
}
}
@reset($result_list);
$search_ids = array();
while( list($post_id, $matches) = each($result_list) )
{
if ( $matches )
{
$search_ids[] = $post_id;
}
}
unset($result_list);
$total_match_count = count($search_ids);
}
// If user is logged in then we'll check to see which (if any) private
// forums they are allowed to view and include them in the search.
//
// If not logged in we explicitly prevent searching of private forums
$auth_sql = '';
// get the object list
$keys = array();
$keys = get_auth_keys($search_where, true, -1, -1, 'auth_read');
$s_flist = '';
for ($i = 0; $i < count($keys['id']); $i++)
{
if ( ($tree['type'][ $keys['idx'][$i] ] == POST_FORUM_URL) && $tree['auth'][ $keys['id'][$i] ]['auth_read'] )
{
$s_flist .= (($s_flist != '') ? ', ' : '') . $tree['id'][ $keys['idx'][$i] ];
}
}
/* Read topics bug which allow to preview private forums with allow to view only when choose only this forum!
if ($s_flist != '')
{
$auth_sql .= (( $auth_sql != '' ) ? " AND" : '') . " f.forum_id IN ($s_flist) ";
}
*/
// Replacing:
if ($s_flist == '')
{
$s_flist = '0';
}
$auth_sql .= (( $auth_sql != '' ) ? " AND" : '') . " f.forum_id IN ($s_flist) ";
// End replacing
// Author name search
if ( $search_author != '' )
{
if ( $search_author != '*' && (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1}$#', str_replace(array('*', '%'), '', trim($search_author)))) )
{
$search_author = '';
}
$search_author = str_replace('*', '%', trim(str_replace("\'", "''", $search_author)));
}
if ( $total_match_count )
{
if ( $show_results == 'topics' )
{
$where_sql = '';
if ( $search_time )
{
$where_sql .= ($search_author == '' && $auth_sql == '') ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
}
if ( $search_author == '' && $auth_sql == '' )
{
$sql = "SELECT topic_id
FROM " . POSTS_TABLE . "
WHERE post_id IN (" . implode(", ", $search_ids) . ")
$where_sql
GROUP BY topic_id";
}
else
{
$from_sql = POSTS_TABLE . " p";
if ( $search_author != '' )
{
$from_sql .= ", " . USERS_TABLE . " u";
$where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
}
if ( $auth_sql != '' )
{
$from_sql .= ", " . FORUMS_TABLE . " f";
$where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql $no_password_forum";
}
$sql = "SELECT p.topic_id
FROM ($from_sql)
WHERE p.post_id IN (" . implode(", ", $search_ids) . ")
$where_sql
GROUP BY p.topic_id";
}
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
}
$search_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
$search_ids[] = $row['topic_id'];
}
$db->sql_freeresult($result);
$total_match_count = sizeof($search_ids);
}
else if ( $search_author != '' || $search_time || $auth_sql != '' )
{
$where_sql = ($search_author == '' && $auth_sql == '') ? 'post_id IN (' . implode(', ', $search_ids) . ')' : 'p.post_id IN (' . implode(', ', $search_ids) . ')';
$from_sql = ($search_author == '' && $auth_sql == '') ? POSTS_TABLE : POSTS_TABLE . ' p';
if ( $search_time )
{
$where_sql .= ($search_author == '' && $auth_sql == '') ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
}
if ( $auth_sql != '' )
{
$from_sql .= ", " . FORUMS_TABLE . " f";
$where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql $no_password_forum";
}
if ( $search_author != '' )
{
$from_sql .= ", " . USERS_TABLE . " u";
$where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
}
$sql = "SELECT p.post_id
FROM ($from_sql)
WHERE $where_sql";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
}
$search_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
$search_ids[] = $row['post_id'];
}
$db->sql_freeresult($result);
$total_match_count = count($search_ids);
}
}
else if ( $search_id == 'unanswered' )
{
if ( $auth_sql != '' )
{
$sql = "SELECT t.topic_id, f.forum_id
FROM (" . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f)
WHERE t.topic_replies = 0
AND t.forum_id = f.forum_id
AND t.topic_moved_id = 0
AND $auth_sql $no_password_forum";
}
else
{
$sql = "SELECT topic_id
FROM " . TOPICS_TABLE . "
WHERE topic_replies = 0
AND topic_moved_id = 0";
}
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
}
$search_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
$search_ids[] = $row['topic_id'];
}
$db->sql_freeresult($result);
$total_match_count = count($search_ids);
// Basic requirements
$show_results = 'topics';
$sort_by = 0;
$sort_dir = 'DESC';
}
else
{
message_die(GENERAL_MESSAGE, $lang['No_search_match']);
}
// Finish building query (for all combinations)
// and run it ...
$sql = "SELECT session_id
FROM " . SESSIONS_TABLE;
if ( $result = $db->sql_query($sql) )
{
$delete_search_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
$delete_search_ids[] = "'" . $row['session_id'] . "'";
}
if ( count($delete_search_ids) )
{
$sql = "DELETE FROM " . SEARCH_TABLE . "
WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
if ( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
}
}
}
// Store new result data
$search_results = implode(', ', $search_ids);
$per_page = ( $show_results == 'posts' ) ? $user_posts_per_page : $user_topics_per_page;
// Combine both results and search data (apart from original query)
// so we can serialize it and place it in the DB
$store_search_data = array();
for($i = 0; $i < count($store_vars); $i++)
{
$store_search_data[$store_vars[$i]] = $$store_vars[$i];
}
$result_array = serialize($store_search_data);
unset($store_search_data);
mt_srand ((double) microtime() * 1000000);
$search_id = mt_rand();
$sql = "UPDATE " . SEARCH_TABLE . "
SET search_id = $search_id, search_array = '" . str_replace("\'", "''", $result_array) . "'
WHERE session_id = '" . $userdata['session_id'] . "'";
if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
{
$sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array)
VALUES($search_id, '" . $userdata['session_id'] . "', '" . str_replace("\'", "''", $result_array) . "')";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
}
}
}
else
{
$search_id = intval($search_id);
if ( $search_id )
{
$sql_sid = ($HTTP_GET_VARS['sid'] && $HTTP_GET_VARS['sid'] != $userdata['session_id']) ? " OR session_id = '" . str_replace("\'", "''", $HTTP_GET_VARS['sid']) . "'" : '';
$sql = "SELECT search_array
FROM " . SEARCH_TABLE . "
WHERE search_id = $search_id
AND (session_id = '". $userdata['session_id'] . "' $sql_sid)";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
$search_data = unserialize($row['search_array']);
for($i = 0; $i < count($store_vars); $i++)
{
$$store_vars[$i] = $search_data[$store_vars[$i]];
}
}
}
}
// Look up data ...
if ( $search_results != '' )
{
$colspan = '1';
$colspan2 = '7';
if ( $board_config['ignore_topics'] && $userdata['session_logged_in'] && isset($HTTP_GET_VARS['search_id']) == 'newposts' && $userdata['view_ignore_topics'] )
{
$template->assign_block_vars('ignore_topics', array(
'U_IGNORE_TOPICS' => append_sid("ignore_topics.$phpEx"),
'L_IGNORE_MARK' => $lang['ignore_mark'],
'L_MARK_ALL' => $lang['Mark_all'])
);
$colspan++; $colspan2++;
}
if ( $show_results == 'posts' )
{
$sql = "SELECT pt.post_text, pt.bbcode_uid, pt.post_subject, p.*, f.forum_id, f.forum_name, f.forum_color, f.forum_moderate, t.*, u.username, u.user_id, u.user_sig, u.user_sig_bbcode_uid, u.user_level, u.user_jr, u.user_allowhtml
FROM (" . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt)
WHERE p.post_id IN ($search_results)
AND pt.post_id = p.post_id
AND f.forum_id = p.forum_id
AND p.topic_id = t.topic_id
AND p.poster_id = u.user_id
$no_password_forum";
}
else
{
if ( $board_config['post_overlib'] && $board_config['overlib'] && $userdata['overlib'] )
{
$post_text_select = ", pt.post_text, pt2.post_text as last_post_text";
$post_text_where = "AND pt.post_id = t.topic_first_post_id AND pt2.post_id = t.topic_last_post_id";
$posts_tables = ", " . POSTS_TEXT_TABLE . " pt, " . POSTS_TEXT_TABLE . " pt2";
}
else
{
$post_text_select = $post_text_where = $posts_tables = '';
}
$sql = "SELECT t.* $post_text_select, f.forum_id, f.forum_name, f.forum_color, f.forum_moderate, u.username, u.user_id, u.user_level, u.user_jr, u2.username as user2, u2.user_id as id2, u2.user_level as user_level2, u2.user_jr as user_jr2, p.post_username, p2.post_username AS post_username2, p2.post_time, ph.post_id as post_helped, p.post_approve, p2.post_approve as post_approve2
FROM (" . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2 $posts_tables)
LEFT JOIN " . POSTS_TABLE . " ph ON (t.topic_id = ph.topic_id AND ph.post_marked = 'y')
WHERE t.topic_id IN ($search_results)
AND t.topic_poster = u.user_id
AND f.forum_id = t.forum_id
AND p.post_id = t.topic_first_post_id
AND p2.post_id = t.topic_last_post_id
AND u2.user_id = p2.poster_id
$post_text_where
$no_password_forum
GROUP by t.topic_id";
}
$per_page = ($show_results == 'posts') ? $user_posts_per_page : $user_topics_per_page;
$sql .= " ORDER BY ";
switch ( $sort_by )
{
case 1:
if ( $board_config['topic_color'] && !$board_config['topic_color_all'] )
{
$sql .= "t.topic_color $sort_dir, ";
}
if ( $show_results == 'posts' )
{
$sql .= "p.post_marked $sort_dir, ";
}
$sql .= "topic_type $sort_dir, u.special_rank $sort_dir, u.user_level $sort_dir, u.user_posts $sort_dir, topic_views $sort_dir, topic_replies";
break;
case 2:
$sql .= ($show_results == 'posts') ? 'pt.post_subject' : 't.topic_title';
break;
case 3:
$sql .= 't.topic_title';
break;
case 4:
$sql .= 'u.username';
break;
case 5:
$sql .= 'f.forum_id';
break;
case 6:
break;
default:
$sql .= ($show_results == 'posts') ? 'p.post_time' : 'p2.post_time';
break;
}
$sql .= " $sort_dir LIMIT $start, " . $per_page;
if ( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
}
$searchset = array();
while( $row = $db->sql_fetchrow($result) )
{
$searchset[] = $row;
}
$db->sql_freeresult($result);
// Define censored word matches
$orig_word = array();
$replacement_word = array();
$replacement_word_html = array();
obtain_word_list($orig_word, $replacement_word, $replacement_word_html);
$count_orig_word = count($orig_word);
// Output header
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
if ( $show_results == 'posts' )
{
$template->set_filenames(array(
'body' => 'search_results_posts.tpl')
);
}
else
{
$template->set_filenames(array(
'body' => 'search_results_topics.tpl')
);
}
make_jumpbox('viewforum.'.$phpEx);
$l_search_matches = ($total_match_count == 1) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
$template->assign_vars(array(
'L_SEARCH_MATCHES' => $l_search_matches,
'L_TOPIC' => $lang['Topic'])
);
$highlight_active = '';
$highlight_match = array();
for($j = 0; $j < count($split_search); $j++ )
{
$split_word = $split_search[$j];
if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
{
$highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
$highlight_active .= " " . $split_word;
for ($k = 0; $k < count($synonym_array); $k++)
{
list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k])));
if ( $replace_synonym == $split_word )
{
$highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
$highlight_active .= ' ' . $match_synonym;
}
}
}
}
$highlight_active = urlencode(trim($highlight_active));
$take_part_topics = array();
if ( $userdata['session_logged_in'] )
{
$sql = "SELECT topic_id FROM " . POSTS_TABLE . "
WHERE poster_id = " . $userdata['user_id'] . "
GROUP BY topic_id";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Couldn\'t obtain list of unread topics', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
do
{
$take_part_topics[] = $row['topic_id'];
}
while( $row = $db->sql_fetchrow($result) );
}
}
if( $userdata['session_logged_in'] && $board_config['overlib'] )
{
$gp_ids = array();
foreach($searchset as $key => $val)
{
$gp_ids[] = $val['topic_id'];
}
get_poster_topic_posts($gp_ids, $userdata['user_id']);
}
for($i = 0; $i < count($searchset); $i++)
{
$forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
$set_highlight = ($highlight_active != '') ? '&highlight=' . $highlight_active : '';
$topic_url = (@in_array($searchset[$i]['topic_id'], $unread_topics_ids)) ? append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "$set_highlight&view=newest") : append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "$set_highlight");
$topic_urls[] = $topic_url;
$post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
$post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
$forum_id = $searchset[$i]['forum_id'];
$topic_id = $searchset[$i]['topic_id'];
$message = $searchset[$i]['post_text'];
$topic_title = $searchset[$i]['topic_title'];
$topic_title_e = $searchset[$i]['topic_title_e'];
$forum_moderate = ($searchset[$i]['forum_moderate']) ? true : false;
$forum_view_moderate = ($forum_moderate && !$is_auth['auth_mod']) ? true : false;
if ( $forum_moderate )
{
$is_auth = array();
$is_auth = $tree['auth'][POST_FORUM_URL . $forum_id];
}
if ( $show_results == 'posts' )
{
if ( isset($return_chars) )
{
$bbcode_uid = $searchset[$i]['bbcode_uid'];
if ( !$forum_moderate )
{
$is_auth = array();
$is_auth = $tree['auth'][POST_FORUM_URL . $forum_id];
}
$show_post_html = ($board_config['allow_html'] && $searchset[$i]['user_allowhtml']) ? true : false;
if ( (($searchset[$i]['user_level'] == MOD && $board_config['mod_html']) || ($board_config['admin_html'] && $searchset[$i]['user_level'] == ADMIN) || ($board_config['jr_admin_html'] && $searchset[$i]['user_jr'])) && $searchset[$i]['user_allowhtml'] )
{
$show_post_html = true;
}
$strip_br = ($show_post_html && (strpos($message, '