Verzeichnisstruktur phpBB-3.0.0


Veröffentlicht
12.12.2007

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

search.php

Zuletzt modifiziert: 09.10.2024, 12:50 - Dateigröße: 38.62 KiB


0001  <?php
0002  /**
0003  *
0004  * @package phpBB3
0005  * @version $Id$
0006  * @copyright (c) 2005 phpBB Group
0007  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
0008  *
0009  */
0010   
0011  /**
0012  * @ignore
0013  */
0014  define('IN_PHPBB', true);
0015  $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
0016  $phpEx = substr(strrchr(__FILE__, '.'), 1);
0017  include($phpbb_root_path . 'common.' . $phpEx);
0018   
0019  // Start session management
0020  $user->session_begin();
0021  $auth->acl($user->data);
0022  $user->setup('search');
0023   
0024  // Define initial vars
0025  $mode            = request_var('mode', '');
0026  $search_id        = request_var('search_id', '');
0027  $start            = max(request_var('start', 0), 0);
0028  $post_id        = request_var('p', 0);
0029  $topic_id        = request_var('t', 0);
0030  $view            = request_var('view', '');
0031   
0032  $submit            = request_var('submit', false);
0033  $keywords        = utf8_normalize_nfc(request_var('keywords', '', true));
0034  $add_keywords    = utf8_normalize_nfc(request_var('add_keywords', '', true));
0035  $author            = request_var('author', '', true);
0036  $author_id        = request_var('author_id', 0);
0037  $show_results    = ($topic_id) ? 'posts' : request_var('sr', 'posts');
0038  $show_results    = ($show_results == 'posts') ? 'posts' : 'topics';
0039  $search_terms    = request_var('terms', 'all');
0040  $search_fields    = request_var('sf', 'all');
0041  $search_child    = request_var('sc', true);
0042   
0043  $sort_days        = request_var('st', 0);
0044  $sort_key        = request_var('sk', 't');
0045  $sort_dir        = request_var('sd', 'd');
0046   
0047  $return_chars    = request_var('ch', ($topic_id) ? -1 : 300);
0048  $search_forum    = request_var('fid', array(0));
0049   
0050  // Is user able to search? Has search been disabled?
0051  if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
0052  {
0053      $template->assign_var('S_NO_SEARCH', true);
0054      trigger_error('NO_SEARCH');
0055  }
0056   
0057  // Check search load limit
0058  if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load'])))
0059  {
0060      $template->assign_var('S_NO_SEARCH', true);
0061      trigger_error('NO_SEARCH_TIME');
0062  }
0063   
0064  // Check flood limit ... if applicable
0065  $interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval'];
0066  if ($interval && !$auth->acl_get('u_ignoreflood'))
0067  {
0068      if ($user->data['user_last_search'] > time() - $interval)
0069      {
0070          $template->assign_var('S_NO_SEARCH', true);
0071          trigger_error('NO_SEARCH_TIME');
0072      }
0073  }
0074   
0075  // Define some vars
0076  $limit_days        = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
0077  $sort_by_text    = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']);
0078   
0079  $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
0080  gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
0081   
0082  if ($keywords || $author || $author_id || $search_id || $submit)
0083  {
0084      // clear arrays
0085      $id_ary = array();
0086   
0087      // egosearch is an author search
0088      if ($search_id == 'egosearch')
0089      {
0090          $author_id = $user->data['user_id'];
0091          
0092          if ($user->data['user_id'] == ANONYMOUS)
0093          {
0094              login_box('', $user->lang['LOGIN_EXPLAIN_EGOSEARCH']);
0095          }
0096      }
0097   
0098      // If we are looking for authors get their ids
0099      $author_id_ary = array();
0100      if ($author_id)
0101      {
0102          $author_id_ary[] = $author_id;
0103      }
0104      else if ($author)
0105      {
0106          if ((strpos($author, '*') !== false) && (utf8_strlen(str_replace(array('*', '%'), '', $author)) < $config['min_search_author_chars']))
0107          {
0108              trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars']));
0109          }
0110   
0111          $sql_where = (strpos($author, '*') !== false) ? ' username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($author))) : " username_clean = '" . $db->sql_escape(utf8_clean_string($author)) . "'";
0112   
0113          $sql = 'SELECT user_id
0114              FROM ' . USERS_TABLE . "
0115              WHERE $sql_where
0116                  AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';
0117          $result = $db->sql_query_limit($sql, 100);
0118   
0119          while ($row = $db->sql_fetchrow($result))
0120          {
0121              $author_id_ary[] = (int) $row['user_id'];
0122          }
0123          $db->sql_freeresult($result);
0124   
0125          if (!sizeof($author_id_ary))
0126          {
0127              trigger_error('NO_SEARCH_RESULTS');
0128          }
0129      }
0130   
0131      // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode
0132      // so we can keep the old keywords in their old mode, but add the new ones as required words
0133      if ($add_keywords)
0134      {
0135          if ($search_terms == 'all')
0136          {
0137              $keywords .= ' ' . $add_keywords;
0138          }
0139          else
0140          {
0141              $search_terms = 'all';
0142              $keywords = implode(' |', explode(' ', preg_replace('#\s+#u', ' ', $keywords))) . ' ' .$add_keywords;
0143          }
0144      }
0145   
0146      // Which forums should not be searched? Author searches are also carried out in unindexed forums
0147      if (empty($keywords) && sizeof($author_id_ary))
0148      {
0149          $ex_fid_ary = array_keys($auth->acl_getf('!f_read', true));
0150      }
0151      else
0152      {
0153          $ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true))));
0154      }
0155   
0156      $not_in_fid = (sizeof($ex_fid_ary)) ? 'WHERE ' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . " OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ')' : "";
0157   
0158      $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id
0159          FROM ' . FORUMS_TABLE . ' f
0160          LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
0161              AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
0162          $not_in_fid
0163          ORDER BY f.left_id";
0164      $result = $db->sql_query($sql);
0165   
0166      $right_id = 0;
0167      $reset_search_forum = true;
0168      while ($row = $db->sql_fetchrow($result))
0169      {
0170          if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
0171          {
0172              $ex_fid_ary[] = (int) $row['forum_id'];
0173              continue;
0174          }
0175   
0176          if (sizeof($search_forum))
0177          {
0178              if ($search_child)
0179              {
0180                  if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)
0181                  {
0182                      $right_id = (int) $row['right_id'];
0183                  }
0184                  else if ($row['right_id'] < $right_id)
0185                  {
0186                      continue;
0187                  }
0188              }
0189   
0190              if (!in_array($row['forum_id'], $search_forum))
0191              {
0192                  $ex_fid_ary[] = (int) $row['forum_id'];
0193                  $reset_search_forum = false;
0194              }
0195          }
0196      }
0197      $db->sql_freeresult($result);
0198   
0199      // find out in which forums the user is allowed to view approved posts
0200      if ($auth->acl_get('m_approve'))
0201      {
0202          $m_approve_fid_ary = array(-1);
0203          $m_approve_fid_sql = '';
0204      }
0205      else if ($auth->acl_getf_global('m_approve'))
0206      {
0207          $m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary);
0208          $m_approve_fid_sql = ' AND (p.post_approved = 1' . ((sizeof($m_approve_fid_ary)) ? ' OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) : '') . ')';
0209      }
0210      else
0211      {
0212          $m_approve_fid_ary = array();
0213          $m_approve_fid_sql = ' AND p.post_approved = 1';
0214      }
0215   
0216      if ($reset_search_forum)
0217      {
0218          $search_forum = array();
0219      }
0220   
0221      // Select which method we'll use to obtain the post_id or topic_id information
0222      $search_type = basename($config['search_type']);
0223   
0224      if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
0225      {
0226          trigger_error('NO_SUCH_SEARCH_MODULE');
0227      }
0228   
0229      require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
0230   
0231      // We do some additional checks in the module to ensure it can actually be utilised
0232      $error = false;
0233      $search = new $search_type($error);
0234   
0235      if ($error)
0236      {
0237          trigger_error($error);
0238      }
0239   
0240      // let the search module split up the keywords
0241      if ($keywords)
0242      {
0243          $correct_query = $search->split_keywords($keywords, $search_terms);
0244          if (!$correct_query || (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id))
0245          {
0246              $ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], implode(' ', $search->common_words)) . '<br />' : '';
0247              trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max']));
0248          }
0249      }
0250   
0251      if (!$keywords && sizeof($author_id_ary))
0252      {
0253          // if it is an author search we want to show topics by default
0254          $show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts');
0255          $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
0256      }
0257   
0258      // define some variables needed for retrieving post_id/topic_id information
0259      $sort_by_sql = array('a' => 'u.username_clean', 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'p.post_subject' : 't.topic_title'));
0260   
0261      // pre-made searches
0262      $sql = $field = $l_search_title = '';
0263      if ($search_id)
0264      {
0265          switch ($search_id)
0266          {
0267              // Oh holy Bob, bring us some activity...
0268              case 'active_topics':
0269                  $l_search_title = $user->lang['SEARCH_ACTIVE_TOPICS'];
0270                  $show_results = 'topics';
0271                  $sort_key = 't';
0272                  $sort_dir = 'd';
0273                  $sort_days = request_var('st', 7);
0274                  $sort_by_sql['t'] = 't.topic_last_post_time';
0275   
0276                  gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
0277                  $s_sort_key = $s_sort_dir = '';
0278   
0279                  $last_post_time_sql = ($sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($sort_days * 24 * 3600)) : '';
0280   
0281                  $sql = 'SELECT t.topic_last_post_time, t.topic_id
0282                      FROM ' . TOPICS_TABLE . " t
0283                      WHERE t.topic_moved_id = 0
0284                          $last_post_time_sql
0285                          " . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
0286                          ' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . '
0287                      ORDER BY t.topic_last_post_time DESC';
0288                  $field = 'topic_id';
0289              break;
0290   
0291              case 'unanswered':
0292                  $l_search_title = $user->lang['SEARCH_UNANSWERED'];
0293                  $show_results = request_var('sr', 'topics');
0294                  $show_results = ($show_results == 'posts') ? 'posts' : 'topics';
0295                  $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
0296                  $sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title';
0297                  $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
0298   
0299                  $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : '';
0300                  $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort;
0301   
0302                  if ($sort_days)
0303                  {
0304                      $last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600));
0305                  }
0306                  else
0307                  {
0308                      $last_post_time = '';
0309                  }
0310   
0311   
0312                  if ($sort_key == 'a')
0313                  {
0314                      $sort_join = USERS_TABLE . ' u, ';
0315                      $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort;
0316                  }
0317                  if ($show_results == 'posts')
0318                  {
0319                      $sql = "SELECT p.post_id
0320                          FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
0321                          WHERE t.topic_replies = 0
0322                              AND p.topic_id = t.topic_id
0323                              $last_post_time
0324                              $m_approve_fid_sql
0325                              " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
0326                              $sql_sort";
0327                      $field = 'post_id';
0328                  }
0329                  else
0330                  {
0331                      $sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id
0332                          FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
0333                          WHERE t.topic_replies = 0
0334                              AND t.topic_moved_id = 0
0335                              AND p.topic_id = t.topic_id
0336                              $last_post_time
0337                              $m_approve_fid_sql
0338                              " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
0339                          $sql_sort";
0340                      $field = 'topic_id';
0341                  }
0342              break;
0343   
0344              case 'newposts':
0345                  $l_search_title = $user->lang['SEARCH_NEW'];
0346                  // force sorting
0347                  $show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics';
0348                  $sort_key = 't';
0349                  $sort_dir = 'd';
0350                  $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time';
0351                  $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
0352   
0353                  gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
0354                  $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = '';
0355   
0356                  if ($show_results == 'posts')
0357                  {
0358                      $sql = 'SELECT p.post_id
0359                          FROM ' . POSTS_TABLE . ' p
0360                          WHERE p.post_time > ' . $user->data['user_lastvisit'] . "
0361                              $m_approve_fid_sql
0362                              " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . "
0363                          $sql_sort";
0364                      $field = 'post_id';
0365                  }
0366                  else
0367                  {
0368                      $sql = 'SELECT t.topic_id
0369                          FROM ' . TOPICS_TABLE . ' t
0370                          WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . '
0371                              AND t.topic_moved_id = 0
0372                              ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . '
0373                              ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . "
0374                          $sql_sort";
0375                      $field = 'topic_id';
0376                  }
0377              break;
0378   
0379              case 'egosearch':
0380                  $l_search_title = $user->lang['SEARCH_SELF'];
0381              break;
0382          }
0383      }
0384   
0385      // show_results should not change after this
0386      $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
0387      $total_match_count = 0;
0388   
0389      if ($search_id)
0390      {
0391          if ($sql)
0392          {
0393              // only return up to 1000 ids (the last one will be removed later)
0394              $result = $db->sql_query_limit($sql, 1001 - $start, $start);
0395   
0396              while ($row = $db->sql_fetchrow($result))
0397              {
0398                  $id_ary[] = $row[$field];
0399              }
0400              $db->sql_freeresult($result);
0401   
0402              $total_match_count = sizeof($id_ary) + $start;
0403              $id_ary = array_slice($id_ary, 0, $per_page);
0404          }
0405          else
0406          {
0407              $search_id = '';
0408          }
0409      }
0410   
0411      // make sure that some arrays are always in the same order
0412      sort($ex_fid_ary);
0413      sort($m_approve_fid_ary);
0414      sort($author_id_ary);
0415   
0416      if (!empty($search->search_query))
0417      {
0418          $total_match_count = $search->keyword_search($show_results, $search_fields, $search_terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page);
0419      }
0420      else if (sizeof($author_id_ary))
0421      {
0422          $firstpost_only = ($search_fields === 'firstpost') ? true : false;
0423          $total_match_count = $search->author_search($show_results, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page);
0424      }
0425   
0426      // For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
0427      if (!sizeof($id_ary) && !$search_id)
0428      {
0429          trigger_error('NO_SEARCH_RESULTS');
0430      }
0431   
0432      $sql_where = '';
0433   
0434      if (sizeof($id_ary))
0435      {
0436          $sql_where .= $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary);
0437          $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : '';
0438          $sql_where .= ($show_results == 'posts') ? $m_approve_fid_sql : str_replace(array('p.post_approved', 'p.forum_id'), array('t.topic_approved', 't.forum_id'), $m_approve_fid_sql);
0439      }
0440   
0441      if ($show_results == 'posts')
0442      {
0443          include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
0444      }
0445      else
0446      {
0447          include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
0448      }
0449   
0450      $user->add_lang('viewtopic');
0451   
0452      // Grab icons
0453      $icons = $cache->obtain_icons();
0454   
0455      // Output header
0456      if ($search_id && ($total_match_count > 1000))
0457      {
0458          // limit the number to 1000 for pre-made searches
0459          $total_match_count--;
0460          $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count);
0461      }
0462      else
0463      {
0464          $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count);
0465      }
0466   
0467      // define some vars for urls
0468      $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '&quot;'), ' ', $keywords))));
0469      $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit)));
0470      $u_show_results = ($show_results != 'posts') ? '&amp;sr=' . $show_results : '';
0471      $u_search_forum = implode('&amp;fid%5B%5D=', $search_forum);
0472   
0473      $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results);
0474      $u_search .= ($search_id) ? '&amp;search_id=' . $search_id : '';
0475      $u_search .= ($u_hilit) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($search->search_query)) : '';
0476      $u_search .= ($topic_id) ? '&amp;t=' . $topic_id : '';
0477      $u_search .= ($author) ? '&amp;author=' . urlencode(htmlspecialchars_decode($author)) : '';
0478      $u_search .= ($author_id) ? '&amp;author_id=' . $author_id : '';
0479      $u_search .= ($u_search_forum) ? '&amp;fid%5B%5D=' . $u_search_forum : '';
0480      $u_search .= (!$search_child) ? '&amp;sc=0' : '';
0481      $u_search .= ($search_fields != 'all') ? '&amp;sf=' . $search_fields : '';
0482      $u_search .= ($return_chars != 300) ? '&amp;ch=' . $return_chars : '';
0483   
0484      $template->assign_vars(array(
0485          'SEARCH_TITLE'        => $l_search_title,
0486          'SEARCH_MATCHES'    => $l_search_matches,
0487          'SEARCH_WORDS'        => $search->search_query,
0488          'IGNORED_WORDS'        => (sizeof($search->common_words)) ? implode(' ', $search->common_words) : '',
0489          'PAGINATION'        => generate_pagination($u_search, $total_match_count, $per_page, $start),
0490          'PAGE_NUMBER'        => on_page($total_match_count, $per_page, $start),
0491          'TOTAL_MATCHES'        => $total_match_count,
0492          'SEARCH_IN_RESULTS'    => ($search_id) ? false : true,
0493   
0494          'S_SELECT_SORT_DIR'        => $s_sort_dir,
0495          'S_SELECT_SORT_KEY'        => $s_sort_key,
0496          'S_SELECT_SORT_DAYS'    => $s_limit_days,
0497          'S_SEARCH_ACTION'        => $u_search,
0498          'S_SHOW_TOPICS'            => ($show_results == 'posts') ? false : true,
0499   
0500          'GOTO_PAGE_IMG'        => $user->img('icon_post_target', 'GOTO_PAGE'),
0501          'NEWEST_POST_IMG'    => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
0502          'REPORTED_IMG'        => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
0503          'UNAPPROVED_IMG'    => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
0504          'LAST_POST_IMG'        => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
0505   
0506          'U_SEARCH_WORDS'    => $u_search,
0507      ));
0508   
0509      if ($sql_where)
0510      {
0511          if ($show_results == 'posts')
0512          {
0513              // @todo Joining this query to the one below?
0514              $sql = 'SELECT zebra_id, friend, foe
0515                  FROM ' . ZEBRA_TABLE . '
0516                  WHERE user_id = ' . $user->data['user_id'];
0517              $result = $db->sql_query($sql);
0518   
0519              $zebra = array();
0520              while ($row = $db->sql_fetchrow($result))
0521              {
0522                  $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id'];
0523              }
0524              $db->sql_freeresult($result);
0525   
0526              $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.username_clean, u.user_sig, u.user_sig_bbcode_uid, u.user_colour
0527                  FROM ' . POSTS_TABLE . ' p
0528                      LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id)
0529                      LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id)
0530                      LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id)
0531                  WHERE $sql_where";
0532          }
0533          else
0534          {
0535              $sql_from = TOPICS_TABLE . ' t
0536                  LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id)
0537                  ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : '');
0538              $sql_select = 't.*, f.forum_id, f.forum_name';
0539   
0540              if ($user->data['is_registered'])
0541              {
0542                  if ($config['load_db_track'])
0543                  {
0544                      $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . '
0545                          AND t.topic_id = tp.topic_id)';
0546                      $sql_select .= ', tp.topic_posted';
0547                  }
0548   
0549                  if ($config['load_db_lastread'])
0550                  {
0551                      $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . '
0552                              AND t.topic_id = tt.topic_id)
0553                          LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
0554                              AND ft.forum_id = f.forum_id)';
0555                      $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time';
0556                  }
0557              }
0558   
0559              if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread']))
0560              {
0561                  $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
0562                  $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
0563              }
0564   
0565              $sql = "SELECT $sql_select
0566                  FROM $sql_from
0567                  WHERE $sql_where";
0568          }
0569          $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
0570          $result = $db->sql_query($sql);
0571          $result_topic_id = 0;
0572   
0573          $rowset = array();
0574   
0575          if ($show_results == 'topics')
0576          {
0577              $forums = $rowset = $shadow_topic_list = array();
0578              while ($row = $db->sql_fetchrow($result))
0579              {
0580                  if ($row['topic_status'] == ITEM_MOVED)
0581                  {
0582                      $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
0583                  }
0584   
0585                  $rowset[$row['topic_id']] = $row;
0586   
0587                  if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread'])
0588                  {
0589                      $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time'];
0590                  }
0591                  $forums[$row['forum_id']]['topic_list'][] = $row['topic_id'];
0592                  $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']];
0593              }
0594              $db->sql_freeresult($result);
0595   
0596              // If we have some shadow topics, update the rowset to reflect their topic information
0597              if (sizeof($shadow_topic_list))
0598              {
0599                  $sql = 'SELECT *
0600                      FROM ' . TOPICS_TABLE . '
0601                      WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
0602                  $result = $db->sql_query($sql);
0603              
0604                  while ($row = $db->sql_fetchrow($result))
0605                  {
0606                      $orig_topic_id = $shadow_topic_list[$row['topic_id']];
0607              
0608                      // We want to retain some values
0609                      $row = array_merge($row, array(
0610                          'topic_moved_id'    => $rowset[$orig_topic_id]['topic_moved_id'],
0611                          'topic_status'        => $rowset[$orig_topic_id]['topic_status'],
0612                          'forum_name'        => $rowset[$orig_topic_id]['forum_name'])
0613                      );
0614              
0615                      $rowset[$orig_topic_id] = $row;
0616                  }
0617                  $db->sql_freeresult($result);
0618              }
0619              unset($shadow_topic_list);
0620   
0621              foreach ($forums as $forum_id => $forum)
0622              {
0623                  if ($user->data['is_registered'] && $config['load_db_lastread'])
0624                  {
0625                      $topic_tracking_info[$forum_id] = get_topic_tracking($forum_id, $forum['topic_list'], $forum['rowset'], array($forum_id => $forum['mark_time']), ($forum_id) ? false : $forum['topic_list']);
0626                  }
0627                  else if ($config['load_anon_lastread'] || $user->data['is_registered'])
0628                  {
0629                      $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']);
0630          
0631                      if (!$user->data['is_registered'])
0632                      {
0633                          $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
0634                      }
0635                  }
0636              }
0637              unset($forums);
0638          }
0639          else
0640          {
0641              $bbcode_bitfield = $text_only_message = '';
0642              $attach_list = array();
0643   
0644              while ($row = $db->sql_fetchrow($result))
0645              {
0646                  // We pre-process some variables here for later usage
0647                  $row['post_text'] = censor_text($row['post_text']);
0648   
0649                  $text_only_message = $row['post_text'];
0650                  // make list items visible as such
0651                  if ($row['bbcode_uid'])
0652                  {
0653                      $text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '&sdot;&nbsp;', $text_only_message);
0654                      // no BBCode in text only message
0655                      strip_bbcode($text_only_message, $row['bbcode_uid']);
0656                  }
0657   
0658                  if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
0659                  {
0660                      $row['display_text_only'] = false;
0661                      $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
0662   
0663                      // Does this post have an attachment? If so, add it to the list
0664                      if ($row['post_attachment'] && $config['allow_attachments'])
0665                      {
0666                          $attach_list[$row['forum_id']][] = $row['post_id'];
0667                      }
0668                  }
0669                  else
0670                  {
0671                      $row['post_text'] = $text_only_message;
0672                      $row['display_text_only'] = true;
0673                  }
0674   
0675                  $rowset[] = $row;
0676              }
0677              $db->sql_freeresult($result);
0678   
0679              unset($text_only_message);
0680   
0681              // Instantiate BBCode if needed
0682              if ($bbcode_bitfield !== '')
0683              {
0684                  include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
0685                  $bbcode = new bbcode(base64_encode($bbcode_bitfield));
0686              }
0687   
0688              // Pull attachment data
0689              if (sizeof($attach_list))
0690              {
0691                  $use_attach_list = $attach_list;
0692                  $attach_list = array();
0693   
0694                  foreach ($use_attach_list as $forum_id => $_list)
0695                  {
0696                      if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
0697                      {
0698                          $attach_list = array_merge($attach_list, $_list);
0699                      }
0700                  }
0701              }
0702   
0703              if (sizeof($attach_list))
0704              {
0705                  $sql = 'SELECT *
0706                      FROM ' . ATTACHMENTS_TABLE . '
0707                      WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
0708                          AND in_message = 0
0709                      ORDER BY filetime DESC, post_msg_id ASC';
0710                  $result = $db->sql_query($sql);
0711          
0712                  while ($row = $db->sql_fetchrow($result))
0713                  {
0714                      $attachments[$row['post_msg_id']][] = $row;
0715                  }
0716                  $db->sql_freeresult($result);
0717              }
0718          }
0719   
0720          if ($hilit)
0721          {
0722              // Remove bad highlights
0723              $hilit_array = array_filter(explode('|', $hilit), 'strlen');
0724              foreach ($hilit_array as $key => $value)
0725              {
0726                  $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#'));
0727                  $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]);
0728              }
0729              $hilit = implode('|', $hilit_array);
0730          }
0731   
0732          foreach ($rowset as $row)
0733          {
0734              $forum_id = $row['forum_id'];
0735              $result_topic_id = $row['topic_id'];
0736              $topic_title = censor_text($row['topic_title']);
0737   
0738              // we need to select a forum id for this global topic
0739              if (!$forum_id)
0740              {
0741                  if (!isset($g_forum_id))
0742                  {
0743                      // Get a list of forums the user cannot read
0744                      $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true)));
0745      
0746                      // Determine first forum the user is able to read (must not be a category)
0747                      $sql = 'SELECT forum_id
0748                          FROM ' . FORUMS_TABLE . '
0749                          WHERE forum_type = ' . FORUM_POST;
0750          
0751                      if (sizeof($forum_ary))
0752                      {
0753                          $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true);
0754                      }
0755   
0756                      $result = $db->sql_query_limit($sql, 1);
0757                      $g_forum_id = (int) $db->sql_fetchfield('forum_id');
0758                  }
0759                  $u_forum_id = $g_forum_id;
0760              }
0761              else
0762              {
0763                  $u_forum_id = $forum_id;
0764              }
0765   
0766              $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$u_forum_id&amp;t=$result_topic_id" . (($u_hilit) ? "&amp;hilit=$u_hilit" : ''));
0767   
0768              $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
0769   
0770              if ($show_results == 'topics')
0771              {
0772                  $folder_img = $folder_alt = $topic_type = '';
0773                  topic_status($row, $replies, (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false, $folder_img, $folder_alt, $topic_type);
0774   
0775                  $unread_topic = (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false;
0776   
0777                  $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
0778                  $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false;
0779                  $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$result_topic_id", true, $user->session_id) : '';
0780   
0781                  $row['topic_title'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['topic_title']);
0782   
0783                  $tpl_ary = array(
0784                      'TOPIC_AUTHOR'                => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
0785                      'TOPIC_AUTHOR_COLOUR'        => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
0786                      'TOPIC_AUTHOR_FULL'            => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
0787                      'FIRST_POST_TIME'            => $user->format_date($row['topic_time']),
0788                      'LAST_POST_SUBJECT'            => $row['topic_last_post_subject'],
0789                      'LAST_POST_TIME'            => $user->format_date($row['topic_last_post_time']),
0790                      'LAST_VIEW_TIME'            => $user->format_date($row['topic_last_view_time']),
0791                      'LAST_POST_AUTHOR'            => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
0792                      'LAST_POST_AUTHOR_COLOUR'    => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
0793                      'LAST_POST_AUTHOR_FULL'        => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
0794   
0795                      'PAGINATION'        => topic_generate_pagination($replies, $view_topic_url),
0796                      'TOPIC_TYPE'        => $topic_type,
0797   
0798                      'TOPIC_FOLDER_IMG'        => $user->img($folder_img, $folder_alt),
0799                      'TOPIC_FOLDER_IMG_SRC'    => $user->img($folder_img, $folder_alt, false, '', 'src'),
0800                      'TOPIC_ICON_IMG'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
0801                      'TOPIC_ICON_IMG_WIDTH'    => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
0802                      'TOPIC_ICON_IMG_HEIGHT'    => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
0803                      'ATTACH_ICON_IMG'        => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
0804                      'UNAPPROVED_IMG'        => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
0805   
0806                      'S_TOPIC_GLOBAL'        => (!$forum_id) ? true : false,
0807                      'S_TOPIC_TYPE'            => $row['topic_type'],
0808                      'S_USER_POSTED'            => (!empty($row['mark_type'])) ? true : false,
0809                      'S_UNREAD_TOPIC'        => $unread_topic,
0810   
0811                      'S_TOPIC_REPORTED'        => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
0812                      'S_TOPIC_UNAPPROVED'    => $topic_unapproved,
0813                      'S_POSTS_UNAPPROVED'    => $posts_unapproved,
0814   
0815                      'U_LAST_POST'            => $view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id'],
0816                      'U_LAST_POST_AUTHOR'    => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
0817                      'U_TOPIC_AUTHOR'        => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
0818                      'U_NEWEST_POST'            => $view_topic_url . '&amp;view=unread#unread',
0819                      'U_MCP_REPORT'            => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;t=' . $result_topic_id, true, $user->session_id),
0820                      'U_MCP_QUEUE'            => $u_mcp_queue,
0821                  );
0822              }
0823              else
0824              {
0825                  if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id']))
0826                  {
0827                      $template->assign_block_vars('searchresults', array(
0828                          'S_IGNORE_POST' => true,
0829   
0830                          'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"$u_search&amp;start=$start&amp;p=" . $row['post_id'] . '&amp;view=show#p' . $row['post_id'] . '">', '</a>'))
0831                      );
0832   
0833                      continue;
0834                  }
0835   
0836                  // Replace naughty words such as farty pants
0837                  $row['post_subject'] = censor_text($row['post_subject']);
0838   
0839                  if ($row['display_text_only'])
0840                  {
0841                      // now find context for the searched words
0842                      $row['post_text'] = get_context($row['post_text'], array_filter(explode('|', $hilit), 'strlen'), $return_chars);
0843                      $row['post_text'] = bbcode_nl2br($row['post_text']);
0844                  }
0845                  else
0846                  {
0847                      // Second parse bbcode here
0848                      if ($row['bbcode_bitfield'])
0849                      {
0850                          $bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
0851                      }
0852   
0853                      $row['post_text'] = bbcode_nl2br($row['post_text']);
0854                      $row['post_text'] = smiley_text($row['post_text']);
0855   
0856                      if (!empty($attachments[$row['post_id']]))
0857                      {
0858                          parse_attachments($forum_id, $row['post_text'], $attachments[$row['post_id']], $update_count);
0859                  
0860                          // we only display inline attachments
0861                          unset($attachments[$row['post_id']]);
0862                      }
0863                  }
0864   
0865                  if ($hilit)
0866                  {
0867                      // post highlighting
0868                      $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_text']);
0869                      $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $row['post_subject']);
0870                  }
0871   
0872                  $tpl_ary = array(
0873                      'POST_AUTHOR_FULL'        => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
0874                      'POST_AUTHOR_COLOUR'    => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
0875                      'POST_AUTHOR'            => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
0876                      'U_POST_AUTHOR'            => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
0877                  
0878                      'POST_SUBJECT'        => $row['post_subject'],
0879                      'POST_DATE'            => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '',
0880                      'MESSAGE'            => $row['post_text']
0881                  );
0882              }
0883   
0884              $template->assign_block_vars('searchresults', array_merge($tpl_ary, array(
0885                  'FORUM_ID'            => $forum_id,
0886                  'TOPIC_ID'            => $result_topic_id,
0887                  'POST_ID'            => ($show_results == 'posts') ? $row['post_id'] : false,
0888   
0889                  'FORUM_TITLE'        => $row['forum_name'],
0890                  'TOPIC_TITLE'        => $topic_title,
0891                  'TOPIC_REPLIES'        => $replies,
0892                  'TOPIC_VIEWS'        => $row['topic_views'],
0893   
0894                  'U_VIEW_TOPIC'        => $view_topic_url,
0895                  'U_VIEW_FORUM'        => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
0896                  'U_VIEW_POST'        => (!empty($row['post_id'])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=" . $row['topic_id'] . '&amp;p=' . $row['post_id'] . (($u_hilit) ? '&amp;hilit=' . $u_hilit : '')) . '#p' . $row['post_id'] : '')
0897              ));
0898          }
0899   
0900          if ($topic_id && ($topic_id == $result_topic_id))
0901          {
0902              $template->assign_vars(array(
0903                  'SEARCH_TOPIC'        => $topic_title,
0904                  'U_SEARCH_TOPIC'    => $view_topic_url
0905              ));
0906          }
0907      }
0908      unset($rowset);
0909   
0910      page_header(($l_search_title) ? $l_search_title : $user->lang['SEARCH']);
0911   
0912      $template->set_filenames(array(
0913          'body' => 'search_results.html')
0914      );
0915      make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
0916   
0917      page_footer();
0918  }
0919   
0920   
0921  // Search forum
0922  $s_forums = '';
0923  $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, fa.user_id
0924      FROM ' . FORUMS_TABLE . ' f
0925      LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
0926          AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
0927      ORDER BY f.left_id ASC";
0928  $result = $db->sql_query($sql);
0929   
0930  $right = $cat_right = $padding_inc = 0;
0931  $padding = $forum_list = $holding = '';
0932  $pad_store = array('0' => '');
0933   
0934  while ($row = $db->sql_fetchrow($result))
0935  {
0936      if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
0937      {
0938          // Non-postable forum with no subforums, don't display
0939          continue;
0940      }
0941   
0942      if ($row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id']))
0943      {
0944          // if this forum is a link or password protected (user has not entered the password yet) then skip to the next branch
0945          continue;
0946      }
0947   
0948      if ($row['left_id'] < $right)
0949      {
0950          $padding .= '&nbsp; &nbsp;';
0951          $pad_store[$row['parent_id']] = $padding;
0952      }
0953      else if ($row['left_id'] > $right + 1)
0954      {
0955          if (isset($pad_store[$row['parent_id']]))
0956          {
0957              $padding = $pad_store[$row['parent_id']];
0958          }
0959          else
0960          {
0961              continue;
0962          }
0963      }
0964   
0965      $right = $row['right_id'];
0966   
0967      if ($auth->acl_gets('!f_search', '!f_list', $row['forum_id']))
0968      {
0969          // if the user does not have permissions to search or see this forum skip only this forum/category
0970          continue;
0971      }
0972   
0973      $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : '';
0974   
0975      if ($row['left_id'] > $cat_right)
0976      {
0977          // make sure we don't forget anything
0978          $s_forums .= $holding;
0979          $holding = '';
0980      }
0981   
0982      if ($row['right_id'] - $row['left_id'] > 1)
0983      {
0984          $cat_right = max($cat_right, $row['right_id']);
0985   
0986          $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
0987      }
0988      else
0989      {
0990          $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>';
0991          $holding = '';
0992      }
0993  }
0994   
0995  if ($holding)
0996  {
0997      $s_forums .= $holding;
0998  }
0999   
1000  $db->sql_freeresult($result);
1001  unset($pad_store);
1002   
1003  if (!$s_forums)
1004  {
1005      trigger_error('NO_SEARCH');
1006  }
1007   
1008  // Number of chars returned
1009  $s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>';
1010  $s_characters .= '<option value="0">0</option>';
1011  $s_characters .= '<option value="25">25</option>';
1012  $s_characters .= '<option value="50">50</option>';
1013   
1014  for ($i = 100; $i <= 1000 ; $i += 100)
1015  {
1016      $selected = ($i == 300) ? ' selected="selected"' : '';
1017      $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
1018  }
1019   
1020  $s_hidden_fields = array('t' => $topic_id);
1021   
1022  if ($_SID)
1023  {
1024      $s_hidden_fields['sid'] = $_SID;
1025  }
1026   
1027  if (!empty($_EXTRA_URL))
1028  {
1029      foreach ($_EXTRA_URL as $url_param)
1030      {
1031          $url_param = explode('=', $url_param, 2);
1032          $s_hidden_fields[$url_param[0]] = $url_param[1];
1033      }
1034  }
1035   
1036  $template->assign_vars(array(
1037      'S_SEARCH_ACTION'        => "{$phpbb_root_path}search.$phpEx",
1038      'S_HIDDEN_FIELDS'        => build_hidden_fields($s_hidden_fields),
1039      'S_CHARACTER_OPTIONS'    => $s_characters,
1040      'S_FORUM_OPTIONS'        => $s_forums,
1041      'S_SELECT_SORT_DIR'        => $s_sort_dir,
1042      'S_SELECT_SORT_KEY'        => $s_sort_key,
1043      'S_SELECT_SORT_DAYS'    => $s_limit_days,
1044      'S_IN_SEARCH'            => true,
1045  ));
1046   
1047  // only show recent searches to search administrators
1048  if ($auth->acl_get('a_search'))
1049  {
1050      // Handle large objects differently for Oracle and MSSQL
1051      switch ($db->sql_layer)
1052      {
1053          case 'oracle':
1054              $sql = 'SELECT search_time, search_keywords
1055                  FROM ' . SEARCH_RESULTS_TABLE . '
1056                  WHERE dbms_lob.getlength(search_keywords) > 0
1057                  ORDER BY search_time DESC';
1058          break;
1059      
1060          case 'mssql':
1061          case 'mssql_odbc':
1062              $sql = 'SELECT search_time, search_keywords
1063                  FROM ' . SEARCH_RESULTS_TABLE . '
1064                  WHERE DATALENGTH(search_keywords) > 0
1065                  ORDER BY search_time DESC';
1066          break;
1067      
1068          default:
1069              $sql = 'SELECT search_time, search_keywords
1070                  FROM ' . SEARCH_RESULTS_TABLE . '
1071                  WHERE search_keywords <> \'\'
1072                  ORDER BY search_time DESC';
1073          break;
1074      }
1075      $result = $db->sql_query_limit($sql, 5);
1076   
1077      while ($row = $db->sql_fetchrow($result))
1078      {
1079          $keywords = $row['search_keywords'];
1080   
1081          $template->assign_block_vars('recentsearch', array(
1082              'KEYWORDS'    => $keywords,
1083              'TIME'        => $user->format_date($row['search_time']),
1084   
1085              'U_KEYWORDS'    => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode(htmlspecialchars_decode($keywords)))
1086          ));
1087      }
1088      $db->sql_freeresult($result);
1089  }
1090   
1091  // Output the basic page
1092  page_header($user->lang['SEARCH']);
1093   
1094  $template->set_filenames(array(
1095      'body' => 'search_body.html')
1096  );
1097  make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
1098   
1099  page_footer();
1100   
1101  ?>