PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_search/helpers/search.php

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