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.15 2004/11/18 17:49:39 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 $search_author = str_replace('*', '%', trim($search_author));
202 $sql = "SELECT user_id
203 FROM " . USERS_TABLE . "
204 WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
205 if ( !($result = $db->sql_query($sql)) )
207 message_die(GENERAL_ERROR, "Couldn't obtain list of matching users (searching for: $search_author)", "", __LINE__, __FILE__, $sql);
210 $matching_userids = '';
211 if ( $row = $db->sql_fetchrow($result) )
215 $matching_userids .= ( ( $matching_userids != '' ) ? ', ' : '' ) . $row['user_id'];
217 while( $row = $db->sql_fetchrow($result) );
221 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
224 $sql = "SELECT post_id
225 FROM " . POSTS_TABLE . "
226 WHERE poster_id IN ($matching_userids)";
230 $sql .= " AND post_time >= " . $search_time;
234 if ( !($result = $db->sql_query($sql)) )
236 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
239 $search_ids = array();
240 while( $row = $db->sql_fetchrow($result) )
242 $search_ids[] = $row['post_id'];
244 $db->sql_freeresult($result);
246 $total_match_count = count($search_ids);
249 else if ( $search_keywords != '' )
251 $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt');
252 $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt');
254 $split_search = array();
255 $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ? split_words(clean_words('search', stripslashes($search_keywords), $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords);
257 $search_msg_only = ( !$search_fields ) ? "AND m.title_match = 0" : ( ( strstr($multibyte_charset, $lang['ENCODING']) ) ? '' : '' );
260 $current_match_type = 'or';
262 $word_match = array();
263 $result_list = array();
265 for($i = 0; $i < count($split_search); $i++)
267 switch ( $split_search[$i] )
270 $current_match_type = 'and';
274 $current_match_type = 'or';
278 $current_match_type = 'not';
282 if ( !empty($search_terms) )
284 $current_match_type = 'and';
287 if ( !strstr($multibyte_charset, $lang['ENCODING']) )
289 $match_word = str_replace('*', '%', $split_search[$i]);
290 $sql = "SELECT m.post_id
291 FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m
292 WHERE w.word_text LIKE '$match_word'
293 AND m.word_id = w.word_id
294 AND w.word_common <> 1
299 $match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
300 $search_msg_only = ( $search_fields ) ? "OR post_subject LIKE '$match_word'" : '';
301 $sql = "SELECT post_id
302 FROM " . POSTS_TEXT_TABLE . "
303 WHERE post_text LIKE '$match_word'
306 if ( !($result = $db->sql_query($sql)) )
308 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
312 while( $temp_row = $db->sql_fetchrow($result) )
314 $row[$temp_row['post_id']] = 1;
318 $result_list[$temp_row['post_id']] = 1;
320 else if ( $current_match_type == 'or' )
322 $result_list[$temp_row['post_id']] = 1;
324 else if ( $current_match_type == 'not' )
326 $result_list[$temp_row['post_id']] = 0;
330 if ( $current_match_type == 'and' && $word_count )
332 @reset($result_list);
333 while( list($post_id, $match_count) = @each($result_list) )
335 if ( !$row[$post_id] )
337 $result_list[$post_id] = 0;
344 $db->sql_freeresult($result);
348 @reset($result_list);
350 $search_ids = array();
351 while( list($post_id, $matches) = each($result_list) )
355 $search_ids[] = $post_id;
360 $total_match_count = count($search_ids);
364 // If user is logged in then we'll check to see which (if any) private
365 // forums they are allowed to view and include them in the search.
367 // If not logged in we explicitly prevent searching of private forums
370 if ( $search_forum != -1 )
372 $is_auth = auth(AUTH_READ, $search_forum, $userdata);
374 if ( !$is_auth['auth_read'] )
376 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
379 $auth_sql = "f.forum_id = $search_forum";
383 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
385 if ( $search_cat != -1 )
387 $auth_sql = "f.cat_id = $search_cat";
390 $ignore_forum_sql = '';
391 while( list($key, $value) = each($is_auth_ary) )
393 if ( !$value['auth_read'] )
395 $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
399 if ( $ignore_forum_sql != '' )
401 $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
406 // Author name search
408 if ( $search_author != '' )
410 $search_author = str_replace('*', '%', trim(str_replace("\'", "''", $search_author)));
413 if ( $total_match_count )
415 if ( $show_results == 'topics' )
418 // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
420 $search_id_chunks = array();
424 if (count($search_ids) > $limiter)
426 for ($i = 0; $i < count($search_ids); $i++)
428 if ($count == $limiter)
434 $search_id_chunks[$chunk][$count] = $search_ids[$i];
440 $search_id_chunks[0] = $search_ids;
443 $search_ids = array();
445 for ($i = 0; $i < count($search_id_chunks); $i++)
451 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
454 if ( $search_author == '' && $auth_sql == '' )
456 $sql = "SELECT topic_id
457 FROM " . POSTS_TABLE . "
458 WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
464 $from_sql = POSTS_TABLE . " p";
466 if ( $search_author != '' )
468 $from_sql .= ", " . USERS_TABLE . " u";
469 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
472 if ( $auth_sql != '' )
474 $from_sql .= ", " . FORUMS_TABLE . " f";
475 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
478 $sql = "SELECT p.topic_id
480 WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
482 GROUP BY p.topic_id";
485 if ( !($result = $db->sql_query($sql)) )
487 message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
490 while ($row = $db->sql_fetchrow($result))
492 $search_ids[] = $row['topic_id'];
494 $db->sql_freeresult($result);
497 $total_match_count = sizeof($search_ids);
500 else if ( $search_author != '' || $search_time || $auth_sql != '' )
502 $search_id_chunks = array();
506 if (count($search_ids) > $limiter)
508 for ($i = 0; $i < count($search_ids); $i++)
510 if ($count == $limiter)
516 $search_id_chunks[$chunk][$count] = $search_ids[$i];
522 $search_id_chunks[0] = $search_ids;
525 $search_ids = array();
527 for ($i = 0; $i < count($search_id_chunks); $i++)
529 $where_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
530 $select_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id' : 'p.post_id';
531 $from_sql = ( $search_author == '' && $auth_sql == '' ) ? POSTS_TABLE : POSTS_TABLE . ' p';
535 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
538 if ( $auth_sql != '' )
540 $from_sql .= ", " . FORUMS_TABLE . " f";
541 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
544 if ( $search_author != '' )
546 $from_sql .= ", " . USERS_TABLE . " u";
547 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
550 $sql = "SELECT " . $select_sql . "
553 if ( !($result = $db->sql_query($sql)) )
555 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
558 while( $row = $db->sql_fetchrow($result) )
560 $search_ids[] = $row['post_id'];
562 $db->sql_freeresult($result);
565 $total_match_count = count($search_ids);
568 else if ( $search_id == 'unanswered' )
570 if ( $auth_sql != '' )
572 $sql = "SELECT t.topic_id, f.forum_id
573 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
574 WHERE t.topic_replies = 0
575 AND t.forum_id = f.forum_id
576 AND t.topic_moved_id = 0
581 $sql = "SELECT topic_id
582 FROM " . TOPICS_TABLE . "
583 WHERE topic_replies = 0
584 AND topic_moved_id = 0";
587 if ( !($result = $db->sql_query($sql)) )
589 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
592 $search_ids = array();
593 while( $row = $db->sql_fetchrow($result) )
595 $search_ids[] = $row['topic_id'];
597 $db->sql_freeresult($result);
599 $total_match_count = count($search_ids);
602 // Basic requirements
604 $show_results = 'topics';
610 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
614 // Finish building query (for all combinations)
617 $sql = "SELECT session_id
618 FROM " . SESSIONS_TABLE;
619 if ( $result = $db->sql_query($sql) )
621 $delete_search_ids = array();
622 while( $row = $db->sql_fetchrow($result) )
624 $delete_search_ids[] = "'" . $row['session_id'] . "'";
627 if ( count($delete_search_ids) )
629 $sql = "DELETE FROM " . SEARCH_TABLE . "
630 WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
631 if ( !$result = $db->sql_query($sql) )
633 message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
639 // Store new result data
641 $search_results = implode(', ', $search_ids);
642 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
645 // Combine both results and search data (apart from original query)
646 // so we can serialize it and place it in the DB
648 $store_search_data = array();
651 // Limit the character length (and with this the results displayed at all following pages) to prevent
652 // truncated result arrays. Normally, search results above 12000 are affected.
653 // - to include or not to include
655 $max_result_length = 60000;
656 if (strlen($search_results) > $max_result_length)
658 $search_results = substr($search_results, 0, $max_result_length);
659 $search_results = substr($search_results, 0, strrpos($search_results, ','));
660 $total_match_count = count(explode(', ', $search_results));
664 for($i = 0; $i < count($store_vars); $i++)
666 $store_search_data[$store_vars[$i]] = $$store_vars[$i];
669 $result_array = serialize($store_search_data);
670 unset($store_search_data);
672 mt_srand ((double) microtime() * 1000000);
673 $search_id = mt_rand();
675 $sql = "UPDATE " . SEARCH_TABLE . "
676 SET search_id = $search_id, search_array = '" . str_replace("\'", "''", $result_array) . "'
677 WHERE session_id = '" . $userdata['session_id'] . "'";
678 if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
680 $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array)
681 VALUES($search_id, '" . $userdata['session_id'] . "', '" . str_replace("\'", "''", $result_array) . "')";
682 if ( !($result = $db->sql_query($sql)) )
684 message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
690 $search_id = intval($search_id);
693 $sql = "SELECT search_array
694 FROM " . SEARCH_TABLE . "
695 WHERE search_id = $search_id
696 AND session_id = '". $userdata['session_id'] . "'";
697 if ( !($result = $db->sql_query($sql)) )
699 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
702 if ( $row = $db->sql_fetchrow($result) )
704 $search_data = unserialize($row['search_array']);
705 for($i = 0; $i < count($store_vars); $i++)
707 $$store_vars[$i] = $search_data[$store_vars[$i]];
716 if ( $search_results != '' )
718 if ( $show_results == 'posts' )
720 $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
721 FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt
722 WHERE p.post_id IN ($search_results)
723 AND pt.post_id = p.post_id
724 AND f.forum_id = p.forum_id
725 AND p.topic_id = t.topic_id
726 AND p.poster_id = u.user_id";
730 $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
731 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
732 WHERE t.topic_id IN ($search_results)
733 AND t.topic_poster = u.user_id
734 AND f.forum_id = t.forum_id
735 AND p.post_id = t.topic_first_post_id
736 AND p2.post_id = t.topic_last_post_id
737 AND u2.user_id = p2.poster_id";
740 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
742 $sql .= " ORDER BY ";
746 $sql .= ( $show_results == 'posts' ) ? 'pt.post_subject' : 't.topic_title';
749 $sql .= 't.topic_title';
752 $sql .= 'u.username';
755 $sql .= 'f.forum_id';
758 $sql .= ( $show_results == 'posts' ) ? 'p.post_time' : 'p2.post_time';
761 $sql .= " $sort_dir LIMIT $start, " . $per_page;
763 if ( !$result = $db->sql_query($sql) )
765 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
768 $searchset = array();
769 while( $row = $db->sql_fetchrow($result) )
774 $db->sql_freeresult($result);
777 // Define censored word matches
779 $orig_word = array();
780 $replacement_word = array();
781 obtain_word_list($orig_word, $replacement_word);
786 $page_title = $lang['Search'];
787 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
789 if ( $show_results == 'posts' )
791 $template->set_filenames(array(
792 'body' => 'search_results_posts.tpl')
797 $template->set_filenames(array(
798 'body' => 'search_results_topics.tpl')
801 make_jumpbox('viewforum.'.$phpEx);
803 $l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
805 $template->assign_vars(array(
806 'L_SEARCH_MATCHES' => $l_search_matches,
807 'L_TOPIC' => $lang['Topic'])
810 $highlight_active = '';
811 $highlight_match = array();
812 for($j = 0; $j < count($split_search); $j++ )
814 $split_word = $split_search[$j];
816 if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
818 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
819 $highlight_active .= " " . $split_word;
821 for ($k = 0; $k < count($synonym_array); $k++)
823 list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k])));
825 if ( $replace_synonym == $split_word )
827 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
828 $highlight_active .= ' ' . $match_synonym;
834 $highlight_active = urlencode(trim($highlight_active));
836 $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
837 $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
839 for($i = 0; $i < count($searchset); $i++)
841 $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
842 $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "&highlight=$highlight_active");
843 $post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
845 $post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
847 $message = $searchset[$i]['post_text'];
848 $topic_title = $searchset[$i]['topic_title'];
850 $forum_id = $searchset[$i]['forum_id'];
851 $topic_id = $searchset[$i]['topic_id'];
853 if ( $show_results == 'posts' )
855 if ( isset($return_chars) )
857 $bbcode_uid = $searchset[$i]['bbcode_uid'];
860 // If the board has HTML off but the post has HTML
861 // on then we process it, else leave it alone
863 if ( $return_chars != -1 )
865 $message = strip_tags($message);
866 $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
867 $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
868 $message = ( strlen($message) > $return_chars ) ? substr($message, 0, $return_chars) . ' ...' : $message;
872 if ( !$board_config['allow_html'] )
874 if ( $postrow[$i]['enable_html'] )
876 $message = preg_replace('#(<)([\/]?.*?)(>)#is', '<\\2>', $message);
880 if ( $bbcode_uid != '' )
882 $message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
885 $message = make_clickable($message);
887 if ( $highlight_active )
889 if ( preg_match('/<.*>/', $message) )
891 $message = preg_replace($highlight_match, '<!-- #sh -->\1<!-- #eh -->', $message);
896 $message = ' ' . $message . ' ';
898 while( $start_html = strpos($message, '<', $start_html) )
900 $grab_length = $start_html - $end_html - 1;
901 $temp_message .= substr($message, $end_html + 1, $grab_length);
903 if ( $end_html = strpos($message, '>', $start_html) )
905 $length = $end_html - $start_html + 1;
906 $hold_string = substr($message, $start_html, $length);
908 if ( strrpos(' ' . $hold_string, '<') != 1 )
910 $end_html = $start_html + 1;
913 while ( $end_counter && $end_html < strlen($message) )
915 if ( substr($message, $end_html, 1) == '>' )
919 else if ( substr($message, $end_html, 1) == '<' )
927 $length = $end_html - $start_html + 1;
928 $hold_string = substr($message, $start_html, $length);
929 $hold_string = str_replace('<!-- #sh -->', '', $hold_string);
930 $hold_string = str_replace('<!-- #eh -->', '', $hold_string);
932 else if ( $hold_string == '<!-- #sh -->' )
934 $hold_string = str_replace('<!-- #sh -->', '<span style="color:#' . $theme['fontcolor3'] . '"><b>', $hold_string);
936 else if ( $hold_string == '<!-- #eh -->' )
938 $hold_string = str_replace('<!-- #eh -->', '</b></span>', $hold_string);
941 $temp_message .= $hold_string;
943 $start_html += $length;
947 $start_html = strlen($message);
951 $grab_length = strlen($message) - $end_html - 1;
952 $temp_message .= substr($message, $end_html + 1, $grab_length);
954 $message = trim($temp_message);
958 $message = preg_replace($highlight_match, '<span style="color:#' . $theme['fontcolor3'] . '"><b>\1</b></span>', $message);
963 if ( count($orig_word) )
965 $topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
966 $post_subject = ( $searchset[$i]['post_subject'] != "" ) ? preg_replace($orig_word, $replacement_word, $searchset[$i]['post_subject']) : $topic_title;
968 $message = preg_replace($orig_word, $replacement_word, $message);
972 $post_subject = ( $searchset[$i]['post_subject'] != '' ) ? $searchset[$i]['post_subject'] : $topic_title;
975 if ($board_config['allow_smilies'] && $searchset[$i]['enable_smilies'])
977 $message = smilies_pass($message);
980 $message = str_replace("\n", '<br />', $message);
984 $poster = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=" . $searchset[$i]['user_id']) . '">' : '';
985 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != "" ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
986 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
988 if ( $userdata['session_logged_in'] && $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
990 if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) )
992 $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
994 else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) )
996 $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
999 if ( $searchset[$i]['post_time'] > $topic_last_read )
1001 $mini_post_img = $images['icon_minipost_new'];
1002 $mini_post_alt = $lang['New_post'];
1006 $mini_post_img = $images['icon_minipost'];
1007 $mini_post_alt = $lang['Post'];
1012 $mini_post_img = $images['icon_minipost'];
1013 $mini_post_alt = $lang['Post'];
1016 $template->assign_block_vars("searchresults", array(
1017 'TOPIC_TITLE' => $topic_title,
1018 'FORUM_NAME' => $searchset[$i]['forum_name'],
1019 'POST_SUBJECT' => $post_subject,
1020 'POST_DATE' => $post_date,
1021 'POSTER_NAME' => $poster,
1022 'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
1023 'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
1024 'MESSAGE' => $message,
1025 'MINI_POST_IMG' => $mini_post_img,
1027 'L_MINI_POST_ALT' => $mini_post_alt,
1029 'U_POST' => $post_url,
1030 'U_TOPIC' => $topic_url,
1031 'U_FORUM' => $forum_url)
1038 if ( count($orig_word) )
1040 $topic_title = preg_replace($orig_word, $replacement_word, $searchset[$i]['topic_title']);
1043 $topic_type = $searchset[$i]['topic_type'];
1045 if ($topic_type == POST_ANNOUNCE)
1047 $topic_type = $lang['Topic_Announcement'] . ' ';
1049 else if ($topic_type == POST_STICKY)
1051 $topic_type = $lang['Topic_Sticky'] . ' ';
1058 if ( $searchset[$i]['topic_vote'] )
1060 $topic_type .= $lang['Topic_Poll'] . ' ';
1063 $views = $searchset[$i]['topic_views'];
1064 $replies = $searchset[$i]['topic_replies'];
1066 if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
1068 $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
1069 $goto_page = ' [ <img src="' . $images['icon_gotopost'] . '" alt="' . $lang['Goto_page'] . '" title="' . $lang['Goto_page'] . '" />' . $lang['Goto_page'] . ': ';
1072 for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
1074 $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&start=$j") . '">' . $times . '</a>';
1075 if ( $times == 1 && $total_pages > 4 )
1077 $goto_page .= ' ... ';
1078 $times = $total_pages - 3;
1079 $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
1081 else if ( $times < $total_pages )
1087 $goto_page .= ' ] ';
1094 if ( $searchset[$i]['topic_status'] == TOPIC_MOVED )
1096 $topic_type = $lang['Topic_Moved'] . ' ';
1097 $topic_id = $searchset[$i]['topic_moved_id'];
1099 $folder_image = '<img src="' . $images['folder'] . '" alt="' . $lang['No_new_posts'] . '" />';
1100 $newest_post_img = '';
1104 if ( $searchset[$i]['topic_status'] == TOPIC_LOCKED )
1106 $folder = $images['folder_locked'];
1107 $folder_new = $images['folder_locked_new'];
1109 else if ( $searchset[$i]['topic_type'] == POST_ANNOUNCE )
1111 $folder = $images['folder_announce'];
1112 $folder_new = $images['folder_announce_new'];
1114 else if ( $searchset[$i]['topic_type'] == POST_STICKY )
1116 $folder = $images['folder_sticky'];
1117 $folder_new = $images['folder_sticky_new'];
1121 if ( $replies >= $board_config['hot_threshold'] )
1123 $folder = $images['folder_hot'];
1124 $folder_new = $images['folder_hot_new'];
1128 $folder = $images['folder'];
1129 $folder_new = $images['folder_new'];
1133 if ( $userdata['session_logged_in'] )
1135 if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1137 if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1140 $unread_topics = true;
1142 if ( !empty($tracking_topics[$topic_id]) )
1144 if ( $tracking_topics[$topic_id] > $searchset[$i]['post_time'] )
1146 $unread_topics = false;
1150 if ( !empty($tracking_forums[$forum_id]) )
1152 if ( $tracking_forums[$forum_id] > $searchset[$i]['post_time'] )
1154 $unread_topics = false;
1158 if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1160 if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $searchset[$i]['post_time'] )
1162 $unread_topics = false;
1166 if ( $unread_topics )
1168 $folder_image = $folder_new;
1169 $folder_alt = $lang['New_posts'];
1171 $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> ';
1175 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1177 $folder_image = $folder;
1178 $folder_alt = $folder_alt;
1179 $newest_post_img = '';
1183 else if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1185 $folder_image = $folder_new;
1186 $folder_alt = $lang['New_posts'];
1188 $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> ';
1192 $folder_image = $folder;
1193 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1194 $newest_post_img = '';
1199 $folder_image = $folder;
1200 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1201 $newest_post_img = '';
1206 $folder_image = $folder;
1207 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1208 $newest_post_img = '';
1213 $topic_author = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '=' . $searchset[$i]['user_id']) . '">' : '';
1214 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != '' ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1216 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1218 $first_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['topic_time'], $board_config['board_timezone']);
1220 $last_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
1222 $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>';
1224 $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>';
1226 $template->assign_block_vars('searchresults', array(
1227 'FORUM_NAME' => $searchset[$i]['forum_name'],
1228 'FORUM_ID' => $forum_id,
1229 'TOPIC_ID' => $topic_id,
1230 'FOLDER' => $folder_image,
1231 'NEWEST_POST_IMG' => $newest_post_img,
1232 'TOPIC_FOLDER_IMG' => $folder_image,
1233 'GOTO_PAGE' => $goto_page,
1234 'REPLIES' => $replies,
1235 'TOPIC_TITLE' => $topic_title,
1236 'TOPIC_TYPE' => $topic_type,
1238 'TOPIC_AUTHOR' => $topic_author,
1239 'FIRST_POST_TIME' => $first_post_time,
1240 'LAST_POST_TIME' => $last_post_time,
1241 'LAST_POST_AUTHOR' => $last_post_author,
1242 'LAST_POST_IMG' => $last_post_url,
1244 'L_TOPIC_FOLDER_ALT' => $folder_alt,
1246 'U_VIEW_FORUM' => $forum_url,
1247 'U_VIEW_TOPIC' => $topic_url)
1252 $base_url = "search.$phpEx?search_id=$search_id";
1254 $template->assign_vars(array(
1255 'PAGINATION' => generate_pagination($base_url, $total_match_count, $per_page, $start),
1256 'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $per_page ) + 1 ), ceil( $total_match_count / $per_page )),
1258 'L_AUTHOR' => $lang['Author'],
1259 'L_MESSAGE' => $lang['Message'],
1260 'L_FORUM' => $lang['Forum'],
1261 'L_TOPICS' => $lang['Topics'],
1262 'L_REPLIES' => $lang['Replies'],
1263 'L_VIEWS' => $lang['Views'],
1264 'L_POSTS' => $lang['Posts'],
1265 'L_LASTPOST' => $lang['Last_Post'],
1266 'L_POSTED' => $lang['Posted'],
1267 'L_SUBJECT' => $lang['Subject'],
1269 'L_GOTO_PAGE' => $lang['Goto_page'])
1272 $template->pparse('body');
1274 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1278 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
1285 $sql = "SELECT c.cat_title, c.cat_id, f.forum_name, f.forum_id
1286 FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
1287 WHERE f.cat_id = c.cat_id
1288 ORDER BY c.cat_id, f.forum_order";
1289 $result = $db->sql_query($sql);
1292 message_die(GENERAL_ERROR, 'Could not obtain forum_name/forum_id', '', __LINE__, __FILE__, $sql);
1295 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
1298 while( $row = $db->sql_fetchrow($result) )
1300 if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
1302 $s_forums .= '<option value="' . $row['forum_id'] . '">' . $row['forum_name'] . '</option>';
1303 if ( empty($list_cat[$row['cat_id']]) )
1305 $list_cat[$row['cat_id']] = $row['cat_title'];
1310 if ( $s_forums != '' )
1312 $s_forums = '<option value="-1">' . $lang['All_available'] . '</option>' . $s_forums;
1315 // Category to search
1317 $s_categories = '<option value="-1">' . $lang['All_available'] . '</option>';
1318 while( list($cat_id, $cat_title) = @each($list_cat))
1320 $s_categories .= '<option value="' . $cat_id . '">' . $cat_title . '</option>';
1325 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
1329 // Number of chars returned
1331 $s_characters = '<option value="-1">' . $lang['All_available'] . '</option>';
1332 $s_characters .= '<option value="0">0</option>';
1333 $s_characters .= '<option value="25">25</option>';
1334 $s_characters .= '<option value="50">50</option>';
1336 for($i = 100; $i < 1100 ; $i += 100)
1338 $selected = ( $i == 200 ) ? ' selected="selected"' : '';
1339 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1346 for($i = 0; $i < count($sort_by_types); $i++)
1348 $s_sort_by .= '<option value="' . $i . '">' . $sort_by_types[$i] . '</option>';
1354 $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
1355 $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']);
1358 for($i = 0; $i < count($previous_days); $i++)
1360 $selected = ( $topic_days == $previous_days[$i] ) ? ' selected="selected"' : '';
1361 $s_time .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
1365 // Output the basic page
1367 $page_title = $lang['Search'];
1368 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1370 $template->set_filenames(array(
1371 'body' => 'search_body.tpl')
1373 make_jumpbox('viewforum.'.$phpEx);
1375 $template->assign_vars(array(
1376 'L_SEARCH_QUERY' => $lang['Search_query'],
1377 'L_SEARCH_OPTIONS' => $lang['Search_options'],
1378 'L_SEARCH_KEYWORDS' => $lang['Search_keywords'],
1379 'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'],
1380 'L_SEARCH_AUTHOR' => $lang['Search_author'],
1381 'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'],
1382 'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
1383 'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'],
1384 'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'],
1385 'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'],
1386 'L_CATEGORY' => $lang['Category'],
1387 'L_RETURN_FIRST' => $lang['Return_first'],
1388 'L_CHARACTERS' => $lang['characters_posts'],
1389 'L_SORT_BY' => $lang['Sort_by'],
1390 'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
1391 'L_SORT_DESCENDING' => $lang['Sort_Descending'],
1392 'L_SEARCH_PREVIOUS' => $lang['Search_previous'],
1393 'L_DISPLAY_RESULTS' => $lang['Display_results'],
1394 'L_FORUM' => $lang['Forum'],
1395 'L_TOPICS' => $lang['Topics'],
1396 'L_POSTS' => $lang['Posts'],
1398 'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=results"),
1399 'S_CHARACTER_OPTIONS' => $s_characters,
1400 'S_FORUM_OPTIONS' => $s_forums,
1401 'S_CATEGORY_OPTIONS' => $s_categories,
1402 'S_TIME_OPTIONS' => $s_time,
1403 'S_SORT_OPTIONS' => $s_sort_by,
1404 'S_HIDDEN_FIELDS' => '')
1407 $template->pparse('body');
1409 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);