phpBB
Statistics
| Revision:

root / tags / milestone_3 / phpBB / search.php

History | View | Annotate | Download (22.6 kB)

1
<?php
2
/** 
3
*
4
* @package phpBB3
5
* @version $Id: search.php 5236 2005-10-02 16:48:17Z acydburn $
6
* @copyright (c) 2005 phpBB Group 
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
8
*
9
*/
10
11
/**
12
*/
13
define('IN_PHPBB', true);
14
$phpbb_root_path = './';
15
$phpEx = substr(strrchr(__FILE__, '.'), 1);
16
include($phpbb_root_path . 'common.'.$phpEx);
17
18
// Start session management
19
$user->session_begin();
20
$auth->acl($user->data);
21
$user->setup('search');
22
23
// Define initial vars
24
$mode                = request_var('mode', '');
25
$search_id        = request_var('search_id', '');
26
$search_session_id        = request_var('search_session_id', 0);
27
$start                = request_var('start', 0);
28
$post_id        = request_var('p', 0);
29
$view                = request_var('view', '');
30
31
$keywords                = request_var('keywords', '');
32
$author                        = request_var('author', '');
33
$show_results        = request_var('show_results', 'topics');
34
$search_terms        = request_var('search_terms', 'all');
35
$search_fields        = request_var('search_fields', 'all');
36
$search_child        = request_var('search_child', true);
37
38
$return_chars        = request_var('return_chars', 200);
39
$search_forum        = request_var('search_forum', 0);
40
41
$sort_days        = request_var('st', 0);
42
$sort_key        = request_var('sk', 't');
43
$sort_dir        = request_var('sd', 'd');
44
45
// Is user able to search? Has search been disabled?
46
if (!$auth->acl_get('u_search') || !$config['load_search'])
47
{
48
        trigger_error($user->lang['NO_SEARCH']);
49
}
50
51
// Define some vars
52
$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'], 364 => $user->lang['1_YEAR']);
53
$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']);
54
55
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
56
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);
57
58
$store_vars                = array('sort_key', 'sort_dir', 'sort_days', 'show_results', 'return_chars', 'total_match_count');
59
$current_time        = time();
60
61
// Check last search time ... if applicable
62
if ($config['search_interval'])
63
{
64
        $sql = 'SELECT MAX(search_time) as last_time
65
                FROM ' . SEARCH_TABLE;
66
        $result = $db->sql_query($sql);
67
68
        if ($row = $db->sql_fetchrow($result))
69
        {
70
                if ($row['last_time'] > time() - $config['search_interval'])
71
                {
72
                        trigger_error($user->lang['NO_SEARCH_TIME']);
73
                }
74
        }
75
}
76
77
if ($keywords || $author || $search_id || $search_session_id)
78
{
79
        // clear arrays
80
        $pid_ary = $fid_ary = array();
81
82
        // Which forums can we view?
83
        $sql_where = (sizeof($search_forum) && !$search_child) ? 'WHERE f.forum_id IN (' . implode(', ', $search_forum) . ')' : '';
84
        $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id
85
                FROM (' . FORUMS_TABLE . ' f
86
                LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON  (fa.forum_id = f.forum_id
87
                        AND fa.session_id = '" . $db->sql_escape($user->data['session_id']) . "'))
88
                $sql_where
89
                ORDER BY f.left_id";
90
        $result = $db->sql_query($sql);
91
92
        $right_id = 0;
93
        while ($row = $db->sql_fetchrow($result))
94
        {
95
                if ($search_child)
96
                {
97
                        if (!$search_forum || (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id))
98
                        {
99
                                $right_id = $row['right_id'];
100
                        }
101
                        else if ($row['right_id'] > $right_id)
102
                        {
103
                                continue;
104
                        }
105
                }
106
107
                if ($auth->acl_get('f_read', $row['forum_id']) && (!$row['forum_password'] || $row['user_id'] == $user->data['user_id']))
108
                {
109
                        $fid_ary[] = $row['forum_id'];
110
                }
111
        }
112
        $db->sql_freeresult($result);
113
        unset($search_forum);
114
115
        if (!sizeof($fid_ary))
116
        {
117
                trigger_error($user->lang['NO_SEARCH_RESULTS']);
118
        }
119
120
        if ($search_id == 'egosearch')
121
        {
122
                $author = $user->data['username'];
123
        }
124
125
        // Are we looking for a user?
126
        $author_id = 0;
127
        if ($author)
128
        {
129
                $sql_where = (strstr($author, '*') !== false) ? ' LIKE ' : ' = ';
130
                $sql = 'SELECT user_id
131
                        FROM ' . USERS_TABLE . "
132
                        WHERE username $sql_where '" . $db->sql_escape(preg_replace('#\*+#', '%', $author)) . "'
133
                                AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';
134
                $result = $db->sql_query($sql);
135
136
                if (!$row = $db->sql_fetchrow($result))
137
                {
138
                        trigger_error($user->lang['NO_SEARCH_RESULTS']);
139
                }
140
                $db->sql_freeresult($result);
141
142
                $author_id = (int) $row['user_id'];
143
        }
144
145
146
        if ($search_id)
147
        {
148
                $sql_in = $sql_where = '';
149
150
                switch ($search_id)
151
                {
152
                        // Oh holy Bob, bring us some activity...
153
                        case 'active_topics':
154
                                $show_results = 'topics';
155
156
                                if (!$sort_days)
157
                                {
158
                                        $sort_days = 1;
159
                                        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);
160
                                }
161
162
                                $last_post_time = (time() - ($sort_days * 24 * 3600));
163
164
                                $sql = 'SELECT DISTINCT t.topic_id
165
                                        FROM ' . POSTS_TABLE . ' p
166
                                        LEFT JOIN ' . TOPICS_TABLE . " t ON (t.topic_approved = 1 AND p.topic_id = t.topic_id)
167
                                        WHERE p.post_time > $last_post_time
168
                                                " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . '
169
                                        ORDER BY t.topic_last_post_time DESC';
170
                                $result = $db->sql_query_limit($sql, 1000);
171
172
                                while ($row = $db->sql_fetchrow($result))
173
                                {
174
                                        $pid_ary[] = $row['topic_id'];
175
                                }
176
                                $db->sql_freeresult($result);
177
                                break;
178
179
                        case 'egosearch':
180
                                break;
181
182
                        case 'unanswered':
183
                                if ($show_results == 'posts')
184
                                {
185
                                        $sql = 'SELECT p.post_id
186
                                                FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
187
                                                WHERE t.topic_replies = 0
188
                                                        AND p.topic_id = t.topic_id
189
                                                        " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '');
190
                                        $field = 'post_id';
191
                                }
192
                                else
193
                                {
194
                                        $sql = 'SELECT t.topic_id
195
                                                FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
196
                                                WHERE t.topic_replies = 0
197
                                                        AND p.topic_id = t.topic_id
198
                                                        " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . '
199
                                                GROUP BY p.topic_id';
200
                                        $field = 'topic_id';
201
                                }
202
                                $result = $db->sql_query($sql);
203
204
                                while ($row = $db->sql_fetchrow($result))
205
                                {
206
                                        $pid_ary[] = $row[$field];
207
                                }
208
                                $db->sql_freeresult($result);
209
210
                                if (!sizeof($pid_ary))
211
                                {
212
                                        trigger_error($user->lang['NO_SEARCH_RESULTS']);
213
                                }
214
                                break;
215
216
                        case 'newposts':
217
                                if ($show_results == 'posts')
218
                                {
219
                                        $sql = 'SELECT p.post_id
220
                                                FROM ' . POSTS_TABLE . ' p
221
                                                WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
222
                                                        " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '');
223
                                        $field = 'post_id';
224
                                }
225
                                else
226
                                {
227
                                        $sql = 'SELECT t.topic_id
228
                                                FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
229
                                                WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
230
                                                        AND t.topic_id = p.topic_id
231
                                                        " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . '
232
                                                GROUP by p.topic_id';
233
                                        $field = 'topic_id';
234
                                }
235
                                $result = $db->sql_query($sql);
236
237
                                while ($row = $db->sql_fetchrow($result))
238
                                {
239
                                        $pid_ary[] = $row[$field];
240
                                }
241
                                $db->sql_freeresult($result);
242
243
                                if (!sizeof($pid_ary))
244
                                {
245
                                        trigger_error($user->lang['NO_SEARCH_RESULTS']);
246
                                }
247
                                break;
248
                }
249
        }        
250
        
251
        /**
252
        * @todo add to config
253
        */
254
        $config['search_type'] = 'mysql';
255
256
        // Select which method we'll use to obtain the post_id information
257
        $smid = '';
258
        switch ($config['search_type'])
259
        {
260
                case 'phpbb':
261
                        $smid = 'fulltext_phpbb';
262
                        break;
263
                case 'mysql':
264
                        $smid = 'fulltext_mysql';
265
                        break;
266
/*                case 'mssql':
267
                case 'pgsql':
268
                        $smid = 'fulltext_pgmssql';
269
                        break;
270
                case 'like':
271
                        $smid = 'like';
272
                        break;
273
                case 'preg':
274
                        $smid = 'preg_mysql';
275
                        break;*/
276
                default:
277
                        trigger_error('NO_SUCH_SEARCH_MODULE');
278
        }
279
280
        require($phpbb_root_path . 'includes/search/' . $smid . '.' . $phpEx);
281
282
        // We do some additional checks in each module to ensure it can actually be utilised
283
        $error = false;
284
        $search = new $smid($error);
285
        
286
        if ($error)
287
        {
288
                trigger_error($error);
289
        }
290
291
        if ($search_session_id)
292
        {
293
                $sql = 'SELECT search_array
294
                        FROM ' . SEARCH_TABLE . "
295
                        WHERE search_id = $search_session_id
296
                                AND session_id = '" . $db->sql_escape($user->data['session_id']) . "'";
297
                $result = $db->sql_query($sql);
298
299
                if ($row = $db->sql_fetchrow($result))
300
                {
301
                        $pid_ary = explode('#', $row['search_array']);
302
303
                        $search->split_words = unserialize(array_shift($pid_ary));
304
                        if ($keywords)
305
                        {
306
                                // If we're wanting to search on these results we store the existing split word array
307
                                $search->old_split_words = $search->split_words;
308
                        }
309
                        $search->common_words = unserialize(array_shift($pid_ary));
310
311
                        foreach ($store_vars as $var)
312
                        {
313
                                $$var = array_shift($pid_ary);
314
                        }
315
                }
316
                $db->sql_freeresult($result);
317
        }
318
319
        $total_match_count = 0;
320
        $search->search($show_results, $search_fields, $search_terms, $fid_ary, $keywords, $author_id, $pid_ary, $sort_days);
321
322
        if ($pid_ary)
323
        {
324
                // Finish building query (for all combinations) and run it ...
325
                $sql = 'SELECT session_id
326
                        FROM ' . SESSIONS_TABLE;
327
                $result = $db->sql_query($sql);
328
329
                $delete_search_ids = array();
330
                while ($row = $db->sql_fetchrow($result))
331
                {
332
                        $delete_search_ids[] = "'" . $db->sql_escape($row['session_id']) . "'";
333
                }
334
335
                if (sizeof($delete_search_ids))
336
                {
337
                        $sql = 'DELETE FROM ' . SEARCH_TABLE . '
338
                                WHERE session_id NOT IN (' . implode(", ", $delete_search_ids) . ')';
339
                        $db->sql_query($sql);
340
                }
341
342
                $total_match_count = sizeof($pid_ary);
343
                $sql_where = (($show_results == 'posts') ? 'p.post_id' : 't.topic_id') . ' IN (' . implode(', ', $pid_ary) . ')';
344
345
                if (sizeof($search->old_split_words) && array_diff($search->split_words, $search->old_split_words))
346
                {
347
                        $search->split_words = array_merge($search->split_words, $search->old_split_words);
348
                }
349
350
                $data = serialize($search->split_words);
351
                $data .= '#' . serialize($search->common_words);
352
                
353
                foreach ($store_vars as $var)
354
                {
355
                        $data .= '#' . $$var;
356
                }
357
                $data .= '#' . implode('#', $pid_ary);
358
                
359
                unset($pid_ary);
360
361
                srand ((double) microtime() * 1000000);
362
                $search_session_id = rand();
363
364
                $sql_ary = array(
365
                        'search_id'                => $search_session_id,
366
                        'session_id'        => $user->data['session_id'],
367
                        'search_time'        => $current_time,
368
                        'search_array'        => $data
369
                );
370
371
                $sql = 'INSERT INTO ' . SEARCH_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
372
                $db->sql_query($sql);
373
                unset($data);
374
        }
375
376
        if ($show_results == 'posts')
377
        {
378
                include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
379
        }
380
        else
381
        {
382
                include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
383
        }
384
385
        // Look up data ...
386
        $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
387
388
        // Grab icons
389
        $icons = array();
390
        $cache->obtain_icons($icons);
391
392
        // Output header
393
        $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);
394
395
        $hilit = htmlspecialchars(implode('|', str_replace(array('+', '-', '|'), '', $search->split_words)));
396
        $split_words = (sizeof($search->split_words)) ? htmlspecialchars(implode(' ', $search->split_words)) : '';
397
398
        $template->assign_vars(array(
399
                'SEARCH_MATCHES'        => $l_search_matches,
400
                'SEARCH_WORDS'                => $split_words, 
401
                'IGNORED_WORDS'                => (sizeof($search->common_words)) ? htmlspecialchars(implode(' ', $search->common_words)) : '', 
402
                'PAGINATION'                => generate_pagination("{$phpbb_root_path}search.$phpEx$SID&amp;search_session_id=$search_session_id&amp;search_id=$search_id&amp;hilit=$hilit&amp;$u_sort_param", $total_match_count, $per_page, $start),
403
                'PAGE_NUMBER'                => on_page($total_match_count, $per_page, $start),
404
                'TOTAL_MATCHES'                => $total_match_count,
405
406
                'S_SELECT_SORT_DIR'                => $s_sort_dir,
407
                'S_SELECT_SORT_KEY'                => $s_sort_key,
408
                'S_SELECT_SORT_DAYS'        => $s_limit_days,
409
                'S_SEARCH_ACTION'                => "{$phpbb_root_path}search.$phpEx$SID&amp;search_session_id=$search_session_id&amp;search_id=$search_id", 
410
                'S_SHOW_TOPICS'                        => ($show_results == 'posts') ? false : true,
411
412
                'REPORTED_IMG'                        => $user->img('icon_reported', 'TOPIC_REPORTED'),
413
                'UNAPPROVED_IMG'                => $user->img('icon_unapproved', 'TOPIC_UNAPPROVED'),
414
                'GOTO_PAGE_IMG'                        => $user->img('icon_post', 'GOTO_PAGE'),
415
416
                'U_SEARCH_WORDS'        => "{$phpbb_root_path}search.$phpEx$SID&amp;show_results=$show_results&amp;keywords=" . urlencode($split_words))
417
        );
418
419
        $u_hilit = urlencode($split_words);
420
421
        // Define ordering sql field, do it here because the order may be defined
422
        // within an existing search result set
423
        $sort_by_sql        = array('a' => (($show_results == 'posts') ? 'u.username' : 't.topic_poster'), 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'pt.post_subject' : 't.topic_title'));
424
425
        if ($sql_where)
426
        {
427
                if ($show_results == 'posts')
428
                {
429
                        // Not joining this query to the one below at present ... may do in future
430
                        $sql = 'SELECT zebra_id, friend, foe
431
                                FROM ' . ZEBRA_TABLE . ' 
432
                                WHERE user_id = ' . $user->data['user_id'];
433
                        $result = $db->sql_query($sql);
434
435
                        $zebra = array();
436
                        while ($row = $db->sql_fetchrow($result))
437
                        {
438
                                $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
439
                        }
440
                        $db->sql_freeresult($result);
441
442
                        $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_sig, u.user_sig_bbcode_uid
443
                                FROM ' . FORUMS_TABLE . ' f, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u, ' . POSTS_TABLE . " p 
444
                                WHERE $sql_where 
445
                                        AND f.forum_id = p.forum_id
446
                                        AND p.topic_id = t.topic_id
447
                                        AND p.poster_id = u.user_id";
448
                }
449
                else
450
                {
451
                        $sql = 'SELECT t.*, f.forum_id, f.forum_name
452
                                FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f 
453
                                WHERE $sql_where 
454
                                        AND f.forum_id = t.forum_id";
455
                }
456
                $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
457
                $result = $db->sql_query_limit($sql, $per_page, $start);
458
459
                while ($row = $db->sql_fetchrow($result))
460
                {
461
                        $forum_id = $row['forum_id'];
462
                        $topic_id = $row['topic_id'];
463
464
                        $view_topic_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id&amp;hilit=$u_hilit";
465
466
                        if ($show_results == 'topics')
467
                        {
468
                                $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
469
470
                                $folder_img = $folder_alt = $topic_type = '';
471
                                topic_status($row, $replies, time(), time(), $folder_img, $folder_alt, $topic_type);
472
473
                                $tpl_ary = array(
474
                                        'TOPIC_AUTHOR'                 => topic_topic_author($row),
475
                                        'FIRST_POST_TIME'         => $user->format_date($row['topic_time']),
476
                                        'LAST_POST_TIME'        => $user->format_date($row['topic_last_post_time']),
477
                                        'LAST_VIEW_TIME'        => $user->format_date($row['topic_last_view_time']),
478
                                        'LAST_POST_AUTHOR'         => ($row['topic_last_poster_name'] != '') ? $row['topic_last_poster_name'] : $user->lang['GUEST'],
479
                                        'PAGINATION'                 => topic_generate_pagination($replies, $view_topic_url),
480
                                        'REPLIES'                         => $replies,
481
                                        'VIEWS'                         => $row['topic_views'],
482
                                        'TOPIC_TYPE'                 => $topic_type,
483
484
                                        'LAST_POST_IMG'         => $user->img('icon_post_latest', 'VIEW_LATEST_POST'),
485
                                        'TOPIC_FOLDER_IMG'         => $user->img($folder_img, $folder_alt),
486
                                        'TOPIC_ICON_IMG'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
487
                                        'TOPIC_ICON_IMG_WIDTH'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
488
                                        'TOPIC_ICON_IMG_HEIGHT'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
489
                                        'ATTACH_ICON_IMG'        => ($auth->acl_gets('f_download', 'u_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
490
491
                                        'S_TOPIC_TYPE'                        => $row['topic_type'],
492
                                        'S_USER_POSTED'                        => (!empty($row['mark_type'])) ? true : false,
493
494
                                        'S_TOPIC_REPORTED'                => (!empty($row['topic_reported']) && $auth->acl_gets('m_', $forum_id)) ? true : false,
495
                                        'S_TOPIC_UNAPPROVED'        => (!$row['topic_approved'] && $auth->acl_gets('m_approve', $forum_id)) ? true : false,
496
497
                                        'U_LAST_POST'                => $view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id'],
498
                                        'U_LAST_POST_AUTHOR'=> ($row['topic_last_poster_id'] != ANONYMOUS && $row['topic_last_poster_id']) ? "{$phpbb_root_path}memberlist.$phpEx$SID&amp;mode=viewprofile&amp;u={$row['topic_last_poster_id']}" : '',
499
                                        'U_MCP_REPORT'                => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&amp;mode=reports&amp;t=$topic_id",
500
                                        'U_MCP_QUEUE'                => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&amp;i=queue&amp;mode=approve_details&amp;t=$topic_id"
501
                                );
502
                        }
503
                        else
504
                        {
505
                                if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
506
                                {
507
                                        $template->assign_block_vars('searchresults', array(
508
                                                'S_IGNORE_POST' => true, 
509
510
                                                'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"search.$phpEx$SID&amp;search_session_id=$search_session_id&amp;$u_sort_param&amp;p=" . $row['post_id'] . '&amp;view=show#' . $row['post_id'] . '">', '</a>'))
511
                                        );
512
        
513
                                        continue;
514
                                }
515
516
                                if ($row['enable_html'])
517
                                {
518
                                        $row['post_text'] = preg_replace('#(<!\-\- h \-\-><)([\/]?.*?)(><!\-\- h \-\->)#is', "&lt;\\2&gt;", $row['post_text']);
519
                                }
520
521
                                $row['post_text'] = censor_text($row['post_text']);
522
                                decode_message($row['post_text'], $row['bbcode_uid']);
523
                
524
                                if ($return_chars)
525
                                {
526
                                        $row['post_text'] = (strlen($row['post_text']) < $return_chars + 3) ? $row['post_text'] : substr($row['post_text'], 0, $return_chars) . '...';
527
                                }
528
529
                                if ($hilit)
530
                                {
531
                                        // This was shamelessly 'borrowed' from volker at multiartstudio dot de
532
                                        // via php.net's annotated manual
533
                                        $row['post_text'] = str_replace('\"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . str_replace('\\', '\\\\', $hilit) . ")\b#i', '<span class=\"posthilit\">\\\\1</span>', '\\0')", '>' . $row['post_text'] . '<'), 1, -1));
534
                                }
535
536
                                $row['post_text'] = smiley_text($row['post_text']);
537
538
                                // Replace naughty words such as farty pants
539
                                $row['post_subject'] = censor_text($row['post_subject']);
540
                                $row['post_text'] = str_replace("\n", '<br />', censor_text($row['post_text']));
541
542
                                $tpl_ary = array(
543
                                        'POSTER_NAME'                => ($row['poster_id'] == ANONYMOUS) ? ((!empty($row['post_username'])) ? $row['post_username'] : $user->lang['GUEST']) : $row['username'], 
544
                                        'POST_SUBJECT'                => censor_text($row['post_subject']), 
545
                                        'POST_DATE'                        => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '', 
546
                                        'MESSAGE'                         => $row['post_text']
547
                                );
548
                        }
549
550
                        $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
551
                                'FORUM_ID'                         => $forum_id,
552
                                'TOPIC_ID'                         => $topic_id,
553
                                'POST_ID'                        => ($show_results == 'posts') ? $row['post_id'] : false, 
554
555
                                'FORUM_TITLE'                => $row['forum_name'], 
556
                                'TOPIC_TITLE'                 => censor_text($row['topic_title']),
557
558
                                'U_VIEW_TOPIC'                => $view_topic_url,
559
                                'U_VIEW_FORUM'                => "viewforum.$phpEx$SID&amp;f=$forum_id", 
560
                                'U_VIEW_POST'                => (!empty($row['post_id'])) ? "viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=" . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '&amp;hilit=' . $u_hilit . '#' . $row['post_id'] : '')
561
                        ));
562
                }
563
                $db->sql_freeresult($result);
564
        }
565
        else
566
        {
567
                $template->assign_vars(array(
568
                        'S_NO_SEARCH_RESULTS'        => true)
569
                );
570
        }
571
572
        page_header($user->lang['SEARCH']);
573
574
        $template->set_filenames(array(
575
                'body' =>  'search_results.html')
576
        );
577
        make_jumpbox('viewforum.'.$phpEx);
578
579
        page_footer();
580
}
581
582
583
// Search forum
584
$s_forums = '';
585
$sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, fa.user_id
586
        FROM (' . FORUMS_TABLE . ' f
587
        LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON  (fa.forum_id = f.forum_id
588
                AND fa.session_id = '" . $db->sql_escape($user->data['session_id']) . "'))
589
        ORDER BY f.left_id ASC";
590
$result = $db->sql_query($sql);
591
592
$right = $cat_right = $padding_inc = 0;
593
$padding = $forum_list = $holding = '';
594
$pad_store = array('0' => '');
595
$search_forums = array();
596
597
while ($row = $db->sql_fetchrow($result))
598
{
599
        if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
600
        {
601
                // Non-postable forum with no subforums, don't display
602
                continue;
603
        }
604
605
        if (!$auth->acl_get('f_list', $row['forum_id']) || $row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
606
        {
607
                // if the user does not have permissions to list this forum skip
608
                continue;
609
        }
610
611
        if ($row['left_id'] < $right)
612
        {
613
                $padding .= '&nbsp; &nbsp;';
614
                $pad_store[$row['parent_id']] = $padding;
615
        }
616
        else if ($row['left_id'] > $right + 1)
617
        {
618
                $padding = $pad_store[$row['parent_id']];
619
        }
620
621
        $right = $row['right_id'];
622
623
        $selected = (!sizeof($search_forums) || in_array($row['forum_id'], $search_forums)) ? ' selected="selected"' : '';
624
625
        if ($row['left_id'] > $cat_right)
626
        {
627
                $holding = '';
628
        }
629
630
        if ($row['right_id'] - $row['left_id'] > 1)
631
        {
632
                $cat_right = max($cat_right, $row['right_id']);
633
634
                $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
635
        }
636
        else
637
        {
638
                $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
639
                $holding = '';
640
        }
641
}
642
$db->sql_freeresult($result);
643
unset($pad_store);
644
645
// Number of chars returned
646
$s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
647
$s_characters .= '<option value="0">0</option>';
648
$s_characters .= '<option value="25">25</option>';
649
$s_characters .= '<option value="50">50</option>';
650
651
for ($i = 100; $i <= 1000 ; $i += 100)
652
{
653
        $selected = ($i == 200) ? ' selected="selected"' : '';
654
        $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
655
}
656
657
$template->assign_vars(array(
658
        'S_SEARCH_ACTION'                => "{$phpbb_root_path}search.$phpEx$SID&amp;mode=results",
659
        'S_CHARACTER_OPTIONS'        => $s_characters,
660
        'S_FORUM_OPTIONS'                => $s_forums,
661
        'S_SELECT_SORT_DIR'                => $s_sort_dir,
662
        'S_SELECT_SORT_KEY'                => $s_sort_key,
663
        'S_SELECT_SORT_DAYS'        => $s_limit_days)
664
);
665
666
$sql = 'SELECT search_id, search_time, search_array
667
        FROM ' . SEARCH_TABLE . '
668
        ORDER BY search_time DESC';
669
$result = $db->sql_query($sql);
670
671
$i = 0;
672
while ($row = $db->sql_fetchrow($result))
673
{
674
        if ($i == 5)
675
        {
676
                break;
677
        }
678
679
        $data = explode('#', $row['search_array']);
680
        $split_words = htmlspecialchars(implode(' ', unserialize(array_shift($data))));
681
682
        if (!$split_words)
683
        {
684
                continue;
685
        }
686
687
        $common_words = htmlspecialchars(implode(' ', unserialize(array_shift($data))));
688
        unset($data);
689
690
        $template->assign_block_vars('recentsearch', array(
691
                'KEYWORDS'        => $split_words,
692
                'TIME'                => $user->format_date($row['search_time']),
693
694
                'U_KEYWORDS'        => "{$phpbb_root_path}search.$phpEx$SID&amp;keywords=" . urlencode($split_words))
695
        );
696
697
        $i++;
698
}
699
$db->sql_freeresult($result);
700
701
// Output the basic page
702
page_header($user->lang['SEARCH']);
703
704
$template->set_filenames(array(
705
        'body' => 'search_body.html')
706
);
707
make_jumpbox('viewforum.'.$phpEx);
708
709
page_footer();
710
711
?>