1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: 22: 23:
24: class MytsSyntaxhighlight extends MyTextSanitizerExtension
25: {
26: 27: 28: 29: 30: 31: 32:
33: public function load($ts, $source, $language)
34: {
35: $config = parent::loadConfig(__DIR__);
36: if (empty($config['highlight'])) {
37: return "<pre>{$source}</pre>";
38: }
39: $source = $ts->undoHtmlSpecialChars($source);
40: $source = stripslashes($source);
41: $source = MytsSyntaxhighlight::php($source);
42:
43: return $source;
44: }
45:
46: 47: 48: 49: 50:
51: public function php($text)
52: {
53: $text = trim($text);
54: $addedtag_open = 0;
55: if (!strpos($text, '<?php') && (substr($text, 0, 5) !== '<?php')) {
56: $text = '<?php ' . $text;
57: $addedtag_open = 1;
58: }
59: $addedtag_close = 0;
60: if (!strpos($text, '?>')) {
61: $text .= '?>';
62: $addedtag_close = 1;
63: }
64: $oldlevel = error_reporting(0);
65:
66:
67:
68: $text = str_replace("\\", 'XxxX', $text);
69:
70: $buffer = highlight_string($text, true);
71:
72:
73: $buffer = str_replace('XxxX', "\\", $buffer);
74:
75: error_reporting($oldlevel);
76: $pos_open = $pos_close = 0;
77: if ($addedtag_open) {
78: $pos_open = strpos($buffer, '<?php ');
79: }
80: if ($addedtag_close) {
81: $pos_close = strrpos($buffer, '?>');
82: }
83:
84: $str_open = $addedtag_open ? substr($buffer, 0, $pos_open) : '';
85: $str_close = $pos_close ? substr($buffer, $pos_close + 5) : '';
86:
87: $length_open = $addedtag_open ? $pos_open + 14 : 0;
88: $length_text = $pos_close ? $pos_close - $length_open : 0;
89: $str_internal = $length_text ? substr($buffer, $length_open, $length_text) : substr($buffer, $length_open);
90:
91: $buffer = $str_open . $str_internal . $str_close;
92:
93: return $buffer;
94: }
95: }
96: