phpBB 2.0.19-scripts
[autoinstalls/phpBB.git] / search.php
1 <?php
2 /***************************************************************************
3  *                                search.php
4  *                            -------------------
5  *   begin                : Saturday, Feb 13, 2001
6  *   copyright            : (C) 2001 The phpBB Group
7  *   email                : support@phpbb.com
8  *
9  *   $Id: search.php,v 1.72.2.17 2005/09/14 18:14:30 acydburn Exp $
10  *
11  *
12  ***************************************************************************/
13
14 /***************************************************************************
15  *
16  *   This program is free software; you can redistribute it and/or modify
17  *   it under the terms of the GNU General Public License as published by
18  *   the Free Software Foundation; either version 2 of the License, or
19  *   (at your option) any later version.
20  *
21  ***************************************************************************/
22
23 define('IN_PHPBB', true);
24 $phpbb_root_path = './';
25 include($phpbb_root_path . 'extension.inc');
26 include($phpbb_root_path . 'common.'.$phpEx);
27 include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
28 include($phpbb_root_path . 'includes/functions_search.'.$phpEx);
29
30 //
31 // Start session management
32 //
33 $userdata = session_pagestart($user_ip, PAGE_SEARCH);
34 init_userprefs($userdata);
35 //
36 // End session management
37 //
38
39 //
40 // Define initial vars
41 //
42 if ( isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']) )
43 {
44         $mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
45 }
46 else
47 {
48         $mode = '';
49 }
50
51 if ( isset($HTTP_POST_VARS['search_keywords']) || isset($HTTP_GET_VARS['search_keywords']) )
52 {
53         $search_keywords = ( isset($HTTP_POST_VARS['search_keywords']) ) ? $HTTP_POST_VARS['search_keywords'] : $HTTP_GET_VARS['search_keywords'];
54 }
55 else
56 {
57         $search_keywords = '';
58 }
59
60 if ( isset($HTTP_POST_VARS['search_author']) || isset($HTTP_GET_VARS['search_author']))
61 {
62         $search_author = ( isset($HTTP_POST_VARS['search_author']) ) ? $HTTP_POST_VARS['search_author'] : $HTTP_GET_VARS['search_author'];
63         $search_author = phpbb_clean_username($search_author);
64 }
65 else
66 {
67         $search_author = '';
68 }
69
70 $search_id = ( isset($HTTP_GET_VARS['search_id']) ) ? $HTTP_GET_VARS['search_id'] : '';
71
72 $show_results = ( isset($HTTP_POST_VARS['show_results']) ) ? $HTTP_POST_VARS['show_results'] : 'posts';
73 $show_results = ($show_results == 'topics') ? 'topics' : 'posts';
74
75 if ( isset($HTTP_POST_VARS['search_terms']) )
76 {
77         $search_terms = ( $HTTP_POST_VARS['search_terms'] == 'all' ) ? 1 : 0;
78 }
79 else
80 {
81         $search_terms = 0;
82 }
83
84 if ( isset($HTTP_POST_VARS['search_fields']) )
85 {
86         $search_fields = ( $HTTP_POST_VARS['search_fields'] == 'all' ) ? 1 : 0;
87 }
88 else
89 {
90         $search_fields = 0;
91 }
92
93 $return_chars = ( isset($HTTP_POST_VARS['return_chars']) ) ? intval($HTTP_POST_VARS['return_chars']) : 200;
94
95 $search_cat = ( isset($HTTP_POST_VARS['search_cat']) ) ? intval($HTTP_POST_VARS['search_cat']) : -1;
96 $search_forum = ( isset($HTTP_POST_VARS['search_forum']) ) ? intval($HTTP_POST_VARS['search_forum']) : -1;
97
98 $sort_by = ( isset($HTTP_POST_VARS['sort_by']) ) ? intval($HTTP_POST_VARS['sort_by']) : 0;
99
100 if ( isset($HTTP_POST_VARS['sort_dir']) )
101 {
102         $sort_dir = ( $HTTP_POST_VARS['sort_dir'] == 'DESC' ) ? 'DESC' : 'ASC';
103 }
104 else
105 {
106         $sort_dir =  'DESC';
107 }
108
109 if ( !empty($HTTP_POST_VARS['search_time']) || !empty($HTTP_GET_VARS['search_time']))
110 {
111         $search_time = time() - ( ( ( !empty($HTTP_POST_VARS['search_time']) ) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']) ) * 86400 );
112         $topic_days = (!empty($HTTP_POST_VARS['search_time'])) ? intval($HTTP_POST_VARS['search_time']) : intval($HTTP_GET_VARS['search_time']);
113 }
114 else
115 {
116         $search_time = 0;
117         $topic_days = 0;
118 }
119
120 $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
121
122 $sort_by_types = array($lang['Sort_Time'], $lang['Sort_Post_Subject'], $lang['Sort_Topic_Title'], $lang['Sort_Author'], $lang['Sort_Forum']);
123
124 //
125 // encoding match for workaround
126 //
127 $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
128
129 //
130 // Begin core code
131 //
132 if ( $mode == 'searchuser' )
133 {
134         //
135         // This handles the simple windowed user search functions called from various other scripts
136         //
137         if ( isset($HTTP_POST_VARS['search_username']) )
138         {
139                 username_search($HTTP_POST_VARS['search_username']);
140         }
141         else
142         {
143                 username_search('');
144         }
145
146         exit;
147 }
148 else if ( $search_keywords != '' || $search_author != '' || $search_id )
149 {
150         $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'sort_dir', 'show_results', 'return_chars');
151         $search_results = '';
152
153         //
154         // Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
155         $limiter = 5000;
156
157         //
158         // Cycle through options ...
159         //
160         if ( $search_id == 'newposts' || $search_id == 'egosearch' || $search_id == 'unanswered' || $search_keywords != '' || $search_author != '' )
161         {
162                 if ( $search_id == 'newposts' || $search_id == 'egosearch' || ( $search_author != '' && $search_keywords == '' )  )
163                 {
164                         if ( $search_id == 'newposts' )
165                         {
166                                 if ( $userdata['session_logged_in'] )
167                                 {
168                                         $sql = "SELECT post_id 
169                                                 FROM " . POSTS_TABLE . " 
170                                                 WHERE post_time >= " . $userdata['user_lastvisit'];
171                                 }
172                                 else
173                                 {
174                                         redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=newposts", true));
175                                 }
176
177                                 $show_results = 'topics';
178                                 $sort_by = 0;
179                                 $sort_dir = 'DESC';
180                         }
181                         else if ( $search_id == 'egosearch' )
182                         {
183                                 if ( $userdata['session_logged_in'] )
184                                 {
185                                         $sql = "SELECT post_id 
186                                                 FROM " . POSTS_TABLE . " 
187                                                 WHERE poster_id = " . $userdata['user_id'];
188                                 }
189                                 else
190                                 {
191                                         redirect(append_sid("login.$phpEx?redirect=search.$phpEx&search_id=egosearch", true));
192                                 }
193
194                                 $show_results = 'topics';
195                                 $sort_by = 0;
196                                 $sort_dir = 'DESC';
197                         }
198                         else
199                         {
200                                 if (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($search_author))))
201                                 {
202                                         $search_author = '';
203                                 }
204
205                                 $search_author = str_replace('*', '%', trim($search_author));
206                                 
207                                 $sql = "SELECT user_id
208                                         FROM " . USERS_TABLE . "
209                                         WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
210                                 if ( !($result = $db->sql_query($sql)) )
211                                 {
212                                         message_die(GENERAL_ERROR, "Couldn't obtain list of matching users (searching for: $search_author)", "", __LINE__, __FILE__, $sql);
213                                 }
214
215                                 $matching_userids = '';
216                                 if ( $row = $db->sql_fetchrow($result) )
217                                 {
218                                         do
219                                         {
220                                                 $matching_userids .= ( ( $matching_userids != '' ) ? ', ' : '' ) . $row['user_id'];
221                                         }
222                                         while( $row = $db->sql_fetchrow($result) );
223                                 }
224                                 else
225                                 {
226                                         message_die(GENERAL_MESSAGE, $lang['No_search_match']);
227                                 }
228
229                                 $sql = "SELECT post_id 
230                                         FROM " . POSTS_TABLE . " 
231                                         WHERE poster_id IN ($matching_userids)";
232                                 
233                                 if ($search_time)
234                                 {
235                                         $sql .= " AND post_time >= " . $search_time;
236                                 }
237                         }
238
239                         if ( !($result = $db->sql_query($sql)) )
240                         {
241                                 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
242                         }
243
244                         $search_ids = array();
245                         while( $row = $db->sql_fetchrow($result) )
246                         {
247                                 $search_ids[] = $row['post_id'];
248                         }
249                         $db->sql_freeresult($result);
250
251                         $total_match_count = count($search_ids);
252
253                 }
254                 else if ( $search_keywords != '' )
255                 {
256                         $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt'); 
257                         $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt'); 
258
259                         $split_search = array();
260                         $stripped_keywords = stripslashes($search_keywords);
261                         $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ?  split_words(clean_words('search', $stripped_keywords, $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords); 
262                         unset($stripped_keywords);
263
264                         $search_msg_only = ( !$search_fields ) ? "AND m.title_match = 0" : ( ( strstr($multibyte_charset, $lang['ENCODING']) ) ? '' : '' );
265
266                         $word_count = 0;
267                         $current_match_type = 'or';
268
269                         $word_match = array();
270                         $result_list = array();
271
272                         for($i = 0; $i < count($split_search); $i++)
273                         {
274                                 if (preg_match('#^[\*%]+$#', trim($split_search[$i])) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($split_search[$i]))))
275                                 {
276                                         $split_search[$i] = '';
277                                         continue;
278                                 }
279
280                                 switch ( $split_search[$i] )
281                                 {
282                                         case 'and':
283                                                 $current_match_type = 'and';
284                                                 break;
285
286                                         case 'or':
287                                                 $current_match_type = 'or';
288                                                 break;
289
290                                         case 'not':
291                                                 $current_match_type = 'not';
292                                                 break;
293
294                                         default:
295                                                 if ( !empty($search_terms) )
296                                                 {
297                                                         $current_match_type = 'and';
298                                                 }
299
300                                                 if ( !strstr($multibyte_charset, $lang['ENCODING']) )
301                                                 {
302                                                         $match_word = str_replace('*', '%', $split_search[$i]);
303                                                         $sql = "SELECT m.post_id 
304                                                                 FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m 
305                                                                 WHERE w.word_text LIKE '$match_word' 
306                                                                         AND m.word_id = w.word_id 
307                                                                         AND w.word_common <> 1 
308                                                                         $search_msg_only";
309                                                 }
310                                                 else
311                                                 {
312                                                         $match_word =  addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
313                                                         $search_msg_only = ( $search_fields ) ? "OR post_subject LIKE '$match_word'" : '';
314                                                         $sql = "SELECT post_id
315                                                                 FROM " . POSTS_TEXT_TABLE . "
316                                                                 WHERE post_text LIKE '$match_word'
317                                                                 $search_msg_only";
318                                                 }
319                                                 if ( !($result = $db->sql_query($sql)) )
320                                                 {
321                                                         message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
322                                                 }
323
324                                                 $row = array();
325                                                 while( $temp_row = $db->sql_fetchrow($result) )
326                                                 {
327                                                         $row[$temp_row['post_id']] = 1;
328
329                                                         if ( !$word_count )
330                                                         {
331                                                                 $result_list[$temp_row['post_id']] = 1;
332                                                         }
333                                                         else if ( $current_match_type == 'or' )
334                                                         {
335                                                                 $result_list[$temp_row['post_id']] = 1;
336                                                         }
337                                                         else if ( $current_match_type == 'not' )
338                                                         {
339                                                                 $result_list[$temp_row['post_id']] = 0;
340                                                         }
341                                                 }
342
343                                                 if ( $current_match_type == 'and' && $word_count )
344                                                 {
345                                                         @reset($result_list);
346                                                         while( list($post_id, $match_count) = @each($result_list) )
347                                                         {
348                                                                 if ( !$row[$post_id] )
349                                                                 {
350                                                                         $result_list[$post_id] = 0;
351                                                                 }
352                                                         }
353                                                 }
354
355                                                 $word_count++;
356
357                                                 $db->sql_freeresult($result);
358                                         }
359                         }
360
361                         @reset($result_list);
362
363                         $search_ids = array();
364                         while( list($post_id, $matches) = each($result_list) )
365                         {
366                                 if ( $matches )
367                                 {
368                                         $search_ids[] = $post_id;
369                                 }
370                         }       
371                         
372                         unset($result_list);
373                         $total_match_count = count($search_ids);
374                 }
375
376                 //
377                 // If user is logged in then we'll check to see which (if any) private
378                 // forums they are allowed to view and include them in the search.
379                 //
380                 // If not logged in we explicitly prevent searching of private forums
381                 //
382                 $auth_sql = '';
383                 if ( $search_forum != -1 )
384                 {
385                         $is_auth = auth(AUTH_READ, $search_forum, $userdata);
386
387                         if ( !$is_auth['auth_read'] )
388                         {
389                                 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
390                         }
391
392                         $auth_sql = "f.forum_id = $search_forum";
393                 }
394                 else
395                 {
396                         $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata); 
397
398                         if ( $search_cat != -1 )
399                         {
400                                 $auth_sql = "f.cat_id = $search_cat";
401                         }
402
403                         $ignore_forum_sql = '';
404                         while( list($key, $value) = each($is_auth_ary) )
405                         {
406                                 if ( !$value['auth_read'] )
407                                 {
408                                         $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
409                                 }
410                         }
411
412                         if ( $ignore_forum_sql != '' )
413                         {
414                                 $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
415                         }
416                 }
417
418                 //
419                 // Author name search 
420                 //
421                 if ( $search_author != '' )
422                 {
423                         if (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($search_author))))
424                         {
425                                 $search_author = '';
426                         }
427
428                         $search_author = str_replace('*', '%', trim(str_replace("\'", "''", $search_author)));
429                 }
430
431                 if ( $total_match_count )
432                 {
433                         if ( $show_results == 'topics' )
434                         {
435                                 //
436                                 // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
437                                 //
438                                 $search_id_chunks = array();
439                                 $count = 0;
440                                 $chunk = 0;
441
442                                 if (count($search_ids) > $limiter)
443                                 {
444                                         for ($i = 0; $i < count($search_ids); $i++) 
445                                         {
446                                                 if ($count == $limiter)
447                                                 {
448                                                         $chunk++;
449                                                         $count = 0;
450                                                 }
451                                         
452                                                 $search_id_chunks[$chunk][$count] = $search_ids[$i];
453                                                 $count++;
454                                         }
455                                 }
456                                 else
457                                 {
458                                         $search_id_chunks[0] = $search_ids;
459                                 }
460
461                                 $search_ids = array();
462
463                                 for ($i = 0; $i < count($search_id_chunks); $i++)
464                                 {
465                                         $where_sql = '';
466
467                                         if ( $search_time )
468                                         {
469                                                 $where_sql .= ( $search_author == '' && $auth_sql == ''  ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
470                                         }
471         
472                                         if ( $search_author == '' && $auth_sql == '' )
473                                         {
474                                                 $sql = "SELECT topic_id 
475                                                         FROM " . POSTS_TABLE . "
476                                                         WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ") 
477                                                         $where_sql 
478                                                         GROUP BY topic_id";
479                                         }
480                                         else
481                                         {
482                                                 $from_sql = POSTS_TABLE . " p"; 
483
484                                                 if ( $search_author != '' )
485                                                 {
486                                                         $from_sql .= ", " . USERS_TABLE . " u";
487                                                         $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
488                                                 }
489
490                                                 if ( $auth_sql != '' )
491                                                 {
492                                                         $from_sql .= ", " . FORUMS_TABLE . " f";
493                                                         $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
494                                                 }
495
496                                                 $sql = "SELECT p.topic_id 
497                                                         FROM $from_sql 
498                                                         WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ") 
499                                                                 $where_sql 
500                                                         GROUP BY p.topic_id";
501                                         }
502
503                                         if ( !($result = $db->sql_query($sql)) )
504                                         {
505                                                 message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
506                                         }
507
508                                         while ($row = $db->sql_fetchrow($result))
509                                         {
510                                                 $search_ids[] = $row['topic_id'];
511                                         }
512                                         $db->sql_freeresult($result);
513                                 }
514
515                                 $total_match_count = sizeof($search_ids);
516                 
517                         }
518                         else if ( $search_author != '' || $search_time || $auth_sql != '' )
519                         {
520                                 $search_id_chunks = array();
521                                 $count = 0;
522                                 $chunk = 0;
523
524                                 if (count($search_ids) > $limiter)
525                                 {
526                                         for ($i = 0; $i < count($search_ids); $i++) 
527                                         {
528                                                 if ($count == $limiter)
529                                                 {
530                                                         $chunk++;
531                                                         $count = 0;
532                                                 }
533                                         
534                                                 $search_id_chunks[$chunk][$count] = $search_ids[$i];
535                                                 $count++;
536                                         }
537                                 }
538                                 else
539                                 {
540                                         $search_id_chunks[0] = $search_ids;
541                                 }
542
543                                 $search_ids = array();
544
545                                 for ($i = 0; $i < count($search_id_chunks); $i++)
546                                 {
547                                         $where_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
548                                         $select_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id' : 'p.post_id';
549                                         $from_sql = (  $search_author == '' && $auth_sql == '' ) ? POSTS_TABLE : POSTS_TABLE . ' p';
550
551                                         if ( $search_time )
552                                         {
553                                                 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
554                                         }
555
556                                         if ( $auth_sql != '' )
557                                         {
558                                                 $from_sql .= ", " . FORUMS_TABLE . " f";
559                                                 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
560                                         }
561
562                                         if ( $search_author != '' )
563                                         {
564                                                 $from_sql .= ", " . USERS_TABLE . " u";
565                                                 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
566                                         }
567
568                                         $sql = "SELECT " . $select_sql . " 
569                                                 FROM $from_sql 
570                                                 WHERE $where_sql";
571                                         if ( !($result = $db->sql_query($sql)) )
572                                         {
573                                                 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
574                                         }
575
576                                         while( $row = $db->sql_fetchrow($result) )
577                                         {
578                                                 $search_ids[] = $row['post_id'];
579                                         }
580                                         $db->sql_freeresult($result);
581                                 }
582
583                                 $total_match_count = count($search_ids);
584                         }
585                 }
586                 else if ( $search_id == 'unanswered' )
587                 {
588                         if ( $auth_sql != '' )
589                         {
590                                 $sql = "SELECT t.topic_id, f.forum_id
591                                         FROM " . TOPICS_TABLE . "  t, " . FORUMS_TABLE . " f
592                                         WHERE t.topic_replies = 0 
593                                                 AND t.forum_id = f.forum_id
594                                                 AND t.topic_moved_id = 0
595                                                 AND $auth_sql";
596                         }
597                         else
598                         {
599                                 $sql = "SELECT topic_id 
600                                         FROM " . TOPICS_TABLE . "  
601                                         WHERE topic_replies = 0 
602                                                 AND topic_moved_id = 0";
603                         }
604                                 
605                         if ( !($result = $db->sql_query($sql)) )
606                         {
607                                 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
608                         }
609
610                         $search_ids = array();
611                         while( $row = $db->sql_fetchrow($result) )
612                         {
613                                 $search_ids[] = $row['topic_id'];
614                         }
615                         $db->sql_freeresult($result);
616
617                         $total_match_count = count($search_ids);
618
619                         //
620                         // Basic requirements
621                         //
622                         $show_results = 'topics';
623                         $sort_by = 0;
624                         $sort_dir = 'DESC';
625                 }
626                 else
627                 {
628                         message_die(GENERAL_MESSAGE, $lang['No_search_match']);
629                 }
630
631                 //
632                 // Finish building query (for all combinations)
633                 // and run it ...
634                 //
635                 $sql = "SELECT session_id 
636                         FROM " . SESSIONS_TABLE;
637                 if ( $result = $db->sql_query($sql) )
638                 {
639                         $delete_search_ids = array();
640                         while( $row = $db->sql_fetchrow($result) )
641                         {
642                                 $delete_search_ids[] = "'" . $row['session_id'] . "'";
643                         }
644
645                         if ( count($delete_search_ids) )
646                         {
647                                 $sql = "DELETE FROM " . SEARCH_TABLE . " 
648                                         WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
649                                 if ( !$result = $db->sql_query($sql) )
650                                 {
651                                         message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
652                                 }
653                         }
654                 }
655
656                 //
657                 // Store new result data
658                 //
659                 $search_results = implode(', ', $search_ids);
660                 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
661
662                 //
663                 // Combine both results and search data (apart from original query)
664                 // so we can serialize it and place it in the DB
665                 //
666                 $store_search_data = array();
667
668                 //
669                 // Limit the character length (and with this the results displayed at all following pages) to prevent
670                 // truncated result arrays. Normally, search results above 12000 are affected.
671                 // - to include or not to include
672                 /*
673                 $max_result_length = 60000;
674                 if (strlen($search_results) > $max_result_length)
675                 {
676                         $search_results = substr($search_results, 0, $max_result_length);
677                         $search_results = substr($search_results, 0, strrpos($search_results, ','));
678                         $total_match_count = count(explode(', ', $search_results));
679                 }
680                 */
681
682                 for($i = 0; $i < count($store_vars); $i++)
683                 {
684                         $store_search_data[$store_vars[$i]] = $$store_vars[$i];
685                 }
686
687                 $result_array = serialize($store_search_data);
688                 unset($store_search_data);
689
690                 mt_srand ((double) microtime() * 1000000);
691                 $search_id = mt_rand();
692
693                 $sql = "UPDATE " . SEARCH_TABLE . " 
694                         SET search_id = $search_id, search_array = '" . str_replace("\'", "''", $result_array) . "'
695                         WHERE session_id = '" . $userdata['session_id'] . "'";
696                 if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
697                 {
698                         $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array) 
699                                 VALUES($search_id, '" . $userdata['session_id'] . "', '" . str_replace("\'", "''", $result_array) . "')";
700                         if ( !($result = $db->sql_query($sql)) )
701                         {
702                                 message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
703                         }
704                 }
705         }
706         else
707         {
708                 $search_id = intval($search_id);
709                 if ( $search_id )
710                 {
711                         $sql = "SELECT search_array 
712                                 FROM " . SEARCH_TABLE . " 
713                                 WHERE search_id = $search_id  
714                                         AND session_id = '". $userdata['session_id'] . "'";
715                         if ( !($result = $db->sql_query($sql)) )
716                         {
717                                 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
718                         }
719
720                         if ( $row = $db->sql_fetchrow($result) )
721                         {
722                                 $search_data = unserialize($row['search_array']);
723                                 for($i = 0; $i < count($store_vars); $i++)
724                                 {
725                                         $$store_vars[$i] = $search_data[$store_vars[$i]];
726                                 }
727                         }
728                 }
729         }
730
731         //
732         // Look up data ...
733         //
734         if ( $search_results != '' )
735         {
736                 if ( $show_results == 'posts' )
737                 {
738                         $sql = "SELECT pt.post_text, pt.bbcode_uid, pt.post_subject, p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_id, u.user_sig, u.user_sig_bbcode_uid  
739                                 FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt 
740                                 WHERE p.post_id IN ($search_results)
741                                         AND pt.post_id = p.post_id
742                                         AND f.forum_id = p.forum_id
743                                         AND p.topic_id = t.topic_id
744                                         AND p.poster_id = u.user_id";
745                 }
746                 else
747                 {
748                         $sql = "SELECT t.*, f.forum_id, f.forum_name, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time 
749                                 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
750                                 WHERE t.topic_id IN ($search_results) 
751                                         AND t.topic_poster = u.user_id
752                                         AND f.forum_id = t.forum_id 
753                                         AND p.post_id = t.topic_first_post_id
754                                         AND p2.post_id = t.topic_last_post_id
755                                         AND u2.user_id = p2.poster_id";
756                 }
757
758                 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
759
760                 $sql .= " ORDER BY ";
761                 switch ( $sort_by )
762                 {
763                         case 1:
764                                 $sql .= ( $show_results == 'posts' ) ? 'pt.post_subject' : 't.topic_title';
765                                 break;
766                         case 2:
767                                 $sql .= 't.topic_title';
768                                 break;
769                         case 3:
770                                 $sql .= 'u.username';
771                                 break;
772                         case 4:
773                                 $sql .= 'f.forum_id';
774                                 break;
775                         default:
776                                 $sql .= ( $show_results == 'posts' ) ? 'p.post_time' : 'p2.post_time';
777                                 break;
778                 }
779                 $sql .= " $sort_dir LIMIT $start, " . $per_page;
780
781                 if ( !$result = $db->sql_query($sql) )
782                 {
783                         message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
784                 }
785
786                 $searchset = array();
787                 while( $row = $db->sql_fetchrow($result) )
788                 {
789                         $searchset[] = $row;
790                 }
791                 
792                 $db->sql_freeresult($result);           
793                 
794                 //
795                 // Define censored word matches
796                 //
797                 $orig_word = array();
798                 $replacement_word = array();
799                 obtain_word_list($orig_word, $replacement_word);
800
801                 //
802                 // Output header
803                 //
804                 $page_title = $lang['Search'];
805                 include($phpbb_root_path . 'includes/page_header.'.$phpEx);     
806
807                 if ( $show_results == 'posts' )
808                 {
809                         $template->set_filenames(array(
810                                 'body' => 'search_results_posts.tpl')
811                         );
812                 }
813                 else
814                 {
815                         $template->set_filenames(array(
816                                 'body' => 'search_results_topics.tpl')
817                         );
818                 }
819                 make_jumpbox('viewforum.'.$phpEx);
820
821                 $l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
822
823                 $template->assign_vars(array(
824                         'L_SEARCH_MATCHES' => $l_search_matches, 
825                         'L_TOPIC' => $lang['Topic'])
826                 );
827
828                 $highlight_active = '';
829                 $highlight_match = array();
830                 for($j = 0; $j < count($split_search); $j++ )
831                 {
832                         $split_word = $split_search[$j];
833
834                         if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
835                         {
836                                 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
837                                 $highlight_active .= " " . $split_word;
838
839                                 for ($k = 0; $k < count($synonym_array); $k++)
840                                 { 
841                                         list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k]))); 
842
843                                         if ( $replace_synonym == $split_word )
844                                         {
845                                                 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
846                                                 $highlight_active .= ' ' . $match_synonym;
847                                         }
848                                 } 
849                         }
850                 }
851
852                 $highlight_active = urlencode(trim($highlight_active));
853
854                 $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
855                 $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
856
857                 for($i = 0; $i < count($searchset); $i++)
858                 {
859                         $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
860                         $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "&amp;highlight=$highlight_active");
861                         $post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&amp;highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
862
863                         $post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
864
865                         $message = $searchset[$i]['post_text'];
866                         $topic_title = $searchset[$i]['topic_title'];
867
868                         $forum_id = $searchset[$i]['forum_id'];
869                         $topic_id = $searchset[$i]['topic_id'];
870
871                         if ( $show_results == 'posts' )
872                         {
873                                 if ( isset($return_chars) )
874                                 {
875                                         $bbcode_uid = $searchset[$i]['bbcode_uid'];
876
877                                         //
878                                         // If the board has HTML off but the post has HTML
879                                         // on then we process it, else leave it alone
880                                         //
881                                         if ( $return_chars != -1 )
882                                         {
883                                                 $message = strip_tags($message);
884                                                 $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
885                                                 $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
886                                                 $message = ( strlen($message) > $return_chars ) ? substr($message, 0, $return_chars) . ' ...' : $message;
887                                         }
888                                         else
889                                         {
890                                                 if ( !$board_config['allow_html'] )
891                                                 {
892                                                         if ( $postrow[$i]['enable_html'] )
893                                                         {
894                                                                 $message = preg_replace('#(<)([\/]?.*?)(>)#is', '&lt;\\2&gt;', $message);
895                                                         }
896                                                 }
897
898                                                 if ( $bbcode_uid != '' )
899                                                 {
900                                                         $message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
901                                                 }
902
903                                                 $message = make_clickable($message);
904
905                                                 if ( $highlight_active )
906                                                 {
907                                                         if ( preg_match('/<.*>/', $message) )
908                                                         {
909                                                                 $message = preg_replace($highlight_match, '<!-- #sh -->\1<!-- #eh -->', $message);
910
911                                                                 $end_html = 0;
912                                                                 $start_html = 1;
913                                                                 $temp_message = '';
914                                                                 $message = ' ' . $message . ' ';
915
916                                                                 while( $start_html = strpos($message, '<', $start_html) )
917                                                                 {
918                                                                         $grab_length = $start_html - $end_html - 1;
919                                                                         $temp_message .= substr($message, $end_html + 1, $grab_length);
920
921                                                                         if ( $end_html = strpos($message, '>', $start_html) )
922                                                                         {
923                                                                                 $length = $end_html - $start_html + 1;
924                                                                                 $hold_string = substr($message, $start_html, $length);
925
926                                                                                 if ( strrpos(' ' . $hold_string, '<') != 1 )
927                                                                                 {
928                                                                                         $end_html = $start_html + 1;
929                                                                                         $end_counter = 1;
930
931                                                                                         while ( $end_counter && $end_html < strlen($message) )
932                                                                                         {
933                                                                                                 if ( substr($message, $end_html, 1) == '>' )
934                                                                                                 {
935                                                                                                         $end_counter--;
936                                                                                                 }
937                                                                                                 else if ( substr($message, $end_html, 1) == '<' )
938                                                                                                 {
939                                                                                                         $end_counter++;
940                                                                                                 }
941
942                                                                                                 $end_html++;
943                                                                                         }
944
945                                                                                         $length = $end_html - $start_html + 1;
946                                                                                         $hold_string = substr($message, $start_html, $length);
947                                                                                         $hold_string = str_replace('<!-- #sh -->', '', $hold_string);
948                                                                                         $hold_string = str_replace('<!-- #eh -->', '', $hold_string);
949                                                                                 }
950                                                                                 else if ( $hold_string == '<!-- #sh -->' )
951                                                                                 {
952                                                                                         $hold_string = str_replace('<!-- #sh -->', '<span style="color:#' . $theme['fontcolor3'] . '"><b>', $hold_string);
953                                                                                 }
954                                                                                 else if ( $hold_string == '<!-- #eh -->' )
955                                                                                 {
956                                                                                         $hold_string = str_replace('<!-- #eh -->', '</b></span>', $hold_string);
957                                                                                 }
958
959                                                                                 $temp_message .= $hold_string;
960
961                                                                                 $start_html += $length;
962                                                                         }
963                                                                         else
964                                                                         {
965                                                                                 $start_html = strlen($message);
966                                                                         }
967                                                                 }
968
969                                                                 $grab_length = strlen($message) - $end_html - 1;
970                                                                 $temp_message .= substr($message, $end_html + 1, $grab_length);
971
972                                                                 $message = trim($temp_message);
973                                                         }
974                                                         else
975                                                         {
976                                                                 $message = preg_replace($highlight_match, '<span style="color:#' . $theme['fontcolor3'] . '"><b>\1</b></span>', $message);
977                                                         }
978                                                 }
979                                         }
980
981                                         if ( count($orig_word) )
982                                         {
983                                                 $topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
984                                                 $post_subject = ( $searchset[$i]['post_subject'] != "" ) ? preg_replace($orig_word, $replacement_word, $searchset[$i]['post_subject']) : $topic_title;
985
986                                                 $message = preg_replace($orig_word, $replacement_word, $message);
987                                         }
988                                         else
989                                         {
990                                                 $post_subject = ( $searchset[$i]['post_subject'] != '' ) ? $searchset[$i]['post_subject'] : $topic_title;
991                                         }
992
993                                         if ($board_config['allow_smilies'] && $searchset[$i]['enable_smilies'])
994                                         {
995                                                 $message = smilies_pass($message);
996                                         }
997
998                                         $message = str_replace("\n", '<br />', $message);
999
1000                                 }
1001
1002                                 $poster = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=" . $searchset[$i]['user_id']) . '">' : '';
1003                                 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != "" ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1004                                 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1005
1006                                 if ( $userdata['session_logged_in'] && $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
1007                                 {
1008                                         if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) )
1009                                         {
1010                                                 $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1011                                         }
1012                                         else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) )
1013                                         {
1014                                                 $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
1015                                         }
1016
1017                                         if ( $searchset[$i]['post_time'] > $topic_last_read )
1018                                         {
1019                                                 $mini_post_img = $images['icon_minipost_new'];
1020                                                 $mini_post_alt = $lang['New_post'];
1021                                         }
1022                                         else
1023                                         {
1024                                                 $mini_post_img = $images['icon_minipost'];
1025                                                 $mini_post_alt = $lang['Post'];
1026                                         }
1027                                 }
1028                                 else
1029                                 {
1030                                         $mini_post_img = $images['icon_minipost'];
1031                                         $mini_post_alt = $lang['Post'];
1032                                 }
1033
1034                                 $template->assign_block_vars("searchresults", array( 
1035                                         'TOPIC_TITLE' => $topic_title,
1036                                         'FORUM_NAME' => $searchset[$i]['forum_name'],
1037                                         'POST_SUBJECT' => $post_subject,
1038                                         'POST_DATE' => $post_date,
1039                                         'POSTER_NAME' => $poster,
1040                                         'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
1041                                         'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
1042                                         'MESSAGE' => $message,
1043                                         'MINI_POST_IMG' => $mini_post_img, 
1044
1045                                         'L_MINI_POST_ALT' => $mini_post_alt, 
1046
1047                                         'U_POST' => $post_url,
1048                                         'U_TOPIC' => $topic_url,
1049                                         'U_FORUM' => $forum_url)
1050                                 );
1051                         }
1052                         else
1053                         {
1054                                 $message = '';
1055
1056                                 if ( count($orig_word) )
1057                                 {
1058                                         $topic_title = preg_replace($orig_word, $replacement_word, $searchset[$i]['topic_title']);
1059                                 }
1060
1061                                 $topic_type = $searchset[$i]['topic_type'];
1062
1063                                 if ($topic_type == POST_ANNOUNCE)
1064                                 {
1065                                         $topic_type = $lang['Topic_Announcement'] . ' ';
1066                                 }
1067                                 else if ($topic_type == POST_STICKY)
1068                                 {
1069                                         $topic_type = $lang['Topic_Sticky'] . ' ';
1070                                 }
1071                                 else
1072                                 {
1073                                         $topic_type = '';
1074                                 }
1075
1076                                 if ( $searchset[$i]['topic_vote'] )
1077                                 {
1078                                         $topic_type .= $lang['Topic_Poll'] . ' ';
1079                                 }
1080
1081                                 $views = $searchset[$i]['topic_views'];
1082                                 $replies = $searchset[$i]['topic_replies'];
1083
1084                                 if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
1085                                 {
1086                                         $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
1087                                         $goto_page = ' [ <img src="' . $images['icon_gotopost'] . '" alt="' . $lang['Goto_page'] . '" title="' . $lang['Goto_page'] . '" />' . $lang['Goto_page'] . ': ';
1088
1089                                         $times = 1;
1090                                         for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
1091                                         {
1092                                                 $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&amp;start=$j") . '">' . $times . '</a>';
1093                                                 if ( $times == 1 && $total_pages > 4 )
1094                                                 {
1095                                                         $goto_page .= ' ... ';
1096                                                         $times = $total_pages - 3;
1097                                                         $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
1098                                                 }
1099                                                 else if ( $times < $total_pages )
1100                                                 {
1101                                                         $goto_page .= ', ';
1102                                                 }
1103                                                 $times++;
1104                                         }
1105                                         $goto_page .= ' ] ';
1106                                 }
1107                                 else
1108                                 {
1109                                         $goto_page = '';
1110                                 }
1111
1112                                 if ( $searchset[$i]['topic_status'] == TOPIC_MOVED )
1113                                 {
1114                                         $topic_type = $lang['Topic_Moved'] . ' ';
1115                                         $topic_id = $searchset[$i]['topic_moved_id'];
1116
1117                                         $folder_image = '<img src="' . $images['folder'] . '" alt="' . $lang['No_new_posts'] . '" />';
1118                                         $newest_post_img = '';
1119                                 }
1120                                 else
1121                                 {
1122                                         if ( $searchset[$i]['topic_status'] == TOPIC_LOCKED )
1123                                         {
1124                                                 $folder = $images['folder_locked'];
1125                                                 $folder_new = $images['folder_locked_new'];
1126                                         }
1127                                         else if ( $searchset[$i]['topic_type'] == POST_ANNOUNCE )
1128                                         {
1129                                                 $folder = $images['folder_announce'];
1130                                                 $folder_new = $images['folder_announce_new'];
1131                                         }
1132                                         else if ( $searchset[$i]['topic_type'] == POST_STICKY )
1133                                         {
1134                                                 $folder = $images['folder_sticky'];
1135                                                 $folder_new = $images['folder_sticky_new'];
1136                                         }
1137                                         else
1138                                         {
1139                                                 if ( $replies >= $board_config['hot_threshold'] )
1140                                                 {
1141                                                         $folder = $images['folder_hot'];
1142                                                         $folder_new = $images['folder_hot_new'];
1143                                                 }
1144                                                 else
1145                                                 {
1146                                                         $folder = $images['folder'];
1147                                                         $folder_new = $images['folder_new'];
1148                                                 }
1149                                         }
1150
1151                                         if ( $userdata['session_logged_in'] )
1152                                         {
1153                                                 if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] ) 
1154                                                 {
1155                                                         if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1156                                                         {
1157
1158                                                                 $unread_topics = true;
1159
1160                                                                 if ( !empty($tracking_topics[$topic_id]) )
1161                                                                 {
1162                                                                         if ( $tracking_topics[$topic_id] > $searchset[$i]['post_time'] )
1163                                                                         {
1164                                                                                 $unread_topics = false;
1165                                                                         }
1166                                                                 }
1167
1168                                                                 if ( !empty($tracking_forums[$forum_id]) )
1169                                                                 {
1170                                                                         if ( $tracking_forums[$forum_id] > $searchset[$i]['post_time'] )
1171                                                                         {
1172                                                                                 $unread_topics = false;
1173                                                                         }
1174                                                                 }
1175
1176                                                                 if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1177                                                                 {
1178                                                                         if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $searchset[$i]['post_time'] )
1179                                                                         {
1180                                                                                 $unread_topics = false;
1181                                                                         }
1182                                                                 }
1183
1184                                                                 if ( $unread_topics )
1185                                                                 {
1186                                                                         $folder_image = $folder_new;
1187                                                                         $folder_alt = $lang['New_posts'];
1188
1189                                                                         $newest_post_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&amp;view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
1190                                                                 }
1191                                                                 else
1192                                                                 {
1193                                                                         $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1194
1195                                                                         $folder_image = $folder;
1196                                                                         $folder_alt = $folder_alt;
1197                                                                         $newest_post_img = '';
1198                                                                 }
1199
1200                                                         }
1201                                                         else if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] ) 
1202                                                         {
1203                                                                 $folder_image = $folder_new;
1204                                                                 $folder_alt = $lang['New_posts'];
1205
1206                                                                 $newest_post_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&amp;view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
1207                                                         }
1208                                                         else 
1209                                                         {
1210                                                                 $folder_image = $folder;
1211                                                                 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1212                                                                 $newest_post_img = '';
1213                                                         }
1214                                                 }
1215                                                 else
1216                                                 {
1217                                                         $folder_image = $folder;
1218                                                         $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1219                                                         $newest_post_img = '';
1220                                                 }
1221                                         }
1222                                         else
1223                                         {
1224                                                 $folder_image = $folder;
1225                                                 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1226                                                 $newest_post_img = '';
1227                                         }
1228                                 }
1229
1230
1231                                 $topic_author = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '=' . $searchset[$i]['user_id']) . '">' : '';
1232                                 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != '' ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1233
1234                                 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1235
1236                                 $first_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['topic_time'], $board_config['board_timezone']);
1237
1238                                 $last_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
1239
1240                                 $last_post_author = ( $searchset[$i]['id2'] == ANONYMOUS ) ? ( ($searchset[$i]['post_username2'] != '' ) ? $searchset[$i]['post_username2'] . ' ' : $lang['Guest'] . ' ' ) : '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '='  . $searchset[$i]['id2']) . '">' . $searchset[$i]['user2'] . '</a>';
1241
1242                                 $last_post_url = '<a href="' . append_sid("viewtopic.$phpEx?"  . POST_POST_URL . '=' . $searchset[$i]['topic_last_post_id']) . '#' . $searchset[$i]['topic_last_post_id'] . '"><img src="' . $images['icon_latest_reply'] . '" alt="' . $lang['View_latest_post'] . '" title="' . $lang['View_latest_post'] . '" border="0" /></a>';
1243
1244                                 $template->assign_block_vars('searchresults', array( 
1245                                         'FORUM_NAME' => $searchset[$i]['forum_name'],
1246                                         'FORUM_ID' => $forum_id,
1247                                         'TOPIC_ID' => $topic_id,
1248                                         'FOLDER' => $folder_image,
1249                                         'NEWEST_POST_IMG' => $newest_post_img, 
1250                                         'TOPIC_FOLDER_IMG' => $folder_image, 
1251                                         'GOTO_PAGE' => $goto_page,
1252                                         'REPLIES' => $replies,
1253                                         'TOPIC_TITLE' => $topic_title,
1254                                         'TOPIC_TYPE' => $topic_type,
1255                                         'VIEWS' => $views,
1256                                         'TOPIC_AUTHOR' => $topic_author, 
1257                                         'FIRST_POST_TIME' => $first_post_time, 
1258                                         'LAST_POST_TIME' => $last_post_time,
1259                                         'LAST_POST_AUTHOR' => $last_post_author,
1260                                         'LAST_POST_IMG' => $last_post_url,
1261
1262                                         'L_TOPIC_FOLDER_ALT' => $folder_alt, 
1263
1264                                         'U_VIEW_FORUM' => $forum_url, 
1265                                         'U_VIEW_TOPIC' => $topic_url)
1266                                 );
1267                         }
1268                 }
1269
1270                 $base_url = "search.$phpEx?search_id=$search_id";
1271
1272                 $template->assign_vars(array(
1273                         'PAGINATION' => generate_pagination($base_url, $total_match_count, $per_page, $start),
1274                         'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $per_page ) + 1 ), ceil( $total_match_count / $per_page )), 
1275
1276                         'L_AUTHOR' => $lang['Author'],
1277                         'L_MESSAGE' => $lang['Message'],
1278                         'L_FORUM' => $lang['Forum'],
1279                         'L_TOPICS' => $lang['Topics'],
1280                         'L_REPLIES' => $lang['Replies'],
1281                         'L_VIEWS' => $lang['Views'],
1282                         'L_POSTS' => $lang['Posts'],
1283                         'L_LASTPOST' => $lang['Last_Post'], 
1284                         'L_POSTED' => $lang['Posted'], 
1285                         'L_SUBJECT' => $lang['Subject'],
1286
1287                         'L_GOTO_PAGE' => $lang['Goto_page'])
1288                 );
1289
1290                 $template->pparse('body');
1291
1292                 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1293         }
1294         else
1295         {
1296                 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
1297         }
1298 }
1299
1300 //
1301 // Search forum
1302 //
1303 $sql = "SELECT c.cat_title, c.cat_id, f.forum_name, f.forum_id  
1304         FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
1305         WHERE f.cat_id = c.cat_id 
1306         ORDER BY c.cat_order, f.forum_order";
1307 $result = $db->sql_query($sql);
1308 if ( !$result )
1309 {
1310         message_die(GENERAL_ERROR, 'Could not obtain forum_name/forum_id', '', __LINE__, __FILE__, $sql);
1311 }
1312
1313 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
1314
1315 $s_forums = '';
1316 while( $row = $db->sql_fetchrow($result) )
1317 {
1318         if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
1319         {
1320                 $s_forums .= '<option value="' . $row['forum_id'] . '">' . $row['forum_name'] . '</option>';
1321                 if ( empty($list_cat[$row['cat_id']]) )
1322                 {
1323                         $list_cat[$row['cat_id']] = $row['cat_title'];
1324                 }
1325         }
1326 }
1327
1328 if ( $s_forums != '' )
1329 {
1330         $s_forums = '<option value="-1">' . $lang['All_available'] . '</option>' . $s_forums;
1331
1332         //
1333         // Category to search
1334         //
1335         $s_categories = '<option value="-1">' . $lang['All_available'] . '</option>';
1336         while( list($cat_id, $cat_title) = @each($list_cat))
1337         {
1338                 $s_categories .= '<option value="' . $cat_id . '">' . $cat_title . '</option>';
1339         }
1340 }
1341 else
1342 {
1343         message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
1344 }
1345
1346 //
1347 // Number of chars returned
1348 //
1349 $s_characters = '<option value="-1">' . $lang['All_available'] . '</option>';
1350 $s_characters .= '<option value="0">0</option>';
1351 $s_characters .= '<option value="25">25</option>';
1352 $s_characters .= '<option value="50">50</option>';
1353
1354 for($i = 100; $i < 1100 ; $i += 100)
1355 {
1356         $selected = ( $i == 200 ) ? ' selected="selected"' : '';
1357         $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1358 }
1359
1360 //
1361 // Sorting
1362 //
1363 $s_sort_by = "";
1364 for($i = 0; $i < count($sort_by_types); $i++)
1365 {
1366         $s_sort_by .= '<option value="' . $i . '">' . $sort_by_types[$i] . '</option>';
1367 }
1368
1369 //
1370 // Search time
1371 //
1372 $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
1373 $previous_days_text = array($lang['All_Posts'], $lang['1_Day'], $lang['7_Days'], $lang['2_Weeks'], $lang['1_Month'], $lang['3_Months'], $lang['6_Months'], $lang['1_Year']);
1374
1375 $s_time = '';
1376 for($i = 0; $i < count($previous_days); $i++)
1377 {
1378         $selected = ( $topic_days == $previous_days[$i] ) ? ' selected="selected"' : '';
1379         $s_time .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
1380 }
1381
1382 //
1383 // Output the basic page
1384 //
1385 $page_title = $lang['Search'];
1386 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1387
1388 $template->set_filenames(array(
1389         'body' => 'search_body.tpl')
1390 );
1391 make_jumpbox('viewforum.'.$phpEx);
1392
1393 $template->assign_vars(array(
1394         'L_SEARCH_QUERY' => $lang['Search_query'], 
1395         'L_SEARCH_OPTIONS' => $lang['Search_options'], 
1396         'L_SEARCH_KEYWORDS' => $lang['Search_keywords'], 
1397         'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'], 
1398         'L_SEARCH_AUTHOR' => $lang['Search_author'],
1399         'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'], 
1400         'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
1401         'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'], 
1402         'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'], 
1403         'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'], 
1404         'L_CATEGORY' => $lang['Category'], 
1405         'L_RETURN_FIRST' => $lang['Return_first'],
1406         'L_CHARACTERS' => $lang['characters_posts'], 
1407         'L_SORT_BY' => $lang['Sort_by'],
1408         'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
1409         'L_SORT_DESCENDING' => $lang['Sort_Descending'],
1410         'L_SEARCH_PREVIOUS' => $lang['Search_previous'], 
1411         'L_DISPLAY_RESULTS' => $lang['Display_results'], 
1412         'L_FORUM' => $lang['Forum'],
1413         'L_TOPICS' => $lang['Topics'],
1414         'L_POSTS' => $lang['Posts'],
1415
1416         'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=results"),
1417         'S_CHARACTER_OPTIONS' => $s_characters,
1418         'S_FORUM_OPTIONS' => $s_forums, 
1419         'S_CATEGORY_OPTIONS' => $s_categories, 
1420         'S_TIME_OPTIONS' => $s_time, 
1421         'S_SORT_OPTIONS' => $s_sort_by,
1422         'S_HIDDEN_FIELDS' => '')
1423 );
1424
1425 $template->pparse('body');
1426
1427 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1428
1429 ?>