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

Language PHP Lines 98
MD5 Hash b371be3965e662dbca017c9c66240819
Repository https://github.com/chregu/fluxcms.git View Raw File View Project SPDX
 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
<?php
// +----------------------------------------------------------------------+
// | popoon                                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2006 Bitflux GmbH                                 |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Christian Stocker <chregu@bitflux.ch>                        |
// +----------------------------------------------------------------------+
//
// $Id$

include_once("popoon/components/transformer.php");

/**
* Highlights a search string inside a document
*
* Examines the HTTP referrer and checks for a URL parameter named q.
* If present, the document is rewritten, all occurences of q are wrapped
* into <span class="searchHighlight">, except if they occur inside one of the tags
* listed in $tabooTagNames.
*
* @author   Christian Stocker <chregu@bitflux.ch>
* @version  $Id$
* @package  popoon
*/
class popoon_components_transformers_searchhighlighter extends popoon_components_transformer {

    public $XmlFormat = "Own";

    /**
    * The tags inside of which highlighting is forbidden.
    * @var array
    * @see highlightConditional()
    */
    public $tabooTagNames = array('title', 'meta', 'script', 'style',);


    function __construct(&$sitemap) {
        parent::__construct($sitemap);
    }

    function DomStart(&$xml)
    {
        
        if (isset($_SERVER['HTTP_REFERER']) || !isset($_GET['q'])) {
           if (isset($_GET['q'])) {
	     	$query = $_GET['q'];
	     }	else if (isset($_SERVER['HTTP_REFERER'])  && strpos($_SERVER['HTTP_REFERER'],"q=")) {

                $ref = parse_url($_SERVER['HTTP_REFERER']);
                parse_str($ref['query'],$para);
                $query =  $para['q'];
	    }

                if (isset($query)) {
                    parent::DomStart($xml);
                    popoon_sitemap::var2XMLString($xml);
                    $strings = explode(" ", $query);
                    $search = array();
                    foreach ($strings as $st) {
                        $search[] = '#(<([^>\s/]+)>[^<]*)('.trim($st).')#i';
                        
                    }
                    $xml = preg_replace_callback($search, array($this, 'highlightConditional') ,$xml);
                }
        }

    }

    protected function highlightConditional($matches){
        $beforeWanted = $matches[1];
        $tagName = strtolower($matches[2]);
        $wanted  = $matches[3];

        if(!in_array($tagName, $this->tabooTagNames)){
            return $beforeWanted . '<span class="searchHighlight">'.$wanted.'</span>';
        }
        else {
            return $beforeWanted . $wanted;
        }

    }

}


?>
Back to Top