]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2_old/search.php
Updated to clarify we temp. use USB and added reference to SD-only install option.
[www.vanrenterghem.biz.git] / phpBB2_old / 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.15 2004/11/18 17:49:39 acydburn Exp $
10  *
11  *
12  ***************************************************************************/
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  ***************************************************************************/
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);
30 //
31 // Start session management
32 //
33 $userdata = session_pagestart($user_ip, PAGE_SEARCH);
34 init_userprefs($userdata);
35 //
36 // End session management
37 //
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 }
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 }
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 }
70 $search_id = ( isset($HTTP_GET_VARS['search_id']) ) ? $HTTP_GET_VARS['search_id'] : '';
72 $show_results = ( isset($HTTP_POST_VARS['show_results']) ) ? $HTTP_POST_VARS['show_results'] : 'posts';
73 $show_results = ($show_results == 'topics') ? 'topics' : 'posts';
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 }
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 }
93 $return_chars = ( isset($HTTP_POST_VARS['return_chars']) ) ? intval($HTTP_POST_VARS['return_chars']) : 200;
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;
98 $sort_by = ( isset($HTTP_POST_VARS['sort_by']) ) ? intval($HTTP_POST_VARS['sort_by']) : 0;
100 if ( isset($HTTP_POST_VARS['sort_dir']) )
102         $sort_dir = ( $HTTP_POST_VARS['sort_dir'] == 'DESC' ) ? 'DESC' : 'ASC';
104 else
106         $sort_dir =  'DESC';
109 if ( !empty($HTTP_POST_VARS['search_time']) || !empty($HTTP_GET_VARS['search_time']))
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']);
114 else
116         $search_time = 0;
117         $topic_days = 0;
120 $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
122 $sort_by_types = array($lang['Sort_Time'], $lang['Sort_Post_Subject'], $lang['Sort_Topic_Title'], $lang['Sort_Author'], $lang['Sort_Forum']);
124 //
125 // encoding match for workaround
126 //
127 $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
129 //
130 // Begin core code
131 //
132 if ( $mode == 'searchuser' )
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         }
146         exit;
148 else if ( $search_keywords != '' || $search_author != '' || $search_id )
150         $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'sort_dir', 'show_results', 'return_chars');
151         $search_results = '';
153         //
154         // Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
155         $limiter = 5000;
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                                 }
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                                 }
194                                 $show_results = 'topics';
195                                 $sort_by = 0;
196                                 $sort_dir = 'DESC';
197                         }
198                         else
199                         {
200                                 $search_author = str_replace('*', '%', trim($search_author));
201                                 
202                                 $sql = "SELECT user_id
203                                         FROM " . USERS_TABLE . "
204                                         WHERE username LIKE '" . str_replace("\'", "''", $search_author) . "'";
205                                 if ( !($result = $db->sql_query($sql)) )
206                                 {
207                                         message_die(GENERAL_ERROR, "Couldn't obtain list of matching users (searching for: $search_author)", "", __LINE__, __FILE__, $sql);
208                                 }
210                                 $matching_userids = '';
211                                 if ( $row = $db->sql_fetchrow($result) )
212                                 {
213                                         do
214                                         {
215                                                 $matching_userids .= ( ( $matching_userids != '' ) ? ', ' : '' ) . $row['user_id'];
216                                         }
217                                         while( $row = $db->sql_fetchrow($result) );
218                                 }
219                                 else
220                                 {
221                                         message_die(GENERAL_MESSAGE, $lang['No_search_match']);
222                                 }
224                                 $sql = "SELECT post_id 
225                                         FROM " . POSTS_TABLE . " 
226                                         WHERE poster_id IN ($matching_userids)";
227                                 
228                                 if ($search_time)
229                                 {
230                                         $sql .= " AND post_time >= " . $search_time;
231                                 }
232                         }
234                         if ( !($result = $db->sql_query($sql)) )
235                         {
236                                 message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
237                         }
239                         $search_ids = array();
240                         while( $row = $db->sql_fetchrow($result) )
241                         {
242                                 $search_ids[] = $row['post_id'];
243                         }
244                         $db->sql_freeresult($result);
246                         $total_match_count = count($search_ids);
248                 }
249                 else if ( $search_keywords != '' )
250                 {
251                         $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_stopwords.txt'); 
252                         $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/search_synonyms.txt'); 
254                         $split_search = array();
255                         $split_search = ( !strstr($multibyte_charset, $lang['ENCODING']) ) ?  split_words(clean_words('search', stripslashes($search_keywords), $stopword_array, $synonym_array), 'search') : split(' ', $search_keywords);     
257                         $search_msg_only = ( !$search_fields ) ? "AND m.title_match = 0" : ( ( strstr($multibyte_charset, $lang['ENCODING']) ) ? '' : '' );
259                         $word_count = 0;
260                         $current_match_type = 'or';
262                         $word_match = array();
263                         $result_list = array();
265                         for($i = 0; $i < count($split_search); $i++)
266                         {
267                                 switch ( $split_search[$i] )
268                                 {
269                                         case 'and':
270                                                 $current_match_type = 'and';
271                                                 break;
273                                         case 'or':
274                                                 $current_match_type = 'or';
275                                                 break;
277                                         case 'not':
278                                                 $current_match_type = 'not';
279                                                 break;
281                                         default:
282                                                 if ( !empty($search_terms) )
283                                                 {
284                                                         $current_match_type = 'and';
285                                                 }
287                                                 if ( !strstr($multibyte_charset, $lang['ENCODING']) )
288                                                 {
289                                                         $match_word = str_replace('*', '%', $split_search[$i]);
290                                                         $sql = "SELECT m.post_id 
291                                                                 FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m 
292                                                                 WHERE w.word_text LIKE '$match_word' 
293                                                                         AND m.word_id = w.word_id 
294                                                                         AND w.word_common <> 1 
295                                                                         $search_msg_only";
296                                                 }
297                                                 else
298                                                 {
299                                                         $match_word =  addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
300                                                         $search_msg_only = ( $search_fields ) ? "OR post_subject LIKE '$match_word'" : '';
301                                                         $sql = "SELECT post_id
302                                                                 FROM " . POSTS_TEXT_TABLE . "
303                                                                 WHERE post_text LIKE '$match_word'
304                                                                 $search_msg_only";
305                                                 }
306                                                 if ( !($result = $db->sql_query($sql)) )
307                                                 {
308                                                         message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
309                                                 }
311                                                 $row = array();
312                                                 while( $temp_row = $db->sql_fetchrow($result) )
313                                                 {
314                                                         $row[$temp_row['post_id']] = 1;
316                                                         if ( !$word_count )
317                                                         {
318                                                                 $result_list[$temp_row['post_id']] = 1;
319                                                         }
320                                                         else if ( $current_match_type == 'or' )
321                                                         {
322                                                                 $result_list[$temp_row['post_id']] = 1;
323                                                         }
324                                                         else if ( $current_match_type == 'not' )
325                                                         {
326                                                                 $result_list[$temp_row['post_id']] = 0;
327                                                         }
328                                                 }
330                                                 if ( $current_match_type == 'and' && $word_count )
331                                                 {
332                                                         @reset($result_list);
333                                                         while( list($post_id, $match_count) = @each($result_list) )
334                                                         {
335                                                                 if ( !$row[$post_id] )
336                                                                 {
337                                                                         $result_list[$post_id] = 0;
338                                                                 }
339                                                         }
340                                                 }
342                                                 $word_count++;
344                                                 $db->sql_freeresult($result);
345                                         }
346                         }
348                         @reset($result_list);
350                         $search_ids = array();
351                         while( list($post_id, $matches) = each($result_list) )
352                         {
353                                 if ( $matches )
354                                 {
355                                         $search_ids[] = $post_id;
356                                 }
357                         }       
358                         
359                         unset($result_list);
360                         $total_match_count = count($search_ids);
361                 }
363                 //
364                 // If user is logged in then we'll check to see which (if any) private
365                 // forums they are allowed to view and include them in the search.
366                 //
367                 // If not logged in we explicitly prevent searching of private forums
368                 //
369                 $auth_sql = '';
370                 if ( $search_forum != -1 )
371                 {
372                         $is_auth = auth(AUTH_READ, $search_forum, $userdata);
374                         if ( !$is_auth['auth_read'] )
375                         {
376                                 message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
377                         }
379                         $auth_sql = "f.forum_id = $search_forum";
380                 }
381                 else
382                 {
383                         $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata); 
385                         if ( $search_cat != -1 )
386                         {
387                                 $auth_sql = "f.cat_id = $search_cat";
388                         }
390                         $ignore_forum_sql = '';
391                         while( list($key, $value) = each($is_auth_ary) )
392                         {
393                                 if ( !$value['auth_read'] )
394                                 {
395                                         $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
396                                 }
397                         }
399                         if ( $ignore_forum_sql != '' )
400                         {
401                                 $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
402                         }
403                 }
405                 //
406                 // Author name search 
407                 //
408                 if ( $search_author != '' )
409                 {
410                         $search_author = str_replace('*', '%', trim(str_replace("\'", "''", $search_author)));
411                 }
413                 if ( $total_match_count )
414                 {
415                         if ( $show_results == 'topics' )
416                         {
417                                 //
418                                 // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
419                                 //
420                                 $search_id_chunks = array();
421                                 $count = 0;
422                                 $chunk = 0;
424                                 if (count($search_ids) > $limiter)
425                                 {
426                                         for ($i = 0; $i < count($search_ids); $i++) 
427                                         {
428                                                 if ($count == $limiter)
429                                                 {
430                                                         $chunk++;
431                                                         $count = 0;
432                                                 }
433                                         
434                                                 $search_id_chunks[$chunk][$count] = $search_ids[$i];
435                                                 $count++;
436                                         }
437                                 }
438                                 else
439                                 {
440                                         $search_id_chunks[0] = $search_ids;
441                                 }
443                                 $search_ids = array();
445                                 for ($i = 0; $i < count($search_id_chunks); $i++)
446                                 {
447                                         $where_sql = '';
449                                         if ( $search_time )
450                                         {
451                                                 $where_sql .= ( $search_author == '' && $auth_sql == ''  ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
452                                         }
453         
454                                         if ( $search_author == '' && $auth_sql == '' )
455                                         {
456                                                 $sql = "SELECT topic_id 
457                                                         FROM " . POSTS_TABLE . "
458                                                         WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ") 
459                                                         $where_sql 
460                                                         GROUP BY topic_id";
461                                         }
462                                         else
463                                         {
464                                                 $from_sql = POSTS_TABLE . " p"; 
466                                                 if ( $search_author != '' )
467                                                 {
468                                                         $from_sql .= ", " . USERS_TABLE . " u";
469                                                         $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author' ";
470                                                 }
472                                                 if ( $auth_sql != '' )
473                                                 {
474                                                         $from_sql .= ", " . FORUMS_TABLE . " f";
475                                                         $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
476                                                 }
478                                                 $sql = "SELECT p.topic_id 
479                                                         FROM $from_sql 
480                                                         WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ") 
481                                                                 $where_sql 
482                                                         GROUP BY p.topic_id";
483                                         }
485                                         if ( !($result = $db->sql_query($sql)) )
486                                         {
487                                                 message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
488                                         }
490                                         while ($row = $db->sql_fetchrow($result))
491                                         {
492                                                 $search_ids[] = $row['topic_id'];
493                                         }
494                                         $db->sql_freeresult($result);
495                                 }
497                                 $total_match_count = sizeof($search_ids);
498                 
499                         }
500                         else if ( $search_author != '' || $search_time || $auth_sql != '' )
501                         {
502                                 $search_id_chunks = array();
503                                 $count = 0;
504                                 $chunk = 0;
506                                 if (count($search_ids) > $limiter)
507                                 {
508                                         for ($i = 0; $i < count($search_ids); $i++) 
509                                         {
510                                                 if ($count == $limiter)
511                                                 {
512                                                         $chunk++;
513                                                         $count = 0;
514                                                 }
515                                         
516                                                 $search_id_chunks[$chunk][$count] = $search_ids[$i];
517                                                 $count++;
518                                         }
519                                 }
520                                 else
521                                 {
522                                         $search_id_chunks[0] = $search_ids;
523                                 }
525                                 $search_ids = array();
527                                 for ($i = 0; $i < count($search_id_chunks); $i++)
528                                 {
529                                         $where_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
530                                         $select_sql = ( $search_author == '' && $auth_sql == '' ) ? 'post_id' : 'p.post_id';
531                                         $from_sql = (  $search_author == '' && $auth_sql == '' ) ? POSTS_TABLE : POSTS_TABLE . ' p';
533                                         if ( $search_time )
534                                         {
535                                                 $where_sql .= ( $search_author == '' && $auth_sql == '' ) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
536                                         }
538                                         if ( $auth_sql != '' )
539                                         {
540                                                 $from_sql .= ", " . FORUMS_TABLE . " f";
541                                                 $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
542                                         }
544                                         if ( $search_author != '' )
545                                         {
546                                                 $from_sql .= ", " . USERS_TABLE . " u";
547                                                 $where_sql .= " AND u.user_id = p.poster_id AND u.username LIKE '$search_author'";
548                                         }
550                                         $sql = "SELECT " . $select_sql . " 
551                                                 FROM $from_sql 
552                                                 WHERE $where_sql";
553                                         if ( !($result = $db->sql_query($sql)) )
554                                         {
555                                                 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
556                                         }
558                                         while( $row = $db->sql_fetchrow($result) )
559                                         {
560                                                 $search_ids[] = $row['post_id'];
561                                         }
562                                         $db->sql_freeresult($result);
563                                 }
565                                 $total_match_count = count($search_ids);
566                         }
567                 }
568                 else if ( $search_id == 'unanswered' )
569                 {
570                         if ( $auth_sql != '' )
571                         {
572                                 $sql = "SELECT t.topic_id, f.forum_id
573                                         FROM " . TOPICS_TABLE . "  t, " . FORUMS_TABLE . " f
574                                         WHERE t.topic_replies = 0 
575                                                 AND t.forum_id = f.forum_id
576                                                 AND t.topic_moved_id = 0
577                                                 AND $auth_sql";
578                         }
579                         else
580                         {
581                                 $sql = "SELECT topic_id 
582                                         FROM " . TOPICS_TABLE . "  
583                                         WHERE topic_replies = 0 
584                                                 AND topic_moved_id = 0";
585                         }
586                                 
587                         if ( !($result = $db->sql_query($sql)) )
588                         {
589                                 message_die(GENERAL_ERROR, 'Could not obtain post ids', '', __LINE__, __FILE__, $sql);
590                         }
592                         $search_ids = array();
593                         while( $row = $db->sql_fetchrow($result) )
594                         {
595                                 $search_ids[] = $row['topic_id'];
596                         }
597                         $db->sql_freeresult($result);
599                         $total_match_count = count($search_ids);
601                         //
602                         // Basic requirements
603                         //
604                         $show_results = 'topics';
605                         $sort_by = 0;
606                         $sort_dir = 'DESC';
607                 }
608                 else
609                 {
610                         message_die(GENERAL_MESSAGE, $lang['No_search_match']);
611                 }
613                 //
614                 // Finish building query (for all combinations)
615                 // and run it ...
616                 //
617                 $sql = "SELECT session_id 
618                         FROM " . SESSIONS_TABLE;
619                 if ( $result = $db->sql_query($sql) )
620                 {
621                         $delete_search_ids = array();
622                         while( $row = $db->sql_fetchrow($result) )
623                         {
624                                 $delete_search_ids[] = "'" . $row['session_id'] . "'";
625                         }
627                         if ( count($delete_search_ids) )
628                         {
629                                 $sql = "DELETE FROM " . SEARCH_TABLE . " 
630                                         WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
631                                 if ( !$result = $db->sql_query($sql) )
632                                 {
633                                         message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
634                                 }
635                         }
636                 }
638                 //
639                 // Store new result data
640                 //
641                 $search_results = implode(', ', $search_ids);
642                 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
644                 //
645                 // Combine both results and search data (apart from original query)
646                 // so we can serialize it and place it in the DB
647                 //
648                 $store_search_data = array();
650                 //
651                 // Limit the character length (and with this the results displayed at all following pages) to prevent
652                 // truncated result arrays. Normally, search results above 12000 are affected.
653                 // - to include or not to include
654                 /*
655                 $max_result_length = 60000;
656                 if (strlen($search_results) > $max_result_length)
657                 {
658                         $search_results = substr($search_results, 0, $max_result_length);
659                         $search_results = substr($search_results, 0, strrpos($search_results, ','));
660                         $total_match_count = count(explode(', ', $search_results));
661                 }
662                 */
664                 for($i = 0; $i < count($store_vars); $i++)
665                 {
666                         $store_search_data[$store_vars[$i]] = $$store_vars[$i];
667                 }
669                 $result_array = serialize($store_search_data);
670                 unset($store_search_data);
672                 mt_srand ((double) microtime() * 1000000);
673                 $search_id = mt_rand();
675                 $sql = "UPDATE " . SEARCH_TABLE . " 
676                         SET search_id = $search_id, search_array = '" . str_replace("\'", "''", $result_array) . "'
677                         WHERE session_id = '" . $userdata['session_id'] . "'";
678                 if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() )
679                 {
680                         $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array) 
681                                 VALUES($search_id, '" . $userdata['session_id'] . "', '" . str_replace("\'", "''", $result_array) . "')";
682                         if ( !($result = $db->sql_query($sql)) )
683                         {
684                                 message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql);
685                         }
686                 }
687         }
688         else
689         {
690                 $search_id = intval($search_id);
691                 if ( $search_id )
692                 {
693                         $sql = "SELECT search_array 
694                                 FROM " . SEARCH_TABLE . " 
695                                 WHERE search_id = $search_id  
696                                         AND session_id = '". $userdata['session_id'] . "'";
697                         if ( !($result = $db->sql_query($sql)) )
698                         {
699                                 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
700                         }
702                         if ( $row = $db->sql_fetchrow($result) )
703                         {
704                                 $search_data = unserialize($row['search_array']);
705                                 for($i = 0; $i < count($store_vars); $i++)
706                                 {
707                                         $$store_vars[$i] = $search_data[$store_vars[$i]];
708                                 }
709                         }
710                 }
711         }
713         //
714         // Look up data ...
715         //
716         if ( $search_results != '' )
717         {
718                 if ( $show_results == 'posts' )
719                 {
720                         $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  
721                                 FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TEXT_TABLE . " pt 
722                                 WHERE p.post_id IN ($search_results)
723                                         AND pt.post_id = p.post_id
724                                         AND f.forum_id = p.forum_id
725                                         AND p.topic_id = t.topic_id
726                                         AND p.poster_id = u.user_id";
727                 }
728                 else
729                 {
730                         $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 
731                                 FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
732                                 WHERE t.topic_id IN ($search_results) 
733                                         AND t.topic_poster = u.user_id
734                                         AND f.forum_id = t.forum_id 
735                                         AND p.post_id = t.topic_first_post_id
736                                         AND p2.post_id = t.topic_last_post_id
737                                         AND u2.user_id = p2.poster_id";
738                 }
740                 $per_page = ( $show_results == 'posts' ) ? $board_config['posts_per_page'] : $board_config['topics_per_page'];
742                 $sql .= " ORDER BY ";
743                 switch ( $sort_by )
744                 {
745                         case 1:
746                                 $sql .= ( $show_results == 'posts' ) ? 'pt.post_subject' : 't.topic_title';
747                                 break;
748                         case 2:
749                                 $sql .= 't.topic_title';
750                                 break;
751                         case 3:
752                                 $sql .= 'u.username';
753                                 break;
754                         case 4:
755                                 $sql .= 'f.forum_id';
756                                 break;
757                         default:
758                                 $sql .= ( $show_results == 'posts' ) ? 'p.post_time' : 'p2.post_time';
759                                 break;
760                 }
761                 $sql .= " $sort_dir LIMIT $start, " . $per_page;
763                 if ( !$result = $db->sql_query($sql) )
764                 {
765                         message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
766                 }
768                 $searchset = array();
769                 while( $row = $db->sql_fetchrow($result) )
770                 {
771                         $searchset[] = $row;
772                 }
773                 
774                 $db->sql_freeresult($result);           
775                 
776                 //
777                 // Define censored word matches
778                 //
779                 $orig_word = array();
780                 $replacement_word = array();
781                 obtain_word_list($orig_word, $replacement_word);
783                 //
784                 // Output header
785                 //
786                 $page_title = $lang['Search'];
787                 include($phpbb_root_path . 'includes/page_header.'.$phpEx);     
789                 if ( $show_results == 'posts' )
790                 {
791                         $template->set_filenames(array(
792                                 'body' => 'search_results_posts.tpl')
793                         );
794                 }
795                 else
796                 {
797                         $template->set_filenames(array(
798                                 'body' => 'search_results_topics.tpl')
799                         );
800                 }
801                 make_jumpbox('viewforum.'.$phpEx);
803                 $l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
805                 $template->assign_vars(array(
806                         'L_SEARCH_MATCHES' => $l_search_matches, 
807                         'L_TOPIC' => $lang['Topic'])
808                 );
810                 $highlight_active = '';
811                 $highlight_match = array();
812                 for($j = 0; $j < count($split_search); $j++ )
813                 {
814                         $split_word = $split_search[$j];
816                         if ( $split_word != 'and' && $split_word != 'or' && $split_word != 'not' )
817                         {
818                                 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
819                                 $highlight_active .= " " . $split_word;
821                                 for ($k = 0; $k < count($synonym_array); $k++)
822                                 { 
823                                         list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_array[$k]))); 
825                                         if ( $replace_synonym == $split_word )
826                                         {
827                                                 $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
828                                                 $highlight_active .= ' ' . $match_synonym;
829                                         }
830                                 } 
831                         }
832                 }
834                 $highlight_active = urlencode(trim($highlight_active));
836                 $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
837                 $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
839                 for($i = 0; $i < count($searchset); $i++)
840                 {
841                         $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $searchset[$i]['forum_id']);
842                         $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $searchset[$i]['topic_id'] . "&amp;highlight=$highlight_active");
843                         $post_url = append_sid("viewtopic.$phpEx?" . POST_POST_URL . '=' . $searchset[$i]['post_id'] . "&amp;highlight=$highlight_active") . '#' . $searchset[$i]['post_id'];
845                         $post_date = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
847                         $message = $searchset[$i]['post_text'];
848                         $topic_title = $searchset[$i]['topic_title'];
850                         $forum_id = $searchset[$i]['forum_id'];
851                         $topic_id = $searchset[$i]['topic_id'];
853                         if ( $show_results == 'posts' )
854                         {
855                                 if ( isset($return_chars) )
856                                 {
857                                         $bbcode_uid = $searchset[$i]['bbcode_uid'];
859                                         //
860                                         // If the board has HTML off but the post has HTML
861                                         // on then we process it, else leave it alone
862                                         //
863                                         if ( $return_chars != -1 )
864                                         {
865                                                 $message = strip_tags($message);
866                                                 $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
867                                                 $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
868                                                 $message = ( strlen($message) > $return_chars ) ? substr($message, 0, $return_chars) . ' ...' : $message;
869                                         }
870                                         else
871                                         {
872                                                 if ( !$board_config['allow_html'] )
873                                                 {
874                                                         if ( $postrow[$i]['enable_html'] )
875                                                         {
876                                                                 $message = preg_replace('#(<)([\/]?.*?)(>)#is', '&lt;\\2&gt;', $message);
877                                                         }
878                                                 }
880                                                 if ( $bbcode_uid != '' )
881                                                 {
882                                                         $message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
883                                                 }
885                                                 $message = make_clickable($message);
887                                                 if ( $highlight_active )
888                                                 {
889                                                         if ( preg_match('/<.*>/', $message) )
890                                                         {
891                                                                 $message = preg_replace($highlight_match, '<!-- #sh -->\1<!-- #eh -->', $message);
893                                                                 $end_html = 0;
894                                                                 $start_html = 1;
895                                                                 $temp_message = '';
896                                                                 $message = ' ' . $message . ' ';
898                                                                 while( $start_html = strpos($message, '<', $start_html) )
899                                                                 {
900                                                                         $grab_length = $start_html - $end_html - 1;
901                                                                         $temp_message .= substr($message, $end_html + 1, $grab_length);
903                                                                         if ( $end_html = strpos($message, '>', $start_html) )
904                                                                         {
905                                                                                 $length = $end_html - $start_html + 1;
906                                                                                 $hold_string = substr($message, $start_html, $length);
908                                                                                 if ( strrpos(' ' . $hold_string, '<') != 1 )
909                                                                                 {
910                                                                                         $end_html = $start_html + 1;
911                                                                                         $end_counter = 1;
913                                                                                         while ( $end_counter && $end_html < strlen($message) )
914                                                                                         {
915                                                                                                 if ( substr($message, $end_html, 1) == '>' )
916                                                                                                 {
917                                                                                                         $end_counter--;
918                                                                                                 }
919                                                                                                 else if ( substr($message, $end_html, 1) == '<' )
920                                                                                                 {
921                                                                                                         $end_counter++;
922                                                                                                 }
924                                                                                                 $end_html++;
925                                                                                         }
927                                                                                         $length = $end_html - $start_html + 1;
928                                                                                         $hold_string = substr($message, $start_html, $length);
929                                                                                         $hold_string = str_replace('<!-- #sh -->', '', $hold_string);
930                                                                                         $hold_string = str_replace('<!-- #eh -->', '', $hold_string);
931                                                                                 }
932                                                                                 else if ( $hold_string == '<!-- #sh -->' )
933                                                                                 {
934                                                                                         $hold_string = str_replace('<!-- #sh -->', '<span style="color:#' . $theme['fontcolor3'] . '"><b>', $hold_string);
935                                                                                 }
936                                                                                 else if ( $hold_string == '<!-- #eh -->' )
937                                                                                 {
938                                                                                         $hold_string = str_replace('<!-- #eh -->', '</b></span>', $hold_string);
939                                                                                 }
941                                                                                 $temp_message .= $hold_string;
943                                                                                 $start_html += $length;
944                                                                         }
945                                                                         else
946                                                                         {
947                                                                                 $start_html = strlen($message);
948                                                                         }
949                                                                 }
951                                                                 $grab_length = strlen($message) - $end_html - 1;
952                                                                 $temp_message .= substr($message, $end_html + 1, $grab_length);
954                                                                 $message = trim($temp_message);
955                                                         }
956                                                         else
957                                                         {
958                                                                 $message = preg_replace($highlight_match, '<span style="color:#' . $theme['fontcolor3'] . '"><b>\1</b></span>', $message);
959                                                         }
960                                                 }
961                                         }
963                                         if ( count($orig_word) )
964                                         {
965                                                 $topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
966                                                 $post_subject = ( $searchset[$i]['post_subject'] != "" ) ? preg_replace($orig_word, $replacement_word, $searchset[$i]['post_subject']) : $topic_title;
968                                                 $message = preg_replace($orig_word, $replacement_word, $message);
969                                         }
970                                         else
971                                         {
972                                                 $post_subject = ( $searchset[$i]['post_subject'] != '' ) ? $searchset[$i]['post_subject'] : $topic_title;
973                                         }
975                                         if ($board_config['allow_smilies'] && $searchset[$i]['enable_smilies'])
976                                         {
977                                                 $message = smilies_pass($message);
978                                         }
980                                         $message = str_replace("\n", '<br />', $message);
982                                 }
984                                 $poster = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=" . $searchset[$i]['user_id']) . '">' : '';
985                                 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != "" ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
986                                 $poster .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
988                                 if ( $userdata['session_logged_in'] && $searchset[$i]['post_time'] > $userdata['user_lastvisit'] )
989                                 {
990                                         if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) )
991                                         {
992                                                 $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
993                                         }
994                                         else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) )
995                                         {
996                                                 $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
997                                         }
999                                         if ( $searchset[$i]['post_time'] > $topic_last_read )
1000                                         {
1001                                                 $mini_post_img = $images['icon_minipost_new'];
1002                                                 $mini_post_alt = $lang['New_post'];
1003                                         }
1004                                         else
1005                                         {
1006                                                 $mini_post_img = $images['icon_minipost'];
1007                                                 $mini_post_alt = $lang['Post'];
1008                                         }
1009                                 }
1010                                 else
1011                                 {
1012                                         $mini_post_img = $images['icon_minipost'];
1013                                         $mini_post_alt = $lang['Post'];
1014                                 }
1016                                 $template->assign_block_vars("searchresults", array( 
1017                                         'TOPIC_TITLE' => $topic_title,
1018                                         'FORUM_NAME' => $searchset[$i]['forum_name'],
1019                                         'POST_SUBJECT' => $post_subject,
1020                                         'POST_DATE' => $post_date,
1021                                         'POSTER_NAME' => $poster,
1022                                         'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
1023                                         'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
1024                                         'MESSAGE' => $message,
1025                                         'MINI_POST_IMG' => $mini_post_img, 
1027                                         'L_MINI_POST_ALT' => $mini_post_alt, 
1029                                         'U_POST' => $post_url,
1030                                         'U_TOPIC' => $topic_url,
1031                                         'U_FORUM' => $forum_url)
1032                                 );
1033                         }
1034                         else
1035                         {
1036                                 $message = '';
1038                                 if ( count($orig_word) )
1039                                 {
1040                                         $topic_title = preg_replace($orig_word, $replacement_word, $searchset[$i]['topic_title']);
1041                                 }
1043                                 $topic_type = $searchset[$i]['topic_type'];
1045                                 if ($topic_type == POST_ANNOUNCE)
1046                                 {
1047                                         $topic_type = $lang['Topic_Announcement'] . ' ';
1048                                 }
1049                                 else if ($topic_type == POST_STICKY)
1050                                 {
1051                                         $topic_type = $lang['Topic_Sticky'] . ' ';
1052                                 }
1053                                 else
1054                                 {
1055                                         $topic_type = '';
1056                                 }
1058                                 if ( $searchset[$i]['topic_vote'] )
1059                                 {
1060                                         $topic_type .= $lang['Topic_Poll'] . ' ';
1061                                 }
1063                                 $views = $searchset[$i]['topic_views'];
1064                                 $replies = $searchset[$i]['topic_replies'];
1066                                 if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
1067                                 {
1068                                         $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
1069                                         $goto_page = ' [ <img src="' . $images['icon_gotopost'] . '" alt="' . $lang['Goto_page'] . '" title="' . $lang['Goto_page'] . '" />' . $lang['Goto_page'] . ': ';
1071                                         $times = 1;
1072                                         for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
1073                                         {
1074                                                 $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&amp;start=$j") . '">' . $times . '</a>';
1075                                                 if ( $times == 1 && $total_pages > 4 )
1076                                                 {
1077                                                         $goto_page .= ' ... ';
1078                                                         $times = $total_pages - 3;
1079                                                         $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
1080                                                 }
1081                                                 else if ( $times < $total_pages )
1082                                                 {
1083                                                         $goto_page .= ', ';
1084                                                 }
1085                                                 $times++;
1086                                         }
1087                                         $goto_page .= ' ] ';
1088                                 }
1089                                 else
1090                                 {
1091                                         $goto_page = '';
1092                                 }
1094                                 if ( $searchset[$i]['topic_status'] == TOPIC_MOVED )
1095                                 {
1096                                         $topic_type = $lang['Topic_Moved'] . ' ';
1097                                         $topic_id = $searchset[$i]['topic_moved_id'];
1099                                         $folder_image = '<img src="' . $images['folder'] . '" alt="' . $lang['No_new_posts'] . '" />';
1100                                         $newest_post_img = '';
1101                                 }
1102                                 else
1103                                 {
1104                                         if ( $searchset[$i]['topic_status'] == TOPIC_LOCKED )
1105                                         {
1106                                                 $folder = $images['folder_locked'];
1107                                                 $folder_new = $images['folder_locked_new'];
1108                                         }
1109                                         else if ( $searchset[$i]['topic_type'] == POST_ANNOUNCE )
1110                                         {
1111                                                 $folder = $images['folder_announce'];
1112                                                 $folder_new = $images['folder_announce_new'];
1113                                         }
1114                                         else if ( $searchset[$i]['topic_type'] == POST_STICKY )
1115                                         {
1116                                                 $folder = $images['folder_sticky'];
1117                                                 $folder_new = $images['folder_sticky_new'];
1118                                         }
1119                                         else
1120                                         {
1121                                                 if ( $replies >= $board_config['hot_threshold'] )
1122                                                 {
1123                                                         $folder = $images['folder_hot'];
1124                                                         $folder_new = $images['folder_hot_new'];
1125                                                 }
1126                                                 else
1127                                                 {
1128                                                         $folder = $images['folder'];
1129                                                         $folder_new = $images['folder_new'];
1130                                                 }
1131                                         }
1133                                         if ( $userdata['session_logged_in'] )
1134                                         {
1135                                                 if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] ) 
1136                                                 {
1137                                                         if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1138                                                         {
1140                                                                 $unread_topics = true;
1142                                                                 if ( !empty($tracking_topics[$topic_id]) )
1143                                                                 {
1144                                                                         if ( $tracking_topics[$topic_id] > $searchset[$i]['post_time'] )
1145                                                                         {
1146                                                                                 $unread_topics = false;
1147                                                                         }
1148                                                                 }
1150                                                                 if ( !empty($tracking_forums[$forum_id]) )
1151                                                                 {
1152                                                                         if ( $tracking_forums[$forum_id] > $searchset[$i]['post_time'] )
1153                                                                         {
1154                                                                                 $unread_topics = false;
1155                                                                         }
1156                                                                 }
1158                                                                 if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
1159                                                                 {
1160                                                                         if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $searchset[$i]['post_time'] )
1161                                                                         {
1162                                                                                 $unread_topics = false;
1163                                                                         }
1164                                                                 }
1166                                                                 if ( $unread_topics )
1167                                                                 {
1168                                                                         $folder_image = $folder_new;
1169                                                                         $folder_alt = $lang['New_posts'];
1171                                                                         $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> ';
1172                                                                 }
1173                                                                 else
1174                                                                 {
1175                                                                         $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1177                                                                         $folder_image = $folder;
1178                                                                         $folder_alt = $folder_alt;
1179                                                                         $newest_post_img = '';
1180                                                                 }
1182                                                         }
1183                                                         else if ( $searchset[$i]['post_time'] > $userdata['user_lastvisit'] ) 
1184                                                         {
1185                                                                 $folder_image = $folder_new;
1186                                                                 $folder_alt = $lang['New_posts'];
1188                                                                 $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> ';
1189                                                         }
1190                                                         else 
1191                                                         {
1192                                                                 $folder_image = $folder;
1193                                                                 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1194                                                                 $newest_post_img = '';
1195                                                         }
1196                                                 }
1197                                                 else
1198                                                 {
1199                                                         $folder_image = $folder;
1200                                                         $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1201                                                         $newest_post_img = '';
1202                                                 }
1203                                         }
1204                                         else
1205                                         {
1206                                                 $folder_image = $folder;
1207                                                 $folder_alt = ( $searchset[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
1208                                                 $newest_post_img = '';
1209                                         }
1210                                 }
1213                                 $topic_author = ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '=' . $searchset[$i]['user_id']) . '">' : '';
1214                                 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? $searchset[$i]['username'] : ( ( $searchset[$i]['post_username'] != '' ) ? $searchset[$i]['post_username'] : $lang['Guest'] );
1216                                 $topic_author .= ( $searchset[$i]['user_id'] != ANONYMOUS ) ? '</a>' : '';
1218                                 $first_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['topic_time'], $board_config['board_timezone']);
1220                                 $last_post_time = create_date($board_config['default_dateformat'], $searchset[$i]['post_time'], $board_config['board_timezone']);
1222                                 $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>';
1224                                 $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>';
1226                                 $template->assign_block_vars('searchresults', array( 
1227                                         'FORUM_NAME' => $searchset[$i]['forum_name'],
1228                                         'FORUM_ID' => $forum_id,
1229                                         'TOPIC_ID' => $topic_id,
1230                                         'FOLDER' => $folder_image,
1231                                         'NEWEST_POST_IMG' => $newest_post_img, 
1232                                         'TOPIC_FOLDER_IMG' => $folder_image, 
1233                                         'GOTO_PAGE' => $goto_page,
1234                                         'REPLIES' => $replies,
1235                                         'TOPIC_TITLE' => $topic_title,
1236                                         'TOPIC_TYPE' => $topic_type,
1237                                         'VIEWS' => $views,
1238                                         'TOPIC_AUTHOR' => $topic_author, 
1239                                         'FIRST_POST_TIME' => $first_post_time, 
1240                                         'LAST_POST_TIME' => $last_post_time,
1241                                         'LAST_POST_AUTHOR' => $last_post_author,
1242                                         'LAST_POST_IMG' => $last_post_url,
1244                                         'L_TOPIC_FOLDER_ALT' => $folder_alt, 
1246                                         'U_VIEW_FORUM' => $forum_url, 
1247                                         'U_VIEW_TOPIC' => $topic_url)
1248                                 );
1249                         }
1250                 }
1252                 $base_url = "search.$phpEx?search_id=$search_id";
1254                 $template->assign_vars(array(
1255                         'PAGINATION' => generate_pagination($base_url, $total_match_count, $per_page, $start),
1256                         'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $per_page ) + 1 ), ceil( $total_match_count / $per_page )), 
1258                         'L_AUTHOR' => $lang['Author'],
1259                         'L_MESSAGE' => $lang['Message'],
1260                         'L_FORUM' => $lang['Forum'],
1261                         'L_TOPICS' => $lang['Topics'],
1262                         'L_REPLIES' => $lang['Replies'],
1263                         'L_VIEWS' => $lang['Views'],
1264                         'L_POSTS' => $lang['Posts'],
1265                         'L_LASTPOST' => $lang['Last_Post'], 
1266                         'L_POSTED' => $lang['Posted'], 
1267                         'L_SUBJECT' => $lang['Subject'],
1269                         'L_GOTO_PAGE' => $lang['Goto_page'])
1270                 );
1272                 $template->pparse('body');
1274                 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1275         }
1276         else
1277         {
1278                 message_die(GENERAL_MESSAGE, $lang['No_search_match']);
1279         }
1282 //
1283 // Search forum
1284 //
1285 $sql = "SELECT c.cat_title, c.cat_id, f.forum_name, f.forum_id  
1286         FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
1287         WHERE f.cat_id = c.cat_id 
1288         ORDER BY c.cat_id, f.forum_order";
1289 $result = $db->sql_query($sql);
1290 if ( !$result )
1292         message_die(GENERAL_ERROR, 'Could not obtain forum_name/forum_id', '', __LINE__, __FILE__, $sql);
1295 $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
1297 $s_forums = '';
1298 while( $row = $db->sql_fetchrow($result) )
1300         if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
1301         {
1302                 $s_forums .= '<option value="' . $row['forum_id'] . '">' . $row['forum_name'] . '</option>';
1303                 if ( empty($list_cat[$row['cat_id']]) )
1304                 {
1305                         $list_cat[$row['cat_id']] = $row['cat_title'];
1306                 }
1307         }
1310 if ( $s_forums != '' )
1312         $s_forums = '<option value="-1">' . $lang['All_available'] . '</option>' . $s_forums;
1314         //
1315         // Category to search
1316         //
1317         $s_categories = '<option value="-1">' . $lang['All_available'] . '</option>';
1318         while( list($cat_id, $cat_title) = @each($list_cat))
1319         {
1320                 $s_categories .= '<option value="' . $cat_id . '">' . $cat_title . '</option>';
1321         }
1323 else
1325         message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
1328 //
1329 // Number of chars returned
1330 //
1331 $s_characters = '<option value="-1">' . $lang['All_available'] . '</option>';
1332 $s_characters .= '<option value="0">0</option>';
1333 $s_characters .= '<option value="25">25</option>';
1334 $s_characters .= '<option value="50">50</option>';
1336 for($i = 100; $i < 1100 ; $i += 100)
1338         $selected = ( $i == 200 ) ? ' selected="selected"' : '';
1339         $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1342 //
1343 // Sorting
1344 //
1345 $s_sort_by = "";
1346 for($i = 0; $i < count($sort_by_types); $i++)
1348         $s_sort_by .= '<option value="' . $i . '">' . $sort_by_types[$i] . '</option>';
1351 //
1352 // Search time
1353 //
1354 $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
1355 $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']);
1357 $s_time = '';
1358 for($i = 0; $i < count($previous_days); $i++)
1360         $selected = ( $topic_days == $previous_days[$i] ) ? ' selected="selected"' : '';
1361         $s_time .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
1364 //
1365 // Output the basic page
1366 //
1367 $page_title = $lang['Search'];
1368 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1370 $template->set_filenames(array(
1371         'body' => 'search_body.tpl')
1372 );
1373 make_jumpbox('viewforum.'.$phpEx);
1375 $template->assign_vars(array(
1376         'L_SEARCH_QUERY' => $lang['Search_query'], 
1377         'L_SEARCH_OPTIONS' => $lang['Search_options'], 
1378         'L_SEARCH_KEYWORDS' => $lang['Search_keywords'], 
1379         'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'], 
1380         'L_SEARCH_AUTHOR' => $lang['Search_author'],
1381         'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'], 
1382         'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
1383         'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'], 
1384         'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'], 
1385         'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'], 
1386         'L_CATEGORY' => $lang['Category'], 
1387         'L_RETURN_FIRST' => $lang['Return_first'],
1388         'L_CHARACTERS' => $lang['characters_posts'], 
1389         'L_SORT_BY' => $lang['Sort_by'],
1390         'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
1391         'L_SORT_DESCENDING' => $lang['Sort_Descending'],
1392         'L_SEARCH_PREVIOUS' => $lang['Search_previous'], 
1393         'L_DISPLAY_RESULTS' => $lang['Display_results'], 
1394         'L_FORUM' => $lang['Forum'],
1395         'L_TOPICS' => $lang['Topics'],
1396         'L_POSTS' => $lang['Posts'],
1398         'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=results"),
1399         'S_CHARACTER_OPTIONS' => $s_characters,
1400         'S_FORUM_OPTIONS' => $s_forums, 
1401         'S_CATEGORY_OPTIONS' => $s_categories, 
1402         'S_TIME_OPTIONS' => $s_time, 
1403         'S_SORT_OPTIONS' => $s_sort_by,
1404         'S_HIDDEN_FIELDS' => '')
1405 );
1407 $template->pparse('body');
1409 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1411 ?>