[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // No direct access.
   8  defined('_JEXEC') or die;
   9  
  10  /**
  11   * Search component helper.
  12   *
  13   * @package        Joomla.Administrator
  14   * @subpackage    com_search
  15   */
  16  class SearchHelper
  17  {
  18      /**
  19       * Configure the Linkbar.
  20       *
  21       * @param    string    The name of the active view.
  22       * @since    1.6
  23       */
  24  	public static function addSubmenu($vName)
  25      {
  26          // Not required.
  27      }
  28  
  29      /**
  30       * Gets a list of the actions that can be performed.
  31       *
  32       * @return    JObject
  33       */
  34  	public static function getActions()
  35      {
  36          $user    = JFactory::getUser();
  37          $result    = new JObject;
  38          $assetName = 'com_search';
  39  
  40          $actions = array(
  41              'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.state', 'core.delete'
  42          );
  43  
  44          foreach ($actions as $action) {
  45              $result->set($action,    $user->authorise($action, $assetName));
  46          }
  47  
  48          return $result;
  49      }
  50  
  51  	static function santiseSearchWord(&$searchword, $searchphrase)
  52      {
  53          $ignored = false;
  54  
  55          $lang = JFactory::getLanguage();
  56  
  57          $tag            = $lang->getTag();
  58          $search_ignore    = $lang->getIgnoredSearchWords();
  59  
  60          // Deprecated in 1.6 use $lang->getIgnoredSearchWords instead
  61          $ignoreFile        = $lang->getLanguagePath() . '/' . $tag . '/' . $tag.'.ignore.php';
  62          if (file_exists($ignoreFile)) {
  63              include $ignoreFile;
  64          }
  65  
  66          // check for words to ignore
  67          $aterms = explode(' ', JString::strtolower($searchword));
  68  
  69          // first case is single ignored word
  70          if (count($aterms) == 1 && in_array(JString::strtolower($searchword), $search_ignore)) {
  71              $ignored = true;
  72          }
  73  
  74          // filter out search terms that are too small
  75          $lower_limit = $lang->getLowerLimitSearchWord();
  76          foreach($aterms as $aterm) {
  77              if (JString::strlen($aterm) < $lower_limit) {
  78                  $search_ignore[] = $aterm;
  79              }
  80          }
  81  
  82          // next is to remove ignored words from type 'all' or 'any' (not exact) searches with multiple words
  83          if (count($aterms) > 1 && $searchphrase != 'exact') {
  84              $pruned = array_diff($aterms, $search_ignore);
  85              $searchword = implode(' ', $pruned);
  86          }
  87  
  88          return $ignored;
  89      }
  90  
  91  	static function limitSearchWord(&$searchword)
  92      {
  93          $restriction = false;
  94  
  95          $lang = JFactory::getLanguage();
  96  
  97          // limit searchword to a maximum of characters
  98          $upper_limit = $lang->getUpperLimitSearchWord();
  99          if (JString::strlen($searchword) > $upper_limit) {
 100              $searchword        = JString::substr($searchword, 0, $upper_limit - 1);
 101              $restriction    = true;
 102          }
 103  
 104          // searchword must contain a minimum of characters
 105          if ($searchword && JString::strlen($searchword) < $lang->getLowerLimitSearchWord()) {
 106              $searchword        = '';
 107              $restriction    = true;
 108          }
 109  
 110          return $restriction;
 111      }
 112  
 113  	static function logSearch($search_term)
 114      {
 115          $db = JFactory::getDbo();
 116  
 117          $params = JComponentHelper::getParams('com_search');
 118          $enable_log_searches = $params->get('enabled');
 119  
 120          $search_term = $db->escape(trim($search_term));
 121  
 122          if (@$enable_log_searches)
 123          {
 124              $db = JFactory::getDbo();
 125              $query = 'SELECT hits'
 126              . ' FROM #__core_log_searches'
 127              . ' WHERE LOWER(search_term) = "'.$search_term.'"'
 128              ;
 129              $db->setQuery($query);
 130              $hits = intval($db->loadResult());
 131              if ($hits) {
 132                  $query = 'UPDATE #__core_log_searches'
 133                  . ' SET hits = (hits + 1)'
 134                  . ' WHERE LOWER(search_term) = "'.$search_term.'"'
 135                  ;
 136                  $db->setQuery($query);
 137                  $db->query();
 138              } else {
 139                  $query = 'INSERT INTO #__core_log_searches VALUES ("'.$search_term.'", 1)';
 140                  $db->setQuery($query);
 141                  $db->query();
 142              }
 143          }
 144      }
 145  
 146      /**
 147       * Prepares results from search for display
 148       *
 149       * @param string The source string
 150       * @param string The searchword to select around
 151       * @return string
 152       */
 153  	public static function prepareSearchContent($text, $searchword)
 154      {
 155          // strips tags won't remove the actual jscript
 156          $text = preg_replace("'<script[^>]*>.*?</script>'si", "", $text);
 157          $text = preg_replace('/{.+?}/', '', $text);
 158          //$text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text);
 159          // replace line breaking tags with whitespace
 160          $text = preg_replace("'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text);
 161  
 162          return self::_smartSubstr(strip_tags($text), $searchword);
 163      }
 164  
 165      /**
 166       * Checks an object for search terms (after stripping fields of HTML)
 167       *
 168       * @param object The object to check
 169       * @param string Search words to check for
 170       * @param array List of object variables to check against
 171       * @returns boolean True if searchTerm is in object, false otherwise
 172       */
 173  	public static function checkNoHtml($object, $searchTerm, $fields)
 174      {
 175          $searchRegex = array(
 176                  '#<script[^>]*>.*?</script>#si',
 177                  '#<style[^>]*>.*?</style>#si',
 178                  '#<!.*?(--|]])>#si',
 179                  '#<[^>]*>#i'
 180                  );
 181          $terms = explode(' ', $searchTerm);
 182          if (empty($fields)) return false;
 183          foreach($fields as $field) {
 184              if (!isset($object->$field)) continue;
 185              $text = $object->$field;
 186              foreach($searchRegex as $regex) {
 187                  $text = preg_replace($regex, '', $text);
 188              }
 189              foreach($terms as $term) {
 190                  if (JString::stristr($text, $term) !== false) {
 191                      return true;
 192                  }
 193              }
 194          }
 195          return false;
 196      }
 197  
 198      /**
 199       * returns substring of characters around a searchword
 200       *
 201       * @param string The source string
 202       * @param int Number of chars to return
 203       * @param string The searchword to select around
 204       * @return string
 205       */
 206  	static function _smartSubstr($text, $searchword)
 207      {
 208          $lang = JFactory::getLanguage();
 209          $length = $lang->getSearchDisplayedCharactersNumber();
 210          $textlen = JString::strlen($text);
 211          $lsearchword = JString::strtolower($searchword);
 212          $wordfound = false;
 213          $pos = 0;
 214          while ($wordfound === false && $pos < $textlen) {
 215              if (($wordpos = @JString::strpos($text, ' ', $pos + $length)) !== false) {
 216                  $chunk_size = $wordpos - $pos;
 217              } else {
 218                  $chunk_size = $length;
 219              }
 220              $chunk = JString::substr($text, $pos, $chunk_size);
 221              $wordfound = JString::strpos(JString::strtolower($chunk), $lsearchword);
 222              if ($wordfound === false) {
 223                  $pos += $chunk_size + 1;
 224              }
 225          }
 226  
 227          if ($wordfound !== false) {
 228              return (($pos > 0) ? '...&#160;' : '') . $chunk . '&#160;...';
 229          } else {
 230              if (($wordpos = @JString::strpos($text, ' ', $length)) !== false) {
 231                  return JString::substr($text, 0, $wordpos) . '&#160;...';
 232              } else {
 233                  return JString::substr($text, 0, $length);
 234              }
 235          }
 236      }
 237  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1