home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 April (DVD) / PCWorld_2008-04_DVD.iso / temadvd / phpbb / phpBB-2.0.22.exe / phpBB2 / search.php < prev    next >
Encoding:
PHP Script  |  2006-12-19  |  42.7 KB  |  1,434 lines

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