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

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

mise a jour du trunk

File size: 37.7 KB
Line 
1<?php
2
3/**
4 * Copyright (C) 2008-2011 FluxBB
5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 */
8
9// The contents of this file are very much inspired by the file search.php
10// from the phpBB Group forum software phpBB2 (http://www.phpbb.com)
11
12define('PUN_ROOT', dirname(__FILE__).'/');
13require PUN_ROOT.'include/common.php';
14
15// Load the search.php language file
16require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php';
17require PUN_ROOT.'lang/'.$pun_user['language'].'/forum.php';
18
19
20if ($pun_user['g_read_board'] == '0')
21        message($lang_common['No view']);
22else if ($pun_user['g_search'] == '0')
23        message($lang_search['No search permission']);
24
25require PUN_ROOT.'include/search_idx.php';
26
27// Figure out what to do :-)
28if (isset($_GET['action']) || isset($_GET['search_id']))
29{
30        $action = (isset($_GET['action'])) ? $_GET['action'] : null;
31        $forums = isset($_GET['forums']) ? (is_array($_GET['forums']) ? $_GET['forums'] : explode(',', $_GET['forums'])) : (isset($_GET['forum']) ? array($_GET['forum']) : array());
32        $sort_dir = (isset($_GET['sort_dir']) && $_GET['sort_dir'] == 'DESC') ? 'DESC' : 'ASC';
33
34        $forums = array_map('intval', $forums);
35
36        // Allow the old action names for backwards compatibility reasons
37        if ($action == 'show_user')
38                $action = 'show_user_posts';
39        else if ($action == 'show_24h')
40                $action = 'show_recent';
41
42        // If a search_id was supplied
43        if (isset($_GET['search_id']))
44        {
45                $search_id = intval($_GET['search_id']);
46                if ($search_id < 1)
47                        message($lang_common['Bad request']);
48        }
49        // If it's a regular search (keywords and/or author)
50        else if ($action == 'search')
51        {
52                $keywords = (isset($_GET['keywords'])) ? utf8_strtolower(pun_trim($_GET['keywords'])) : null;
53                $author = (isset($_GET['author'])) ? utf8_strtolower(pun_trim($_GET['author'])) : null;
54
55                if (preg_match('%^[\*\%]+$%', $keywords) || (pun_strlen(str_replace(array('*', '%'), '', $keywords)) < PUN_SEARCH_MIN_WORD && !is_cjk($keywords)))
56                        $keywords = '';
57
58                if (preg_match('%^[\*\%]+$%', $author) || pun_strlen(str_replace(array('*', '%'), '', $author)) < 2)
59                        $author = '';
60
61                if (!$keywords && !$author)
62                        message($lang_search['No terms']);
63
64                if ($author)
65                        $author = str_replace('*', '%', $author);
66
67                $show_as = (isset($_GET['show_as']) && $_GET['show_as'] == 'topics') ? 'topics' : 'posts';
68                $sort_by = (isset($_GET['sort_by'])) ? intval($_GET['sort_by']) : 0;
69                $search_in = (!isset($_GET['search_in']) || $_GET['search_in'] == '0') ? 0 : (($_GET['search_in'] == '1') ? 1 : -1);
70        }
71        // If it's a user search (by ID)
72        else if ($action == 'show_user_posts' || $action == 'show_user_topics' || $action == 'show_subscriptions')
73        {
74                $user_id = (isset($_GET['user_id'])) ? intval($_GET['user_id']) : $pun_user['id'];
75                if ($user_id < 2)
76                        message($lang_common['Bad request']);
77
78                // Subscribed topics can only be viewed by admins, moderators and the users themselves
79                if ($action == 'show_subscriptions' && !$pun_user['is_admmod'] && $user_id != $pun_user['id'])
80                        message($lang_common['No permission']);
81        }
82        else if ($action == 'show_recent')
83                $interval = isset($_GET['value']) ? intval($_GET['value']) : 86400;
84        else if ($action == 'show_replies')
85        {
86                if ($pun_user['is_guest'])
87                        message($lang_common['Bad request']);
88        }
89        else if ($action != 'show_new' && $action != 'show_unanswered')
90                message($lang_common['Bad request']);
91
92
93        // If a valid search_id was supplied we attempt to fetch the search results from the db
94        if (isset($search_id))
95        {
96                $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
97
98                $result = $db->query('SELECT search_data FROM '.$db->prefix.'search_cache WHERE id='.$search_id.' AND ident=\''.$db->escape($ident).'\'') or error('Unable to fetch search results', __FILE__, __LINE__, $db->error());
99                if ($row = $db->fetch_assoc($result))
100                {
101                        $temp = unserialize($row['search_data']);
102
103                        $search_ids = unserialize($temp['search_ids']);
104                        $num_hits = $temp['num_hits'];
105                        $sort_by = $temp['sort_by'];
106                        $sort_dir = $temp['sort_dir'];
107                        $show_as = $temp['show_as'];
108                        $search_type = $temp['search_type'];
109
110                        unset($temp);
111                }
112                else
113                        message($lang_search['No hits']);
114        }
115        else
116        {
117                $keyword_results = $author_results = array();
118
119                // Search a specific forum?
120                $forum_sql = (!empty($forums) || (empty($forums) && $pun_config['o_search_all_forums'] == '0' && !$pun_user['is_admmod'])) ? ' AND t.forum_id IN ('.implode(',', $forums).')' : '';
121
122                if (!empty($author) || !empty($keywords))
123                {
124                        // Flood protection
125                        if ($pun_user['last_search'] && (time() - $pun_user['last_search']) < $pun_user['g_search_flood'] && (time() - $pun_user['last_search']) >= 0)
126                                message(sprintf($lang_search['Search flood'], $pun_user['g_search_flood']));
127
128                        if (!$pun_user['is_guest'])
129                                $db->query('UPDATE '.$db->prefix.'users SET last_search='.time().' WHERE id='.$pun_user['id']) or error('Unable to update user', __FILE__, __LINE__, $db->error());
130                        else
131                                $db->query('UPDATE '.$db->prefix.'online SET last_search='.time().' WHERE ident=\''.$db->escape(get_remote_address()).'\'' ) or error('Unable to update user', __FILE__, __LINE__, $db->error());
132
133                        switch ($sort_by)
134                        {
135                                case 1:
136                                        $sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster';
137                                        $sort_type = SORT_STRING;
138                                        break;
139
140                                case 2:
141                                        $sort_by_sql = 't.subject';
142                                        $sort_type = SORT_STRING;
143                                        break;
144
145                                case 3:
146                                        $sort_by_sql = 't.forum_id';
147                                        $sort_type = SORT_NUMERIC;
148                                        break;
149
150                                case 4:
151                                        $sort_by_sql = 't.last_post';
152                                        $sort_type = SORT_NUMERIC;
153                                        break;
154
155                                default:
156                                        $sort_by_sql = ($show_as == 'topics') ? 't.last_post' : 'p.posted';
157                                        $sort_type = SORT_NUMERIC;
158                                        break;
159                        }
160
161                        // If it's a search for keywords
162                        if ($keywords)
163                        {
164                                // split the keywords into words
165                                $keywords_array = split_words($keywords, false);
166
167                                if (empty($keywords_array))
168                                        message($lang_search['No hits']);
169
170                                // Should we search in message body or topic subject specifically?
171                                $search_in_cond = ($search_in) ? (($search_in > 0) ? ' AND m.subject_match = 0' : ' AND m.subject_match = 1') : '';
172
173                                $word_count = 0;
174                                $match_type = 'and';
175
176                                $sort_data = array();
177                                foreach ($keywords_array as $cur_word)
178                                {
179                                        switch ($cur_word)
180                                        {
181                                                case 'and':
182                                                case 'or':
183                                                case 'not':
184                                                        $match_type = $cur_word;
185                                                        break;
186
187                                                default:
188                                                {
189                                                        if (is_cjk($cur_word))
190                                                        {
191                                                                $where_cond = str_replace('*', '%', $cur_word);
192                                                                $where_cond = ($search_in ? (($search_in > 0) ? 'p.message LIKE \'%'.$db->escape($where_cond).'%\'' : 't.subject LIKE \'%'.$db->escape($where_cond).'%\'') : 'p.message LIKE \'%'.$db->escape($where_cond).'%\' OR t.subject LIKE \'%'.$db->escape($where_cond).'%\'');
193
194                                                                $result = $db->query('SELECT p.id AS post_id, p.topic_id, '.$sort_by_sql.' AS sort_by FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE ('.$where_cond.') AND (fp.read_forum IS NULL OR fp.read_forum=1)'.$forum_sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error());
195                                                        }
196                                                        else
197                                                                $result = $db->query('SELECT m.post_id, p.topic_id, '.$sort_by_sql.' AS sort_by FROM '.$db->prefix.'search_words AS w INNER JOIN '.$db->prefix.'search_matches AS m ON m.word_id = w.id INNER JOIN '.$db->prefix.'posts AS p ON p.id=m.post_id INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE w.word LIKE \''.$db->escape(str_replace('*', '%', $cur_word)).'\''.$search_in_cond.' AND (fp.read_forum IS NULL OR fp.read_forum=1)'.$forum_sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error());
198
199                                                        $row = array();
200                                                        while ($temp = $db->fetch_assoc($result))
201                                                        {
202                                                                $row[$temp['post_id']] = $temp['topic_id'];
203
204                                                                if (!$word_count)
205                                                                {
206                                                                        $keyword_results[$temp['post_id']] = $temp['topic_id'];
207                                                                        $sort_data[$temp['post_id']] = $temp['sort_by'];
208                                                                }
209                                                                else if ($match_type == 'or')
210                                                                {
211                                                                        $keyword_results[$temp['post_id']] = $temp['topic_id'];
212                                                                        $sort_data[$temp['post_id']] = $temp['sort_by'];
213                                                                }
214                                                                else if ($match_type == 'not')
215                                                                {
216                                                                        unset($keyword_results[$temp['post_id']]);
217                                                                        unset($sort_data[$temp['post_id']]);
218                                                                }
219                                                        }
220
221                                                        if ($match_type == 'and' && $word_count)
222                                                        {
223                                                                foreach ($keyword_results as $post_id => $topic_id)
224                                                                {
225                                                                        if (!isset($row[$post_id]))
226                                                                        {
227                                                                                unset($keyword_results[$post_id]);
228                                                                                unset($sort_data[$post_id]);
229                                                                        }
230                                                                }
231                                                        }
232
233                                                        ++$word_count;
234                                                        $db->free_result($result);
235
236                                                        break;
237                                                }
238                                        }
239                                }
240
241                                // Sort the results - annoyingly array_multisort re-indexes arrays with numeric keys, so we need to split the keys out into a seperate array then combine them again after
242                                $post_ids = array_keys($keyword_results);
243                                $topic_ids = array_values($keyword_results);
244
245                                array_multisort(array_values($sort_data), $sort_dir == 'DESC' ? SORT_DESC : SORT_ASC, $sort_type, $post_ids, $topic_ids);
246
247                                // combine the arrays back into a key=>value array (array_combine is PHP5 only unfortunately)
248                                $num_results = count($keyword_results);
249                                $keyword_results = array();
250                                for ($i = 0;$i < $num_results;$i++)
251                                        $keyword_results[$post_ids[$i]] = $topic_ids[$i];
252
253                                unset($sort_data, $post_ids, $topic_ids);
254                        }
255
256                        // If it's a search for author name (and that author name isn't Guest)
257                        if ($author && $author != 'guest' && $author != utf8_strtolower($lang_common['Guest']))
258                        {
259                                switch ($db_type)
260                                {
261                                        case 'pgsql':
262                                                $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username ILIKE \''.$db->escape($author).'\'') or error('Unable to fetch users', __FILE__, __LINE__, $db->error());
263                                                break;
264
265                                        default:
266                                                $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username LIKE \''.$db->escape($author).'\'') or error('Unable to fetch users', __FILE__, __LINE__, $db->error());
267                                                break;
268                                }
269
270                                if ($db->num_rows($result))
271                                {
272                                        $user_ids = array();
273                                        while ($row = $db->fetch_row($result))
274                                                $user_ids[] = $row[0];
275
276                                        $result = $db->query('SELECT p.id AS post_id, p.topic_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id IN('.implode(',', $user_ids).')'.$forum_sql.' ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch matched posts list', __FILE__, __LINE__, $db->error());
277                                        while ($temp = $db->fetch_assoc($result))
278                                                $author_results[$temp['post_id']] = $temp['topic_id'];
279
280                                        $db->free_result($result);
281                                }
282                        }
283
284                        // If we searched for both keywords and author name we want the intersection between the results
285                        if ($author && $keywords)
286                        {
287                                $search_ids = array_intersect_assoc($keyword_results, $author_results);
288                                $search_type = array('both', array($keywords, pun_trim($_GET['author'])), implode(',', $forums), $search_in);
289                        }
290                        else if ($keywords)
291                        {
292                                $search_ids = $keyword_results;
293                                $search_type = array('keywords', $keywords, implode(',', $forums), $search_in);
294                        }
295                        else
296                        {
297                                $search_ids = $author_results;
298                                $search_type = array('author', pun_trim($_GET['author']), implode(',', $forums), $search_in);
299                        }
300
301                        unset($keyword_results, $author_results);
302
303                        if ($show_as == 'topics')
304                                $search_ids = array_values($search_ids);
305                        else
306                                $search_ids = array_keys($search_ids);
307
308                        $search_ids = array_unique($search_ids);
309
310                        $num_hits = count($search_ids);
311                        if (!$num_hits)
312                                message($lang_search['No hits']);
313                }
314                else if ($action == 'show_new' || $action == 'show_recent' || $action == 'show_replies' || $action == 'show_user_posts' || $action == 'show_user_topics' || $action == 'show_subscriptions' || $action == 'show_unanswered')
315                {
316                        $search_type = array('action', $action);
317                        $show_as = 'topics';
318                        // We want to sort things after last post
319                        $sort_by = 0;
320                        $sort_dir = 'DESC';
321
322                        // If it's a search for new posts since last visit
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 LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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'].' AND t.moved_to IS NULL'.(isset($_GET['fid']) ? ' AND t.forum_id='.intval($_GET['fid']) : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 recent posts (in a certain time interval)
335                        else if ($action == 'show_recent')
336                        {
337                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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() - $interval).' AND t.moved_to IS NULL'.(isset($_GET['fid']) ? ' AND t.forum_id='.intval($_GET['fid']) : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 topics in which the user has posted
344                        else if ($action == 'show_replies')
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 LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id='.$pun_user['id'].' GROUP BY t.id'.($db_type == 'pgsql' ? ', t.last_post' : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 posts by a specific user ID
353                        else if ($action == 'show_user_posts')
354                        {
355                                $show_as = 'posts';
356
357                                $result = $db->query('SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON p.topic_id=t.id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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.' ORDER BY p.posted DESC') or error('Unable to fetch user posts', __FILE__, __LINE__, $db->error());
358                                $num_hits = $db->num_rows($result);
359
360                                if (!$num_hits)
361                                        message($lang_search['No user posts']);
362
363                                // Pass on the user ID so that we can later know whos posts we're searching for
364                                $search_type[2] = $user_id;
365                        }
366                        // If it's a search for topics by a specific user ID
367                        else if ($action == 'show_user_topics')
368                        {
369                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.first_post_id=p.id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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.' ORDER BY t.last_post DESC') or error('Unable to fetch user topics', __FILE__, __LINE__, $db->error());
370                                $num_hits = $db->num_rows($result);
371
372                                if (!$num_hits)
373                                        message($lang_search['No user topics']);
374
375                                // Pass on the user ID so that we can later know whos topics we're searching for
376                                $search_type[2] = $user_id;
377                        }
378                        // If it's a search for subscribed topics
379                        else if ($action == 'show_subscriptions')
380                        {
381                                if ($pun_user['is_guest'])
382                                        message($lang_common['Bad request']);
383
384                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$user_id.') LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
385                                $num_hits = $db->num_rows($result);
386
387                                if (!$num_hits)
388                                        message($lang_search['No subscriptions']);
389
390                                // Pass on user ID so that we can later know whose subscriptions we're searching for
391                                $search_type[2] = $user_id;
392                        }
393                        // If it's a search for unanswered posts
394                        else
395                        {
396                                $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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 ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
397                                $num_hits = $db->num_rows($result);
398
399                                if (!$num_hits)
400                                        message($lang_search['No unanswered']);
401                        }
402
403                        $search_ids = array();
404                        while ($row = $db->fetch_row($result))
405                                $search_ids[] = $row[0];
406
407                        $db->free_result($result);
408                }
409                else
410                        message($lang_common['Bad request']);
411
412
413                // Prune "old" search results
414                $old_searches = array();
415                $result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());
416
417                if ($db->num_rows($result))
418                {
419                        while ($row = $db->fetch_row($result))
420                                $old_searches[] = '\''.$db->escape($row[0]).'\'';
421
422                        $db->query('DELETE FROM '.$db->prefix.'search_cache WHERE ident NOT IN('.implode(',', $old_searches).')') or error('Unable to delete search results', __FILE__, __LINE__, $db->error());
423                }
424
425                // Fill an array with our results and search properties
426                $temp = serialize(array(
427                        'search_ids'            => serialize($search_ids),
428                        'num_hits'                      => $num_hits,
429                        'sort_by'                       => $sort_by,
430                        'sort_dir'                      => $sort_dir,
431                        'show_as'                       => $show_as,
432                        'search_type'           => $search_type
433                ));
434                $search_id = mt_rand(1, 2147483647);
435
436                $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
437
438                $db->query('INSERT INTO '.$db->prefix.'search_cache (id, ident, search_data) VALUES('.$search_id.', \''.$db->escape($ident).'\', \''.$db->escape($temp).'\')') or error('Unable to insert search results', __FILE__, __LINE__, $db->error());
439
440                if ($search_type[0] != 'action')
441                {
442                        $db->end_transaction();
443                        $db->close();
444
445                        // Redirect the user to the cached result page
446                        header('Location: search.php?search_id='.$search_id);
447                        exit;
448                }
449        }
450
451        $forum_actions = array();
452
453        // If we're on the new posts search, display a "mark all as read" link
454        if (!$pun_user['is_guest'] && $search_type[0] == 'action' && $search_type[1] == 'show_new')
455                $forum_actions[] = '<a href="misc.php?action=markread">'.$lang_common['Mark all as read'].'</a>';
456
457        // Fetch results to display
458        if (!empty($search_ids))
459        {
460                switch ($sort_by)
461                {
462                        case 1:
463                                $sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster';
464                                break;
465
466                        case 2:
467                                $sort_by_sql = 't.subject';
468                                break;
469
470                        case 3:
471                                $sort_by_sql = 't.forum_id';
472                                break;
473
474                        default:
475                                $sort_by_sql = ($show_as == 'topics') ? 't.last_post' : 'p.posted';
476                                break;
477                }
478
479                // Determine the topic or post offset (based on $_GET['p'])
480                $per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics'];
481                $num_pages = ceil($num_hits / $per_page);
482
483                $p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : intval($_GET['p']);
484                $start_from = $per_page * ($p - 1);
485
486                // Generate paging links
487                $paging_links = '<span class="pages-label">'.$lang_common['Pages'].' </span>'.paginate($num_pages, $p, 'search.php?search_id='.$search_id);
488
489                // throw away the first $start_from of $search_ids, only keep the top $per_page of $search_ids
490                $search_ids = array_slice($search_ids, $start_from, $per_page);
491
492                // Run the query and fetch the results
493                if ($show_as == 'posts')
494                        $result = $db->query('SELECT p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, p.message, p.hide_smilies, t.id AS tid, t.poster, t.subject, t.first_post_id, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id, f.forum_name 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 WHERE p.id IN('.implode(',', $search_ids).') ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error());
495                else
496                        $result = $db->query('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.sticky, t.forum_id, f.forum_name FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id WHERE t.id IN('.implode(',', $search_ids).') ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error());
497
498                $search_set = array();
499                while ($row = $db->fetch_assoc($result))
500                        $search_set[] = $row;
501
502                $crumbs_text = array();
503                $crumbs_text['show_as'] = $lang_search['Search'];
504
505                if ($search_type[0] == 'action')
506                {
507                        if ($search_type[1] == 'show_user_topics')
508                                $crumbs_text['search_type'] = '<a href="search.php?action=show_user_topics&amp;user_id='.$search_type[2].'">'.sprintf($lang_search['Quick search show_user_topics'], pun_htmlspecialchars($search_set[0]['poster'])).'</a>';
509                        else if ($search_type[1] == 'show_user_posts')
510                                $crumbs_text['search_type'] = '<a href="search.php?action=show_user_posts&amp;user_id='.$search_type[2].'">'.sprintf($lang_search['Quick search show_user_posts'], pun_htmlspecialchars($search_set[0]['pposter'])).'</a>';
511                        else if ($search_type[1] == 'show_subscriptions')
512                        {
513                                // Fetch username of subscriber
514                                $subscriber_id = $search_type[2];
515                                $result = $db->query('SELECT username FROM '.$db->prefix.'users WHERE id='.$subscriber_id) or error('Unable to fetch username of subscriber', __FILE__, __LINE__, $db->error());
516
517                                if ($db->num_rows($result))
518                                        $subscriber_name = $db->result($result);
519                                else
520                                        message($lang_common['Bad request']);
521
522                                $crumbs_text['search_type'] = '<a href="search.php?action=show_subscriptions&amp;user_id='.$subscriber_id.'">'.sprintf($lang_search['Quick search show_subscriptions'], pun_htmlspecialchars($subscriber_name)).'</a>';
523                        }
524                        else
525                                $crumbs_text['search_type'] = '<a href="search.php?action='.$search_type[1].'">'.$lang_search['Quick search '.$search_type[1]].'</a>';
526                }
527                else
528                {
529                        $keywords = $author = '';
530
531                        if ($search_type[0] == 'both')
532                        {
533                                list ($keywords, $author) = $search_type[1];
534                                $crumbs_text['search_type'] = sprintf($lang_search['By both show as '.$show_as], pun_htmlspecialchars($keywords), pun_htmlspecialchars($author));
535                        }
536                        else if ($search_type[0] == 'keywords')
537                        {
538                                $keywords = $search_type[1];
539                                $crumbs_text['search_type'] = sprintf($lang_search['By keywords show as '.$show_as], pun_htmlspecialchars($keywords));
540                        }
541                        else if ($search_type[0] == 'author')
542                        {
543                                $author = $search_type[1];
544                                $crumbs_text['search_type'] = sprintf($lang_search['By user show as '.$show_as], pun_htmlspecialchars($author));
545                        }
546
547                        $crumbs_text['search_type'] = '<a href="search.php?action=search&amp;keywords='.urlencode($keywords).'&amp;author='.urlencode($author).'&amp;forums='.$search_type[2].'&amp;search_in='.$search_type[3].'&amp;sort_by='.$sort_by.'&amp;sort_dir='.$sort_dir.'&amp;show_as='.$show_as.'">'.$crumbs_text['search_type'].'</a>';
548                }
549
550                $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_search['Search results']);
551                define('PUN_ACTIVE_PAGE', 'search');
552                require PUN_ROOT.'header.php';
553
554?>
555<div class="linkst">
556        <div class="inbox crumbsplus">
557                <ul class="crumbs">
558                        <li><a href="index.php"><?php echo $lang_common['Index'] ?></a></li>
559                        <li><span>»&#160;</span><a href="search.php"><?php echo $crumbs_text['show_as'] ?></a></li>
560                        <li><span>»&#160;</span><strong><?php echo $crumbs_text['search_type'] ?></strong></li>
561                </ul>
562                <div class="pagepost">
563                        <p class="pagelink"><?php echo $paging_links ?></p>
564                </div>
565                <div class="clearer"></div>
566        </div>
567</div>
568
569<?php
570
571                if ($show_as == 'topics')
572                {
573                        $topic_count = 0;
574
575?>
576<div id="vf" class="blocktable">
577        <h2><span><?php echo $lang_search['Search results'] ?></span></h2>
578        <div class="box">
579                <div class="inbox">
580                        <table cellspacing="0">
581                        <thead>
582                                <tr>
583                                        <th class="tcl" scope="col"><?php echo $lang_common['Topic'] ?></th>
584                                        <th class="tc2" scope="col"><?php echo $lang_common['Forum'] ?></th>
585                                        <th class="tc3" scope="col"><?php echo $lang_common['Replies'] ?></th>
586                                        <th class="tcr" scope="col"><?php echo $lang_common['Last post'] ?></th>
587                                </tr>
588                        </thead>
589                        <tbody>
590<?php
591
592                }
593                else if ($show_as == 'posts')
594                {
595                        require PUN_ROOT.'lang/'.$pun_user['language'].'/topic.php';
596
597                        require PUN_ROOT.'include/parser.php';
598
599                        $post_count = 0;
600                }
601
602                // Get topic/forum tracking data
603                if (!$pun_user['is_guest'])
604                        $tracked_topics = get_tracked_topics();
605
606                foreach ($search_set as $cur_search)
607                {
608                        $forum = '<a href="viewforum.php?id='.$cur_search['forum_id'].'">'.pun_htmlspecialchars($cur_search['forum_name']).'</a>';
609
610                        if ($pun_config['o_censoring'] == '1')
611                                $cur_search['subject'] = censor_words($cur_search['subject']);
612
613                        if ($show_as == 'posts')
614                        {
615                                ++$post_count;
616                                $icon_type = 'icon';
617
618                                if (!$pun_user['is_guest'] && $cur_search['last_post'] > $pun_user['last_visit'] && (!isset($tracked_topics['topics'][$cur_search['tid']]) || $tracked_topics['topics'][$cur_search['tid']] < $cur_search['last_post']) && (!isset($tracked_topics['forums'][$cur_search['forum_id']]) || $tracked_topics['forums'][$cur_search['forum_id']] < $cur_search['last_post']))
619                                {
620                                        $item_status = 'inew';
621                                        $icon_type = 'icon icon-new';
622                                        $icon_text = $lang_topic['New icon'];
623                                }
624                                else
625                                {
626                                        $item_status = '';
627                                        $icon_text = '<!-- -->';
628                                }
629
630                                if ($pun_config['o_censoring'] == '1')
631                                        $cur_search['message'] = censor_words($cur_search['message']);
632
633                                $message = parse_message($cur_search['message'], $cur_search['hide_smilies']);
634                                $pposter = pun_htmlspecialchars($cur_search['pposter']);
635
636                                if ($cur_search['poster_id'] > 1)
637                                {
638                                        if ($pun_user['g_view_users'] == '1')
639                                                $pposter = '<strong><a href="profile.php?id='.$cur_search['poster_id'].'">'.$pposter.'</a></strong>';
640                                        else
641                                                $pposter = '<strong>'.$pposter.'</strong>';
642                                }
643
644
645?>
646<div class="blockpost<?php echo ($post_count % 2 == 0) ? ' roweven' : ' rowodd' ?><?php if ($cur_search['pid'] == $cur_search['first_post_id']) echo ' firstpost' ?><?php if ($post_count == 1) echo ' blockpost1' ?><?php if ($item_status != '') echo ' '.$item_status ?>">
647        <h2><span><span class="conr">#<?php echo ($start_from + $post_count) ?></span> <span><?php if ($cur_search['pid'] != $cur_search['first_post_id']) echo $lang_topic['Re'].' ' ?><?php echo $forum ?></span> <span>»&#160;<a href="viewtopic.php?id=<?php echo $cur_search['tid'] ?>"><?php echo pun_htmlspecialchars($cur_search['subject']) ?></a></span> <span>»&#160;<a href="viewtopic.php?pid=<?php echo $cur_search['pid'].'#p'.$cur_search['pid'] ?>"><?php echo format_time($cur_search['pposted']) ?></a></span></span></h2>
648        <div class="box">
649                <div class="inbox">
650                        <div class="postbody">
651                                <div class="postleft">
652                                        <dl>
653                                                <dt><?php echo $pposter ?></dt>
654<?php if ($cur_search['pid'] == $cur_search['first_post_id']) : ?>                                              <dd><span><?php echo $lang_topic['Replies'].' '.forum_number_format($cur_search['num_replies']) ?></span></dd>
655<?php endif; ?>
656                                                <dd><div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo $icon_text ?></div></div></dd>
657                                        </dl>
658                                </div>
659                                <div class="postright">
660                                        <div class="postmsg">
661                                                <?php echo $message."\n" ?>
662                                        </div>
663                                </div>
664                                <div class="clearer"></div>
665                        </div>
666                </div>
667                <div class="inbox">
668                        <div class="postfoot clearb">
669                                <div class="postfootright">
670                                        <ul>
671                                                <li><span><a href="viewtopic.php?id=<?php echo $cur_search['tid'] ?>"><?php echo $lang_search['Go to topic'] ?></a></span></li>
672                                                <li><span><a href="viewtopic.php?pid=<?php echo $cur_search['pid'].'#p'.$cur_search['pid'] ?>"><?php echo $lang_search['Go to post'] ?></a></span></li>
673                                        </ul>
674                                </div>
675                        </div>
676                </div>
677        </div>
678</div>
679<?php
680
681                        }
682                        else
683                        {
684                                ++$topic_count;
685                                $status_text = array();
686                                $item_status = ($topic_count % 2 == 0) ? 'roweven' : 'rowodd';
687                                $icon_type = 'icon';
688
689                                $subject = '<a href="viewtopic.php?id='.$cur_search['tid'].'">'.pun_htmlspecialchars($cur_search['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_search['poster']).'</span>';
690
691                                if ($cur_search['sticky'] == '1')
692                                {
693                                        $item_status .= ' isticky';
694                                        $status_text[] = '<span class="stickytext">'.$lang_forum['Sticky'].'</span>';
695                                }
696
697                                if ($cur_search['closed'] != '0')
698                                {
699                                        $status_text[] = '<span class="closedtext">'.$lang_forum['Closed'].'</span>';
700                                        $item_status .= ' iclosed';
701                                }
702
703                                if (!$pun_user['is_guest'] && $cur_search['last_post'] > $pun_user['last_visit'] && (!isset($tracked_topics['topics'][$cur_search['tid']]) || $tracked_topics['topics'][$cur_search['tid']] < $cur_search['last_post']) && (!isset($tracked_topics['forums'][$cur_search['forum_id']]) || $tracked_topics['forums'][$cur_search['forum_id']] < $cur_search['last_post']))
704                                {
705                                        $item_status .= ' inew';
706                                        $icon_type = 'icon icon-new';
707                                        $subject = '<strong>'.$subject.'</strong>';
708                                        $subject_new_posts = '<span class="newtext">[ <a href="viewtopic.php?id='.$cur_search['tid'].'&amp;action=new" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a> ]</span>';
709                                }
710                                else
711                                        $subject_new_posts = null;
712
713                                // Insert the status text before the subject
714                                $subject = implode(' ', $status_text).' '.$subject;
715
716                                $num_pages_topic = ceil(($cur_search['num_replies'] + 1) / $pun_user['disp_posts']);
717
718                                if ($num_pages_topic > 1)
719                                        $subject_multipage = '<span class="pagestext">[ '.paginate($num_pages_topic, -1, 'viewtopic.php?id='.$cur_search['tid']).' ]</span>';
720                                else
721                                        $subject_multipage = null;
722
723                                // Should we show the "New posts" and/or the multipage links?
724                                if (!empty($subject_new_posts) || !empty($subject_multipage))
725                                {
726                                        $subject .= !empty($subject_new_posts) ? ' '.$subject_new_posts : '';
727                                        $subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
728                                }
729
730?>
731                                <tr class="<?php echo $item_status ?>">
732                                        <td class="tcl">
733                                                <div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo forum_number_format($topic_count + $start_from) ?></div></div>
734                                                <div class="tclcon">
735                                                        <div>
736                                                                <?php echo $subject."\n" ?>
737                                                        </div>
738                                                </div>
739                                        </td>
740                                        <td class="tc2"><?php echo $forum ?></td>
741                                        <td class="tc3"><?php echo forum_number_format($cur_search['num_replies']) ?></td>
742                                        <td class="tcr"><?php echo '<a href="viewtopic.php?pid='.$cur_search['last_post_id'].'#p'.$cur_search['last_post_id'].'">'.format_time($cur_search['last_post']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_search['last_poster']) ?></span></td>
743                                </tr>
744<?php
745
746                        }
747                }
748
749                if ($show_as == 'topics')
750                        echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";
751
752?>
753<div class="<?php echo ($show_as == 'topics') ? 'linksb' : 'postlinksb'; ?>">
754        <div class="inbox crumbsplus">
755                <div class="pagepost">
756                        <p class="pagelink"><?php echo $paging_links ?></p>
757                </div>
758                <ul class="crumbs">
759                        <li><a href="index.php"><?php echo $lang_common['Index'] ?></a></li>
760                        <li><span>»&#160;</span><a href="search.php"><?php echo $crumbs_text['show_as'] ?></a></li>
761                        <li><span>»&#160;</span><strong><?php echo $crumbs_text['search_type'] ?></strong></li>
762                </ul>
763<?php echo (!empty($forum_actions) ? "\t\t".'<p class="subscribelink clearb">'.implode(' - ', $forum_actions).'</p>'."\n" : '') ?>
764                <div class="clearer"></div>
765        </div>
766</div>
767<?php
768
769                require PUN_ROOT.'footer.php';
770        }
771        else
772                message($lang_search['No hits']);
773}
774
775
776$page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_search['Search']);
777$focus_element = array('search', 'keywords');
778define('PUN_ACTIVE_PAGE', 'search');
779require PUN_ROOT.'header.php';
780
781?>
782<div id="searchform" class="blockform">
783        <h2><span><?php echo $lang_search['Search'] ?></span></h2>
784        <div class="box">
785                <form id="search" method="get" action="search.php">
786                        <div class="inform">
787                                <fieldset>
788                                        <legend><?php echo $lang_search['Search criteria legend'] ?></legend>
789                                        <div class="infldset">
790                                                <input type="hidden" name="action" value="search" />
791                                                <label class="conl"><?php echo $lang_search['Keyword search'] ?><br /><input type="text" name="keywords" size="40" maxlength="100" /><br /></label>
792                                                <label class="conl"><?php echo $lang_search['Author search'] ?><br /><input id="author" type="text" name="author" size="25" maxlength="25" /><br /></label>
793                                                <p class="clearb"><?php echo $lang_search['Search info'] ?></p>
794                                        </div>
795                                </fieldset>
796                        </div>
797                        <div class="inform">
798                                <fieldset>
799                                        <legend><?php echo $lang_search['Search in legend'] ?></legend>
800                                        <div class="infldset">
801<?php
802
803$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('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());
804
805// We either show a list of forums of which multiple can be selected
806if ($pun_config['o_search_all_forums'] == '1' || $pun_user['is_admmod'])
807{
808        echo "\t\t\t\t\t\t".'<div class="conl multiselect">'.$lang_search['Forum search']."\n";
809        echo "\t\t\t\t\t\t".'<br />'."\n";
810        echo "\t\t\t\t\t\t".'<div class="checklist">'."\n";
811
812        $cur_category = 0;
813        while ($cur_forum = $db->fetch_assoc($result))
814        {
815                if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
816                {
817                        if ($cur_category)
818                                echo "\t\t\t\t\t\t\t".'</fieldset>'."\n";
819
820                        echo "\t\t\t\t\t\t\t".'<fieldset><legend><span>'.pun_htmlspecialchars($cur_forum['cat_name']).'</span></legend>'."\n";
821                        $cur_category = $cur_forum['cid'];
822                }
823
824                echo "\t\t\t\t\t\t\t\t".'<div class="checklist-item"><span class="fld-input"><input type="checkbox" name="forums[]" id="forum-'.$cur_forum['fid'].'" value="'.$cur_forum['fid'].'" /></span> <label for="forum-'.$cur_forum['fid'].'">'.pun_htmlspecialchars($cur_forum['forum_name']).'</label></div>'."\n";
825        }
826
827        echo "\t\t\t\t\t\t\t".'</fieldset>'."\n";
828        echo "\t\t\t\t\t\t".'</div>'."\n";
829        echo "\t\t\t\t\t\t".'</div>'."\n";
830}
831// ... or a simple select list for one forum only
832else
833{
834        echo "\t\t\t\t\t\t".'<label class="conl">'.$lang_search['Forum search']."\n";
835        echo "\t\t\t\t\t\t".'<br />'."\n";
836        echo "\t\t\t\t\t\t".'<select id="forum" name="forum">'."\n";
837
838        $cur_category = 0;
839        while ($cur_forum = $db->fetch_assoc($result))
840        {
841                if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
842                {
843                        if ($cur_category)
844                                echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
845
846                        echo "\t\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
847                        $cur_category = $cur_forum['cid'];
848                }
849
850                echo "\t\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'">'.pun_htmlspecialchars($cur_forum['forum_name']).'</option>'."\n";
851        }
852
853        echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
854        echo "\t\t\t\t\t\t".'</select>'."\n";
855        echo "\t\t\t\t\t\t".'<br /></label>'."\n";
856}
857
858?>
859                                                <label class="conl"><?php echo $lang_search['Search in']."\n" ?>
860                                                <br /><select id="search_in" name="search_in">
861                                                        <option value="0"><?php echo $lang_search['Message and subject'] ?></option>
862                                                        <option value="1"><?php echo $lang_search['Message only'] ?></option>
863                                                        <option value="-1"><?php echo $lang_search['Topic only'] ?></option>
864                                                </select>
865                                                <br /></label>
866                                                <p class="clearl"><?php echo $lang_search['Search in info'] ?></p>
867<?php echo ($pun_config['o_search_all_forums'] == '1' || $pun_user['is_admmod'] ? '<p>'.$lang_search['Search multiple forums info'].'</p>' : '') ?>
868                                        </div>
869                                </fieldset>
870                        </div>
871                        <div class="inform">
872                                <fieldset>
873                                        <legend><?php echo $lang_search['Search results legend'] ?></legend>
874                                        <div class="infldset">
875                                                <label class="conl"><?php echo $lang_search['Sort by']."\n" ?>
876                                                <br /><select name="sort_by">
877                                                        <option value="0"><?php echo $lang_search['Sort by post time'] ?></option>
878                                                        <option value="1"><?php echo $lang_search['Sort by author'] ?></option>
879                                                        <option value="2"><?php echo $lang_search['Sort by subject'] ?></option>
880                                                        <option value="3"><?php echo $lang_search['Sort by forum'] ?></option>
881                                                </select>
882                                                <br /></label>
883                                                <label class="conl"><?php echo $lang_search['Sort order']."\n" ?>
884                                                <br /><select name="sort_dir">
885                                                        <option value="DESC"><?php echo $lang_search['Descending'] ?></option>
886                                                        <option value="ASC"><?php echo $lang_search['Ascending'] ?></option>
887                                                </select>
888                                                <br /></label>
889                                                <label class="conl"><?php echo $lang_search['Show as']."\n" ?>
890                                                <br /><select name="show_as">
891                                                        <option value="topics"><?php echo $lang_search['Show as topics'] ?></option>
892                                                        <option value="posts"><?php echo $lang_search['Show as posts'] ?></option>
893                                                </select>
894                                                <br /></label>
895                                                <p class="clearb"><?php echo $lang_search['Search results info'] ?></p>
896                                        </div>
897                                </fieldset>
898                        </div>
899                        <p class="buttons"><input type="submit" name="search" value="<?php echo $lang_common['Submit'] ?>" accesskey="s" /></p>
900                </form>
901        </div>
902</div>
903<?php
904
905require PUN_ROOT.'footer.php';
Note: See TracBrowser for help on using the repository browser.