source: branches/posledni/forum/search.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 42.7 KB
Line 
1<?php
2/**
3*
4* @package phpBB3
5* @version $Id$
6* @copyright (c) 2005 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14define('IN_PHPBB', true);
15$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
16$phpEx = substr(strrchr(__FILE__, '.'), 1);
17include($phpbb_root_path . 'common.' . $phpEx);
18
19// Start session management
20$user->session_begin();
21$auth->acl($user->data);
22$user->setup('search');
23
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', '');
31
32$submit = request_var('submit', false);
33$keywords = utf8_normalize_nfc(request_var('keywords', '', true));
34$add_keywords = utf8_normalize_nfc(request_var('add_keywords', '', true));
35$author = request_var('author', '', true);
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);
42
43$sort_days = request_var('st', 0);
44$sort_key = request_var('sk', 't');
45$sort_dir = request_var('sd', 'd');
46
47$return_chars = request_var('ch', ($topic_id) ? -1 : 300);
48$search_forum = request_var('fid', array(0));
49
50// We put login boxes for the case if search_id is egosearch or unreadposts
51// because a guest should be able to log in even if guests search is not permitted
52
53// Egosearch is an author search
54if ($search_id == 'egosearch')
55{
56 $author_id = $user->data['user_id'];
57
58 if ($user->data['user_id'] == ANONYMOUS)
59 {
60 login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
61 }
62}
63
64// Search for unread posts needs user to be logged in if topics tracking for guests is disabled
65if ($search_id == 'unreadposts' && !$config['load_anon_lastread'] && !$user->data['is_registered'])
66{
67 login_box('', $user->lang['LOGIN_EXPLAIN_UNREADSEARCH']);
68}
69
70// Is user able to search? Has search been disabled?
71if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
72{
73 $template->assign_var('S_NO_SEARCH', true);
74 trigger_error('NO_SEARCH');
75}
76
77// Check search load limit
78if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load'])))
79{
80 $template->assign_var('S_NO_SEARCH', true);
81 trigger_error('NO_SEARCH_TIME');
82}
83
84// Check flood limit ... if applicable
85$interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];
86if ($interval && !$auth->acl_get('u_ignoreflood'))
87{
88 if ($user->data['user_last_search'] > time() - $interval)
89 {
90 $template->assign_var('S_NO_SEARCH', true);
91 trigger_error('NO_SEARCH_TIME');
92 }
93}
94
95// Define some vars
96$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']);
97$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']);
98
99$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
100gen_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);
101
102if ($keywords || $author || $author_id || $search_id || $submit)
103{
104 // clear arrays
105 $id_ary = array();
106
107 // If we are looking for authors get their ids
108 $author_id_ary = array();
109 $sql_author_match = '';
110 if ($author_id)
111 {
112 $author_id_ary[] = $author_id;
113 }
114 else if ($author)
115 {
116 if ((strpos($author, '*') !== false) && (utf8_strlen(str_replace(array('*', '%'), '', $author)) < $config['min_search_author_chars']))
117 {
118 trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars']));
119 }
120
121 $sql_where = (strpos($author, '*') !== false) ? ' username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " username_clean = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
122
123 $sql = 'SELECT user_id
124 FROM ' . USERS_TABLE . "
125 WHERE $sql_where
126 AND user_type <> " . USER_IGNORE;
127 $result = $db->sql_query_limit($sql, 100);
128
129 while ($row = $db->sql_fetchrow($result))
130 {
131 $author_id_ary[] = (int) $row['user_id'];
132 }
133 $db->sql_freeresult($result);
134
135 $sql_where = (strpos($author, '*') !== false) ? ' post_username ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " post_username = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
136
137 $sql = 'SELECT 1 as guest_post
138 FROM ' . POSTS_TABLE . "
139 WHERE $sql_where
140 AND poster_id = " . ANONYMOUS;
141 $result = $db->sql_query_limit($sql, 1);
142 $found_guest_post = $db->sql_fetchfield('guest_post');
143 $db->sql_freeresult($result);
144
145 if ($found_guest_post)
146 {
147 $author_id_ary[] = ANONYMOUS;
148 $sql_author_match = (strpos($author, '*') !== false) ? ' ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
149 }
150
151 if (!sizeof($author_id_ary))
152 {
153 trigger_error('NO_SEARCH_RESULTS');
154 }
155 }
156
157 // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode
158 // so we can keep the old keywords in their old mode, but add the new ones as required words
159 if ($add_keywords)
160 {
161 if ($search_terms == 'all')
162 {
163 $keywords .= ' ' . $add_keywords;
164 }
165 else
166 {
167 $search_terms = 'all';
168 $keywords = implode(' |', explode(' ', preg_replace('#\s+#u', ' ', $keywords))) . ' ' .$add_keywords;
169 }
170 }
171
172 // Which forums should not be searched? Author searches are also carried out in unindexed forums
173 if (empty($keywords) && sizeof($author_id_ary))
174 {
175 $ex_fid_ary = array_keys($auth->acl_getf('!f_read', true));
176 }
177 else
178 {
179 $ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true))));
180 }
181
182 $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'] . ')' : "";
183
184 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, f.forum_flags, fa.user_id
185 FROM ' . FORUMS_TABLE . ' f
186 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
187 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
188 $not_in_fid
189 ORDER BY f.left_id";
190 $result = $db->sql_query($sql);
191
192 $right_id = 0;
193 $reset_search_forum = true;
194 while ($row = $db->sql_fetchrow($result))
195 {
196 if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
197 {
198 $ex_fid_ary[] = (int) $row['forum_id'];
199 continue;
200 }
201
202 // Exclude forums from active topics
203 if (!($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && ($search_id == 'active_topics'))
204 {
205 $ex_fid_ary[] = (int) $row['forum_id'];
206 continue;
207 }
208
209 if (sizeof($search_forum))
210 {
211 if ($search_child)
212 {
213 if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)
214 {
215 $right_id = (int) $row['right_id'];
216 }
217 else if ($row['right_id'] < $right_id)
218 {
219 continue;
220 }
221 }
222
223 if (!in_array($row['forum_id'], $search_forum))
224 {
225 $ex_fid_ary[] = (int) $row['forum_id'];
226 $reset_search_forum = false;
227 }
228 }
229 }
230 $db->sql_freeresult($result);
231
232 // find out in which forums the user is allowed to view approved posts
233 if ($auth->acl_get('m_approve'))
234 {
235 $m_approve_fid_ary = array(-1);
236 $m_approve_fid_sql = '';
237 }
238 else if ($auth->acl_getf_global('m_approve'))
239 {
240 $m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary);
241 $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) : '') . ')';
242 }
243 else
244 {
245 $m_approve_fid_ary = array();
246 $m_approve_fid_sql = ' AND p.post_approved = 1';
247 }
248
249 if ($reset_search_forum)
250 {
251 $search_forum = array();
252 }
253
254 // Select which method we'll use to obtain the post_id or topic_id information
255 $search_type = basename($config['search_type']);
256
257 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
258 {
259 trigger_error('NO_SUCH_SEARCH_MODULE');
260 }
261
262 require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
263
264 // We do some additional checks in the module to ensure it can actually be utilised
265 $error = false;
266 $search = new $search_type($error);
267
268 if ($error)
269 {
270 trigger_error($error);
271 }
272
273 // let the search module split up the keywords
274 if ($keywords)
275 {
276 $correct_query = $search->split_keywords($keywords, $search_terms);
277 if (!$correct_query || (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id))
278 {
279 $ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], implode(' ', $search->common_words)) . '<br />' : '';
280 trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max']));
281 }
282 }
283
284 if (!$keywords && sizeof($author_id_ary))
285 {
286 // if it is an author search we want to show topics by default
287 $show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts');
288 $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
289 }
290
291 // define some variables needed for retrieving post_id/topic_id information
292 $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'));
293
294 // pre-made searches
295 $sql = $field = $l_search_title = '';
296 if ($search_id)
297 {
298 switch ($search_id)
299 {
300 // Oh holy Bob, bring us some activity...
301 case 'active_topics':
302 $l_search_title = $user->lang['SEARCH_ACTIVE_TOPICS'];
303 $show_results = 'topics';
304 $sort_key = 't';
305 $sort_dir = 'd';
306 $sort_days = request_var('st', 7);
307 $sort_by_sql['t'] = 't.topic_last_post_time';
308
309 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);
310 $s_sort_key = $s_sort_dir = '';
311
312 $last_post_time_sql = ($sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($sort_days * 24 * 3600)) : '';
313
314 $sql = 'SELECT t.topic_last_post_time, t.topic_id
315 FROM ' . TOPICS_TABLE . " t
316 WHERE t.topic_moved_id = 0
317 $last_post_time_sql
318 " . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
319 ' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . '
320 ORDER BY t.topic_last_post_time DESC';
321 $field = 'topic_id';
322 break;
323
324 case 'unanswered':
325 $l_search_title = $user->lang['SEARCH_UNANSWERED'];
326 $show_results = request_var('sr', 'topics');
327 $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
328 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
329 $sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title';
330 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
331
332 $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : '';
333 $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort;
334
335 if ($sort_days)
336 {
337 $last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600));
338 }
339 else
340 {
341 $last_post_time = '';
342 }
343
344 if ($sort_key == 'a')
345 {
346 $sort_join = USERS_TABLE . ' u, ';
347 $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort;
348 }
349 if ($show_results == 'posts')
350 {
351 $sql = "SELECT p.post_id
352 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
353 WHERE t.topic_replies = 0
354 AND p.topic_id = t.topic_id
355 $last_post_time
356 $m_approve_fid_sql
357 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
358 $sql_sort";
359 $field = 'post_id';
360 }
361 else
362 {
363 $sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id
364 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
365 WHERE t.topic_replies = 0
366 AND t.topic_moved_id = 0
367 AND p.topic_id = t.topic_id
368 $last_post_time
369 $m_approve_fid_sql
370 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
371 $sql_sort";
372 $field = 'topic_id';
373 }
374 break;
375
376 case 'unreadposts':
377 $l_search_title = $user->lang['SEARCH_UNREAD'];
378 // force sorting
379 $show_results = 'topics';
380 $sort_key = 't';
381 $sort_by_sql['t'] = 't.topic_last_post_time';
382 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
383
384 $sql_where = 'AND t.topic_moved_id = 0
385 ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
386 ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '');
387
388 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);
389 $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
390
391 $unread_list = array();
392 $unread_list = get_unread_topics($user->data['user_id'], $sql_where, $sql_sort);
393
394 if (!empty($unread_list))
395 {
396 $sql = 'SELECT t.topic_id
397 FROM ' . TOPICS_TABLE . ' t
398 WHERE ' . $db->sql_in_set('t.topic_id', array_keys($unread_list)) . "
399 $sql_sort";
400 $field = 'topic_id';
401 }
402 break;
403
404 case 'newposts':
405 $l_search_title = $user->lang['SEARCH_NEW'];
406 // force sorting
407 $show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics';
408 $sort_key = 't';
409 $sort_dir = 'd';
410 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
411 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
412
413 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);
414 $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
415
416 if ($show_results == 'posts')
417 {
418 $sql = 'SELECT p.post_id
419 FROM ' . POSTS_TABLE . ' p
420 WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
421 $m_approve_fid_sql
422 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
423 $sql_sort";
424 $field = 'post_id';
425 }
426 else
427 {
428 $sql = 'SELECT t.topic_id
429 FROM ' . TOPICS_TABLE . ' t
430 WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . '
431 AND t.topic_moved_id = 0
432 ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
433 ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
434 $sql_sort";
435/*
436 [Fix] queued replies missing from "view new posts" (Bug #42705 - Patch by Paul)
437 - Creates temporary table, query is far from optimized
438
439 $sql = 'SELECT t.topic_id
440 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
441 WHERE p.post_time > ' . $user->data['user_lastvisit'] . '
442 AND t.topic_id = p.topic_id
443 AND t.topic_moved_id = 0
444 ' . $m_approve_fid_sql . '
445 ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
446 GROUP BY t.topic_id
447 $sql_sort";
448*/
449 $field = 'topic_id';
450 }
451 break;
452
453 case 'egosearch':
454 $l_search_title = $user->lang['SEARCH_SELF'];
455 break;
456 }
457 }
458
459 // show_results should not change after this
460 $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
461 $total_match_count = 0;
462
463 if ($search_id)
464 {
465 if ($sql)
466 {
467 // only return up to 1000 ids (the last one will be removed later)
468 $result = $db->sql_query_limit($sql, 1001 - $start, $start);
469
470 while ($row = $db->sql_fetchrow($result))
471 {
472 $id_ary[] = (int) $row[$field];
473 }
474 $db->sql_freeresult($result);
475
476 $total_match_count = sizeof($id_ary) + $start;
477 $id_ary = array_slice($id_ary, 0, $per_page);
478 }
479 else
480 {
481 $search_id = '';
482 }
483 }
484
485 // make sure that some arrays are always in the same order
486 sort($ex_fid_ary);
487 sort($m_approve_fid_ary);
488 sort($author_id_ary);
489
490 if (!empty($search->search_query))
491 {
492 $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, $sql_author_match, $id_ary, $start, $per_page);
493 }
494 else if (sizeof($author_id_ary))
495 {
496 $firstpost_only = ($search_fields === 'firstpost' || $search_fields == 'titleonly') ? true : false;
497 $total_match_count = $search->author_search($show_results, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $sql_author_match, $id_ary, $start, $per_page);
498 }
499
500 // For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
501 if (!sizeof($id_ary) && !$search_id)
502 {
503 trigger_error('NO_SEARCH_RESULTS');
504 }
505
506 $sql_where = '';
507
508 if (sizeof($id_ary))
509 {
510 $sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
511 $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
512 $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);
513 }
514
515 if ($show_results == 'posts')
516 {
517 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
518 }
519 else
520 {
521 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
522 }
523
524 $user->add_lang('viewtopic');
525
526 // Grab icons
527 $icons = $cache->obtain_icons();
528
529 // Output header
530 if ($search_id && ($total_match_count > 1000))
531 {
532 // limit the number to 1000 for pre-made searches
533 $total_match_count--;
534 $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
535 }
536 else
537 {
538 $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);
539 }
540
541 // define some vars for urls
542 $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
543 // Do not allow *only* wildcard being used for hilight
544 $hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit;
545
546 $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
547 $u_show_results = '&amp;sr=' . $show_results;
548 $u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
549
550 $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
551 $u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
552 $u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
553 $u_search .= ($search_terms != 'all') ? '&amp;terms=' . $search_terms : '';
554 $u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
555 $u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
556 $u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
557 $u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
558 $u_search .= (!$search_child) ? '&amp;sc=0' : '';
559 $u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
560 $u_search .= ($return_chars != 300) ? '&amp;ch=' . $return_chars : '';
561
562 $template->assign_vars(array(
563 'SEARCH_TITLE' => $l_search_title,
564 'SEARCH_MATCHES' => $l_search_matches,
565 'SEARCH_WORDS' => $search->search_query,
566 'IGNORED_WORDS' => (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
567 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start),
568 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start),
569 'TOTAL_MATCHES' => $total_match_count,
570 'SEARCH_IN_RESULTS' => ($search_id) ? false : true,
571
572 'S_SELECT_SORT_DIR' => $s_sort_dir,
573 'S_SELECT_SORT_KEY' => $s_sort_key,
574 'S_SELECT_SORT_DAYS' => $s_limit_days,
575 'S_SEARCH_ACTION' => $u_search,
576 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true,
577
578 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'),
579 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
580 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
581 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
582 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
583
584 'U_SEARCH_WORDS' => $u_search,
585 ));
586
587 if ($sql_where)
588 {
589 if ($show_results == 'posts')
590 {
591 // @todo Joining this query to the one below?
592 $sql = 'SELECT zebra_id, friend, foe
593 FROM ' . ZEBRA_TABLE . '
594 WHERE user_id = ' . $user->data['user_id'];
595 $result = $db->sql_query($sql);
596
597 $zebra = array();
598 while ($row = $db->sql_fetchrow($result))
599 {
600 $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
601 }
602 $db->sql_freeresult($result);
603
604 $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
605 FROM ' . POSTS_TABLE . ' p
606 LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
607 LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
608 LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
609 WHERE $sql_where";
610 }
611 else
612 {
613 $sql_from = TOPICS_TABLE . ' t
614 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
615 ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
616 $sql_select = 't.*, f.forum_id, f.forum_name';
617
618 if ($user->data['is_registered'])
619 {
620 if ($config['load_db_track'] && $author_id !== $user->data['user_id'])
621 {
622 $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
623 AND t.topic_id = tp.topic_id)';
624 $sql_select .= ', tp.topic_posted';
625 }
626
627 if ($config['load_db_lastread'])
628 {
629 $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
630 AND t.topic_id = tt.topic_id)
631 LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
632 AND ft.forum_id = f.forum_id)';
633 $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
634 }
635 }
636
637 if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
638 {
639 $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
640 $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
641 }
642
643 $sql = "SELECT $sql_select
644 FROM $sql_from
645 WHERE $sql_where";
646 }
647 $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
648 $result = $db->sql_query($sql);
649 $result_topic_id = 0;
650
651 $rowset = array();
652
653 if ($show_results == 'topics')
654 {
655 $forums = $rowset = $shadow_topic_list = array();
656 while ($row = $db->sql_fetchrow($result))
657 {
658 $row['forum_id'] = (int) $row['forum_id'];
659 $row['topic_id'] = (int) $row['topic_id'];
660
661 if ($row['topic_status'] == ITEM_MOVED)
662 {
663 $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
664 }
665
666 $rowset[$row['topic_id']] = $row;
667
668 if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
669 {
670 $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
671 }
672 $forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
673 $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
674 }
675 $db->sql_freeresult($result);
676
677 // If we have some shadow topics, update the rowset to reflect their topic information
678 if (sizeof($shadow_topic_list))
679 {
680 $sql = 'SELECT *
681 FROM ' . TOPICS_TABLE . '
682 WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
683 $result = $db->sql_query($sql);
684
685 while ($row = $db->sql_fetchrow($result))
686 {
687 $orig_topic_id = $shadow_topic_list[$row['topic_id']];
688
689 // We want to retain some values
690 $row = array_merge($row, array(
691 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
692 'topic_status' => $rowset[$orig_topic_id]['topic_status'],
693 'forum_name' => $rowset[$orig_topic_id]['forum_name'])
694 );
695
696 $rowset[$orig_topic_id] = $row;
697 }
698 $db->sql_freeresult($result);
699 }
700 unset($shadow_topic_list);
701
702 foreach ($forums as $forum_id => $forum)
703 {
704 if ($user->data['is_registered'] && $config['load_db_lastread'])
705 {
706 $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']);
707 }
708 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
709 {
710 $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
711
712 if (!$user->data['is_registered'])
713 {
714 $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
715 }
716 }
717 }
718 unset($forums);
719 }
720 else
721 {
722 $bbcode_bitfield = $text_only_message = '';
723 $attach_list = array();
724
725 while ($row = $db->sql_fetchrow($result))
726 {
727 // We pre-process some variables here for later usage
728 $row['post_text'] = censor_text($row['post_text']);
729
730 $text_only_message = $row['post_text'];
731 // make list items visible as such
732 if ($row['bbcode_uid'])
733 {
734 $text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
735 // no BBCode in text only message
736 strip_bbcode($text_only_message, $row['bbcode_uid']);
737 }
738
739 if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
740 {
741 $row['display_text_only'] = false;
742 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
743
744 // Does this post have an attachment? If so, add it to the list
745 if ($row['post_attachment'] && $config['allow_attachments'])
746 {
747 $attach_list[$row['forum_id']][] = $row['post_id'];
748 }
749 }
750 else
751 {
752 $row['post_text'] = $text_only_message;
753 $row['display_text_only'] = true;
754 }
755
756 $rowset[] = $row;
757 }
758 $db->sql_freeresult($result);
759
760 unset($text_only_message);
761
762 // Instantiate BBCode if needed
763 if ($bbcode_bitfield !== '')
764 {
765 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
766 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
767 }
768
769 // Pull attachment data
770 if (sizeof($attach_list))
771 {
772 $use_attach_list = $attach_list;
773 $attach_list = array();
774
775 foreach ($use_attach_list as $forum_id => $_list)
776 {
777 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
778 {
779 $attach_list = array_merge($attach_list, $_list);
780 }
781 }
782 }
783
784 if (sizeof($attach_list))
785 {
786 $sql = 'SELECT *
787 FROM ' . ATTACHMENTS_TABLE . '
788 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
789 AND in_message = 0
790 ORDER BY filetime DESC, post_msg_id ASC';
791 $result = $db->sql_query($sql);
792
793 while ($row = $db->sql_fetchrow($result))
794 {
795 $attachments[$row['post_msg_id']][] = $row;
796 }
797 $db->sql_freeresult($result);
798 }
799 }
800
801 if ($hilit)
802 {
803 // Remove bad highlights
804 $hilit_array = array_filter(explode('|', $hilit), 'strlen');
805 foreach ($hilit_array as $key => $value)
806 {
807 $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
808 $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
809 }
810 $hilit = implode('|', $hilit_array);
811 }
812
813 foreach ($rowset as $row)
814 {
815 $forum_id = $row['forum_id'];
816 $result_topic_id = $row['topic_id'];
817 $topic_title = censor_text($row['topic_title']);
818
819 // we need to select a forum id for this global topic
820 if (!$forum_id)
821 {
822 if (!isset($g_forum_id))
823 {
824 // Get a list of forums the user cannot read
825 $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
826
827 // Determine first forum the user is able to read (must not be a category)
828 $sql = 'SELECT forum_id
829 FROM ' . FORUMS_TABLE . '
830 WHERE forum_type = ' . FORUM_POST;
831
832 if (sizeof($forum_ary))
833 {
834 $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
835 }
836
837 $result = $db->sql_query_limit($sql, 1);
838 $g_forum_id = (int) $db->sql_fetchfield('forum_id');
839 }
840 $u_forum_id = $g_forum_id;
841 }
842 else
843 {
844 $u_forum_id = $forum_id;
845 }
846
847 $view_topic_url_params = "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : '');
848 $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
849
850 $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
851
852 if ($show_results == 'topics')
853 {
854 if ($config['load_db_track'] && $author_id === $user->data['user_id'])
855 {
856 $row['topic_posted'] = 1;
857 }
858
859 $folder_img = $folder_alt = $topic_type = '';
860 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);
861
862 $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;
863
864 $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
865 $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
866 $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) : '';
867
868 $row['topic_title'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['topic_title']);
869
870 $tpl_ary = array(
871 'TOPIC_AUTHOR' => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
872 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
873 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
874 'FIRST_POST_TIME' => $user->format_date($row['topic_time']),
875 'LAST_POST_SUBJECT' => $row['topic_last_post_subject'],
876 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']),
877 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']),
878 'LAST_POST_AUTHOR' => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
879 'LAST_POST_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
880 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
881
882 'PAGINATION' => topic_generate_pagination($replies, $view_topic_url),
883 'TOPIC_TYPE' => $topic_type,
884
885 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt),
886 'TOPIC_FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'),
887 'TOPIC_FOLDER_IMG_ALT' => $user->lang[$folder_alt],
888 'TOPIC_FOLDER_IMG_WIDTH'=> $user->img($folder_img, '', false, '', 'width'),
889 'TOPIC_FOLDER_IMG_HEIGHT' => $user->img($folder_img, '', false, '', 'height'),
890
891 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
892 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
893 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
894 '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']) : '',
895 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
896
897 'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false,
898 'S_TOPIC_TYPE' => $row['topic_type'],
899 'S_USER_POSTED' => (!empty($row['mark_type'])) ? true : false,
900 'S_UNREAD_TOPIC' => $unread_topic,
901
902 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
903 'S_TOPIC_UNAPPROVED' => $topic_unapproved,
904 'S_POSTS_UNAPPROVED' => $posts_unapproved,
905
906 'U_LAST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;p=' . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
907 'U_LAST_POST_AUTHOR' => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
908 'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
909 'U_NEWEST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
910 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
911 'U_MCP_QUEUE' => $u_mcp_queue,
912 );
913 }
914 else
915 {
916 if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
917 {
918 $template->assign_block_vars('searchresults', array(
919 'S_IGNORE_POST' => true,
920
921 '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>'))
922 );
923
924 continue;
925 }
926
927 // Replace naughty words such as farty pants
928 $row['post_subject'] = censor_text($row['post_subject']);
929
930 if ($row['display_text_only'])
931 {
932 // now find context for the searched words
933 $row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
934 $row['post_text'] = bbcode_nl2br($row['post_text']);
935 }
936 else
937 {
938 // Second parse bbcode here
939 if ($row['bbcode_bitfield'])
940 {
941 $bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
942 }
943
944 $row['post_text'] = bbcode_nl2br($row['post_text']);
945 $row['post_text'] = smiley_text($row['post_text']);
946
947 if (!empty($attachments[$row['post_id']]))
948 {
949 parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
950
951 // we only display inline attachments
952 unset($attachments[$row['post_id']]);
953 }
954 }
955
956 if ($hilit)
957 {
958 // post highlighting
959 $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
960 $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_subject']);
961 }
962
963 $tpl_ary = array(
964 'POST_AUTHOR_FULL' => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
965 'POST_AUTHOR_COLOUR' => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
966 'POST_AUTHOR' => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
967 'U_POST_AUTHOR' => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
968
969 'POST_SUBJECT' => $row['post_subject'],
970 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
971 'MESSAGE' => $row['post_text']
972 );
973 }
974
975 $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
976 'FORUM_ID' => $forum_id,
977 'TOPIC_ID' => $result_topic_id,
978 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false,
979
980 'FORUM_TITLE' => $row['forum_name'],
981 'TOPIC_TITLE' => $topic_title,
982 'TOPIC_REPLIES' => $replies,
983 'TOPIC_VIEWS' => $row['topic_views'],
984
985 'U_VIEW_TOPIC' => $view_topic_url,
986 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
987 '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'] . (($u_hilit) ? '&amp;hilit=' . $u_hilit : '')) . '#p' . $row['post_id'] : '')
988 ));
989 }
990
991 if ($topic_id && ($topic_id == $result_topic_id))
992 {
993 $template->assign_vars(array(
994 'SEARCH_TOPIC' => $topic_title,
995 'U_SEARCH_TOPIC' => $view_topic_url
996 ));
997 }
998 }
999 unset($rowset);
1000
1001 page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
1002
1003 $template->set_filenames(array(
1004 'body' => 'search_results.html')
1005 );
1006 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
1007
1008 page_footer();
1009}
1010
1011// Search forum
1012$s_forums = '';
1013$sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, f.enable_indexing, fa.user_id
1014 FROM ' . FORUMS_TABLE . ' f
1015 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
1016 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
1017 ORDER BY f.left_id ASC";
1018$result = $db->sql_query($sql);
1019
1020$right = $cat_right = $padding_inc = 0;
1021$padding = $forum_list = $holding = '';
1022$pad_store = array('0' => '');
1023
1024while ($row = $db->sql_fetchrow($result))
1025{
1026 if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
1027 {
1028 // Non-postable forum with no subforums, don't display
1029 continue;
1030 }
1031
1032 if ($row['forum_type'] == FORUM_POST && ($row['left_id'] + 1 == $row['right_id']) && !$row['enable_indexing'])
1033 {
1034 // Postable forum with no subforums and indexing disabled, don't display
1035 continue;
1036 }
1037
1038 if ($row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
1039 {
1040 // if this forum is a link or password protected (user has not entered the password yet) then skip to the next branch
1041 continue;
1042 }
1043
1044 if ($row['left_id'] < $right)
1045 {
1046 $padding .= '&nbsp; &nbsp;';
1047 $pad_store[$row['parent_id']] = $padding;
1048 }
1049 else if ($row['left_id'] > $right + 1)
1050 {
1051 if (isset($pad_store[$row['parent_id']]))
1052 {
1053 $padding = $pad_store[$row['parent_id']];
1054 }
1055 else
1056 {
1057 continue;
1058 }
1059 }
1060
1061 $right = $row['right_id'];
1062
1063 if ($auth->acl_gets('!f_search', '!f_list', $row['forum_id']))
1064 {
1065 // if the user does not have permissions to search or see this forum skip only this forum/category
1066 continue;
1067 }
1068
1069 $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
1070
1071 if ($row['left_id'] > $cat_right)
1072 {
1073 // make sure we don't forget anything
1074 $s_forums .= $holding;
1075 $holding = '';
1076 }
1077
1078 if ($row['right_id'] - $row['left_id'] > 1)
1079 {
1080 $cat_right = max($cat_right, $row['right_id']);
1081
1082 $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
1083 }
1084 else
1085 {
1086 $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
1087 $holding = '';
1088 }
1089}
1090
1091if ($holding)
1092{
1093 $s_forums .= $holding;
1094}
1095
1096$db->sql_freeresult($result);
1097unset($pad_store);
1098
1099if (!$s_forums)
1100{
1101 trigger_error('NO_SEARCH');
1102}
1103
1104// Number of chars returned
1105$s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
1106$s_characters .= '<option value="0">0</option>';
1107$s_characters .= '<option value="25">25</option>';
1108$s_characters .= '<option value="50">50</option>';
1109
1110for ($i = 100; $i <= 1000 ; $i += 100)
1111{
1112 $selected = ($i == 300) ? ' selected="selected"' : '';
1113 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1114}
1115
1116$s_hidden_fields = array('t' => $topic_id);
1117
1118if ($_SID)
1119{
1120 $s_hidden_fields['sid'] = $_SID;
1121}
1122
1123if (!empty($_EXTRA_URL))
1124{
1125 foreach ($_EXTRA_URL as $url_param)
1126 {
1127 $url_param = explode('=', $url_param, 2);
1128 $s_hidden_fields[$url_param[0]] = $url_param[1];
1129 }
1130}
1131
1132$template->assign_vars(array(
1133 'S_SEARCH_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx", false, true, 0), // We force no ?sid= appending by using 0
1134 'S_HIDDEN_FIELDS' => build_hidden_fields($s_hidden_fields),
1135 'S_CHARACTER_OPTIONS' => $s_characters,
1136 'S_FORUM_OPTIONS' => $s_forums,
1137 'S_SELECT_SORT_DIR' => $s_sort_dir,
1138 'S_SELECT_SORT_KEY' => $s_sort_key,
1139 'S_SELECT_SORT_DAYS' => $s_limit_days,
1140 'S_IN_SEARCH' => true,
1141));
1142
1143// only show recent searches to search administrators
1144if ($auth->acl_get('a_search'))
1145{
1146 // Handle large objects differently for Oracle and MSSQL
1147 switch ($db->sql_layer)
1148 {
1149 case 'oracle':
1150 $sql = 'SELECT search_time, search_keywords
1151 FROM ' . SEARCH_RESULTS_TABLE . '
1152 WHERE dbms_lob.getlength(search_keywords) > 0
1153 ORDER BY search_time DESC';
1154 break;
1155
1156 case 'mssql':
1157 case 'mssql_odbc':
1158 $sql = 'SELECT search_time, search_keywords
1159 FROM ' . SEARCH_RESULTS_TABLE . '
1160 WHERE DATALENGTH(search_keywords) > 0
1161 ORDER BY search_time DESC';
1162 break;
1163
1164 default:
1165 $sql = 'SELECT search_time, search_keywords
1166 FROM ' . SEARCH_RESULTS_TABLE . '
1167 WHERE search_keywords <> \'\'
1168 ORDER BY search_time DESC';
1169 break;
1170 }
1171 $result = $db->sql_query_limit($sql, 5);
1172
1173 while ($row = $db->sql_fetchrow($result))
1174 {
1175 $keywords = $row['search_keywords'];
1176
1177 $template->assign_block_vars('recentsearch', array(
1178 'KEYWORDS' => $keywords,
1179 'TIME' => $user->format_date($row['search_time']),
1180
1181 'U_KEYWORDS' => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
1182 ));
1183 }
1184 $db->sql_freeresult($result);
1185}
1186
1187// Output the basic page
1188page_header($user->lang['SEARCH']);
1189
1190$template->set_filenames(array(
1191 'body' => 'search_body.html')
1192);
1193make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
1194
1195page_footer();
1196
1197?>
Note: See TracBrowser for help on using the repository browser.