source: trunk/web/punbb/search.php @ 1

Last change on this file since 1 was 1, checked in by dj3c1t, 12 years ago

import initial

File size: 29.2 KB
Line 
1<?php
2/***********************************************************************
3
4  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
5
6  This file is part of PunBB.
7
8  PunBB is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published
10  by the Free Software Foundation; either version 2 of the License,
11  or (at your option) any later version.
12
13  PunBB is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  MA  02111-1307  USA
22
23************************************************************************/
24
25
26// The contents of this file are very much inspired by the file search.php
27// from the phpBB Group forum software phpBB2 (http://www.phpbb.com).
28
29
30define('PUN_ROOT', './');
31require PUN_ROOT.'include/common.php';
32
33
34// Load the search.php language file
35require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php';
36
37
38if ($pun_user['g_read_board'] == '0')
39        message($lang_common['No view']);
40else if ($pun_user['g_search'] == '0')
41        message($lang_search['No search permission']);
42
43
44// Detect two byte character sets
45$multibyte = (isset($lang_common['lang_multibyte']) && $lang_common['lang_multibyte']) ? true : false;
46
47
48// Figure out what to do :-)
49if (isset($_GET['action']) || isset($_GET['search_id']))
50{
51        $action = (isset($_GET['action'])) ? $_GET['action'] : null;
52        $forum = (isset($_GET['forum'])) ? intval($_GET['forum']) : -1;
53        $sort_dir = (isset($_GET['sort_dir'])) ? (($_GET['sort_dir'] == 'DESC') ? 'DESC' : 'ASC') : 'DESC';
54        if (isset($search_id)) unset($search_id);
55
56        // If a search_id was supplied
57        if (isset($_GET['search_id']))
58        {
59                $search_id = intval($_GET['search_id']);
60                if ($search_id < 1)
61                        message($lang_common['Bad request']);
62        }
63        // If it's a regular search (keywords and/or author)
64        else if ($action == 'search')
65        {
66                $keywords = (isset($_GET['keywords'])) ? strtolower(trim($_GET['keywords'])) : null;
67                $author = (isset($_GET['author'])) ? strtolower(trim($_GET['author'])) : null;
68               
69                if (preg_match('#^[\*%]+$#', $keywords) || strlen(str_replace(array('*', '%'), '', $keywords)) < 3)
70                        $keywords = '';
71
72                if (preg_match('#^[\*%]+$#', $author) || strlen(str_replace(array('*', '%'), '', $author)) < 3)
73                        $author = '';
74
75                if (!$keywords && !$author)
76                        message($lang_search['No terms']);
77
78                if ($author)
79                        $author = str_replace('*', '%', $author);
80
81                $show_as = (isset($_GET['show_as'])) ? $_GET['show_as'] : 'posts';
82                $sort_by = (isset($_GET['sort_by'])) ? intval($_GET['sort_by']) : null;
83                $search_in = (!isset($_GET['search_in']) || $_GET['search_in'] == 'all') ? 0 : (($_GET['search_in'] == 'message') ? 1 : -1);
84        }
85        // If it's a user search (by id)
86        else if ($action == 'show_user')
87        {
88                $user_id = intval($_GET['user_id']);
89                if ($user_id < 2)
90                        message($lang_common['Bad request']);
91        }
92        else
93        {
94                if ($action != 'show_new' && $action != 'show_24h' && $action != 'show_unanswered' && $action != 'show_subscriptions')
95                        message($lang_common['Bad request']);
96        }
97
98
99        // If a valid search_id was supplied we attempt to fetch the search results from the db
100        if (isset($search_id))
101        {
102                $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
103
104                $result = $db->query('SELECT search_data FROM '.$db->prefix.'search_cache WHERE id='.$search_id.' AND ident=\''.$db->escape($ident).'\'') or error('Impossible de retrouver les résultats de la recherche', __FILE__, __LINE__, $db->error());
105                if ($row = $db->fetch_assoc($result))
106                {
107                        $temp = unserialize($row['search_data']);
108
109                        $search_results = $temp['search_results'];
110                        $num_hits = $temp['num_hits'];
111                        $sort_by = $temp['sort_by'];
112                        $sort_dir = $temp['sort_dir'];
113                        $show_as = $temp['show_as'];
114
115                        unset($temp);
116                }
117                else
118                        message($lang_search['No hits']);
119        }
120        else
121        {
122                $keyword_results = $author_results = array();
123
124                // Search a specific forum?
125                $forum_sql = ($forum != -1) ? ' AND t.forum_id = '.$forum : '';
126
127                if (!empty($author) || !empty($keywords))
128                {
129                        // If it's a search for keywords
130                        if ($keywords)
131                        {
132                                $stopwords = (array)@file(PUN_ROOT.'lang/'.$pun_user['language'].'/stopwords.txt');
133                                $stopwords = array_map('trim', $stopwords);
134
135                                // Are we searching for multibyte charset text?
136                                if ($multibyte)
137                                {
138                                        // Strip out excessive whitespace
139                                        $keywords = trim(preg_replace('#\s+#', ' ', $keywords));
140
141                                        $keywords_array = explode(' ', $keywords);
142                                }
143                                else
144                                {
145                                        // Filter out non-alphabetical chars
146                                        $noise_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '€');
147                                        $noise_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' ,  ' ', ' ', ' ', ' ',  ' ', ' ', ' ');
148                                        $keywords = str_replace($noise_match, $noise_replace, $keywords);
149
150                                        // Strip out excessive whitespace
151                                        $keywords = trim(preg_replace('#\s+#', ' ', $keywords));
152
153                                        // Fill an array with all the words
154                                        $keywords_array = explode(' ', $keywords);
155
156                                        if (empty($keywords_array))
157                                                message($lang_search['No hits']);
158
159                                        while (list($i, $word) = @each($keywords_array))
160                                        {
161                                                $num_chars = pun_strlen($word);
162
163                                                if ($num_chars < 3 || $num_chars > 20 || in_array($word, $stopwords))
164                                                        unset($keywords_array[$i]);
165                                        }
166
167                                        // Should we search in message body or topic subject specifically?
168                                        $search_in_cond = ($search_in) ? (($search_in > 0) ? ' AND m.subject_match = 0' : ' AND m.subject_match = 1') : '';
169                                }
170
171                                $word_count = 0;
172                                $match_type = 'and';
173                                @reset($keywords_array);
174                                while (list(, $cur_word) = @each($keywords_array))
175                                {
176                                        switch ($cur_word)
177                                        {
178                                                case 'and':
179                                                case 'or':
180                                                case 'not':
181                                                        $match_type = $cur_word;
182                                                        break;
183
184                                                default:
185                                                {
186                                                        // Are we searching for multibyte charset text?
187                                                        if ($multibyte)
188                                                        {
189                                                                $cur_word = $db->escape('%'.str_replace('*', '', $cur_word).'%');
190                                                                $cur_word_like = ($db_type == 'pgsql') ? 'ILIKE \''.$cur_word.'\'' : 'LIKE \''.$cur_word.'\'';
191
192                                                                if ($search_in > 0)
193                                                                        $sql = 'SELECT id FROM '.$db->prefix.'posts WHERE message '.$cur_word_like;
194                                                                else if ($search_in < 0)
195                                                                        $sql = 'SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.subject '.$cur_word_like.' GROUP BY p.id, t.id';
196                                                                else
197                                                                        $sql = 'SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.message '.$cur_word_like.' OR t.subject '.$cur_word_like.' GROUP BY p.id, t.id';
198                                                        }
199                                                        else
200                                                        {
201                                                                $cur_word = str_replace('*', '%', $cur_word);
202                                                                $sql = 'SELECT m.post_id FROM '.$db->prefix.'search_words AS w INNER JOIN '.$db->prefix.'search_matches AS m ON m.word_id = w.id WHERE w.word LIKE \''.$cur_word.'\''.$search_in_cond;
203                                                        }
204
205                                                        $result = $db->query($sql, true) or error('Impossible de rechercher les messages', __FILE__, __LINE__, $db->error());
206
207                                                        $row = array();
208                                                        while ($temp = $db->fetch_row($result))
209                                                        {
210                                                                $row[$temp[0]] = 1;
211
212                                                                if (!$word_count)
213                                                                        $result_list[$temp[0]] = 1;
214                                                                else if ($match_type == 'or')
215                                                                        $result_list[$temp[0]] = 1;
216                                                                else if ($match_type == 'not')
217                                                                        $result_list[$temp[0]] = 0;
218                                                        }
219
220                                                        if ($match_type == 'and' && $word_count)
221                                                        {
222                                                                @reset($result_list);
223                                                                while (list($post_id,) = @each($result_list))
224                                                                {
225                                                                        if (!isset($row[$post_id]))
226                                                                                $result_list[$post_id] = 0;
227                                                                }
228                                                        }
229
230                                                        ++$word_count;
231                                                        $db->free_result($result);
232
233                                                        break;
234                                                }
235                                        }
236                                }
237
238                                @reset($result_list);
239                                while (list($post_id, $matches) = @each($result_list))
240                                {
241                                        if ($matches)
242                                                $keyword_results[] = $post_id;
243                                }
244
245                                unset($result_list);
246                        }
247
248                        // If it's a search for author name (and that author name isn't Guest)
249                        if ($author && strcasecmp($author, 'Guest') && strcasecmp($author, $lang_common['Guest']))
250                        {
251                                switch ($db_type)
252                                {
253                                        case 'pgsql':
254                                                $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username ILIKE \''.$db->escape($author).'\'') or error('Impossible de retrouver les membres', __FILE__, __LINE__, $db->error());
255                                                break;
256
257                                        default:
258                                                $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username LIKE \''.$db->escape($author).'\'') or error('Impossible de retrouver les membres', __FILE__, __LINE__, $db->error());
259                                                break;
260                                }
261
262                                if ($db->num_rows($result))
263                                {
264                                        $user_ids = '';
265                                        while ($row = $db->fetch_row($result))
266                                                $user_ids .= (($user_ids != '') ? ',' : '').$row[0];
267
268                                        $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE poster_id IN('.$user_ids.')') or error('Impossible de retrouver la liste des messages correspondants', __FILE__, __LINE__, $db->error());
269
270                                        $search_ids = array();
271                                        while ($row = $db->fetch_row($result))
272                                                $author_results[] = $row[0];
273
274                                        $db->free_result($result);
275                                }
276                        }
277
278
279                        if ($author && $keywords)
280                        {
281                                // If we searched for both keywords and author name we want the intersection between the results
282                                $search_ids = array_intersect($keyword_results, $author_results);
283                                unset($keyword_results, $author_results);
284                        }
285                        else if ($keywords)
286                                $search_ids = $keyword_results;
287                        else
288                                $search_ids = $author_results;
289
290                        $num_hits = count($search_ids);
291                        if (!$num_hits)
292                                message($lang_search['No hits']);
293
294
295                        if ($show_as == 'topics')
296                        {
297                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql.' GROUP BY t.id', true) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
298
299                                $search_ids = array();
300                                while ($row = $db->fetch_row($result))
301                                        $search_ids[] = $row[0];
302
303                                $db->free_result($result);
304
305                                $num_hits = count($search_ids);
306                        }
307                        else
308                        {
309                                $result = $db->query('SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql, true) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
310
311                                $search_ids = array();
312                                while ($row = $db->fetch_row($result))
313                                        $search_ids[] = $row[0];
314
315                                $db->free_result($result);
316
317                                $num_hits = count($search_ids);
318                        }
319                }
320                else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' || $action == 'show_subscriptions' || $action == 'show_unanswered')
321                {
322                        // If it's a search for new posts
323                        if ($action == 'show_new')
324                        {
325                                if ($pun_user['is_guest'])
326                                        message($lang_common['No permission']);
327
328                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit']) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
329                                $num_hits = $db->num_rows($result);
330
331                                if (!$num_hits)
332                                        message($lang_search['No new posts']);
333                        }
334                        // If it's a search for todays posts
335                        else if ($action == 'show_24h')
336                        {
337                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.(time() - 86400)) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
338                                $num_hits = $db->num_rows($result);
339
340                                if (!$num_hits)
341                                        message($lang_search['No recent posts']);
342                        }
343                        // If it's a search for posts by a specific user ID
344                        else if ($action == 'show_user')
345                        {
346                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id='.$user_id.' GROUP BY t.id') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
347                                $num_hits = $db->num_rows($result);
348
349                                if (!$num_hits)
350                                        message($lang_search['No user posts']);
351                        }
352                        // If it's a search for subscribed topics
353                        else if ($action == 'show_subscriptions')
354                        {
355                                if ($pun_user['is_guest'])
356                                        message($lang_common['Bad request']);
357
358                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$pun_user['id'].') INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1)') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
359                                $num_hits = $db->num_rows($result);
360
361                                if (!$num_hits)
362                                        message($lang_search['No subscriptions']);
363                        }
364                        // If it's a search for unanswered posts
365                        else
366                        {
367                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.num_replies=0 AND t.moved_to IS NULL') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error());
368                                $num_hits = $db->num_rows($result);
369
370                                if (!$num_hits)
371                                        message($lang_search['No unanswered']);
372                        }
373
374                        // We want to sort things after last post
375                        $sort_by = 4;
376
377                        $search_ids = array();
378                        while ($row = $db->fetch_row($result))
379                                $search_ids[] = $row[0];
380
381                        $db->free_result($result);
382
383                        $show_as = 'topics';
384                }
385                else
386                        message($lang_common['Bad request']);
387
388
389                // Prune "old" search results
390                $old_searches = array();
391                $result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Impossible de retrouver la liste de utilisateurs en ligne', __FILE__, __LINE__, $db->error());
392
393                if ($db->num_rows($result))
394                {
395                        while ($row = $db->fetch_row($result))
396                                $old_searches[] = '\''.$db->escape($row[0]).'\'';
397
398                        $db->query('DELETE FROM '.$db->prefix.'search_cache WHERE ident NOT IN('.implode(',', $old_searches).')') or error('Impossible de supprimer les résultats de recherches', __FILE__, __LINE__, $db->error());
399                }
400
401                // Final search results
402                $search_results = implode(',', $search_ids);
403
404                // Fill an array with our results and search properties
405                $temp['search_results'] = $search_results;
406                $temp['num_hits'] = $num_hits;
407                $temp['sort_by'] = $sort_by;
408                $temp['sort_dir'] = $sort_dir;
409                $temp['show_as'] = $show_as;
410                $temp = serialize($temp);
411                $search_id = mt_rand(1, 2147483647);
412
413                $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
414
415                $db->query('INSERT INTO '.$db->prefix.'search_cache (id, ident, search_data) VALUES('.$search_id.', \''.$db->escape($ident).'\', \''.$db->escape($temp).'\')') or error('Impossible d\'insérer les résultats de recherche', __FILE__, __LINE__, $db->error());
416
417                if ($action != 'show_new' && $action != 'show_24h')
418                {
419                        $db->end_transaction();
420                        $db->close();
421
422                        // Redirect the user to the cached result page
423                        header('Location: search.php?search_id='.$search_id);
424                        exit;
425                }
426        }
427
428
429        // Fetch results to display
430        if ($search_results != '')
431        {
432                $group_by_sql = '';
433                switch ($sort_by)
434                {
435                        case 1:
436                                $sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster';
437                                break;
438
439                        case 2:
440                                $sort_by_sql = 't.subject';
441                                break;
442
443                        case 3:
444                                $sort_by_sql = 't.forum_id';
445                                break;
446
447                        case 4:
448                                $sort_by_sql = 't.last_post';
449                                break;
450
451                        default:
452                        {
453                                $sort_by_sql = ($show_as == 'topics') ? 't.posted' : 'p.posted';
454
455                                if ($show_as == 'topics')
456                                        $group_by_sql = ', t.posted';
457
458                                break;
459                        }
460                }
461
462                if ($show_as == 'posts')
463                {
464                        $substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR';
465                        $sql = 'SELECT p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, '.$substr_sql.'(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.id IN('.$search_results.') ORDER BY '.$sort_by_sql;
466                }
467                else
468                        $sql = 'SELECT t.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.id IN('.$search_results.') GROUP BY t.id, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id'.$group_by_sql.' ORDER BY '.$sort_by_sql;
469
470
471                // Determine the topic or post offset (based on $_GET['p'])
472                $per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics'];
473                $num_pages = ceil($num_hits / $per_page);
474
475                $p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p'];
476                $start_from = $per_page * ($p - 1);
477
478                // Generate paging links
479                $paging_links = $lang_common['Pages'].': '.paginate($num_pages, $p, 'search.php?search_id='.$search_id);
480
481
482                $sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page;
483
484                $result = $db->query($sql) or error('Impossible de retrouver les résultats de recherches', __FILE__, __LINE__, $db->error());
485
486                $search_set = array();
487                while ($row = $db->fetch_assoc($result))
488                        $search_set[] = $row;
489
490                $db->free_result($result);
491
492                $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search results'];
493                require PUN_ROOT.'header.php';
494
495
496?>
497<div class="linkst">
498        <div class="inbox">
499                <p class="pagelink"><?php echo $paging_links ?></p>
500        </div>
501</div>
502
503<?php
504
505                //Set background switching on for show as posts
506                $bg_switch = true;
507
508                if ($show_as == 'topics')
509                {
510
511?>
512<div id="vf" class="blocktable">
513        <h2><span><?php echo $lang_search['Search results']; ?></span></h2>
514        <div class="box">
515                <div class="inbox">
516                        <table cellspacing="0">
517                        <thead>
518                                <tr>
519                                        <th class="tcl" scope="col"><?php echo $lang_common['Topic']; ?></th>
520                                        <th class="tc2" scope="col"><?php echo $lang_common['Forum'] ?></th>
521                                        <th class="tc3" scope="col"><?php echo $lang_common['Replies'] ?></th>
522                                        <th class="tcr" scope="col"><?php echo $lang_common['Last post'] ?></th>
523                                </tr>
524                        </thead>
525                        <tbody>
526<?php
527
528                }
529
530                // Fetch the list of forums
531                $result = $db->query('SELECT id, forum_name FROM '.$db->prefix.'forums') or error('Impossible de retrouver la liste des forums', __FILE__, __LINE__, $db->error());
532
533                $forum_list = array();
534                while ($forum_list[] = $db->fetch_row($result))
535                        ;
536
537                // Finally, lets loop through the results and output them
538                for ($i = 0; $i < count($search_set); ++$i)
539                {
540                        @reset($forum_list);
541                        while (list(, $temp) = @each($forum_list))
542                        {
543                                if ($temp[0] == $search_set[$i]['forum_id'])
544                                        $forum = '<a href="viewforum.php?id='.$temp[0].'">'.pun_htmlspecialchars($temp[1]).'</a>';
545                        }
546
547                        if ($pun_config['o_censoring'] == '1')
548                                $search_set[$i]['subject'] = censor_words($search_set[$i]['subject']);
549
550
551                        if ($show_as == 'posts')
552                        {
553                                $icon = '<div class="icon"><div class="nosize">'.$lang_common['Normal icon'].'</div></div>'."\n";
554                                $subject = '<a href="viewtopic.php?id='.$search_set[$i]['tid'].'">'.pun_htmlspecialchars($search_set[$i]['subject']).'</a>';
555
556                                if (!$pun_user['is_guest'] && $search_set[$i]['last_post'] > $pun_user['last_visit'])
557                                        $icon = '<div class="icon inew"><div class="nosize">'.$lang_common['New icon'].'</div></div>'."\n";
558
559
560                                if ($pun_config['o_censoring'] == '1')
561                                        $search_set[$i]['message'] = censor_words($search_set[$i]['message']);
562
563                                $message = str_replace("\n", '<br />', pun_htmlspecialchars($search_set[$i]['message']));
564                                $pposter = pun_htmlspecialchars($search_set[$i]['pposter']);
565
566                                if ($search_set[$i]['poster_id'] > 1)
567                                        $pposter = '<strong><a href="profile.php?id='.$search_set[$i]['poster_id'].'">'.$pposter.'</a></strong>';
568
569                                if (pun_strlen($message) >= 1000)
570                                        $message .= ' &hellip;';
571
572                                $vtpost1 = ($i == 0) ? ' vtp1' : '';
573
574                                // Switch the background color for every message.
575                                $bg_switch = ($bg_switch) ? $bg_switch = false : $bg_switch = true;
576                                $vtbg = ($bg_switch) ? ' rowodd' : ' roweven';
577
578
579?>
580<div class="blockpost searchposts<?php echo $vtbg ?>">
581        <h2><?php echo $forum ?>&#160;&raquo;&#160;<?php echo $subject ?>&#160;&raquo;&#160;<a href="viewtopic.php?pid=<?php echo $search_set[$i]['pid'].'#p'.$search_set[$i]['pid'] ?>"><?php echo format_time($search_set[$i]['pposted']) ?></a></h2>
582        <div class="box">
583                <div class="inbox">
584                        <div class="postleft">
585                                <dl>
586                                        <dt><?php echo $pposter ?></dt>
587                                        <dd>Replies: <?php echo $search_set[$i]['num_replies'] ?></dd>
588                                        <dd><?php echo $icon; ?></dd>
589                                        <dd><p class="clearb"><a href="viewtopic.php?pid=<?php echo $search_set[$i]['pid'].'#p'.$search_set[$i]['pid'] ?>"><?php echo $lang_search['Go to post'] ?></a></p></dd>
590                                </dl>
591                        </div>
592                        <div class="postright">
593                                <div class="postmsg">
594                                        <p><?php echo $message ?></p>
595                                </div>
596                        </div>
597                        <div class="clearer"></div>
598                </div>
599        </div>
600</div>
601<?php
602
603                        }
604                        else
605                        {
606                                $icon = '<div class="icon"><div class="nosize">'.$lang_common['Normal icon'].'</div></div>'."\n";
607
608                                $icon_text = $lang_common['Normal icon'];
609                                $item_status = '';
610                                $icon_type = 'icon';
611
612
613                                $subject = '<a href="viewtopic.php?id='.$search_set[$i]['tid'].'">'.pun_htmlspecialchars($search_set[$i]['subject']).'</a> <span class="byuser">'.$lang_common['by'].'&#160;'.pun_htmlspecialchars($search_set[$i]['poster']).'</span>';
614
615                                if ($search_set[$i]['closed'] != '0')
616                                {
617                                        $icon_text = $lang_common['Closed icon'];
618                                        $item_status = 'iclosed';
619                                }
620
621                                if (!$pun_user['is_guest'] && $search_set[$i]['last_post'] > $pun_user['last_visit'])
622                                {
623                                        $icon_text .= ' '.$lang_common['New icon'];
624                                        $item_status .= ' inew';
625                                        $icon_type = 'icon inew';
626                                        $subject = '<strong>'.$subject.'</strong>';
627                                        $subject_new_posts = '<span class="newtext">[&#160;<a href="viewtopic.php?id='.$search_set[$i]['tid'].'&amp;action=new" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a>&#160;]</span>';
628                                }
629                                else
630                                        $subject_new_posts = null;
631
632                                $num_pages_topic = ceil(($search_set[$i]['num_replies'] + 1) / $pun_user['disp_posts']);
633
634                                if ($num_pages_topic > 1)
635                                        $subject_multipage = '[ '.paginate($num_pages_topic, -1, 'viewtopic.php?id='.$search_set[$i]['tid']).' ]';
636                                else
637                                        $subject_multipage = null;
638
639                                // Should we show the "New posts" and/or the multipage links?
640                                if (!empty($subject_new_posts) || !empty($subject_multipage))
641                                {
642                                        $subject .= '&#160; '.(!empty($subject_new_posts) ? $subject_new_posts : '');
643                                        $subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
644                                }
645
646?>
647                                <tr<?php if ($item_status != '') echo ' class="'.trim($item_status).'"'; ?>>
648                                        <td class="tcl">
649                                                <div class="intd">
650                                                        <div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo trim($icon_text) ?></div></div>
651                                                        <div class="tclcon">
652                                                                <?php echo $subject."\n" ?>
653                                                        </div>
654                                                </div>
655                                        </td>
656                                        <td class="tc2"><?php echo $forum ?></td>
657                                        <td class="tc3"><?php echo $search_set[$i]['num_replies'] ?></td>
658                                        <td class="tcr"><?php echo '<a href="viewtopic.php?pid='.$search_set[$i]['last_post_id'].'#p'.$search_set[$i]['last_post_id'].'">'.format_time($search_set[$i]['last_post']).'</a> '.$lang_common['by'].'&#160;'.pun_htmlspecialchars($search_set[$i]['last_poster']) ?></td>
659                                </tr>
660<?php
661
662                        }
663                }
664
665                if ($show_as == 'topics')
666                        echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";
667
668?>
669<div class="<?php echo ($show_as == 'topics') ? 'linksb' : 'postlinksb'; ?>">
670        <div class="inbox">
671                <p class="pagelink"><?php echo $paging_links ?></p>
672        </div>
673</div>
674<?php
675
676                $footer_style = 'search';
677                require PUN_ROOT.'footer.php';
678        }
679        else
680                message($lang_search['No hits']);
681}
682
683
684$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search'];
685$focus_element = array('search', 'keywords');
686require PUN_ROOT.'header.php';
687
688?>
689<div id="searchform" class="blockform">
690        <h2><span><?php echo $lang_search['Search'] ?></span></h2>
691        <div class="box">
692                <form id="search" method="get" action="search.php">
693                        <div class="inform">
694                                <fieldset>
695                                        <legend><?php echo $lang_search['Search criteria legend'] ?></legend>
696                                        <div class="infldset">
697                                                <input type="hidden" name="action" value="search" />
698                                                <label class="conl"><?php echo $lang_search['Keyword search'] ?><br /><input type="text" name="keywords" size="40" maxlength="100" /><br /></label>
699                                                <label class="conl"><?php echo $lang_search['Author search'] ?><br /><input id="author" type="text" name="author" size="25" maxlength="25" /><br /></label>
700                                                <p class="clearb"><?php echo $lang_search['Search info'] ?></p>
701                                        </div>
702                                </fieldset>
703                        </div>
704                        <div class="inform">
705                                <fieldset>
706                                        <legend><?php echo $lang_search['Search in legend'] ?></legend>
707                                        <div class="infldset">
708                                                <label class="conl"><?php echo $lang_search['Forum search'] ?>
709                                                <br /><select id="forum" name="forum">
710<?php
711
712if ($pun_config['o_search_all_forums'] == '1' || $pun_user['g_id'] < PUN_GUEST)
713        echo "\t\t\t\t\t\t\t".'<option value="-1">'.$lang_search['All forums'].'</option>'."\n";
714
715$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.redirect_url IS NULL ORDER BY c.disp_position, c.id, f.disp_position', true) or error('Impossible de retrouver la liste des catégories et des forums', __FILE__, __LINE__, $db->error());
716
717$cur_category = 0;
718while ($cur_forum = $db->fetch_assoc($result))
719{
720        if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
721        {
722                if ($cur_category)
723                        echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
724
725                echo "\t\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
726                $cur_category = $cur_forum['cid'];
727        }
728
729        echo "\t\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'">'.pun_htmlspecialchars($cur_forum['forum_name']).'</option>'."\n";
730}
731
732?>
733                                                        </optgroup>
734                                                </select>
735                                                <br /></label>
736                                                <label class="conl"><?php echo $lang_search['Search in'] ?>
737                                                <br /><select id="search_in" name="search_in">
738                                                        <option value="all"><?php echo $lang_search['Message and subject'] ?></option>
739                                                        <option value="message"><?php echo $lang_search['Message only'] ?></option>
740                                                        <option value="topic"><?php echo $lang_search['Topic only'] ?></option>
741                                                </select>
742                                                <br /></label>
743                                                <p class="clearb"><?php echo $lang_search['Search in info'] ?></p>
744                                        </div>
745                                </fieldset>
746                        </div>
747                        <div class="inform">
748                                <fieldset>
749                                        <legend><?php echo $lang_search['Search results legend'] ?></legend>
750                                        <div class="infldset">
751                                                <label class="conl"><?php echo $lang_search['Sort by'] ?>
752                                                <br /><select name="sort_by">
753                                                        <option value="0"><?php echo $lang_search['Sort by post time'] ?></option>
754                                                        <option value="1"><?php echo $lang_search['Sort by author'] ?></option>
755                                                        <option value="2"><?php echo $lang_search['Sort by subject'] ?></option>
756                                                        <option value="3"><?php echo $lang_search['Sort by forum'] ?></option>
757                                                </select>
758                                                <br /></label>
759                                                <label class="conl"><?php echo $lang_search['Sort order'] ?>
760                                                <br /><select name="sort_dir">
761                                                        <option value="DESC"><?php echo $lang_search['Descending'] ?></option>
762                                                        <option value="ASC"><?php echo $lang_search['Ascending'] ?></option>
763                                                </select>
764                                                <br /></label>
765                                                <label class="conl"><?php echo $lang_search['Show as'] ?>
766                                                <br /><select name="show_as">
767                                                        <option value="topics"><?php echo $lang_search['Show as topics'] ?></option>
768                                                        <option value="posts"><?php echo $lang_search['Show as posts'] ?></option>
769                                                </select>
770                                                <br /></label>
771                                                <p class="clearb"><?php echo $lang_search['Search results info'] ?></p>
772                                        </div>
773                                </fieldset>
774                        </div>
775                        <p><input type="submit" name="search" value="<?php echo $lang_common['Submit'] ?>" accesskey="s" /></p>
776                </form>
777        </div>
778</div>
779<?php
780
781require PUN_ROOT.'footer.php';
Note: See TracBrowser for help on using the repository browser.