Completed
Pull Request — master (#563)
by Richard
08:33
created

SyntaxHighlight::geshi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 6
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Text\Sanitizer\Extensions;
13
14
use Xoops\Core\Text\Sanitizer;
15
use Xoops\Core\Text\Sanitizer\FilterAbstract;
16
17
/**
18
 * TextSanitizer filter
19
 *
20
 * @category  Sanitizer
21
 * @package   Xoops\Core\Text
22
 * @author    Taiwen Jiang <[email protected]>
23
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class SyntaxHighlight extends FilterAbstract
28
{
29
    /**
30
     * @var array default configuration values
31
     */
32
    protected static $defaultConfiguration = [
33
        'enabled' => false,
34
        'highlighter' => 'php', // Source code highlight: '' - disable; 'php' - php highlight; 'geshi' - geshi highlight
35
        'language' => 'PHP'
36
    ];
37
38
    /**
39
     * apply syntax highlighting to a text string
40
     *
41
     * @param string $source   source code text to highlight
42
     * @param string $language of source code
43
     *
44
     * @return bool|mixed|string
45
     */
46 2
    public function applyFilter($source, $language = 'php')
47
    {
48 2
        $config = $this->config;
49 2
        if (empty($config['highlighter'])) {
50
            return "<pre>{$source}</pre>";
51
        }
52 2
        $source = $this->ts->undoHtmlSpecialChars($source);
53 2
        $source = stripslashes($source);
54 2
        if ($config['highlighter'] === 'geshi') {
55
            $language = str_replace('=', '', $language);
56
            $language = ($language) ? $language : $config['language'];
57
            $language = strtolower($language);
58
            if ($source2 = SyntaxHighlight::geshi($source, $language)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Xoops\Core\Text\Sanitize...yntaxHighlight::geshi() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            if ($source2 = SyntaxHighlight::/** @scrutinizer ignore-call */ geshi($source, $language)) {
Loading history...
59
                return $source2;
60
            }
61
        }
62 2
        $source = SyntaxHighlight::php($source);
0 ignored issues
show
Bug Best Practice introduced by
The method Xoops\Core\Text\Sanitize...\SyntaxHighlight::php() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $source = SyntaxHighlight::php($source);
Loading history...
63 2
        return $source;
64
    }
65
66
    /**
67
     * apply PHP highlight_string
68
     *
69
     * @param string $text source string
70
     *
71
     * @return string
72
     */
73 2
    public function php($text)
74
    {
75 2
        $text = trim($text);
76 2
        $addedOpenTag = false;
77 2
        if (!strpos($text, "<?php") && (substr($text, 0, 5) !== "<?php")) {
78 2
            $text = "<?php " . $text;
79 2
            $addedOpenTag = true;
80
        }
81
82 2
        $oldlevel = error_reporting(0);
83
84
        //There is a bug in the highlight function(php < 5.3) that it doesn't render
85
        //backslashes properly like in \s. So here we replace any backslashes
86 2
        $text = str_replace("\\", "XxxX", $text);
87
88 2
        $buffer = highlight_string($text, true); // Require PHP 4.20+
89
90
        //Placing backspaces back again
91 2
        $buffer = str_replace("XxxX", "\\", $buffer);
92
93 2
        error_reporting($oldlevel);
94 2
        $pos_open = $pos_close = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pos_close is dead and can be removed.
Loading history...
95 2
        if ($addedOpenTag) {
96 2
            $pos_open = strpos($buffer, '&lt;?php&nbsp;');
97
        }
98
99 2
        $str_open = ($addedOpenTag) ? substr($buffer, 0, $pos_open) : "";
100
101 2
        $length_open = ($addedOpenTag) ? $pos_open + 14 : 0;
102 2
        $str_internal = substr($buffer, $length_open);
103
104 2
        $buffer = $str_open . $str_internal;
105 2
        return $buffer;
106
    }
107
108
    /**
109
     * apply geshi highlighting
110
     *
111
     * @param string $source   source code text to highlight
112
     * @param string $language of source code
113
     *
114
     * @return bool
115
     */
116
    public function geshi($source, $language)
117
    {
118
        if (!@\XoopsLoad::load("geshi", "framework")) {
119
            return false;
120
        }
121
122
        // Create the new XoopsGeshi object, passing relevant stuff
123
        // XoopsGeshi should be extending geSHi in Frameworks/geshi/xoopsgeshi.php
124
        $geshi = new XoopsGeshi($source, $language);
0 ignored issues
show
Bug introduced by
The type Xoops\Core\Text\Sanitizer\Extensions\XoopsGeshi was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
126
        // Enclose the code in a <div>
127
        $geshi->set_header_type(GESHI_HEADER_NONE);
0 ignored issues
show
Bug introduced by
The constant Xoops\Core\Text\Sanitize...sions\GESHI_HEADER_NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
129
        // Sets the proper encoding charset other than "ISO-8859-1"
130
        $geshi->set_encoding(\XoopsLocale::getCharset());
131
132
        $geshi->set_link_target("_blank");
133
134
        // Parse the code
135
        $code = $geshi->parse_code();
136
137
        return $code;
138
    }
139
}
140