PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/popoon/components/transformers/searchhighlighter.php

https://github.com/chregu/fluxcms
PHP | 97 lines | 43 code | 18 blank | 36 comment | 9 complexity | b371be3965e662dbca017c9c66240819 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | popoon |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2001-2006 Bitflux GmbH |
  6. // +----------------------------------------------------------------------+
  7. // | Licensed under the Apache License, Version 2.0 (the "License"); |
  8. // | you may not use this file except in compliance with the License. |
  9. // | You may obtain a copy of the License at |
  10. // | http://www.apache.org/licenses/LICENSE-2.0 |
  11. // | Unless required by applicable law or agreed to in writing, software |
  12. // | distributed under the License is distributed on an "AS IS" BASIS, |
  13. // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  14. // | implied. See the License for the specific language governing |
  15. // | permissions and limitations under the License. |
  16. // +----------------------------------------------------------------------+
  17. // | Author: Christian Stocker <chregu@bitflux.ch> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id$
  21. include_once("popoon/components/transformer.php");
  22. /**
  23. * Highlights a search string inside a document
  24. *
  25. * Examines the HTTP referrer and checks for a URL parameter named q.
  26. * If present, the document is rewritten, all occurences of q are wrapped
  27. * into <span class="searchHighlight">, except if they occur inside one of the tags
  28. * listed in $tabooTagNames.
  29. *
  30. * @author Christian Stocker <chregu@bitflux.ch>
  31. * @version $Id$
  32. * @package popoon
  33. */
  34. class popoon_components_transformers_searchhighlighter extends popoon_components_transformer {
  35. public $XmlFormat = "Own";
  36. /**
  37. * The tags inside of which highlighting is forbidden.
  38. * @var array
  39. * @see highlightConditional()
  40. */
  41. public $tabooTagNames = array('title', 'meta', 'script', 'style',);
  42. function __construct(&$sitemap) {
  43. parent::__construct($sitemap);
  44. }
  45. function DomStart(&$xml)
  46. {
  47. if (isset($_SERVER['HTTP_REFERER']) || !isset($_GET['q'])) {
  48. if (isset($_GET['q'])) {
  49. $query = $_GET['q'];
  50. } else if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],"q=")) {
  51. $ref = parse_url($_SERVER['HTTP_REFERER']);
  52. parse_str($ref['query'],$para);
  53. $query = $para['q'];
  54. }
  55. if (isset($query)) {
  56. parent::DomStart($xml);
  57. popoon_sitemap::var2XMLString($xml);
  58. $strings = explode(" ", $query);
  59. $search = array();
  60. foreach ($strings as $st) {
  61. $search[] = '#(<([^>\s/]+)>[^<]*)('.trim($st).')#i';
  62. }
  63. $xml = preg_replace_callback($search, array($this, 'highlightConditional') ,$xml);
  64. }
  65. }
  66. }
  67. protected function highlightConditional($matches){
  68. $beforeWanted = $matches[1];
  69. $tagName = strtolower($matches[2]);
  70. $wanted = $matches[3];
  71. if(!in_array($tagName, $this->tabooTagNames)){
  72. return $beforeWanted . '<span class="searchHighlight">'.$wanted.'</span>';
  73. }
  74. else {
  75. return $beforeWanted . $wanted;
  76. }
  77. }
  78. }
  79. ?>