- md5
- 86e71518a3195cd740dc413947b57663
- sha1
- 03e33d45d7af3f81e5502b515c0445f76b216876
- sha256
- c509e88dcb52a97da001cb831fa8645788e5142dccbddd0145fdf94db7e6aa37
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
<?php
/**
* @package Joomla.Administrator
* @subpackage com_search
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Search component helper.
*
* @package Joomla.Administrator
* @subpackage com_search
* @since 1.5
*/
class SearchHelper
{
/**
* Configure the Linkbar.
*
* @param string The name of the active view.
* @since 1.6
*/
public static function addSubmenu($vName)
{
// Not required.
}
/**
* Gets a list of the actions that can be performed.
*
* @return JObject
*/
public static function getActions()
{
$user = JFactory::getUser();
$result = new JObject;
$assetName = 'com_search';
$actions = JAccess::getActions($assetName);
foreach ($actions as $action) {
$result->set($action->name, $user->authorise($action->name, $assetName));
}
return $result;
}
public static function santiseSearchWord(&$searchword, $searchphrase)
{
$ignored = false;
$lang = JFactory::getLanguage();
$tag = $lang->getTag();
$search_ignore = $lang->getIgnoredSearchWords();
// Deprecated in 1.6 use $lang->getIgnoredSearchWords instead
$ignoreFile = $lang->getLanguagePath() . '/' . $tag . '/' . $tag.'.ignore.php';
if (file_exists($ignoreFile)) {
include $ignoreFile;
}
// check for words to ignore
$aterms = explode(' ', JString::strtolower($searchword));
// first case is single ignored word
if (count($aterms) == 1 && in_array(JString::strtolower($searchword), $search_ignore)) {
$ignored = true;
}
// filter out search terms that are too small
$lower_limit = $lang->getLowerLimitSearchWord();
foreach($aterms as $aterm) {
if (JString::strlen($aterm) < $lower_limit) {
$search_ignore[] = $aterm;
}
}
// next is to remove ignored words from type 'all' or 'any' (not exact) searches with multiple words
if (count($aterms) > 1 && $searchphrase != 'exact') {
$pruned = array_diff($aterms, $search_ignore);
$searchword = implode(' ', $pruned);
}
return $ignored;
}
/**
* @since 1.5
*/
public static function limitSearchWord(&$searchword)
{
$restriction = false;
$lang = JFactory::getLanguage();
// limit searchword to a maximum of characters
$upper_limit = $lang->getUpperLimitSearchWord();
if (JString::strlen($searchword) > $upper_limit) {
$searchword = JString::substr($searchword, 0, $upper_limit - 1);
$restriction = true;
}
// searchword must contain a minimum of characters
if ($searchword && JString::strlen($searchword) < $lang->getLowerLimitSearchWord()) {
$searchword = '';
$restriction = true;
}
return $restriction;
}
/**
* Logs a search term
*
* @param string $search_term The term being searched
*
* @return void
*
* @since 1.5
* @deprecated 4.0 Use JSearchHelper::logSearch() instead
*/
public static function logSearch($search_term)
{
JLog::add(__METHOD__ . '() is deprecated, use JSearchHelper::logSearch() instead.', JLog::WARNING, 'deprecated');
JSearchHelper::logSearch($search_term, 'com_search');
}
/**
* Prepares results from search for display
*
* @param string The source string
* @param string The searchword to select around
* @return string
*
* @since 1.5
*/
public static function prepareSearchContent($text, $searchword)
{
// strips tags won't remove the actual jscript
$text = preg_replace("'<script[^>]*>.*?</script>'si", "", $text);
$text = preg_replace('/{.+?}/', '', $text);
//$text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text);
// replace line breaking tags with whitespace
$text = preg_replace("'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text);
return self::_smartSubstr(strip_tags($text), $searchword);
}
/**
* Checks an object for search terms (after stripping fields of HTML)
*
* @param object The object to check
* @param string Search words to check for
* @param array List of object variables to check against
* @returns boolean True if searchTerm is in object, false otherwise
*/
public static function checkNoHtml($object, $searchTerm, $fields)
{
$searchRegex = array(
'#<script[^>]*>.*?</script>#si',
'#<style[^>]*>.*?</style>#si',
'#<!.*?(--|]])>#si',
'#<[^>]*>#i'
);
$terms = explode(' ', $searchTerm);
if (empty($fields)) {
return false;
}
foreach($fields as $field) {
if (!isset($object->$field)) {
continue;
}
$text = $object->$field;
foreach($searchRegex as $regex) {
$text = preg_replace($regex, '', $text);
}
foreach($terms as $term) {
if (JString::stristr($text, $term) !== false) {
return true;
}
}
}
return false;
}
/**
* returns substring of characters around a searchword
*
* @param string The source string
* @param int Number of chars to return
* @param string The searchword to select around
* @return string
*
* @since 1.5
*/
public static function _smartSubstr($text, $searchword)
{
$lang = JFactory::getLanguage();
$length = $lang->getSearchDisplayedCharactersNumber();
$textlen = JString::strlen($text);
$lsearchword = JString::strtolower($searchword);
$wordfound = false;
$pos = 0;
while ($wordfound === false && $pos < $textlen) {
if (($wordpos = @JString::strpos($text, ' ', $pos + $length)) !== false) {
$chunk_size = $wordpos - $pos;
} else {
$chunk_size = $length;
}
$chunk = JString::substr($text, $pos, $chunk_size);
$wordfound = JString::strpos(JString::strtolower($chunk), $lsearchword);
if ($wordfound === false) {
$pos += $chunk_size + 1;
}
}
if ($wordfound !== false) {
return (($pos > 0) ? '... ' : '') . $chunk . ' ...';
} else {
if (($wordpos = @JString::strpos($text, ' ', $length)) !== false) {
return JString::substr($text, 0, $wordpos) . ' ...';
} else {
return JString::substr($text, 0, $length);
}
}
}
}
The same content is in 3 releases, Joomla 3.0.0 to 3.0.2. See them all, or every version of this file.