1<?php
2/**
3 * Escapes tags in <html> with security concerns
4 */
5
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_htmlsafe extends DokuWiki_Syntax_Plugin {
14
15    function getType() { return 'protected';}
16    function getPType() { return 'normal';}
17    function getSort() { return 189; }
18
19    /**
20     * Connect pattern to lexer
21     */
22    function connectTo($mode) {
23        $this->Lexer->addSpecialPattern('<html>.*?</html>', $mode, 'plugin_htmlsafe');
24        $this->Lexer->addSpecialPattern('<HTML>.*?</HTML>', $mode, 'plugin_htmlsafe');
25    }
26
27    /**
28     * Handle the match
29     */
30    function handle($match, $state, $pos, &$handler){
31		return array( substr($match,1,4), substr($match,6,-7) );
32    }
33    /**
34     * Create output
35     */
36    function render($format, &$renderer, $data) {
37        if($format == 'xhtml'){
38            list($tag,$content) = $data;
39			global $conf;
40			$wrapper = ($tag === "HTML") ? "pre" : "code";
41			if($conf['htmlok']){
42				$strict = strtolower(str_replace(',',' ',$this->getConf('filter')));
43				$strict = array_unique(array_filter(explode(' ',$strict)));
44				$strict = implode( "|", $strict );
45				$renderer->doc .= preg_replace( "/<(\/?)($strict)(\s|>)/i", "&lt;$1$2$3", $content );
46			}
47			else {
48			  $renderer->doc .= p_xhtml_cached_geshi($content, 'html4strict', $wrapper);
49			}
50            return true;
51        }
52        return false;
53    }
54}
55