1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* TextSanitizer extension |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
13
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) |
14
|
|
|
* @package class |
15
|
|
|
* @subpackage textsanitizer |
16
|
|
|
* @since 2.3.0 |
17
|
|
|
* @author Taiwen Jiang <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class MytsSyntaxhighlight |
23
|
|
|
*/ |
24
|
|
|
class MytsSyntaxhighlight extends MyTextSanitizerExtension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param $ts |
28
|
|
|
* @param $source |
29
|
|
|
* @param $language |
30
|
|
|
* |
31
|
|
|
* @return bool|mixed|string |
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
|
|
|
* @param $text |
48
|
|
|
* |
49
|
|
|
* @return mixed|string |
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
|
|
|
//There is a bug in the highlight function(php < 5.3) that it doesn't render |
67
|
|
|
//backslashes properly like in \s. So here we replace any backslashes |
68
|
|
|
$text = str_replace("\\", 'XxxX', $text); |
69
|
|
|
|
70
|
|
|
$buffer = highlight_string($text, true); // Require PHP 4.20+ |
71
|
|
|
|
72
|
|
|
//Placing backspaces back again |
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
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.