Source for file search.php

Documentation is available at search.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_search
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Search component helper.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_search
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * Configure the Linkbar.
  22.      *
  23.      * @param   string    The name of the active view.
  24.      * @since   1.6
  25.      */
  26.     public static function addSubmenu($vName)
  27.     {
  28.         // Not required.
  29.     }
  30.  
  31.     /**
  32.      * Gets a list of the actions that can be performed.
  33.      *
  34.      * @return  JObject 
  35.      */
  36.     public static function getActions()
  37.     {
  38.         $user       JFactory::getUser();
  39.         $result       new JObject;
  40.         $assetName 'com_search';
  41.  
  42.         $actions JAccess::getActions($assetName);
  43.  
  44.         foreach ($actions as $action)
  45.         {
  46.             $result->set($action->name,    $user->authorise($action->name$assetName));
  47.         }
  48.  
  49.         return $result;
  50.     }
  51.  
  52.     public static function santiseSearchWord(&$searchword$searchphrase)
  53.     {
  54.         $ignored false;
  55.  
  56.         $lang          JFactory::getLanguage();
  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.  
  63.         if (file_exists($ignoreFile))
  64.         {
  65.             include $ignoreFile;
  66.         }
  67.  
  68.         // Check for words to ignore
  69.         $aterms explode(' 'JString::strtolower($searchword));
  70.  
  71.         // First case is single ignored word
  72.         if (count($aterms== && in_array(JString::strtolower($searchword)$search_ignore))
  73.         {
  74.             $ignored true;
  75.         }
  76.  
  77.         // Filter out search terms that are too small
  78.         $lower_limit $lang->getLowerLimitSearchWord();
  79.  
  80.         foreach ($aterms as $aterm)
  81.         {
  82.             if (JString::strlen($aterm$lower_limit)
  83.             {
  84.                 $search_ignore[$aterm;
  85.             }
  86.         }
  87.  
  88.         // Next is to remove ignored words from type 'all' or 'any' (not exact) searches with multiple words
  89.         if (count($aterms&& $searchphrase != 'exact')
  90.         {
  91.             $pruned     array_diff($aterms$search_ignore);
  92.             $searchword implode(' '$pruned);
  93.         }
  94.  
  95.         return $ignored;
  96.     }
  97.  
  98.     /**
  99.      * @since  1.5
  100.      */
  101.     public static function limitSearchWord(&$searchword)
  102.     {
  103.         $restriction false;
  104.  
  105.         $lang JFactory::getLanguage();
  106.  
  107.         // Limit searchword to a maximum of characters
  108.         $upper_limit $lang->getUpperLimitSearchWord();
  109.  
  110.         if (JString::strlen($searchword$upper_limit)
  111.         {
  112.             $searchword  JString::substr($searchword0$upper_limit 1);
  113.             $restriction true;
  114.         }
  115.  
  116.         // Searchword must contain a minimum of characters
  117.         if ($searchword && JString::strlen($searchword$lang->getLowerLimitSearchWord())
  118.         {
  119.             $searchword  '';
  120.             $restriction true;
  121.         }
  122.  
  123.         return $restriction;
  124.     }
  125.  
  126.     /**
  127.      * Logs a search term
  128.      *
  129.      * @param   string  $search_term  The term being searched
  130.      *
  131.      * @return  void 
  132.      *
  133.      * @since   1.5
  134.      * @deprecated  4.0  Use JSearchHelper::logSearch() instead
  135.      */
  136.     public static function logSearch($search_term)
  137.     {
  138.         JLog::add(__METHOD__ . '() is deprecated, use JSearchHelper::logSearch() instead.'JLog::WARNING'deprecated');
  139.  
  140.         JSearchHelper::logSearch($search_term'com_search');
  141.     }
  142.  
  143.     /**
  144.      * Prepares results from search for display
  145.      *
  146.      * @param   string  $text        The source string
  147.      * @param   string  $searchword  The searchword to select around
  148.      *
  149.      * @return  string 
  150.      *
  151.      * @since   1.5
  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.  
  159.         // $text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text);
  160.  
  161.         // Replace line breaking tags with whitespace
  162.         $text preg_replace("'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si"' '$text);
  163.  
  164.         return self::_smartSubstr(strip_tags($text)$searchword);
  165.     }
  166.  
  167.     /**
  168.      * Checks an object for search terms (after stripping fields of HTML)
  169.      *
  170.      * @param   object  $object      The object to check
  171.      * @param   string  $searchTerm  Search words to check for
  172.      * @param   array   $fields      List of object variables to check against
  173.      *
  174.      * @return  boolean True if searchTerm is in object, false otherwise
  175.      */
  176.     public static function checkNoHtml($object$searchTerm$fields)
  177.     {
  178.         $searchRegex array(
  179.             '#<script[^>]*>.*?</script>#si',
  180.             '#<style[^>]*>.*?</style>#si',
  181.             '#<!.*?(--|]])>#si',
  182.             '#<[^>]*>#i'
  183.         );
  184.         $terms explode(' '$searchTerm);
  185.  
  186.         if (empty($fields))
  187.         {
  188.             return false;
  189.         }
  190.  
  191.         foreach ($fields as $field)
  192.         {
  193.             if (!isset($object->$field))
  194.             {
  195.                 continue;
  196.             }
  197.  
  198.             $text self::remove_accents($object->$field);
  199.  
  200.             foreach ($searchRegex as $regex)
  201.             {
  202.                 $text preg_replace($regex''$text);
  203.             }
  204.  
  205.             foreach ($terms as $term)
  206.             {
  207.                 $term self::remove_accents($term);
  208.  
  209.                 if (JString::stristr($text$term!== false)
  210.                 {
  211.                     return true;
  212.                 }
  213.             }
  214.         }
  215.  
  216.         return false;
  217.     }
  218.  
  219.     /**
  220.      * Transliterates given text to ASCII//TRANSLIT.
  221.      * Simulates glibc transliteration style even if libiconv is used by PHP
  222.      *
  223.      * @param   string  $str  String to remove accents from
  224.      *
  225.      * @return  string 
  226.      *
  227.      * @since   3.2
  228.      */
  229.     public static function remove_accents($str)
  230.     {
  231.         setlocale(LC_ALL"en_GB.UTF-8");
  232.         $str iconv("UTF-8""ASCII//TRANSLIT//IGNORE"$str);
  233.         //TODO: remove other prefixes as well?
  234.         return preg_replace("/[\"'^]([a-z])/ui"'\1'$str);
  235.     }
  236.  
  237.     /**
  238.      * returns substring of characters around a searchword
  239.      *
  240.      * @param   string   $text        The source string
  241.      * @param   integer  $searchword  Number of chars to return
  242.      *
  243.      * @return  string 
  244.      *
  245.      * @since   1.5
  246.      */
  247.     public static function _smartSubstr($text$searchword)
  248.     {
  249.         $lang        JFactory::getLanguage();
  250.         $length      $lang->getSearchDisplayedCharactersNumber();
  251.         $ltext       self::remove_accents($text);
  252.         $textlen     JString::strlen($ltext);
  253.         $lsearchword JString::strtolower(self::remove_accents($searchword));
  254.         $wordfound   false;
  255.         $pos         0;
  256.  
  257.         while ($wordfound === false && $pos $textlen)
  258.         {
  259.             if (($wordpos @JString::strpos($ltext' '$pos $length)) !== false)
  260.             {
  261.                 $chunk_size $wordpos $pos;
  262.             }
  263.             else
  264.             {
  265.                 $chunk_size $length;
  266.             }
  267.  
  268.             $chunk     JString::substr($ltext$pos$chunk_size);
  269.             $wordfound JString::strpos(JString::strtolower($chunk)$lsearchword);
  270.  
  271.             if ($wordfound === false)
  272.             {
  273.                 $pos += $chunk_size 1;
  274.             }
  275.         }
  276.  
  277.         if ($wordfound !== false)
  278.         {
  279.             return (($pos 0'...&#160;' ''JString::substr($text$pos$chunk_size'&#160;...';
  280.         }
  281.         else
  282.         {
  283.             if (($wordpos @JString::strpos($text' '$length)) !== false)
  284.             {
  285.                 return JString::substr($text0$wordpos'&#160;...';
  286.             }
  287.             else
  288.             {
  289.                 return JString::substr($text0$length);
  290.             }
  291.         }
  292.     }
  293. }

Documentation generated on Tue, 19 Nov 2013 15:12:44 +0100 by phpDocumentor 1.4.3