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