hopefully more suitable site logo for subsilver2
[phpbb.git] / phpBB / search.php
blobed1b43fd9e294d3d0e1ac997c579d0771d6c903c
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 define('IN_PHPBB', true);
15 $phpbb_root_path = './';
16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
17 include($phpbb_root_path . 'common.' . $phpEx);
19 // Start session management
20 $user->session_begin();
21 $auth->acl($user->data);
22 $user->setup('search');
24 // Define initial vars
25 $mode = request_var('mode', '');
26 $search_id = request_var('search_id', '');
27 $start = max(request_var('start', 0), 0);
28 $post_id = request_var('p', 0);
29 $topic_id = request_var('t', 0);
30 $view = request_var('view', '');
32 $submit = request_var('submit', false);
33 $keywords = request_var('keywords', '', true);
34 $add_keywords = request_var('add_keywords', '', true);
35 $author = request_var('author', '');
36 $author_id = request_var('author_id', 0);
37 $show_results = ($topic_id) ? 'posts' : request_var('sr', 'posts');
38 $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
39 $search_terms = request_var('terms', 'all');
40 $search_fields = request_var('sf', 'all');
41 $search_child = request_var('sc', true);
43 $sort_days = request_var('st', 0);
44 $sort_key = request_var('sk', 't');
45 $sort_dir = request_var('sd', 'd');
47 $return_chars = request_var('ch', ($topic_id) ? -1 : 200);
48 $search_forum = request_var('fid', array(0));
50 // Is user able to search? Has search been disabled?
51 if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
53 trigger_error($user->lang['NO_SEARCH']);
56 // Check search load limit
57 if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load'])))
59 trigger_error($user->lang['NO_SEARCH_TIME']);
62 // Check flood limit ... if applicable
63 $interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];
64 if ($interval && !$auth->acl_get('u_ignoreflood'))
66 if ($user->data['user_last_search'] > time() - $interval)
68 trigger_error($user->lang['NO_SEARCH_TIME']);
72 // Define some vars
73 $limit_days = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
74 $sort_by_text = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']);
76 $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
77 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
79 if ($keywords || $author || $author_id || $search_id || $submit)
81 // clear arrays
82 $id_ary = array();
84 // egosearch is an author search
85 if ($search_id == 'egosearch')
87 $author = $user->data['username'];
90 // If we are looking for authors get their ids
91 $author_id_ary = array();
92 if ($author_id)
94 $author_id_ary[] = $author_id;
96 else if ($author)
98 if ((strpos($author, '*') !== false) && (utf8_strlen(str_replace(array('*', '%'), '', $author)) < $config['min_search_author_chars']))
100 trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars']));
103 $sql_where = (strpos($author, '*') !== false) ? ' LIKE ' : ' = ';
104 $sql = 'SELECT user_id
105 FROM ' . USERS_TABLE . "
106 WHERE username_clean $sql_where '" . $db->sql_escape(preg_replace('#\*+#', '%', utf8_clean_string($author))) . "'
107 AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';
108 $result = $db->sql_query_limit($sql, 100);
110 while ($row = $db->sql_fetchrow($result))
112 $author_id_ary[] = (int) $row['user_id'];
114 $db->sql_freeresult($result);
116 if (!sizeof($author_id_ary))
118 trigger_error($user->lang['NO_SEARCH_RESULTS']);
122 // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode
123 // so we can keep the old keywords in their old mode, but add the new ones as required words
124 if ($add_keywords)
126 if ($search_terms == 'all')
128 $keywords .= ' ' . $add_keywords;
130 else
132 $search_terms = 'all';
133 $keywords = implode(' |', explode(' ', preg_replace('#\s+#u', ' ', $keywords))) . ' ' .$add_keywords;
137 // Which forums should not be searched? Author searches are also carried out in unindexed forums
138 if (empty($keywords) && sizeof($author_id_ary))
140 $ex_fid_ary = array_keys($auth->acl_getf('!f_read', true));
142 else
144 $ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true))));
147 $not_in_fid = (sizeof($ex_fid_ary)) ? 'WHERE ' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . " OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ')' : "";
149 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id
150 FROM ' . FORUMS_TABLE . ' f
151 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
152 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
153 $not_in_fid
154 ORDER BY f.left_id";
155 $result = $db->sql_query($sql);
157 $right_id = 0;
158 $reset_search_forum = true;
159 while ($row = $db->sql_fetchrow($result))
161 if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
163 $ex_fid_ary[] = (int) $row['forum_id'];
164 continue;
167 if (sizeof($search_forum))
169 if ($search_child)
171 if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)
173 $right_id = (int) $row['right_id'];
175 else if ($row['right_id'] < $right_id)
177 continue;
181 if (!in_array($row['forum_id'], $search_forum))
183 $ex_fid_ary[] = (int) $row['forum_id'];
184 $reset_search_forum = false;
188 $db->sql_freeresult($result);
190 // find out in which forums the user is allowed to view approved posts
191 if ($auth->acl_get('m_approve'))
193 $m_approve_fid_ary = array(-1);
194 $m_approve_fid_sql = '';
196 else if ($auth->acl_getf_global('m_approve'))
198 $m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary);
199 $m_approve_fid_sql = ' AND (p.post_approved = 1' . ((sizeof($m_approve_fid_ary)) ? ' OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) : '') . ')';
201 else
203 $m_approve_fid_ary = array();
204 $m_approve_fid_sql = ' AND p.post_approved = 1';
207 if ($reset_search_forum)
209 $search_forum = array();
212 // Select which method we'll use to obtain the post_id or topic_id information
213 $search_type = basename($config['search_type']);
215 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
217 trigger_error('NO_SUCH_SEARCH_MODULE');
220 require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
222 // We do some additional checks in the module to ensure it can actually be utilised
223 $error = false;
224 $search = new $search_type($error);
226 if ($error)
228 trigger_error($error);
231 // let the search module split up the keywords
232 if ($keywords)
234 $correct_query = $search->split_keywords($keywords, $search_terms);
235 if (!$correct_query || (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id))
237 $ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], implode(' ', $search->common_words)) . '<br />' : '';
238 trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max']));
242 if (!$keywords && sizeof($author_id_ary))
244 // if it is an author search we want to show topics by default
245 $show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts');
246 $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
249 // define some variables needed for retrieving post_id/topic_id information
250 $sort_by_sql = array('a' => 'u.username_clean', 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'p.post_subject' : 't.topic_title'));
252 // pre-made searches
253 $sql = $field = $l_search_title = '';
254 if ($search_id)
256 switch ($search_id)
258 // Oh holy Bob, bring us some activity...
259 case 'active_topics':
260 $l_search_title = $user->lang['SEARCH_ACTIVE_TOPICS'];
261 $show_results = 'topics';
262 $sort_key = 't';
263 $sort_dir = 'd';
264 $sort_days = request_var('st', 7);
265 $sort_by_sql['t'] = 't.topic_last_post_time';
267 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
268 $s_sort_key = $s_sort_dir = '';
270 $last_post_time_sql = ($sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($sort_days * 24 * 3600)) : '';
272 $sql = 'SELECT t.topic_last_post_time, t.topic_id
273 FROM ' . TOPICS_TABLE . " t
274 WHERE t.topic_moved_id = 0
275 $last_post_time_sql
276 " . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
277 ' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . '
278 ORDER BY t.topic_last_post_time DESC';
279 $field = 'topic_id';
280 break;
282 case 'unanswered':
283 $l_search_title = $user->lang['SEARCH_UNANSWERED'];
284 $show_results = request_var('sr', 'topics');
285 $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
286 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
287 $sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title';
288 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
290 $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : '';
291 $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort;
293 if ($sort_days)
295 $last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600));
297 else
299 $last_post_time = '';
302 if ($show_results == 'posts')
304 if ($sort_key == 'a')
306 $sort_join = USERS_TABLE . ' u, ';
307 $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort;
310 $sql = "SELECT p.post_id
311 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
312 WHERE t.topic_replies = 0
313 AND p.topic_id = t.topic_id
314 $last_post_time
315 $m_approve_fid_sql
316 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
317 $sql_sort";
318 $field = 'post_id';
320 else
322 $sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id
323 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
324 WHERE t.topic_replies = 0
325 AND t.topic_moved_id = 0
326 AND p.topic_id = t.topic_id
327 $last_post_time
328 $m_approve_fid_sql
329 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
330 $sql_sort";
331 $field = 'topic_id';
333 break;
335 case 'newposts':
336 $l_search_title = $user->lang['SEARCH_NEW'];
337 // force sorting
338 $show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics';
339 $sort_key = 't';
340 $sort_dir = 'd';
341 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
342 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
344 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
345 $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
347 if ($show_results == 'posts')
349 $sql = 'SELECT p.post_id
350 FROM ' . POSTS_TABLE . ' p
351 WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
352 $m_approve_fid_sql
353 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
354 $sql_sort";
355 $field = 'post_id';
357 else
359 $sql = 'SELECT t.topic_id
360 FROM ' . TOPICS_TABLE . ' t
361 WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . '
362 AND t.topic_moved_id = 0
363 ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
364 ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
365 $sql_sort";
366 $field = 'topic_id';
368 break;
370 case 'egosearch':
371 $l_search_title = $user->lang['SEARCH_SELF'];
372 break;
376 // show_results should not change after this
377 $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
378 $total_match_count = 0;
380 if ($search_id)
382 if ($sql)
384 // only return up to 1000 ids (the last one will be removed later)
385 $result = $db->sql_query_limit($sql, 1001 - $start, $start);
387 while ($row = $db->sql_fetchrow($result))
389 $id_ary[] = $row[$field];
391 $db->sql_freeresult($result);
393 $total_match_count = sizeof($id_ary) + $start;
394 $id_ary = array_slice($id_ary, 0, $per_page);
396 else
398 $search_id = '';
402 // make sure that some arrays are always in the same order
403 sort($ex_fid_ary);
404 sort($m_approve_fid_ary);
405 sort($author_id_ary);
407 if (!empty($search->search_query))
409 $total_match_count = $search->keyword_search($show_results, $search_fields, $search_terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page);
411 else if (sizeof($author_id_ary))
413 $total_match_count = $search->author_search($show_results, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page);
416 // For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
417 if (!sizeof($id_ary) && !$search_id)
419 trigger_error($user->lang['NO_SEARCH_RESULTS']);
422 $sql_where = '';
424 if (sizeof($id_ary))
426 $sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
427 $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
428 $sql_where .= ($show_results == 'posts') ? $m_approve_fid_sql : str_replace(array('p.post_approved', 'p.forum_id'), array('t.topic_approved', 't.forum_id'), $m_approve_fid_sql);
431 if ($show_results == 'posts')
433 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
435 else
437 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
440 // Grab icons
441 $icons = $cache->obtain_icons();
443 // Output header
444 if ($search_id && ($total_match_count > 1000))
446 // limit the number to 1000 for pre-made searches
447 $total_match_count--;
448 $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
450 else
452 $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count);
455 // define some vars for urls
456 $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
457 $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
458 $u_show_results = ($show_results != 'posts') ? '&amp;sr=' . $show_results : '';
459 $u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
461 $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
462 $u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
463 $u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($search->search_query)) : '';
464 $u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
465 $u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
466 $u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
467 $u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
468 $u_search .= (!$search_child) ? '&amp;sc=0' : '';
469 $u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
470 $u_search .= ($return_chars != 200) ? '&amp;ch=' . $return_chars : '';
472 $template->assign_vars(array(
473 'SEARCH_TITLE' => $l_search_title,
474 'SEARCH_MATCHES' => $l_search_matches,
475 'SEARCH_WORDS' => $search->search_query,
476 'IGNORED_WORDS' => (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
477 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
478 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),
479 'TOTAL_MATCHES' => $total_match_count,
480 'SEARCH_IN_RESULTS' => ($search_id) ? false : true,
482 'S_SELECT_SORT_DIR' => $s_sort_dir,
483 'S_SELECT_SORT_KEY' => $s_sort_key,
484 'S_SELECT_SORT_DAYS' => $s_limit_days,
485 'S_SEARCH_ACTION' => $u_search,
486 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true,
488 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'),
489 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
490 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
491 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
492 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
494 'U_SEARCH_WORDS' => $u_search,
497 if ($sql_where)
499 if ($show_results == 'posts')
501 // @todo Joining this query to the one below?
502 $sql = 'SELECT zebra_id, friend, foe
503 FROM ' . ZEBRA_TABLE . '
504 WHERE user_id = ' . $user->data['user_id'];
505 $result = $db->sql_query($sql);
507 $zebra = array();
508 while ($row = $db->sql_fetchrow($result))
510 $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
512 $db->sql_freeresult($result);
514 $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.username_clean, u.user_sig, u.user_sig_bbcode_uid, u.user_colour
515 FROM ' . POSTS_TABLE . ' p
516 LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
517 LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
518 LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
519 WHERE $sql_where";
521 else
523 $sql_from = TOPICS_TABLE . ' t
524 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
525 ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
526 $sql_select = 't.*, f.forum_id, f.forum_name';
528 if ($user->data['is_registered'])
530 if ($config['load_db_track'])
532 $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
533 AND t.topic_id = tp.topic_id)';
534 $sql_select .= ', tp.topic_posted';
537 if ($config['load_db_lastread'])
539 $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
540 AND t.topic_id = tt.topic_id)
541 LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
542 AND ft.forum_id = f.forum_id)';
543 $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
547 if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
549 $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
550 $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
553 $sql = "SELECT $sql_select
554 FROM $sql_from
555 WHERE $sql_where";
557 $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
558 $result = $db->sql_query($sql);
559 $result_topic_id = 0;
561 $rowset = array();
563 if ($show_results == 'topics')
565 $forums = $rowset = $shadow_topic_list = array();
566 while ($row = $db->sql_fetchrow($result))
568 if ($row['topic_status'] == ITEM_MOVED)
570 $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
573 $rowset[$row['topic_id']] = $row;
575 if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
577 $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
579 $forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
580 $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
582 $db->sql_freeresult($result);
584 // If we have some shadow topics, update the rowset to reflect their topic information
585 if (sizeof($shadow_topic_list))
587 $sql = 'SELECT *
588 FROM ' . TOPICS_TABLE . '
589 WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
590 $result = $db->sql_query($sql);
592 while ($row = $db->sql_fetchrow($result))
594 $orig_topic_id = $shadow_topic_list[$row['topic_id']];
596 // We want to retain some values
597 $row = array_merge($row, array(
598 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
599 'topic_status' => $rowset[$orig_topic_id]['topic_status'],
600 'forum_name' => $rowset[$orig_topic_id]['forum_name'])
603 $rowset[$orig_topic_id] = $row;
605 $db->sql_freeresult($result);
607 unset($shadow_topic_list);
609 foreach ($forums as $forum_id => $forum)
611 if ($user->data['is_registered'] && $config['load_db_lastread'])
613 $topic_tracking_info[$forum_id] = get_topic_tracking($forum_id, $forum['topic_list'], $forum['rowset'], array($forum_id => $forum['mark_time']), ($forum_id) ? false : $forum['topic_list']);
615 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
617 $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
619 if (!$user->data['is_registered'])
621 $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
625 unset($forums);
627 else
629 $bbcode_bitfield = $text_only_message = '';
630 $attach_list = array();
632 while ($row = $db->sql_fetchrow($result))
634 // We pre-process some variables here for later usage
635 $row['post_text'] = censor_text($row['post_text']);
637 $text_only_message = $row['post_text'];
638 // make list items visible as such
639 if ($row['bbcode_uid'])
641 $text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
642 // no BBCode in text only message
643 strip_bbcode($text_only_message, $row['bbcode_uid']);
646 if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
648 $row['display_text_only'] = false;
649 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
651 // Does this post have an attachment? If so, add it to the list
652 if ($row['post_attachment'] && $config['allow_attachments'])
654 $attach_list[$row['forum_id']][] = $row['post_id'];
657 else
659 $row['post_text'] = $text_only_message;
660 $row['display_text_only'] = true;
663 $rowset[] = $row;
665 $db->sql_freeresult($result);
667 unset($text_only_message);
669 // Instantiate BBCode if needed
670 if ($bbcode_bitfield !== '')
672 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
673 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
676 // Pull attachment data
677 if (sizeof($attach_list))
679 $use_attach_list = $attach_list;
680 $attach_list = array();
682 foreach ($use_attach_list as $forum_id => $_list)
684 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
686 $attach_list = array_merge($attach_list, $_list);
691 if (sizeof($attach_list))
693 $sql = 'SELECT *
694 FROM ' . ATTACHMENTS_TABLE . '
695 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
696 AND in_message = 0
697 ORDER BY filetime DESC, post_msg_id ASC';
698 $result = $db->sql_query($sql);
700 while ($row = $db->sql_fetchrow($result))
702 $attachments[$row['post_msg_id']][] = $row;
704 $db->sql_freeresult($result);
708 if ($hilit)
710 // Remove bad highlights
711 $hilit_array = array_filter(explode('|', $hilit), 'strlen');
712 foreach ($hilit_array as $key => $value)
714 $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
715 $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
717 $hilit = implode('|', $hilit_array);
720 foreach ($rowset as $row)
722 $forum_id = $row['forum_id'];
723 $result_topic_id = $row['topic_id'];
724 $topic_title = censor_text($row['topic_title']);
726 // we need to select a forum id for this global topic
727 if (!$forum_id)
729 if (!isset($g_forum_id))
731 // Get a list of forums the user cannot read
732 $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
734 // Determine first forum the user is able to read (must not be a category)
735 $sql = 'SELECT forum_id
736 FROM ' . FORUMS_TABLE . '
737 WHERE forum_type = ' . FORUM_POST;
739 if (sizeof($forum_ary))
741 $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
744 $result = $db->sql_query_limit($sql, 1);
745 $g_forum_id = (int) $db->sql_fetchfield('forum_id');
747 $u_forum_id = $g_forum_id;
749 else
751 $u_forum_id = $forum_id;
754 $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$u_forum_id&amp;t=$result_topic_id&amp;hilit=$u_hilit");
756 $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
758 if ($show_results == 'topics')
760 $folder_img = $folder_alt = $topic_type = '';
761 topic_status($row, $replies, (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false, $folder_img, $folder_alt, $topic_type);
763 $unread_topic = (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false;
765 $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
766 $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
767 $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$result_topic_id", true, $user->session_id) : '';
769 $tpl_ary = array(
770 'TOPIC_AUTHOR' => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
771 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
772 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
773 'FIRST_POST_TIME' => $user->format_date($row['topic_time']),
774 'LAST_POST_SUBJECT' => $row['topic_last_post_subject'],
775 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']),
776 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']),
777 'LAST_POST_AUTHOR' => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
778 'LAST_POST_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
779 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
781 'PAGINATION' => topic_generate_pagination($replies, $view_topic_url),
782 'TOPIC_TYPE' => $topic_type,
784 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt),
785 'TOPIC_FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'),
786 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
787 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
788 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
789 'ATTACH_ICON_IMG' => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
790 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
792 'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
793 'S_TOPIC_TYPE' => $row['topic_type'],
794 'S_USER_POSTED' => (!empty($row['mark_type'])) ? true : false,
795 'S_UNREAD_TOPIC' => $unread_topic,
797 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
798 'S_TOPIC_UNAPPROVED' => $topic_unapproved,
799 'S_POSTS_UNAPPROVED' => $posts_unapproved,
801 'U_LAST_POST' => $view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id'],
802 'U_LAST_POST_AUTHOR' => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
803 'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
804 'U_NEWEST_POST' => $view_topic_url . '&amp;view=unread#unread',
805 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
806 'U_MCP_QUEUE' => $u_mcp_queue,
809 else
811 if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
813 $template->assign_block_vars('searchresults', array(
814 'S_IGNORE_POST' => true,
816 'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"$u_search&amp;start=$start&amp;p=" . $row['post_id'] . '&amp;view=show#p' . $row['post_id'] . '">', '</a>'))
819 continue;
822 // Replace naughty words such as farty pants
823 $row['post_subject'] = censor_text($row['post_subject']);
825 if ($row['display_text_only'])
827 // now find context for the searched words
828 $row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
829 $row['post_text'] = str_replace("\n", '<br />', $row['post_text']);
831 else
833 $row['post_text'] = str_replace("\n", '<br />', $row['post_text']);
835 // Second parse bbcode here
836 if ($row['bbcode_bitfield'])
838 $bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
841 if (!empty($attachments[$row['post_id']]))
843 parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
845 // we only display inline attachments
846 unset($attachments[$row['post_id']]);
849 // Always process smilies after parsing bbcodes
850 $row['post_text'] = smiley_text($row['post_text']);
853 if ($hilit)
855 // post highlighting
856 $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
859 $tpl_ary = array(
860 'POST_AUTHOR_FULL' => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
861 'POST_AUTHOR_COLOUR' => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
862 'POST_AUTHOR' => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
863 'U_POST_AUTHOR' => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
865 'POST_SUBJECT' => $row['post_subject'],
866 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
867 'MESSAGE' => $row['post_text']
871 $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
872 'FORUM_ID' => $forum_id,
873 'TOPIC_ID' => $result_topic_id,
874 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false,
876 'FORUM_TITLE' => $row['forum_name'],
877 'TOPIC_TITLE' => $topic_title,
878 'TOPIC_REPLIES' => $replies,
879 'TOPIC_VIEWS' => $row['topic_views'],
881 'U_VIEW_TOPIC' => $view_topic_url,
882 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
883 'U_VIEW_POST' => (!empty($row['post_id'])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=" . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '&amp;hilit=' . $u_hilit) . '#p' . $row['post_id'] : '')
887 if ($topic_id && ($topic_id == $result_topic_id))
889 $template->assign_vars(array(
890 'SEARCH_TOPIC' => $topic_title,
891 'U_SEARCH_TOPIC' => $view_topic_url
895 unset($rowset);
897 page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
899 $template->set_filenames(array(
900 'body' => 'search_results.html')
902 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
904 page_footer();
908 // Search forum
909 $s_forums = '';
910 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, fa.user_id
911 FROM ' . FORUMS_TABLE . ' f
912 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
913 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
914 ORDER BY f.left_id ASC";
915 $result = $db->sql_query($sql);
917 $right = $cat_right = $padding_inc = 0;
918 $padding = $forum_list = $holding = '';
919 $pad_store = array('0' => '');
921 while ($row = $db->sql_fetchrow($result))
923 if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
925 // Non-postable forum with no subforums, don't display
926 continue;
929 if (!$auth->acl_get('f_list', $row['forum_id']) || $row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
931 // if the user does not have permissions to list this forum skip to the next branch
932 continue;
935 if ($row['left_id'] < $right)
937 $padding .= '&nbsp; &nbsp;';
938 $pad_store[$row['parent_id']] = $padding;
940 else if ($row['left_id'] > $right + 1)
942 if (isset($pad_store[$row['parent_id']]))
944 $padding = $pad_store[$row['parent_id']];
946 else
948 continue;
952 $right = $row['right_id'];
954 if (!$auth->acl_get('f_search', $row['forum_id']))
956 // if the user does not have permissions to search this forum skip only this forum/category
957 continue;
960 $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
962 if ($row['left_id'] > $cat_right)
964 // make sure we don't forget anything
965 $s_forums .= $holding;
966 $holding = '';
969 if ($row['right_id'] - $row['left_id'] > 1)
971 $cat_right = max($cat_right, $row['right_id']);
973 $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
975 else
977 $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
978 $holding = '';
982 if ($holding)
984 $s_forums .= $holding;
987 $db->sql_freeresult($result);
988 unset($pad_store);
990 if (!$s_forums)
992 trigger_error($user->lang['NO_SEARCH']);
995 // Number of chars returned
996 $s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
997 $s_characters .= '<option value="0">0</option>';
998 $s_characters .= '<option value="25">25</option>';
999 $s_characters .= '<option value="50">50</option>';
1001 for ($i = 100; $i <= 1000 ; $i += 100)
1003 $selected = ($i == 200) ? ' selected="selected"' : '';
1004 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1007 $template->assign_vars(array(
1008 'S_SEARCH_ACTION' => "{$phpbb_root_path}search.$phpEx",
1009 'S_HIDDEN_FIELDS' => build_hidden_fields(array('sid' => $user->session_id, 't' => $topic_id)),
1010 'S_CHARACTER_OPTIONS' => $s_characters,
1011 'S_FORUM_OPTIONS' => $s_forums,
1012 'S_SELECT_SORT_DIR' => $s_sort_dir,
1013 'S_SELECT_SORT_KEY' => $s_sort_key,
1014 'S_SELECT_SORT_DAYS' => $s_limit_days,
1015 'S_IN_SEARCH' => true,
1018 // Handle large objects differently for Oracle and MSSQL
1019 switch ($db->sql_layer)
1021 case 'oracle':
1022 $sql = 'SELECT search_time, search_keywords
1023 FROM ' . SEARCH_RESULTS_TABLE . '
1024 WHERE dbms_lob.getlength(search_keywords) > 0
1025 ORDER BY search_time DESC';
1026 break;
1028 case 'mssql':
1029 case 'mssql_odbc':
1030 $sql = 'SELECT search_time, search_keywords
1031 FROM ' . SEARCH_RESULTS_TABLE . '
1032 WHERE DATALENGTH(search_keywords) > 0
1033 ORDER BY search_time DESC';
1034 break;
1036 default:
1037 $sql = 'SELECT search_time, search_keywords
1038 FROM ' . SEARCH_RESULTS_TABLE . '
1039 WHERE search_keywords <> \'\'
1040 ORDER BY search_time DESC';
1041 break;
1043 $result = $db->sql_query_limit($sql, 5);
1045 while ($row = $db->sql_fetchrow($result))
1047 $keywords = $row['search_keywords'];
1049 $template->assign_block_vars('recentsearch', array(
1050 'KEYWORDS' => $keywords,
1051 'TIME' => $user->format_date($row['search_time']),
1053 'U_KEYWORDS' => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
1056 $db->sql_freeresult($result);
1058 // Output the basic page
1059 page_header($user->lang['SEARCH']);
1061 $template->set_filenames(array(
1062 'body' => 'search_body.html')
1064 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
1066 page_footer();