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.21 2006/12/16 13:11:25 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;
121 $start = ($start < 0) ? 0 : $start;
123 $sort_by_types = array($lang['Sort_Time'], $lang['Sort_Post_Subject'], $lang['Sort_Topic_Title'], $lang['Sort_Author'], $lang['Sort_Forum']);
126 // encoding match for workaround
128 $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
133 if ( $mode == 'searchuser' )
136 // This handles the simple windowed user search functions called from various other scripts
138 if ( isset($HTTP_POST_VARS['search_username']) )
140 username_search($HTTP_POST_VARS['search_username']);
149 else if ( $search_keywords != '' || $search_author != '' || $search_id )
151 $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'sort_dir', 'show_results', 'return_chars');
152 $search_results = '';
155 // Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
157 $current_time = time();
160 // Cycle through options ...
162 if ( $search_id == 'newposts' || $search_id == 'egosearch' || $search_id == 'unanswered' || $search_keywords != '' || $search_author != '' )
167 $where_sql = ($userdata['user_id'] == ANONYMOUS) ? "se.session_ip = '$user_ip'" : 'se.session_user_id = ' . $userdata['user_id'];
168 $sql = 'SELECT MAX(sr.search_time) AS last_search_time
169 FROM ' . SEARCH_TABLE . ' sr, ' . SESSIONS_TABLE . " se
170 WHERE sr.session_id = se.session_id
172 if ($result = $db->sql_query($sql))
174 if ($row = $db->sql_fetchrow($result))
176 if (intval($row['last_search_time']) > 0 && ($current_time - intval($row['last_search_time'])) < intval($board_config['search_flood_interval']))
178 message_die(GENERAL_MESSAGE, $lang['Search_Flood_Error']);
182 if ( $search_id == 'newposts' || $search_id == 'egosearch' || ( $search_author != '' && $search_keywords == '' ) )
184 if ( $search_id == 'newposts' )
186 if ( $userdata['session_logged_in'] )
188 $sql = "SELECT post_id
189 FROM " . POSTS_TABLE . "
190 WHERE post_time >= " . $userdata['user_lastvisit'];
194 redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=newposts", true));
197 $show_results = 'topics';
201 else if ( $search_id == 'egosearch' )
203 if ( $userdata['session_logged_in'] )
205 $sql = "SELECT post_id
206 FROM " . POSTS_TABLE . "
207 WHERE poster_id = " . $userdata['user_id'];
211 redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=egosearch", true));
214 $show_results = 'topics';
220 $search_author = str_replace('*', '%', trim($search_author));
222 if( ( strpos($search_author, '%') !== false ) && ( strlen(str_replace('%', '', $search_author)) < $board_config['search_min_chars'] ) )
227 $sql = "SELECT user_id
228 FROM " . USERS_TABLE . "
229 WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
230 if ( !($result = $db->sql_query($sql)) )
232 message_die(GENERAL_ERROR, "Couldn't obtain list of matching users (searching for: $search_author)", "", __LINE__, __FILE__, $sql);
235 $matching_userids = '';
236 if ( $row = $db->sql_fetchrow($result) )
240 $matching_userids .= ( ( $matching_userids != '' ) ? ', ' : '' ) . $row['user_id'];
242 while( $row = $db->sql_fetchrow($result) );
246 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
249 $sql = "SELECT post_id
250 FROM " . POSTS_TABLE . "
251 WHERE poster_id IN ($matching_userids)";
255 $sql .= " AND post_time >= " . $search_time;
259 if ( !($result = $db->sql_query($sql)) )
261 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
264 $search_ids = array();
265 while( $row = $db->sql_fetchrow($result) )
267 $search_ids[] = $row['post_id'];
269 $db->sql_freeresult($result);
271 $total_match_count = count($search_ids);
274 else if ( $search_keywords != '' )
276 $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt');
277 $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt');
279 $split_search = array();
280 $stripped_keywords = stripslashes($search_keywords);
281 $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ? split_words(clean_words('search', $stripped_keywords, $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords);
282 unset($stripped_keywords);
284 $search_msg_only = ( !$search_fields ) ? "AND m.title_match = 0" : ( ( strstr($multibyte_charset, $lang['ENCODING']) ) ? '' : '' );
287 $current_match_type = 'or';
289 $word_match = array();
290 $result_list = array();
292 for($i = 0; $i < count($split_search); $i++)
294 if ( strlen(str_replace(array('*', '%'), '', trim($split_search[$i]))) < $board_config['search_min_chars'] )
296 $split_search[$i] = '';
300 switch ( $split_search[$i] )
303 $current_match_type = 'and';
307 $current_match_type = 'or';
311 $current_match_type = 'not';
315 if ( !empty($search_terms) )
317 $current_match_type = 'and';
320 if ( !strstr($multibyte_charset, $lang['ENCODING']) )
322 $match_word = str_replace('*', '%', $split_search[$i]);
323 $sql = "SELECT m.post_id
324 FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m
325 WHERE w.word_text LIKE '$match_word'
326 AND m.word_id = w.word_id
327 AND w.word_common <> 1
332 $match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
333 $search_msg_only = ( $search_fields ) ? "OR post_subject LIKE '$match_word'" : '';
334 $sql = "SELECT post_id
335 FROM " . POSTS_TEXT_TABLE . "
336 WHERE post_text LIKE '$match_word'
339 if ( !($result = $db->sql_query($sql)) )
341 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
345 while( $temp_row = $db->sql_fetchrow($result) )
347 $row[$temp_row['post_id']] = 1;
351 $result_list[$temp_row['post_id']] = 1;
353 else if ( $current_match_type == 'or' )
355 $result_list[$temp_row['post_id']] = 1;
357 else if ( $current_match_type == 'not' )
359 $result_list[$temp_row['post_id']] = 0;
363 if ( $current_match_type == 'and' && $word_count )
365 @reset($result_list);
366 while( list($post_id, $match_count) = @each($result_list) )
368 if ( !$row[$post_id] )
370 $result_list[$post_id] = 0;
377 $db->sql_freeresult($result);
381 @reset($result_list);
383 $search_ids = array();
384 while( list($post_id, $matches) = each($result_list) )
388 $search_ids[] = $post_id;
393 $total_match_count = count($search_ids);
397 // If user is logged in then we'll check to see which (if any) private
398 // forums they are allowed to view and include them in the search.
400 // If not logged in we explicitly prevent searching of private forums
403 if ( $search_forum != -1 )
405 $is_auth = auth(AUTH_READ, $search_forum, $userdata);
407 if ( !$is_auth['auth_read'] )
409 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
412 $auth_sql = "f.forum_id = $search_forum";
416 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
418 if ( $search_cat != -1 )
420 $auth_sql = "f.cat_id = $search_cat";
423 $ignore_forum_sql = '';
424 while( list($key, $value) = each($is_auth_ary) )
426 if ( !$value['auth_read'] )
428 $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
432 if ( $ignore_forum_sql != '' )
434 $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
439 // Author name search
441 if ( $search_author != '' )
443 $search_author = str_replace('*', '%', trim($search_author));
445 if( ( strpos($search_author, '%') !== false ) && ( strlen(str_replace('%', '', $search_author)) < $board_config['search_min_chars'] ) )
451 if ( $total_match_count )
453 if ( $show_results == 'topics' )
456 // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
458 $search_id_chunks = array();
462 if (count($search_ids) > $limiter)
464 for ($i = 0; $i < count($search_ids); $i++)
466 if ($count == $limiter)
472 $search_id_chunks[$chunk][$count] = $search_ids[$i];
478 $search_id_chunks[0] = $search_ids;
481 $search_ids = array();
483 for ($i = 0; $i < count($search_id_chunks); $i++)
489 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
492 if ( $search_author == '' && $auth_sql == '' )
494 $sql = "SELECT topic_id
495 FROM " . POSTS_TABLE . "
496 WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
502 $from_sql = POSTS_TABLE . " p";
504 if ( $search_author != '' )
506 $from_sql .= ", " . USERS_TABLE . " u";
507 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
510 if ( $auth_sql != '' )
512 $from_sql .= ", " . FORUMS_TABLE . " f";
513 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
516 $sql = "SELECT p.topic_id
518 WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
520 GROUP BY p.topic_id";
523 if ( !($result = $db->sql_query($sql)) )
525 message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
528 while ($row = $db->sql_fetchrow($result))
530 $search_ids[] = $row['topic_id'];
532 $db->sql_freeresult($result);
535 $total_match_count = sizeof($search_ids);
538 else if ( $search_author != '' || $search_time || $auth_sql != '' )
540 $search_id_chunks = array();
544 if (count($search_ids) > $limiter)
546 for ($i = 0; $i < count($search_ids); $i++)
548 if ($count == $limiter)
554 $search_id_chunks[$chunk][$count] = $search_ids[$i];
560 $search_id_chunks[0] = $search_ids;
563 $search_ids = array();
565 for ($i = 0; $i < count($search_id_chunks); $i++)
567 $where_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
568 $select_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id' : 'p.post_id';
569 $from_sql = ( $search_author == '' && $auth_sql == '' ) ? POSTS_TABLE : POSTS_TABLE . ' p';
573 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
576 if ( $auth_sql != '' )
578 $from_sql .= ", " . FORUMS_TABLE . " f";
579 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
582 if ( $search_author != '' )
584 $from_sql .= ", " . USERS_TABLE . " u";
585 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
588 $sql = "SELECT " . $select_sql . "
591 if ( !($result = $db->sql_query($sql)) )
593 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
596 while( $row = $db->sql_fetchrow($result) )
598 $search_ids[] = $row['post_id'];
600 $db->sql_freeresult($result);
603 $total_match_count = count($search_ids);
606 else if ( $search_id == 'unanswered' )
608 if ( $auth_sql != '' )
610 $sql = "SELECT t.topic_id, f.forum_id
611 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
612 WHERE t.topic_replies = 0
613 AND t.forum_id = f.forum_id
614 AND t.topic_moved_id = 0
619 $sql = "SELECT topic_id
620 FROM " . TOPICS_TABLE . "
621 WHERE topic_replies = 0
622 AND topic_moved_id = 0";
625 if ( !($result = $db->sql_query($sql)) )
627 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
630 $search_ids = array();
631 while( $row = $db->sql_fetchrow($result) )
633 $search_ids[] = $row['topic_id'];
635 $db->sql_freeresult($result);
637 $total_match_count = count($search_ids);
640 // Basic requirements
642 $show_results = 'topics';
648 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
652 // Delete old data from the search result table
654 $sql = 'DELETE FROM ' . SEARCH_TABLE . '
655 WHERE search_time < ' . ($current_time - (int) $board_config['session_length']);
656 if ( !$result = $db->sql_query($sql) )
658 message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
662 // Store new result data
664 $search_results = implode(', ', $search_ids);
665 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
668 // Combine both results and search data (apart from original query)
669 // so we can serialize it and place it in the DB
671 $store_search_data = array();
674 // Limit the character length (and with this the results displayed at all following pages) to prevent
675 // truncated result arrays. Normally, search results above 12000 are affected.
676 // - to include or not to include
678 $max_result_length = 60000;
679 if (strlen($search_results) > $max_result_length)
681 $search_results = substr($search_results, 0, $max_result_length);
682 $search_results = substr($search_results, 0, strrpos($search_results, ','));
683 $total_match_count = count(explode(', ', $search_results));
687 for($i = 0; $i < count($store_vars); $i++)
689 $store_search_data[$store_vars[$i]] = $$store_vars[$i];
692 $result_array = serialize($store_search_data);
693 unset($store_search_data);
695 mt_srand ((double) microtime() * 1000000);
696 $search_id = mt_rand();
698 $sql = "UPDATE " . SEARCH_TABLE . "
699 SET search_id = $search_id, search_time = $current_time, search_array = '" . str_replace("\'", "''", $result_array) . "'
700 WHERE session_id = '" . $userdata['session_id'] . "'";
701 if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
703 $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_time, search_array)
704 VALUES($search_id, '" . $userdata['session_id'] . "', $current_time, '" . str_replace("\'", "''", $result_array) . "')";
705 if ( !($result = $db->sql_query($sql)) )
707 message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
713 $search_id = intval($search_id);
716 $sql = "SELECT search_array
717 FROM " . SEARCH_TABLE . "
718 WHERE search_id = $search_id
719 AND session_id = '". $userdata['session_id'] . "'";
720 if ( !($result = $db->sql_query($sql)) )
722 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
725 if ( $row = $db->sql_fetchrow($result) )
727 $search_data = unserialize($row['search_array']);
728 for($i = 0; $i < count($store_vars); $i++)
730 $$store_vars[$i] = $search_data[$store_vars[$i]];
739 if ( $search_results != '' )
741 if ( $show_results == 'posts' )
743 $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
744 FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt
745 WHERE p.post_id IN ($search_results)
746 AND pt.post_id = p.post_id
747 AND f.forum_id = p.forum_id
748 AND p.topic_id = t.topic_id
749 AND p.poster_id = u.user_id";
753 $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
754 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
755 WHERE t.topic_id IN ($search_results)
756 AND t.topic_poster = u.user_id
757 AND f.forum_id = t.forum_id
758 AND p.post_id = t.topic_first_post_id
759 AND p2.post_id = t.topic_last_post_id
760 AND u2.user_id = p2.poster_id";
763 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
765 $sql .= " ORDER BY ";
769 $sql .= ( $show_results == 'posts' ) ? 'pt.post_subject' : 't.topic_title';
772 $sql .= 't.topic_title';
775 $sql .= 'u.username';
778 $sql .= 'f.forum_id';
781 $sql .= ( $show_results == 'posts' ) ? 'p.post_time' : 'p2.post_time';
784 $sql .= " $sort_dir LIMIT $start, " . $per_page;
786 if ( !$result = $db->sql_query($sql) )
788 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
791 $searchset = array();
792 while( $row = $db->sql_fetchrow($result) )
797 $db->sql_freeresult($result);
800 // Define censored word matches
802 $orig_word = array();
803 $replacement_word = array();
804 obtain_word_list($orig_word, $replacement_word);
809 $page_title = $lang['Search'];
810 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
812 if ( $show_results == 'posts' )
814 $template->set_filenames(array(
815 'body' => 'search_results_posts.tpl')
820 $template->set_filenames(array(
821 'body' => 'search_results_topics.tpl')
824 make_jumpbox('viewforum.'.$phpEx);
826 $l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
828 $template->assign_vars(array(
829 'L_SEARCH_MATCHES' => $l_search_matches,
830 'L_TOPIC' => $lang['Topic'])
833 $highlight_active = '';
834 $highlight_match = array();
835 for($j = 0; $j < count($split_search); $j++ )
837 $split_word = $split_search[$j];
839 if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
841 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
842 $highlight_active .= " " . $split_word;
844 for ($k = 0; $k < count($synonym_array); $k++)
846 list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k])));
848 if ( $replace_synonym == $split_word )
850 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
851 $highlight_active .= ' ' . $match_synonym;
857 $highlight_active = urlencode(trim($highlight_active));
859 $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
860 $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
862 for($i = 0; $i < count($searchset); $i++)
864 $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
865 $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "&highlight=$highlight_active");
866 $post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
868 $post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
870 $message = $searchset[$i]['post_text'];
871 $topic_title = $searchset[$i]['topic_title'];
873 $forum_id = $searchset[$i]['forum_id'];
874 $topic_id = $searchset[$i]['topic_id'];
876 if ( $show_results == 'posts' )
878 if ( isset($return_chars) )
880 $bbcode_uid = $searchset[$i]['bbcode_uid'];
883 // If the board has HTML off but the post has HTML
884 // on then we process it, else leave it alone
886 if ( $return_chars != -1 )
888 $message = strip_tags($message);
889 $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
890 $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
891 $message = ( strlen($message) > $return_chars ) ? substr($message, 0, $return_chars) . ' ...' : $message;
895 if ( !$board_config['allow_html'] )
897 if ( $postrow[$i]['enable_html'] )
899 $message = preg_replace('#(<)([\/]?.*?)(>)#is', '<\\2>', $message);
903 if ( $bbcode_uid != '' )
905 $message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
908 $message = make_clickable($message);
910 if ( $highlight_active )
912 if ( preg_match('/<.*>/', $message) )
914 $message = preg_replace($highlight_match, '<!-- #sh -->\1<!-- #eh -->', $message);
919 $message = ' ' . $message . ' ';
921 while( $start_html = strpos($message, '<', $start_html) )
923 $grab_length = $start_html - $end_html - 1;
924 $temp_message .= substr($message, $end_html + 1, $grab_length);
926 if ( $end_html = strpos($message, '>', $start_html) )
928 $length = $end_html - $start_html + 1;
929 $hold_string = substr($message, $start_html, $length);
931 if ( strrpos(' ' . $hold_string, '<') != 1 )
933 $end_html = $start_html + 1;
936 while ( $end_counter && $end_html < strlen($message) )
938 if ( substr($message, $end_html, 1) == '>' )
942 else if ( substr($message, $end_html, 1) == '<' )
950 $length = $end_html - $start_html + 1;
951 $hold_string = substr($message, $start_html, $length);
952 $hold_string = str_replace('<!-- #sh -->', '', $hold_string);
953 $hold_string = str_replace('<!-- #eh -->', '', $hold_string);
955 else if ( $hold_string == '<!-- #sh -->' )
957 $hold_string = str_replace('<!-- #sh -->', '<span style="color:#' . $theme['fontcolor3'] . '"><b>', $hold_string);
959 else if ( $hold_string == '<!-- #eh -->' )
961 $hold_string = str_replace('<!-- #eh -->', '</b></span>', $hold_string);
964 $temp_message .= $hold_string;
966 $start_html += $length;
970 $start_html = strlen($message);
974 $grab_length = strlen($message) - $end_html - 1;
975 $temp_message .= substr($message, $end_html + 1, $grab_length);
977 $message = trim($temp_message);
981 $message = preg_replace($highlight_match, '<span style="color:#' . $theme['fontcolor3'] . '"><b>\1</b></span>', $message);
986 if ( count($orig_word) )
988 $topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
989 $post_subject = ( $searchset[$i]['post_subject'] != "" ) ? preg_replace($orig_word, $replacement_word, $searchset[$i]['post_subject']) : $topic_title;
991 $message = preg_replace($orig_word, $replacement_word, $message);
995 $post_subject = ( $searchset[$i]['post_subject'] != '' ) ? $searchset[$i]['post_subject'] : $topic_title;
998 if ($board_config['allow_smilies'] && $searchset[$i]['enable_smilies'])
1000 $message = smilies_pass($message);
1003 $message = str_replace("\n", '<br />', $message);
1007 $poster = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=" . $searchset[$i]['user_id']) . '">' : '';
1008 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != "" ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1009 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1011 if ( $userdata['session_logged_in'] && $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1013 if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) )
1015 $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1017 else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) )
1019 $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1022 if ( $searchset[$i]['post_time'] > $topic_last_read )
1024 $mini_post_img = $images['icon_minipost_new'];
1025 $mini_post_alt = $lang['New_post'];
1029 $mini_post_img = $images['icon_minipost'];
1030 $mini_post_alt = $lang['Post'];
1035 $mini_post_img = $images['icon_minipost'];
1036 $mini_post_alt = $lang['Post'];
1039 $template->assign_block_vars("searchresults", array(
1040 'TOPIC_TITLE' => $topic_title,
1041 'FORUM_NAME' => $searchset[$i]['forum_name'],
1042 'POST_SUBJECT' => $post_subject,
1043 'POST_DATE' => $post_date,
1044 'POSTER_NAME' => $poster,
1045 'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
1046 'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
1047 'MESSAGE' => $message,
1048 'MINI_POST_IMG' => $mini_post_img,
1050 'L_MINI_POST_ALT' => $mini_post_alt,
1052 'U_POST' => $post_url,
1053 'U_TOPIC' => $topic_url,
1054 'U_FORUM' => $forum_url)
1061 if ( count($orig_word) )
1063 $topic_title = preg_replace($orig_word, $replacement_word, $searchset[$i]['topic_title']);
1066 $topic_type = $searchset[$i]['topic_type'];
1068 if ($topic_type == POST_ANNOUNCE)
1070 $topic_type = $lang['Topic_Announcement'] . ' ';
1072 else if ($topic_type == POST_STICKY)
1074 $topic_type = $lang['Topic_Sticky'] . ' ';
1081 if ( $searchset[$i]['topic_vote'] )
1083 $topic_type .= $lang['Topic_Poll'] . ' ';
1086 $views = $searchset[$i]['topic_views'];
1087 $replies = $searchset[$i]['topic_replies'];
1089 if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
1091 $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
1092 $goto_page = ' [ <img src="' . $images['icon_gotopost'] . '" alt="' . $lang['Goto_page'] . '" title="' . $lang['Goto_page'] . '" />' . $lang['Goto_page'] . ': ';
1095 for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
1097 $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&start=$j") . '">' . $times . '</a>';
1098 if ( $times == 1 && $total_pages > 4 )
1100 $goto_page .= ' ... ';
1101 $times = $total_pages - 3;
1102 $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
1104 else if ( $times < $total_pages )
1110 $goto_page .= ' ] ';
1117 if ( $searchset[$i]['topic_status'] == TOPIC_MOVED )
1119 $topic_type = $lang['Topic_Moved'] . ' ';
1120 $topic_id = $searchset[$i]['topic_moved_id'];
1122 $folder_image = '<img src="' . $images['folder'] . '" alt="' . $lang['No_new_posts'] . '" />';
1123 $newest_post_img = '';
1127 if ( $searchset[$i]['topic_status'] == TOPIC_LOCKED )
1129 $folder = $images['folder_locked'];
1130 $folder_new = $images['folder_locked_new'];
1132 else if ( $searchset[$i]['topic_type'] == POST_ANNOUNCE )
1134 $folder = $images['folder_announce'];
1135 $folder_new = $images['folder_announce_new'];
1137 else if ( $searchset[$i]['topic_type'] == POST_STICKY )
1139 $folder = $images['folder_sticky'];
1140 $folder_new = $images['folder_sticky_new'];
1144 if ( $replies >= $board_config['hot_threshold'] )
1146 $folder = $images['folder_hot'];
1147 $folder_new = $images['folder_hot_new'];
1151 $folder = $images['folder'];
1152 $folder_new = $images['folder_new'];
1156 if ( $userdata['session_logged_in'] )
1158 if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1160 if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1163 $unread_topics = true;
1165 if ( !empty($tracking_topics[$topic_id]) )
1167 if ( $tracking_topics[$topic_id] > $searchset[$i]['post_time'] )
1169 $unread_topics = false;
1173 if ( !empty($tracking_forums[$forum_id]) )
1175 if ( $tracking_forums[$forum_id] > $searchset[$i]['post_time'] )
1177 $unread_topics = false;
1181 if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1183 if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $searchset[$i]['post_time'] )
1185 $unread_topics = false;
1189 if ( $unread_topics )
1191 $folder_image = $folder_new;
1192 $folder_alt = $lang['New_posts'];
1194 $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> ';
1198 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1200 $folder_image = $folder;
1201 $folder_alt = $folder_alt;
1202 $newest_post_img = '';
1206 else if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1208 $folder_image = $folder_new;
1209 $folder_alt = $lang['New_posts'];
1211 $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> ';
1215 $folder_image = $folder;
1216 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1217 $newest_post_img = '';
1222 $folder_image = $folder;
1223 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1224 $newest_post_img = '';
1229 $folder_image = $folder;
1230 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1231 $newest_post_img = '';
1236 $topic_author = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '=' . $searchset[$i]['user_id']) . '">' : '';
1237 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != '' ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1239 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1241 $first_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['topic_time'], $board_config['board_timezone']);
1243 $last_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
1245 $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>';
1247 $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>';
1249 $template->assign_block_vars('searchresults', array(
1250 'FORUM_NAME' => $searchset[$i]['forum_name'],
1251 'FORUM_ID' => $forum_id,
1252 'TOPIC_ID' => $topic_id,
1253 'FOLDER' => $folder_image,
1254 'NEWEST_POST_IMG' => $newest_post_img,
1255 'TOPIC_FOLDER_IMG' => $folder_image,
1256 'GOTO_PAGE' => $goto_page,
1257 'REPLIES' => $replies,
1258 'TOPIC_TITLE' => $topic_title,
1259 'TOPIC_TYPE' => $topic_type,
1261 'TOPIC_AUTHOR' => $topic_author,
1262 'FIRST_POST_TIME' => $first_post_time,
1263 'LAST_POST_TIME' => $last_post_time,
1264 'LAST_POST_AUTHOR' => $last_post_author,
1265 'LAST_POST_IMG' => $last_post_url,
1267 'L_TOPIC_FOLDER_ALT' => $folder_alt,
1269 'U_VIEW_FORUM' => $forum_url,
1270 'U_VIEW_TOPIC' => $topic_url)
1275 $base_url = "search.$phpEx?search_id=$search_id";
1277 $template->assign_vars(array(
1278 'PAGINATION' => generate_pagination($base_url, $total_match_count, $per_page, $start),
1279 'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $per_page ) + 1 ), ceil( $total_match_count / $per_page )),
1281 'L_AUTHOR' => $lang['Author'],
1282 'L_MESSAGE' => $lang['Message'],
1283 'L_FORUM' => $lang['Forum'],
1284 'L_TOPICS' => $lang['Topics'],
1285 'L_REPLIES' => $lang['Replies'],
1286 'L_VIEWS' => $lang['Views'],
1287 'L_POSTS' => $lang['Posts'],
1288 'L_LASTPOST' => $lang['Last_Post'],
1289 'L_POSTED' => $lang['Posted'],
1290 'L_SUBJECT' => $lang['Subject'],
1292 'L_GOTO_PAGE' => $lang['Goto_page'])
1295 $template->pparse('body');
1297 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1301 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
1308 $sql = "SELECT c.cat_title, c.cat_id, f.forum_name, f.forum_id
1309 FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
1310 WHERE f.cat_id = c.cat_id
1311 ORDER BY c.cat_order, f.forum_order";
1312 $result = $db->sql_query($sql);
1315 message_die(GENERAL_ERROR, 'Could not obtain forum_name/forum_id', '', __LINE__, __FILE__, $sql);
1318 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
1321 while( $row = $db->sql_fetchrow($result) )
1323 if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
1325 $s_forums .= '<option value="' . $row['forum_id'] . '">' . $row['forum_name'] . '</option>';
1326 if ( empty($list_cat[$row['cat_id']]) )
1328 $list_cat[$row['cat_id']] = $row['cat_title'];
1333 if ( $s_forums != '' )
1335 $s_forums = '<option value="-1">' . $lang['All_available'] . '</option>' . $s_forums;
1338 // Category to search
1340 $s_categories = '<option value="-1">' . $lang['All_available'] . '</option>';
1341 while( list($cat_id, $cat_title) = @each($list_cat))
1343 $s_categories .= '<option value="' . $cat_id . '">' . $cat_title . '</option>';
1348 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
1352 // Number of chars returned
1354 $s_characters = '<option value="-1">' . $lang['All_available'] . '</option>';
1355 $s_characters .= '<option value="0">0</option>';
1356 $s_characters .= '<option value="25">25</option>';
1357 $s_characters .= '<option value="50">50</option>';
1359 for($i = 100; $i < 1100 ; $i += 100)
1361 $selected = ( $i == 200 ) ? ' selected="selected"' : '';
1362 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1369 for($i = 0; $i < count($sort_by_types); $i++)
1371 $s_sort_by .= '<option value="' . $i . '">' . $sort_by_types[$i] . '</option>';
1377 $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
1378 $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']);
1381 for($i = 0; $i < count($previous_days); $i++)
1383 $selected = ( $topic_days == $previous_days[$i] ) ? ' selected="selected"' : '';
1384 $s_time .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
1388 // Output the basic page
1390 $page_title = $lang['Search'];
1391 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1393 $template->set_filenames(array(
1394 'body' => 'search_body.tpl')
1396 make_jumpbox('viewforum.'.$phpEx);
1398 $template->assign_vars(array(
1399 'L_SEARCH_QUERY' => $lang['Search_query'],
1400 'L_SEARCH_OPTIONS' => $lang['Search_options'],
1401 'L_SEARCH_KEYWORDS' => $lang['Search_keywords'],
1402 'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'],
1403 'L_SEARCH_AUTHOR' => $lang['Search_author'],
1404 'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'],
1405 'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
1406 'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'],
1407 'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'],
1408 'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'],
1409 'L_CATEGORY' => $lang['Category'],
1410 'L_RETURN_FIRST' => $lang['Return_first'],
1411 'L_CHARACTERS' => $lang['characters_posts'],
1412 'L_SORT_BY' => $lang['Sort_by'],
1413 'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
1414 'L_SORT_DESCENDING' => $lang['Sort_Descending'],
1415 'L_SEARCH_PREVIOUS' => $lang['Search_previous'],
1416 'L_DISPLAY_RESULTS' => $lang['Display_results'],
1417 'L_FORUM' => $lang['Forum'],
1418 'L_TOPICS' => $lang['Topics'],
1419 'L_POSTS' => $lang['Posts'],
1421 'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=results"),
1422 'S_CHARACTER_OPTIONS' => $s_characters,
1423 'S_FORUM_OPTIONS' => $s_forums,
1424 'S_CATEGORY_OPTIONS' => $s_categories,
1425 'S_TIME_OPTIONS' => $s_time,
1426 'S_SORT_OPTIONS' => $s_sort_by,
1427 'S_HIDDEN_FIELDS' => '')
1430 $template->pparse('body');
1432 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);