source: trunk/forum/search.php @ 1596

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