PageRenderTime 40ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/4.8/components/com_search/search.php

http://miacms.googlecode.com/
PHP | 249 lines | 168 code | 41 blank | 40 comment | 34 complexity | 204c7172f64f5b6a6d7ee7d27ac0fdba MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * @package MiaCMS
  4. * @author MiaCMS see README.php
  5. * @copyright see README.php
  6. * See COPYRIGHT.php for copyright notices and details.
  7. * @license GNU/GPL Version 2, see LICENSE.php
  8. * MiaCMS is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2 of the License.
  11. */
  12. /** ensure this file is being included by a parent file */
  13. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  14. require_once( $mainframe->getPath( 'front_html' ) );
  15. switch ( $task ) {
  16. default:
  17. viewSearch();
  18. break;
  19. }
  20. function viewSearch() {
  21. global $mainframe, $mosConfig_absolute_path, $mosConfig_lang, $my;
  22. global $Itemid, $database, $_MAMBOTS;
  23. $gid = $my->gid;
  24. // Adds parameter handling
  25. if( $Itemid > 0 ) {
  26. $menu =& new mosMenu( $database );
  27. $menu->load( $Itemid );
  28. $params =& new mosParameters( $menu->params );
  29. $params->def( 'page_title', 1 );
  30. $params->def( 'pageclass_sfx', '' );
  31. $params->def( 'header', $menu->name, T_('Search') );
  32. $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );
  33. } else {
  34. $params =& new mosParameters('');
  35. $params->def( 'page_title', 1 );
  36. $params->def( 'pageclass_sfx', '' );
  37. $params->def( 'header', T_('Search') );
  38. $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );
  39. }
  40. // html output
  41. search_html::openhtml( $params );
  42. $searchword = mosGetParam( $_REQUEST, 'searchword', '' );
  43. $searchword = str_replace("%20"," ",$searchword );
  44. $searchword = $database->getEscaped( trim( $searchword ) );
  45. // search phrase limit 50
  46. if ( strlen( $searchword ) > 50 ) {
  47. $searchword = substr( $searchword, 0, 49 );
  48. }
  49. $search_ignore = array();
  50. @include "$mosConfig_absolute_path/language/$mosConfig_lang.ignore.php";
  51. $orders = array();
  52. $orders[] = mosHTML::makeOption( 'newest', T_('Newest first') );
  53. $orders[] = mosHTML::makeOption( 'oldest', T_('Oldest first') );
  54. $orders[] = mosHTML::makeOption( 'popular', T_('Most popular') );
  55. $orders[] = mosHTML::makeOption( 'alpha', T_('Alphabetical') );
  56. $orders[] = mosHTML::makeOption( 'category', T_('Section/Category') );
  57. $ordering = mosGetParam( $_REQUEST, 'ordering', 'newest');
  58. if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) $ordering = 'newest';
  59. $lists = array();
  60. $lists['ordering'] = mosHTML::selectList( $orders, 'ordering', 'class="inputbox"', 'value', 'text', $ordering );
  61. $searchphrase = mosGetParam( $_REQUEST, 'searchphrase', 'any' );
  62. if (!in_array($searchphrase, array('any', 'all', 'exact'))) $searchphrase = 'any';
  63. $searchphrases = array();
  64. $phrase = new stdClass();
  65. $phrase->value = 'any';
  66. $phrase->text = T_('Any words');
  67. $searchphrases[] = $phrase;
  68. $phrase = new stdClass();
  69. $phrase->value = 'all';
  70. $phrase->text = T_('All words');
  71. $searchphrases[] = $phrase;
  72. $phrase = new stdClass();
  73. $phrase->value = 'exact';
  74. $phrase->text = T_('Exact phrase');
  75. $searchphrases[] = $phrase;
  76. $lists['searchphrase']= mosHTML::radioList( $searchphrases, 'searchphrase', '', $searchphrase );
  77. // html output
  78. search_html::searchbox( htmlspecialchars( $searchword ), $lists, $params );
  79. $searchtype = mosGetParam( $_REQUEST, 'searchphrase', '' );
  80. if (!in_array($searchtype, array('all', 'exact'))) $searchtype = 'any';
  81. if (in_array($searchtype, array('all', 'any')))
  82. {
  83. $sanitizedsearch = array();
  84. $words = array_unique( preg_split( '/\s+|,/', $searchword ) );
  85. foreach ($words as $word) {
  86. if (strlen($word) > 2)
  87. array_push($sanitizedsearch, $word);
  88. }
  89. if ((count($sanitizedsearch) == 0) && (strlen($searchword) > 0)) {
  90. search_html::message( T_('One or more common words were ignored in the search'), $params );
  91. }
  92. $searchword = implode(' ', $sanitizedsearch);
  93. }
  94. if (!$searchword) {
  95. if ( count( $_POST ) ) {
  96. // html output
  97. // no matches found
  98. search_html::message( T_('No results were found'), $params );
  99. }
  100. } else {
  101. foreach ($search_ignore as $ignore_word) $searchword = preg_replace("/(^|\W)$ignore_word($|\W)/i", '$1$2', $searchword);
  102. $searchword = trim($searchword);
  103. if (!$searchword) search_html::message( T_('One or more common words were ignored in the search'), $params );
  104. }
  105. if ($searchword) {
  106. // html output
  107. search_html::searchintro( htmlspecialchars( $searchword ), $params );
  108. mosLogSearch( $searchword );
  109. $ordering = mosGetParam( $_REQUEST, 'ordering', '' );
  110. if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) $ordering = 'newest';
  111. $_MAMBOTS->loadBotGroup( 'search' );
  112. $results = $_MAMBOTS->trigger( 'onSearch', array( $searchword, $searchphrase, $ordering ) );
  113. $rows = array();
  114. foreach($results as $result) {
  115. if ($result) $rows = array_merge($rows, $result);
  116. }
  117. $totalRows = count( $rows );
  118. for ($i=0; $i < $totalRows; $i++) {
  119. $row = &$rows[$i]->text;
  120. if ($searchphrase == 'exact') {
  121. $searchwords = array($searchword);
  122. $needle = $searchword;
  123. } else {
  124. $searchwords = preg_split( '/\s+|,/', $searchword );
  125. $needle = $searchwords[0];
  126. }
  127. $row = mosPrepareSearchContent( $row, 200, $needle );
  128. foreach ($searchwords as $hlword) {
  129. $row = preg_replace( '/'. preg_quote($hlword, '/'). '/i', "<span class=\"highlight\">\\0</span>", $row);
  130. }
  131. if (!eregi( '^http', $rows[$i]->href )) {
  132. // determines Itemid for Content items
  133. if ( strstr( $rows[$i]->href, 'view' ) ) {
  134. // tests to see if itemid has already been included - this occurs for typed content items
  135. if ( !strstr( $rows[$i]->href, 'Itemid' ) ) {
  136. $temp = explode( 'id=', $rows[$i]->href );
  137. $rows[$i]->href = htmlentities($rows[$i]->href. '&Itemid='. $mainframe->getItemid($temp[1]));
  138. }
  139. }
  140. }
  141. }
  142. $mainframe->setPageTitle( T_('Search') );
  143. if ( $totalRows ) {
  144. // html output
  145. search_html::display( $rows, $params );
  146. } else {
  147. // html output
  148. search_html::displaynoresult();
  149. }
  150. // html output
  151. search_html::conclusion( $totalRows, htmlspecialchars( $searchword ) );
  152. }
  153. // displays back button
  154. echo '<br />';
  155. mosHTML::BackButton ( $params, 0 );
  156. }
  157. function mosLogSearch( $search_term ) {
  158. global $database;
  159. global $mosConfig_enable_log_searches;
  160. if ( @$mosConfig_enable_log_searches ) {
  161. $query = "SELECT hits"
  162. . "\n FROM #__core_log_searches"
  163. . "\n WHERE LOWER(search_term)='$search_term'"
  164. ;
  165. $database->setQuery( $query );
  166. $hits = intval( $database->loadResult() );
  167. if ( $hits ) {
  168. $query = "UPDATE #__core_log_searches SET hits=(hits+1) WHERE LOWER(search_term)='$search_term'";
  169. $database->setQuery( $query );
  170. $database->query();
  171. } else {
  172. $query = "INSERT INTO #__core_log_searches VALUES ('$search_term','1')";
  173. $database->setQuery( $query );
  174. $database->query();
  175. }
  176. }
  177. }
  178. /**
  179. * Prepares results from search for display
  180. * @param string The source string
  181. * @param int Number of chars to trim
  182. * @param string The searchword to select around
  183. * @return string
  184. */
  185. function mosPrepareSearchContent( $text, $length=200, $searchword ) {
  186. // strips tags won't remove the actual jscript
  187. $text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $text );
  188. $text = preg_replace( '/{.+?}/', '', $text);
  189. //$text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text );
  190. return mosSmartSubstr( strip_tags( $text ), $length, $searchword );
  191. }
  192. /**
  193. * returns substring of characters around a searchword
  194. * @param string The source string
  195. * @param int Number of chars to return
  196. * @param string The searchword to select around
  197. * @return string
  198. */
  199. function mosSmartSubstr($text, $length=200, $searchword) {
  200. $wordpos = strpos(strtolower($text), strtolower($searchword));
  201. $halfside = intval($wordpos - $length/2 - strlen($searchword));
  202. if ($wordpos && $halfside > 0) {
  203. return '...' . substr($text, $halfside, $length);
  204. } else {
  205. return substr( $text, 0, $length);
  206. }
  207. }
  208. ?>