[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @version        $Id: search.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package  Joomla
   5   * @subpackage    Search
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  /**
  19   * @package        Joomla
  20   * @subpackage    Search
  21   */
  22  class SearchHelper
  23  {
  24  	function santiseSearchWord(&$searchword, $searchphrase)
  25      {
  26          $ignored = false;
  27  
  28          $lang =& JFactory::getLanguage();
  29  
  30          $search_ignore    = array();
  31          $tag            = $lang->getTag();
  32          $ignoreFile        = $lang->getLanguagePath().DS.$tag.DS.$tag.'.ignore.php';
  33          if (file_exists($ignoreFile)) {
  34              include $ignoreFile;
  35          }
  36  
  37           // check for words to ignore
  38          $aterms = explode( ' ', JString::strtolower( $searchword ) );
  39  
  40          // first case is single ignored word
  41          if ( count( $aterms ) == 1 && in_array( JString::strtolower( $searchword ), $search_ignore ) ) {
  42              $ignored = true;
  43          }
  44  
  45          // filter out search terms that are too small
  46          foreach( $aterms AS $aterm ) {
  47              if (JString::strlen( $aterm ) < 3) {
  48                  $search_ignore[] = $aterm;
  49              }
  50          }
  51  
  52          // next is to remove ignored words from type 'all' or 'any' (not exact) searches with multiple words
  53          if ( count( $aterms ) > 1 && $searchphrase != 'exact' ) {
  54              $pruned = array_diff( $aterms, $search_ignore );
  55              $searchword = implode( ' ', $pruned );
  56          }
  57  
  58          return $ignored;
  59      }
  60  
  61  	function limitSearchWord(&$searchword)
  62      {
  63          $restriction = false;
  64  
  65          // limit searchword to 20 characters
  66          if ( JString::strlen( $searchword ) > 20 ) {
  67              $searchword     = JString::substr( $searchword, 0, 19 );
  68              $restriction     = true;
  69          }
  70  
  71          // searchword must contain a minimum of 3 characters
  72          if ( $searchword && JString::strlen( $searchword ) < 3 ) {
  73              $searchword     = '';
  74              $restriction     = true;
  75          }
  76  
  77          return $restriction;
  78      }
  79  
  80  	function logSearch( $search_term )
  81      {
  82          global $mainframe;
  83  
  84          $db =& JFactory::getDBO();
  85  
  86          $params = &JComponentHelper::getParams( 'com_search' );
  87          $enable_log_searches = $params->get('enabled');
  88  
  89          $search_term = $db->getEscaped( trim( $search_term) );
  90  
  91          if ( @$enable_log_searches )
  92          {
  93              $db =& JFactory::getDBO();
  94              $query = 'SELECT hits'
  95              . ' FROM #__core_log_searches'
  96              . ' WHERE LOWER( search_term ) = "'.$search_term.'"'
  97              ;
  98              $db->setQuery( $query );
  99              $hits = intval( $db->loadResult() );
 100              if ( $hits ) {
 101                  $query = 'UPDATE #__core_log_searches'
 102                  . ' SET hits = ( hits + 1 )'
 103                  . ' WHERE LOWER( search_term ) = "'.$search_term.'"'
 104                  ;
 105                  $db->setQuery( $query );
 106                  $db->query();
 107              } else {
 108                  $query = 'INSERT INTO #__core_log_searches VALUES ( "'.$search_term.'", 1 )';
 109                  $db->setQuery( $query );
 110                  $db->query();
 111              }
 112          }
 113      }
 114  
 115      /**
 116       * Prepares results from search for display
 117       *
 118       * @param string The source string
 119       * @param int Number of chars to trim
 120       * @param string The searchword to select around
 121       * @return string
 122       */
 123  	function prepareSearchContent( $text, $length = 200, $searchword )
 124      {
 125          // strips tags won't remove the actual jscript
 126          $text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $text );
 127          $text = preg_replace( '/{.+?}/', '', $text);
 128          //$text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text );
 129          // replace line breaking tags with whitespace
 130          $text = preg_replace( "'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text );
 131  
 132          return SearchHelper::_smartSubstr( strip_tags( $text ), $length, $searchword );
 133      }
 134  
 135      /**
 136       * Checks an object for search terms (after stripping fields of HTML)
 137       *
 138       * @param object The object to check
 139       * @param string Search words to check for
 140       * @param array List of object variables to check against
 141       * @returns boolean True if searchTerm is in object, false otherwise
 142       */
 143  	function checkNoHtml($object, $searchTerm, $fields) {
 144          $searchRegex = array(
 145                  '#<script[^>]*>.*?</script>#si',
 146                  '#<style[^>]*>.*?</style>#si',
 147                  '#<!.*?(--|]])>#si',
 148                  '#<[^>]*>#i'
 149                  );
 150          $terms = explode(' ', $searchTerm);
 151          if(empty($fields)) return false;
 152          foreach($fields AS $field) {
 153              if(!isset($object->$field)) continue;
 154              $text = $object->$field;
 155              foreach($searchRegex As $regex) {
 156                  $text = preg_replace($regex, '', $text);
 157              }
 158              foreach($terms AS $term) {
 159                  if(JString::stristr($text, $term) !== false) {
 160                      return true;
 161                  }
 162              }
 163          }
 164          return false;
 165      }
 166  
 167      /**
 168       * returns substring of characters around a searchword
 169       *
 170       * @param string The source string
 171       * @param int Number of chars to return
 172       * @param string The searchword to select around
 173       * @return string
 174       */
 175  	function _smartSubstr($text, $length = 200, $searchword)
 176      {
 177          $textlen = JString::strlen($text);
 178          $lsearchword = JString::strtolower($searchword);
 179          $wordfound = false;
 180          $pos = 0;
 181          while ($wordfound === false && $pos < $textlen) {
 182              if (($wordpos = @JString::strpos($text, ' ', $pos + $length)) !== false) {
 183                  $chunk_size = $wordpos - $pos;
 184              } else {
 185                  $chunk_size = $length;
 186              }
 187              $chunk = JString::substr($text, $pos, $chunk_size);
 188              $wordfound = JString::strpos(JString::strtolower($chunk), $lsearchword);
 189              if ($wordfound === false) {
 190                  $pos += $chunk_size + 1;
 191              }
 192          }
 193  
 194          if ($wordfound !== false) {
 195              return (($pos > 0) ? '...&nbsp;' : '') . $chunk . '&nbsp;...';
 196          } else {
 197              if (($wordpos = @JString::strpos($text, ' ', $length)) !== false) {
 198                  return JString::substr($text, 0, $wordpos) . '&nbsp;...';
 199              } else {
 200                  return JString::substr($text, 0, $length);
 201              }
 202          }
 203      }
 204  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1