2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: search.php,v 1.72.2.17 2005/09/14 18:14:30 acydburn Exp $
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 ***************************************************************************/
23 define('IN_PHPBB', true);
24 $phpbb_root_path = './';
25 include($phpbb_root_path . 'extension.inc');
26 include($phpbb_root_path . 'common.'.$phpEx);
27 include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
28 include($phpbb_root_path . 'includes/functions_search.'.$phpEx);
31 // Start session management
33 $userdata = session_pagestart($user_ip, PAGE_SEARCH);
34 init_userprefs($userdata);
36 // End session management
40 // Define initial vars
42 if ( isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']) )
44 $mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
51 if ( isset($HTTP_POST_VARS['search_keywords']) || isset($HTTP_GET_VARS['search_keywords']) )
53 $search_keywords = ( isset($HTTP_POST_VARS['search_keywords']) ) ? $HTTP_POST_VARS['search_keywords'] : $HTTP_GET_VARS['search_keywords'];
57 $search_keywords = '';
60 if ( isset($HTTP_POST_VARS['search_author']) || isset($HTTP_GET_VARS['search_author']))
62 $search_author = ( isset($HTTP_POST_VARS['search_author']) ) ? $HTTP_POST_VARS['search_author'] : $HTTP_GET_VARS['search_author'];
63 $search_author = phpbb_clean_username($search_author);
70 $search_id = ( isset($HTTP_GET_VARS['search_id']) ) ? $HTTP_GET_VARS['search_id'] : '';
72 $show_results = ( isset($HTTP_POST_VARS['show_results']) ) ? $HTTP_POST_VARS['show_results'] : 'posts';
73 $show_results = ($show_results == 'topics') ? 'topics' : 'posts';
75 if ( isset($HTTP_POST_VARS['search_terms']) )
77 $search_terms = ( $HTTP_POST_VARS['search_terms'] == 'all' ) ? 1 : 0;
84 if ( isset($HTTP_POST_VARS['search_fields']) )
86 $search_fields = ( $HTTP_POST_VARS['search_fields'] == 'all' ) ? 1 : 0;
93 $return_chars = ( isset($HTTP_POST_VARS['return_chars']) ) ? intval($HTTP_POST_VARS['return_chars']) : 200;
95 $search_cat = ( isset($HTTP_POST_VARS['search_cat']) ) ? intval($HTTP_POST_VARS['search_cat']) : -1;
96 $search_forum = ( isset($HTTP_POST_VARS['search_forum']) ) ? intval($HTTP_POST_VARS['search_forum']) : -1;
98 $sort_by = ( isset($HTTP_POST_VARS['sort_by']) ) ? intval($HTTP_POST_VARS['sort_by']) : 0;
100 if ( isset($HTTP_POST_VARS['sort_dir']) )
102 $sort_dir = ( $HTTP_POST_VARS['sort_dir'] == 'DESC' ) ? 'DESC' : 'ASC';
109 if ( !empty($HTTP_POST_VARS['search_time']) || !empty($HTTP_GET_VARS['search_time']))
111 $search_time = time() - ( ( ( !empty($HTTP_POST_VARS['search_time']) ) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']) ) * 86400 );
112 $topic_days = (!empty($HTTP_POST_VARS['search_time'])) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']);
120 $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
122 $sort_by_types = array($lang['Sort_Time'], $lang['Sort_Post_Subject'], $lang['Sort_Topic_Title'], $lang['Sort_Author'], $lang['Sort_Forum']);
125 // encoding match for workaround
127 $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
132 if ( $mode == 'searchuser' )
135 // This handles the simple windowed user search functions called from various other scripts
137 if ( isset($HTTP_POST_VARS['search_username']) )
139 username_search($HTTP_POST_VARS['search_username']);
148 else if ( $search_keywords != '' || $search_author != '' || $search_id )
150 $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'sort_dir', 'show_results', 'return_chars');
151 $search_results = '';
154 // Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
158 // Cycle through options ...
160 if ( $search_id == 'newposts' || $search_id == 'egosearch' || $search_id == 'unanswered' || $search_keywords != '' || $search_author != '' )
162 if ( $search_id == 'newposts' || $search_id == 'egosearch' || ( $search_author != '' && $search_keywords == '' ) )
164 if ( $search_id == 'newposts' )
166 if ( $userdata['session_logged_in'] )
168 $sql = "SELECT post_id
169 FROM " . POSTS_TABLE . "
170 WHERE post_time >= " . $userdata['user_lastvisit'];
174 redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=newposts", true));
177 $show_results = 'topics';
181 else if ( $search_id == 'egosearch' )
183 if ( $userdata['session_logged_in'] )
185 $sql = "SELECT post_id
186 FROM " . POSTS_TABLE . "
187 WHERE poster_id = " . $userdata['user_id'];
191 redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=egosearch", true));
194 $show_results = 'topics';
200 if (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($search_author))))
205 $search_author = str_replace('*', '%', trim($search_author));
207 $sql = "SELECT user_id
208 FROM " . USERS_TABLE . "
209 WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
210 if ( !($result = $db->sql_query($sql)) )
212 message_die(GENERAL_ERROR, "Couldn't obtain list of matching users (searching for: $search_author)", "", __LINE__, __FILE__, $sql);
215 $matching_userids = '';
216 if ( $row = $db->sql_fetchrow($result) )
220 $matching_userids .= ( ( $matching_userids != '' ) ? ', ' : '' ) . $row['user_id'];
222 while( $row = $db->sql_fetchrow($result) );
226 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
229 $sql = "SELECT post_id
230 FROM " . POSTS_TABLE . "
231 WHERE poster_id IN ($matching_userids)";
235 $sql .= " AND post_time >= " . $search_time;
239 if ( !($result = $db->sql_query($sql)) )
241 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
244 $search_ids = array();
245 while( $row = $db->sql_fetchrow($result) )
247 $search_ids[] = $row['post_id'];
249 $db->sql_freeresult($result);
251 $total_match_count = count($search_ids);
254 else if ( $search_keywords != '' )
256 $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt');
257 $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt');
259 $split_search = array();
260 $stripped_keywords = stripslashes($search_keywords);
261 $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ? split_words(clean_words('search', $stripped_keywords, $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords);
262 unset($stripped_keywords);
264 $search_msg_only = ( !$search_fields ) ? "AND m.title_match = 0" : ( ( strstr($multibyte_charset, $lang['ENCODING']) ) ? '' : '' );
267 $current_match_type = 'or';
269 $word_match = array();
270 $result_list = array();
272 for($i = 0; $i < count($split_search); $i++)
274 if (preg_match('#^[\*%]+$#', trim($split_search[$i])) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($split_search[$i]))))
276 $split_search[$i] = '';
280 switch ( $split_search[$i] )
283 $current_match_type = 'and';
287 $current_match_type = 'or';
291 $current_match_type = 'not';
295 if ( !empty($search_terms) )
297 $current_match_type = 'and';
300 if ( !strstr($multibyte_charset, $lang['ENCODING']) )
302 $match_word = str_replace('*', '%', $split_search[$i]);
303 $sql = "SELECT m.post_id
304 FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m
305 WHERE w.word_text LIKE '$match_word'
306 AND m.word_id = w.word_id
307 AND w.word_common <> 1
312 $match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
313 $search_msg_only = ( $search_fields ) ? "OR post_subject LIKE '$match_word'" : '';
314 $sql = "SELECT post_id
315 FROM " . POSTS_TEXT_TABLE . "
316 WHERE post_text LIKE '$match_word'
319 if ( !($result = $db->sql_query($sql)) )
321 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
325 while( $temp_row = $db->sql_fetchrow($result) )
327 $row[$temp_row['post_id']] = 1;
331 $result_list[$temp_row['post_id']] = 1;
333 else if ( $current_match_type == 'or' )
335 $result_list[$temp_row['post_id']] = 1;
337 else if ( $current_match_type == 'not' )
339 $result_list[$temp_row['post_id']] = 0;
343 if ( $current_match_type == 'and' && $word_count )
345 @reset($result_list);
346 while( list($post_id, $match_count) = @each($result_list) )
348 if ( !$row[$post_id] )
350 $result_list[$post_id] = 0;
357 $db->sql_freeresult($result);
361 @reset($result_list);
363 $search_ids = array();
364 while( list($post_id, $matches) = each($result_list) )
368 $search_ids[] = $post_id;
373 $total_match_count = count($search_ids);
377 // If user is logged in then we'll check to see which (if any) private
378 // forums they are allowed to view and include them in the search.
380 // If not logged in we explicitly prevent searching of private forums
383 if ( $search_forum != -1 )
385 $is_auth = auth(AUTH_READ, $search_forum, $userdata);
387 if ( !$is_auth['auth_read'] )
389 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
392 $auth_sql = "f.forum_id = $search_forum";
396 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
398 if ( $search_cat != -1 )
400 $auth_sql = "f.cat_id = $search_cat";
403 $ignore_forum_sql = '';
404 while( list($key, $value) = each($is_auth_ary) )
406 if ( !$value['auth_read'] )
408 $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
412 if ( $ignore_forum_sql != '' )
414 $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
419 // Author name search
421 if ( $search_author != '' )
423 if (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($search_author))))
428 $search_author = str_replace('*', '%', trim(str_replace("\'", "''", $search_author)));
431 if ( $total_match_count )
433 if ( $show_results == 'topics' )
436 // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
438 $search_id_chunks = array();
442 if (count($search_ids) > $limiter)
444 for ($i = 0; $i < count($search_ids); $i++)
446 if ($count == $limiter)
452 $search_id_chunks[$chunk][$count] = $search_ids[$i];
458 $search_id_chunks[0] = $search_ids;
461 $search_ids = array();
463 for ($i = 0; $i < count($search_id_chunks); $i++)
469 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
472 if ( $search_author == '' && $auth_sql == '' )
474 $sql = "SELECT topic_id
475 FROM " . POSTS_TABLE . "
476 WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
482 $from_sql = POSTS_TABLE . " p";
484 if ( $search_author != '' )
486 $from_sql .= ", " . USERS_TABLE . " u";
487 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
490 if ( $auth_sql != '' )
492 $from_sql .= ", " . FORUMS_TABLE . " f";
493 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
496 $sql = "SELECT p.topic_id
498 WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
500 GROUP BY p.topic_id";
503 if ( !($result = $db->sql_query($sql)) )
505 message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
508 while ($row = $db->sql_fetchrow($result))
510 $search_ids[] = $row['topic_id'];
512 $db->sql_freeresult($result);
515 $total_match_count = sizeof($search_ids);
518 else if ( $search_author != '' || $search_time || $auth_sql != '' )
520 $search_id_chunks = array();
524 if (count($search_ids) > $limiter)
526 for ($i = 0; $i < count($search_ids); $i++)
528 if ($count == $limiter)
534 $search_id_chunks[$chunk][$count] = $search_ids[$i];
540 $search_id_chunks[0] = $search_ids;
543 $search_ids = array();
545 for ($i = 0; $i < count($search_id_chunks); $i++)
547 $where_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
548 $select_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id' : 'p.post_id';
549 $from_sql = ( $search_author == '' && $auth_sql == '' ) ? POSTS_TABLE : POSTS_TABLE . ' p';
553 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
556 if ( $auth_sql != '' )
558 $from_sql .= ", " . FORUMS_TABLE . " f";
559 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
562 if ( $search_author != '' )
564 $from_sql .= ", " . USERS_TABLE . " u";
565 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
568 $sql = "SELECT " . $select_sql . "
571 if ( !($result = $db->sql_query($sql)) )
573 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
576 while( $row = $db->sql_fetchrow($result) )
578 $search_ids[] = $row['post_id'];
580 $db->sql_freeresult($result);
583 $total_match_count = count($search_ids);
586 else if ( $search_id == 'unanswered' )
588 if ( $auth_sql != '' )
590 $sql = "SELECT t.topic_id, f.forum_id
591 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
592 WHERE t.topic_replies = 0
593 AND t.forum_id = f.forum_id
594 AND t.topic_moved_id = 0
599 $sql = "SELECT topic_id
600 FROM " . TOPICS_TABLE . "
601 WHERE topic_replies = 0
602 AND topic_moved_id = 0";
605 if ( !($result = $db->sql_query($sql)) )
607 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
610 $search_ids = array();
611 while( $row = $db->sql_fetchrow($result) )
613 $search_ids[] = $row['topic_id'];
615 $db->sql_freeresult($result);
617 $total_match_count = count($search_ids);
620 // Basic requirements
622 $show_results = 'topics';
628 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
632 // Finish building query (for all combinations)
635 $sql = "SELECT session_id
636 FROM " . SESSIONS_TABLE;
637 if ( $result = $db->sql_query($sql) )
639 $delete_search_ids = array();
640 while( $row = $db->sql_fetchrow($result) )
642 $delete_search_ids[] = "'" . $row['session_id'] . "'";
645 if ( count($delete_search_ids) )
647 $sql = "DELETE FROM " . SEARCH_TABLE . "
648 WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
649 if ( !$result = $db->sql_query($sql) )
651 message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
657 // Store new result data
659 $search_results = implode(', ', $search_ids);
660 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
663 // Combine both results and search data (apart from original query)
664 // so we can serialize it and place it in the DB
666 $store_search_data = array();
669 // Limit the character length (and with this the results displayed at all following pages) to prevent
670 // truncated result arrays. Normally, search results above 12000 are affected.
671 // - to include or not to include
673 $max_result_length = 60000;
674 if (strlen($search_results) > $max_result_length)
676 $search_results = substr($search_results, 0, $max_result_length);
677 $search_results = substr($search_results, 0, strrpos($search_results, ','));
678 $total_match_count = count(explode(', ', $search_results));
682 for($i = 0; $i < count($store_vars); $i++)
684 $store_search_data[$store_vars[$i]] = $$store_vars[$i];
687 $result_array = serialize($store_search_data);
688 unset($store_search_data);
690 mt_srand ((double) microtime() * 1000000);
691 $search_id = mt_rand();
693 $sql = "UPDATE " . SEARCH_TABLE . "
694 SET search_id = $search_id, search_array = '" . str_replace("\'", "''", $result_array) . "'
695 WHERE session_id = '" . $userdata['session_id'] . "'";
696 if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
698 $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array)
699 VALUES($search_id, '" . $userdata['session_id'] . "', '" . str_replace("\'", "''", $result_array) . "')";
700 if ( !($result = $db->sql_query($sql)) )
702 message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
708 $search_id = intval($search_id);
711 $sql = "SELECT search_array
712 FROM " . SEARCH_TABLE . "
713 WHERE search_id = $search_id
714 AND session_id = '". $userdata['session_id'] . "'";
715 if ( !($result = $db->sql_query($sql)) )
717 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
720 if ( $row = $db->sql_fetchrow($result) )
722 $search_data = unserialize($row['search_array']);
723 for($i = 0; $i < count($store_vars); $i++)
725 $$store_vars[$i] = $search_data[$store_vars[$i]];
734 if ( $search_results != '' )
736 if ( $show_results == 'posts' )
738 $sql = "SELECT pt.post_text, pt.bbcode_uid, pt.post_subject, p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_id, u.user_sig, u.user_sig_bbcode_uid
739 FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt
740 WHERE p.post_id IN ($search_results)
741 AND pt.post_id = p.post_id
742 AND f.forum_id = p.forum_id
743 AND p.topic_id = t.topic_id
744 AND p.poster_id = u.user_id";
748 $sql = "SELECT t.*, f.forum_id, f.forum_name, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time
749 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
750 WHERE t.topic_id IN ($search_results)
751 AND t.topic_poster = u.user_id
752 AND f.forum_id = t.forum_id
753 AND p.post_id = t.topic_first_post_id
754 AND p2.post_id = t.topic_last_post_id
755 AND u2.user_id = p2.poster_id";
758 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
760 $sql .= " ORDER BY ";
764 $sql .= ( $show_results == 'posts' ) ? 'pt.post_subject' : 't.topic_title';
767 $sql .= 't.topic_title';
770 $sql .= 'u.username';
773 $sql .= 'f.forum_id';
776 $sql .= ( $show_results == 'posts' ) ? 'p.post_time' : 'p2.post_time';
779 $sql .= " $sort_dir LIMIT $start, " . $per_page;
781 if ( !$result = $db->sql_query($sql) )
783 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
786 $searchset = array();
787 while( $row = $db->sql_fetchrow($result) )
792 $db->sql_freeresult($result);
795 // Define censored word matches
797 $orig_word = array();
798 $replacement_word = array();
799 obtain_word_list($orig_word, $replacement_word);
804 $page_title = $lang['Search'];
805 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
807 if ( $show_results == 'posts' )
809 $template->set_filenames(array(
810 'body' => 'search_results_posts.tpl')
815 $template->set_filenames(array(
816 'body' => 'search_results_topics.tpl')
819 make_jumpbox('viewforum.'.$phpEx);
821 $l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
823 $template->assign_vars(array(
824 'L_SEARCH_MATCHES' => $l_search_matches,
825 'L_TOPIC' => $lang['Topic'])
828 $highlight_active = '';
829 $highlight_match = array();
830 for($j = 0; $j < count($split_search); $j++ )
832 $split_word = $split_search[$j];
834 if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
836 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
837 $highlight_active .= " " . $split_word;
839 for ($k = 0; $k < count($synonym_array); $k++)
841 list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k])));
843 if ( $replace_synonym == $split_word )
845 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
846 $highlight_active .= ' ' . $match_synonym;
852 $highlight_active = urlencode(trim($highlight_active));
854 $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
855 $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
857 for($i = 0; $i < count($searchset); $i++)
859 $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
860 $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "&highlight=$highlight_active");
861 $post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
863 $post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
865 $message = $searchset[$i]['post_text'];
866 $topic_title = $searchset[$i]['topic_title'];
868 $forum_id = $searchset[$i]['forum_id'];
869 $topic_id = $searchset[$i]['topic_id'];
871 if ( $show_results == 'posts' )
873 if ( isset($return_chars) )
875 $bbcode_uid = $searchset[$i]['bbcode_uid'];
878 // If the board has HTML off but the post has HTML
879 // on then we process it, else leave it alone
881 if ( $return_chars != -1 )
883 $message = strip_tags($message);
884 $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
885 $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
886 $message = ( strlen($message) > $return_chars ) ? substr($message, 0, $return_chars) . ' ...' : $message;
890 if ( !$board_config['allow_html'] )
892 if ( $postrow[$i]['enable_html'] )
894 $message = preg_replace('#(<)([\/]?.*?)(>)#is', '<\\2>', $message);
898 if ( $bbcode_uid != '' )
900 $message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
903 $message = make_clickable($message);
905 if ( $highlight_active )
907 if ( preg_match('/<.*>/', $message) )
909 $message = preg_replace($highlight_match, '<!-- #sh -->\1<!-- #eh -->', $message);
914 $message = ' ' . $message . ' ';
916 while( $start_html = strpos($message, '<', $start_html) )
918 $grab_length = $start_html - $end_html - 1;
919 $temp_message .= substr($message, $end_html + 1, $grab_length);
921 if ( $end_html = strpos($message, '>', $start_html) )
923 $length = $end_html - $start_html + 1;
924 $hold_string = substr($message, $start_html, $length);
926 if ( strrpos(' ' . $hold_string, '<') != 1 )
928 $end_html = $start_html + 1;
931 while ( $end_counter && $end_html < strlen($message) )
933 if ( substr($message, $end_html, 1) == '>' )
937 else if ( substr($message, $end_html, 1) == '<' )
945 $length = $end_html - $start_html + 1;
946 $hold_string = substr($message, $start_html, $length);
947 $hold_string = str_replace('<!-- #sh -->', '', $hold_string);
948 $hold_string = str_replace('<!-- #eh -->', '', $hold_string);
950 else if ( $hold_string == '<!-- #sh -->' )
952 $hold_string = str_replace('<!-- #sh -->', '<span style="color:#' . $theme['fontcolor3'] . '"><b>', $hold_string);
954 else if ( $hold_string == '<!-- #eh -->' )
956 $hold_string = str_replace('<!-- #eh -->', '</b></span>', $hold_string);
959 $temp_message .= $hold_string;
961 $start_html += $length;
965 $start_html = strlen($message);
969 $grab_length = strlen($message) - $end_html - 1;
970 $temp_message .= substr($message, $end_html + 1, $grab_length);
972 $message = trim($temp_message);
976 $message = preg_replace($highlight_match, '<span style="color:#' . $theme['fontcolor3'] . '"><b>\1</b></span>', $message);
981 if ( count($orig_word) )
983 $topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
984 $post_subject = ( $searchset[$i]['post_subject'] != "" ) ? preg_replace($orig_word, $replacement_word, $searchset[$i]['post_subject']) : $topic_title;
986 $message = preg_replace($orig_word, $replacement_word, $message);
990 $post_subject = ( $searchset[$i]['post_subject'] != '' ) ? $searchset[$i]['post_subject'] : $topic_title;
993 if ($board_config['allow_smilies'] && $searchset[$i]['enable_smilies'])
995 $message = smilies_pass($message);
998 $message = str_replace("\n", '<br />', $message);
1002 $poster = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=" . $searchset[$i]['user_id']) . '">' : '';
1003 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != "" ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1004 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1006 if ( $userdata['session_logged_in'] && $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1008 if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) )
1010 $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1012 else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) )
1014 $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1017 if ( $searchset[$i]['post_time'] > $topic_last_read )
1019 $mini_post_img = $images['icon_minipost_new'];
1020 $mini_post_alt = $lang['New_post'];
1024 $mini_post_img = $images['icon_minipost'];
1025 $mini_post_alt = $lang['Post'];
1030 $mini_post_img = $images['icon_minipost'];
1031 $mini_post_alt = $lang['Post'];
1034 $template->assign_block_vars("searchresults", array(
1035 'TOPIC_TITLE' => $topic_title,
1036 'FORUM_NAME' => $searchset[$i]['forum_name'],
1037 'POST_SUBJECT' => $post_subject,
1038 'POST_DATE' => $post_date,
1039 'POSTER_NAME' => $poster,
1040 'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
1041 'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
1042 'MESSAGE' => $message,
1043 'MINI_POST_IMG' => $mini_post_img,
1045 'L_MINI_POST_ALT' => $mini_post_alt,
1047 'U_POST' => $post_url,
1048 'U_TOPIC' => $topic_url,
1049 'U_FORUM' => $forum_url)
1056 if ( count($orig_word) )
1058 $topic_title = preg_replace($orig_word, $replacement_word, $searchset[$i]['topic_title']);
1061 $topic_type = $searchset[$i]['topic_type'];
1063 if ($topic_type == POST_ANNOUNCE)
1065 $topic_type = $lang['Topic_Announcement'] . ' ';
1067 else if ($topic_type == POST_STICKY)
1069 $topic_type = $lang['Topic_Sticky'] . ' ';
1076 if ( $searchset[$i]['topic_vote'] )
1078 $topic_type .= $lang['Topic_Poll'] . ' ';
1081 $views = $searchset[$i]['topic_views'];
1082 $replies = $searchset[$i]['topic_replies'];
1084 if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
1086 $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
1087 $goto_page = ' [ <img src="' . $images['icon_gotopost'] . '" alt="' . $lang['Goto_page'] . '" title="' . $lang['Goto_page'] . '" />' . $lang['Goto_page'] . ': ';
1090 for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
1092 $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&start=$j") . '">' . $times . '</a>';
1093 if ( $times == 1 && $total_pages > 4 )
1095 $goto_page .= ' ... ';
1096 $times = $total_pages - 3;
1097 $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
1099 else if ( $times < $total_pages )
1105 $goto_page .= ' ] ';
1112 if ( $searchset[$i]['topic_status'] == TOPIC_MOVED )
1114 $topic_type = $lang['Topic_Moved'] . ' ';
1115 $topic_id = $searchset[$i]['topic_moved_id'];
1117 $folder_image = '<img src="' . $images['folder'] . '" alt="' . $lang['No_new_posts'] . '" />';
1118 $newest_post_img = '';
1122 if ( $searchset[$i]['topic_status'] == TOPIC_LOCKED )
1124 $folder = $images['folder_locked'];
1125 $folder_new = $images['folder_locked_new'];
1127 else if ( $searchset[$i]['topic_type'] == POST_ANNOUNCE )
1129 $folder = $images['folder_announce'];
1130 $folder_new = $images['folder_announce_new'];
1132 else if ( $searchset[$i]['topic_type'] == POST_STICKY )
1134 $folder = $images['folder_sticky'];
1135 $folder_new = $images['folder_sticky_new'];
1139 if ( $replies >= $board_config['hot_threshold'] )
1141 $folder = $images['folder_hot'];
1142 $folder_new = $images['folder_hot_new'];
1146 $folder = $images['folder'];
1147 $folder_new = $images['folder_new'];
1151 if ( $userdata['session_logged_in'] )
1153 if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1155 if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1158 $unread_topics = true;
1160 if ( !empty($tracking_topics[$topic_id]) )
1162 if ( $tracking_topics[$topic_id] > $searchset[$i]['post_time'] )
1164 $unread_topics = false;
1168 if ( !empty($tracking_forums[$forum_id]) )
1170 if ( $tracking_forums[$forum_id] > $searchset[$i]['post_time'] )
1172 $unread_topics = false;
1176 if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1178 if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $searchset[$i]['post_time'] )
1180 $unread_topics = false;
1184 if ( $unread_topics )
1186 $folder_image = $folder_new;
1187 $folder_alt = $lang['New_posts'];
1189 $newest_post_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
1193 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1195 $folder_image = $folder;
1196 $folder_alt = $folder_alt;
1197 $newest_post_img = '';
1201 else if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1203 $folder_image = $folder_new;
1204 $folder_alt = $lang['New_posts'];
1206 $newest_post_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
1210 $folder_image = $folder;
1211 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1212 $newest_post_img = '';
1217 $folder_image = $folder;
1218 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1219 $newest_post_img = '';
1224 $folder_image = $folder;
1225 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1226 $newest_post_img = '';
1231 $topic_author = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '=' . $searchset[$i]['user_id']) . '">' : '';
1232 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != '' ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1234 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1236 $first_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['topic_time'], $board_config['board_timezone']);
1238 $last_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
1240 $last_post_author = ( $searchset[$i]['id2'] == ANONYMOUS ) ? ( ($searchset[$i]['post_username2'] != '' ) ? $searchset[$i]['post_username2'] . ' ' : $lang['Guest'] . ' ' ) : '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '=' . $searchset[$i]['id2']) . '">' . $searchset[$i]['user2'] . '</a>';
1242 $last_post_url = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['topic_last_post_id']) . '#' . $searchset[$i]['topic_last_post_id'] . '"><img src="' . $images['icon_latest_reply'] . '" alt="' . $lang['View_latest_post'] . '" title="' . $lang['View_latest_post'] . '" border="0" /></a>';
1244 $template->assign_block_vars('searchresults', array(
1245 'FORUM_NAME' => $searchset[$i]['forum_name'],
1246 'FORUM_ID' => $forum_id,
1247 'TOPIC_ID' => $topic_id,
1248 'FOLDER' => $folder_image,
1249 'NEWEST_POST_IMG' => $newest_post_img,
1250 'TOPIC_FOLDER_IMG' => $folder_image,
1251 'GOTO_PAGE' => $goto_page,
1252 'REPLIES' => $replies,
1253 'TOPIC_TITLE' => $topic_title,
1254 'TOPIC_TYPE' => $topic_type,
1256 'TOPIC_AUTHOR' => $topic_author,
1257 'FIRST_POST_TIME' => $first_post_time,
1258 'LAST_POST_TIME' => $last_post_time,
1259 'LAST_POST_AUTHOR' => $last_post_author,
1260 'LAST_POST_IMG' => $last_post_url,
1262 'L_TOPIC_FOLDER_ALT' => $folder_alt,
1264 'U_VIEW_FORUM' => $forum_url,
1265 'U_VIEW_TOPIC' => $topic_url)
1270 $base_url = "search.$phpEx?search_id=$search_id";
1272 $template->assign_vars(array(
1273 'PAGINATION' => generate_pagination($base_url, $total_match_count, $per_page, $start),
1274 'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $per_page ) + 1 ), ceil( $total_match_count / $per_page )),
1276 'L_AUTHOR' => $lang['Author'],
1277 'L_MESSAGE' => $lang['Message'],
1278 'L_FORUM' => $lang['Forum'],
1279 'L_TOPICS' => $lang['Topics'],
1280 'L_REPLIES' => $lang['Replies'],
1281 'L_VIEWS' => $lang['Views'],
1282 'L_POSTS' => $lang['Posts'],
1283 'L_LASTPOST' => $lang['Last_Post'],
1284 'L_POSTED' => $lang['Posted'],
1285 'L_SUBJECT' => $lang['Subject'],
1287 'L_GOTO_PAGE' => $lang['Goto_page'])
1290 $template->pparse('body');
1292 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1296 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
1303 $sql = "SELECT c.cat_title, c.cat_id, f.forum_name, f.forum_id
1304 FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
1305 WHERE f.cat_id = c.cat_id
1306 ORDER BY c.cat_order, f.forum_order";
1307 $result = $db->sql_query($sql);
1310 message_die(GENERAL_ERROR, 'Could not obtain forum_name/forum_id', '', __LINE__, __FILE__, $sql);
1313 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
1316 while( $row = $db->sql_fetchrow($result) )
1318 if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
1320 $s_forums .= '<option value="' . $row['forum_id'] . '">' . $row['forum_name'] . '</option>';
1321 if ( empty($list_cat[$row['cat_id']]) )
1323 $list_cat[$row['cat_id']] = $row['cat_title'];
1328 if ( $s_forums != '' )
1330 $s_forums = '<option value="-1">' . $lang['All_available'] . '</option>' . $s_forums;
1333 // Category to search
1335 $s_categories = '<option value="-1">' . $lang['All_available'] . '</option>';
1336 while( list($cat_id, $cat_title) = @each($list_cat))
1338 $s_categories .= '<option value="' . $cat_id . '">' . $cat_title . '</option>';
1343 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
1347 // Number of chars returned
1349 $s_characters = '<option value="-1">' . $lang['All_available'] . '</option>';
1350 $s_characters .= '<option value="0">0</option>';
1351 $s_characters .= '<option value="25">25</option>';
1352 $s_characters .= '<option value="50">50</option>';
1354 for($i = 100; $i < 1100 ; $i += 100)
1356 $selected = ( $i == 200 ) ? ' selected="selected"' : '';
1357 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1364 for($i = 0; $i < count($sort_by_types); $i++)
1366 $s_sort_by .= '<option value="' . $i . '">' . $sort_by_types[$i] . '</option>';
1372 $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
1373 $previous_days_text = array($lang['All_Posts'], $lang['1_Day'], $lang['7_Days'], $lang['2_Weeks'], $lang['1_Month'], $lang['3_Months'], $lang['6_Months'], $lang['1_Year']);
1376 for($i = 0; $i < count($previous_days); $i++)
1378 $selected = ( $topic_days == $previous_days[$i] ) ? ' selected="selected"' : '';
1379 $s_time .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
1383 // Output the basic page
1385 $page_title = $lang['Search'];
1386 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1388 $template->set_filenames(array(
1389 'body' => 'search_body.tpl')
1391 make_jumpbox('viewforum.'.$phpEx);
1393 $template->assign_vars(array(
1394 'L_SEARCH_QUERY' => $lang['Search_query'],
1395 'L_SEARCH_OPTIONS' => $lang['Search_options'],
1396 'L_SEARCH_KEYWORDS' => $lang['Search_keywords'],
1397 'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'],
1398 'L_SEARCH_AUTHOR' => $lang['Search_author'],
1399 'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'],
1400 'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
1401 'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'],
1402 'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'],
1403 'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'],
1404 'L_CATEGORY' => $lang['Category'],
1405 'L_RETURN_FIRST' => $lang['Return_first'],
1406 'L_CHARACTERS' => $lang['characters_posts'],
1407 'L_SORT_BY' => $lang['Sort_by'],
1408 'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
1409 'L_SORT_DESCENDING' => $lang['Sort_Descending'],
1410 'L_SEARCH_PREVIOUS' => $lang['Search_previous'],
1411 'L_DISPLAY_RESULTS' => $lang['Display_results'],
1412 'L_FORUM' => $lang['Forum'],
1413 'L_TOPICS' => $lang['Topics'],
1414 'L_POSTS' => $lang['Posts'],
1416 'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=results"),
1417 'S_CHARACTER_OPTIONS' => $s_characters,
1418 'S_FORUM_OPTIONS' => $s_forums,
1419 'S_CATEGORY_OPTIONS' => $s_categories,
1420 'S_TIME_OPTIONS' => $s_time,
1421 'S_SORT_OPTIONS' => $s_sort_by,
1422 'S_HIDDEN_FIELDS' => '')
1425 $template->pparse('body');
1427 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);