Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/administrator/components/com_search/helpers/search.php
f43d1a4680a9
5.6 KiB
text/x-php
menubar 0 to 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | <?php
/**
* @version $Id: search.php 10579 2008-07-22 14:54:24Z ircmaxell $
* @package Joomla
* @subpackage Search
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant to the
* GNU General Public License, and as distributed it includes or is derivative
* of works licensed under the GNU General Public License or other free or open
* source software licenses. See COPYRIGHT.php for copyright notices and
* details.
*/
/**
* @package Joomla
* @subpackage Search
*/
class SearchHelper
{
function santiseSearchWord(&$searchword, $searchphrase)
{
$ignored = false;
$lang =& JFactory::getLanguage();
$search_ignore = array();
$tag = $lang->getTag();
$ignoreFile = $lang->getLanguagePath().DS.$tag.DS.$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
foreach( $aterms AS $aterm ) {
if (JString::strlen( $aterm ) < 3) {
$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;
}
function limitSearchWord(&$searchword)
{
$restriction = false;
// limit searchword to 20 characters
if ( JString::strlen( $searchword ) > 20 ) {
$searchword = JString::substr( $searchword, 0, 19 );
$restriction = true;
}
// searchword must contain a minimum of 3 characters
if ( $searchword && JString::strlen( $searchword ) < 3 ) {
$searchword = '';
$restriction = true;
}
return $restriction;
}
function logSearch( $search_term )
{
global $mainframe;
$db =& JFactory::getDBO();
$params = &JComponentHelper::getParams( 'com_search' );
$enable_log_searches = $params->get('enabled');
$search_term = $db->getEscaped( trim( $search_term) );
if ( @$enable_log_searches )
{
$db =& JFactory::getDBO();
$query = 'SELECT hits'
. ' FROM #__core_log_searches'
. ' WHERE LOWER( search_term ) = "'.$search_term.'"'
;
$db->setQuery( $query );
$hits = intval( $db->loadResult() );
if ( $hits ) {
$query = 'UPDATE #__core_log_searches'
. ' SET hits = ( hits + 1 )'
. ' WHERE LOWER( search_term ) = "'.$search_term.'"'
;
$db->setQuery( $query );
$db->query();
} else {
$query = 'INSERT INTO #__core_log_searches VALUES ( "'.$search_term.'", 1 )';
$db->setQuery( $query );
$db->query();
}
}
}
/**
* Prepares results from search for display
*
* @param string The source string
* @param int Number of chars to trim
* @param string The searchword to select around
* @return string
*/
function prepareSearchContent( $text, $length = 200, $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 SearchHelper::_smartSubstr( strip_tags( $text ), $length, $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
*/
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
*/
function _smartSubstr($text, $length = 200, $searchword)
{
$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);
}
}
}
}
|