summaryrefslogtreecommitdiff
path: root/smartyplugins/modifier.highlight.php
blob: be9c7648b68b17cf2b2ebb2004bf48f1405b94ad (plain)
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
<?php

namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;

function smarty_modifier_highlight( $source ) {
	global $gBitSystem, $gBitSmarty;

	if ( empty($_REQUEST['highlight']) ) return $source;

	if( !$gBitSystem->isFeatureActive( 'themes_output_highlighting' ) ) {
		return $source;
	}

	$colorArr = [ '#ffffcc', '#ffcccc', '#a0ffff', '#ffccff', '#ccffcc' ];

	$find = [
		"!(\s|^)%(\s|$)!",
		"!(\s|^)#(\s|$)!",
		"!(\s|^)@(\s|$)!",
		"!(\s|^):(\s|$)!",
		"!(\s|^)&(\s|$)!",
	];

	$words = trim( preg_replace( $find, "$1$2", urldecode( $_REQUEST['highlight'] ?? '' )));
	if( empty( $words )) {
		return $source;
	}

	$highlight = $source;

	$patterns = [
		"!<script[^>]+>.*?</script>!is"           => "@@@##########:#########%:#########&@@@",
		"!<div class=.?maketoc[^>]*>.*?</div>!si" => "@@@##########:#########%:#########@@@@",
		"'<[\/\!]*?[^<>]*?>'si"                   => "@@@##########:#########%:#########:@@@",
	];

	ksort( $patterns );

	foreach( $patterns as $pattern => $replace ) {
		preg_match_all( $pattern, $highlight, $match );
		$matches[$replace] = $match[0];
		$highlight = preg_replace( $pattern, $replace, $highlight );
	}

	$wordArr = [];
	$pattern = '#"([^"]*)"#';
	if( preg_match_all( $pattern, $words, $ms ) ) {
		$wordArr = $ms[1];
		$words = preg_replace( $pattern, "", $words );
	}

	$words = preg_replace( "!\s+!", " ", $words );
	if( !empty( $words ) ) {
		$wordArr = array_merge( $wordArr, explode( ' ', strip_tags($words) ) );
	}

	$i = 0;
	$wordList = KernelTools::tra( "Highlighted words" ).': ';
	foreach ( $wordArr as $word ) {
		$wordList .= '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i].';">'.$word.'</span> ';
		$highlight = preg_replace( "/(".preg_quote( $word, '/' ).")/si", '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i++].';">$1</span>', $highlight );
	}

	krsort( $patterns );

	foreach( $patterns as $pattern ) {
		foreach( $matches[$pattern] as $insert ) {
			$highlight = preg_replace( "!{$pattern}!", $insert, $highlight, 1 );
		}
	}
	$source = $highlight;
	$gBitSmarty->assign( 'highlightWordList', "<div class=\"wordlist\">$wordList</div>" );

	return $source;
}