[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/components/com_search/ -> search.php (source)

   1  <?php
   2  /**
   3  * @package Mambo
   4  * @author Mambo Foundation Inc see README.php
   5  * @copyright (C) 2000 - 2009 Mambo Foundation Inc.
   6  * See COPYRIGHT.php for copyright notices and details.
   7  * @license GNU/GPL Version 2, see LICENSE.php
   8  *
   9  * Redistributions of files must retain the above copyright notice.
  10  *
  11  * Mambo is free software; you can redistribute it and/or
  12  * modify it under the terms of the GNU General Public License
  13  * as published by the Free Software Foundation; version 2 of the License.
  14  */
  15  
  16  /** ensure this file is being included by a parent file */
  17  defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  18  
  19  require_once( $mainframe->getPath( 'front_html' ) );
  20  
  21  switch ( $task ) {
  22      default:
  23          viewSearch();
  24          break;
  25  }
  26  
  27  function viewSearch() {
  28      global $mainframe, $mosConfig_absolute_path, $mosConfig_lang, $my;
  29      global $Itemid, $database, $_MAMBOTS;
  30      
  31      $gid = $my->gid;
  32      
  33      // Adds parameter handling
  34      if( $Itemid > 0 ) {
  35          $menu =& new mosMenu( $database );
  36          $menu->load( $Itemid );
  37          $params =& new mosParameters( $menu->params );
  38          $params->def( 'page_title', 1 );
  39          $params->def( 'pageclass_sfx', '' );
  40          $params->def( 'header', $menu->name, T_('Search') );
  41          $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );
  42      } else {
  43          $params =& new mosParameters('');
  44          $params->def( 'page_title', 1 );
  45          $params->def( 'pageclass_sfx', '' );
  46          $params->def( 'header', T_('Search') );
  47          $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );
  48      }
  49      
  50      // html output
  51      search_html::openhtml( $params );
  52      
  53      $searchword = mosGetParam( $_REQUEST, 'searchword', '' );
  54      $searchword = $database->getEscaped( trim( $searchword ) );
  55      
  56      // search phrase limit 50
  57      if ( strlen( $searchword ) > 50 ) {
  58          $searchword = substr( $searchword, 0, 49 );
  59      }
  60          
  61      $search_ignore = array();
  62      @include "$mosConfig_absolute_path/language/$mosConfig_lang.ignore.php";
  63      
  64      $orders = array();
  65      $orders[] = mosHTML::makeOption( 'newest', T_('Newest first') );
  66      $orders[] = mosHTML::makeOption( 'oldest', T_('Oldest first') );
  67      $orders[] = mosHTML::makeOption( 'popular', T_('Most popular') );
  68      $orders[] = mosHTML::makeOption( 'alpha', T_('Alphabetical') );
  69      $orders[] = mosHTML::makeOption( 'category', T_('Section/Category') );
  70      $ordering = mosGetParam( $_REQUEST, 'ordering', 'newest');
  71      if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) $ordering = 'newest';
  72      $lists = array();
  73      $lists['ordering'] = mosHTML::selectList( $orders, 'ordering', 'class="inputbox"', 'value', 'text', $ordering );
  74  
  75      $searchphrase = mosGetParam( $_REQUEST, 'searchphrase', 'any' );
  76      if (!in_array($searchphrase, array('any', 'all', 'exact'))) $searchphrase = 'any';
  77      $searchphrases = array();
  78      
  79      $phrase = new stdClass();
  80      $phrase->value = 'any';
  81      $phrase->text = T_('Any words');
  82      $searchphrases[] = $phrase;
  83      
  84      $phrase = new stdClass();
  85      $phrase->value = 'all';
  86      $phrase->text = T_('All words');
  87      $searchphrases[] = $phrase;
  88      
  89      $phrase = new stdClass();
  90      $phrase->value = 'exact';
  91      $phrase->text = T_('Exact phrase');
  92      $searchphrases[] = $phrase;    
  93  
  94      $lists['searchphrase']= mosHTML::radioList( $searchphrases, 'searchphrase', '', $searchphrase );
  95  
  96      // html output
  97      search_html::searchbox( htmlspecialchars( $searchword ), $lists, $params );
  98      
  99      
 100      $searchtype     = mosGetParam( $_REQUEST, 'searchphrase', '' );
 101      if (!in_array($searchtype, array('all', 'exact'))) $searchtype = 'any';
 102  
 103      if (in_array($searchtype, array('all', 'any')))
 104      {
 105          $sanitizedsearch = array();
 106          $words = array_unique( preg_split( '/\s+|,/', $searchword ) );
 107          foreach ($words as $word) {
 108              if (strlen($word) > 2)
 109                  array_push($sanitizedsearch, $word);
 110          }
 111          if (count($sanitizedsearch) == 0) { 
 112              search_html::message( T_('One or more common words were ignored in the search'), $params );
 113          }            
 114          $searchword = implode(' ', $sanitizedsearch);        
 115      }
 116  
 117      if (!$searchword) {
 118          if ( count( $_POST ) ) {
 119              // html output
 120              // no matches found
 121              search_html::message( T_('No results were found'), $params );
 122          }
 123      } else {
 124          foreach ($search_ignore as $ignore_word) $searchword = preg_replace("/(^|\W)$ignore_word($|\W)/i", '$1$2', $searchword);
 125          $searchword = trim($searchword);
 126          if (!$searchword) search_html::message( T_('One or more common words were ignored in the search'), $params );
 127      }
 128      if ($searchword) {
 129          // html output
 130          search_html::searchintro( htmlspecialchars( $searchword ), $params );
 131      
 132          mosLogSearch( $searchword );
 133          $ordering     = mosGetParam( $_REQUEST, 'ordering', '' );
 134          if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) $ordering = 'newest';
 135      
 136          
 137          
 138          $_MAMBOTS->loadBotGroup( 'search' );
 139          $results     = $_MAMBOTS->trigger( 'onSearch', array( $searchword, $searchphrase, $ordering ) );
 140          $rows = array();
 141          foreach($results as $result) {
 142              if ($result) $rows = array_merge($rows, $result);
 143          }
 144      
 145          $totalRows = count( $rows );
 146      
 147          for ($i=0; $i < $totalRows; $i++) {
 148          
 149              $row = &$rows[$i]->text;
 150              if ($searchphrase == 'exact') {
 151                  $searchwords = array($searchword);
 152                  $needle = $searchword;
 153                } else {
 154                  $searchwords = preg_split( '/\s+|,/', $searchword );
 155                  $needle = $searchwords[0];
 156                }
 157        
 158              $row = mosPrepareSearchContent( $row, 200, $needle );
 159  
 160                foreach ($searchwords as $hlword) {
 161                  $row = preg_replace( '/'. preg_quote($hlword, '/'). '/i', "<span class=\"highlight\">\\0</span>", $row); 
 162                }
 163      
 164              if (!eregi( '^http', $rows[$i]->href )) {
 165                  // determines Itemid for Content items
 166                  if ( strstr( $rows[$i]->href, 'view' ) ) {
 167                      // tests to see if itemid has already been included - this occurs for typed content items
 168                      if ( !strstr( $rows[$i]->href, 'Itemid' ) ) {
 169                          $temp = explode( 'id=', $rows[$i]->href );
 170                          $rows[$i]->href = $rows[$i]->href. '&amp;Itemid='. $mainframe->getItemid($temp[1]);
 171                      }
 172                  }
 173              }
 174          }
 175  
 176          $mainframe->setPageTitle( T_('Search') );
 177          
 178          if ( $totalRows ) {
 179          // html output
 180              search_html::display( $rows, $params );
 181          } else {
 182          // html output
 183              search_html::displaynoresult();
 184          }
 185      
 186          // html output
 187          search_html::conclusion( $totalRows, htmlspecialchars( $searchword ) );
 188      }
 189      
 190      // displays back button
 191      echo '<br />';
 192      mosHTML::BackButton ( $params, 0 );    
 193  }
 194  
 195  
 196  function mosLogSearch( $search_term ) {
 197      global $database;
 198      global $mosConfig_enable_log_searches;
 199  
 200      if ( @$mosConfig_enable_log_searches ) {
 201          $query = "SELECT hits"
 202          . "\n FROM #__core_log_searches"
 203          . "\n WHERE LOWER(search_term)='$search_term'"
 204          ;
 205          $database->setQuery( $query );
 206          $hits = intval( $database->loadResult() );
 207          if ( $hits ) {
 208              $query = "UPDATE #__core_log_searches SET hits=(hits+1) WHERE LOWER(search_term)='$search_term'";
 209              $database->setQuery( $query );
 210              $database->query();
 211          } else {
 212              $query = "INSERT INTO #__core_log_searches VALUES ('$search_term','1')";
 213              $database->setQuery( $query );
 214              $database->query();
 215          }
 216      }
 217  }
 218  
 219  /**
 220  * Prepares results from search for display
 221  * @param string The source string
 222  * @param int Number of chars to trim
 223  * @param string The searchword to select around
 224  * @return string
 225  */
 226  function mosPrepareSearchContent( $text, $length=200, $searchword ) {
 227      // strips tags won't remove the actual jscript
 228      $text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $text );
 229      $text = preg_replace( '/{.+?}/', '', $text);
 230      //$text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text );
 231      return mosSmartSubstr( strip_tags( $text ), $length, $searchword );
 232  }
 233  
 234  /**
 235  * returns substring of characters around a searchword
 236  * @param string The source string
 237  * @param int Number of chars to return
 238  * @param string The searchword to select around
 239  * @return string
 240  */
 241  function mosSmartSubstr($text, $length=200, $searchword) {
 242    $wordpos = strpos(strtolower($text), strtolower($searchword));
 243    $halfside = intval($wordpos - $length/2 - strlen($searchword));
 244    if ($wordpos && $halfside > 0) {
 245        return '...' . substr($text, $halfside, $length);
 246    } else {
 247      return substr( $text, 0, $length);
 248    }
 249  }
 250  
 251  ?>