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