| 1 | <?php |
|---|
| 2 | Class searchhl extends MvBlog_plugin implements MvBlog_pluginiface { |
|---|
| 3 | |
|---|
| 4 | public $name = "SearchHighlight"; |
|---|
| 5 | public $author = "Michiel van Baak"; |
|---|
| 6 | public $license = "GPL"; |
|---|
| 7 | public $website = "http://www.mvblog.org"; |
|---|
| 8 | public $description = "Plugin to highlight the words typed in at searchengine."; |
|---|
| 9 | |
|---|
| 10 | private $_mvblog; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | public function __construct(&$mvblog) { |
|---|
| 14 | $this->addHook("text_output", "highlightsearch"); |
|---|
| 15 | $this->addHook("css_output" , "searchhlCSS"); |
|---|
| 16 | $this->_mvblog =& $mvblog; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | public function activate() { |
|---|
| 20 | |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | public function deactivate() { |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | public function highlightsearch($text) { |
|---|
| 27 | if (array_key_exists("HTTP_REFERER", $_SERVER) && $_SERVER["HTTP_REFERER"]) { |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | preg_match("/[\?|&]q=(.*)[$|&]/", $_SERVER["HTTP_REFERER"], $matches); |
|---|
| 31 | if (array_key_exists(1, $matches) && $matches[1]) { |
|---|
| 32 | |
|---|
| 33 | preg_match_all("/<a[^>](.*?)>/si", $text, $txtmatches); |
|---|
| 34 | foreach ($txtmatches[0] as $txtmatchk=>$txtmatchv) { |
|---|
| 35 | $text = str_replace($txtmatchv, "##".$txtmatchk."##", $text); |
|---|
| 36 | } |
|---|
| 37 | $searchwords = explode("+", $matches[1]); |
|---|
| 38 | foreach ($searchwords as $k=>$v) { |
|---|
| 39 | $text = str_replace($v, "<span class=\"searchhl_".$k."\">".$v."</span>", $text); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | foreach ($txtmatches[0] as $txtmatchk=>$txtmatchv) { |
|---|
| 43 | $text = str_replace("##".$txtmatchk."##", $txtmatchv, $text); |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | return $text; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public function searchhlCSS($data) { |
|---|
| 51 | $output = $data; |
|---|
| 52 | $output .= "\t<style type=\"text/css\">\n"; |
|---|
| 53 | $output .= "\t\t.searchhl_0 { background-color: #fcc; }\n"; |
|---|
| 54 | $output .= "\t\t.searchhl_1 { background-color: #ff0; }\n"; |
|---|
| 55 | $output .= "\t\t.searchhl_2 { background-color: #f0f; }\n"; |
|---|
| 56 | $output .= "\t\t.searchhl_3 { background-color: #0ff; }\n"; |
|---|
| 57 | $output .= "\t\t.searchhl_4 { background-color:#FF9900; }\n"; |
|---|
| 58 | $output .= "\t\t.searchhl_5 { background-color:#99FF66; }\n"; |
|---|
| 59 | $output .= "\t\t.searchhl_6 { background-color:#99CCFF; }\n"; |
|---|
| 60 | $output .= "\t\t.searchhl_7 { background-color:#FF3333; }\n"; |
|---|
| 61 | $output .= "\t\t.searchhl_8 { background-color:#FF99FF; }\n"; |
|---|
| 62 | $output .= "\t\t.searchhl_9 { background-color:#FFFFCC; }\n"; |
|---|
| 63 | $output .= "\t</style>\n"; |
|---|
| 64 | return $output; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | ?> |
|---|