PageRenderTime 27ms CodeModel.GetById 22ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/class/textsanitizer/syntaxhighlight/syntaxhighlight.php

https://gitlab.com/VoyaTrax/vtCMS3
PHP | 130 lines | 69 code | 18 blank | 43 comment | 10 complexity | 8dcd106eadb9a51f1216e0a360b3ad70 MD5 | raw file
  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/**
 13 * TextSanitizer extension
 14 *
 15 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
 16 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 17 * @package         class
 18 * @subpackage      textsanitizer
 19 * @since           2.3.0
 20 * @author          Taiwen Jiang <phppp@users.sourceforge.net>
 21 * @version         $Id: syntaxhighlight.php 8271 2011-11-11 19:46:00Z trabis $
 22 */
 23
 24defined('XOOPS_ROOT_PATH') or die('Restricted access');
 25
 26class MytsSyntaxhighlight extends MyTextSanitizerExtension
 27{
 28    /**
 29     * @param MyTextSanitizer $ts
 30     * @param string $source
 31     * @param string $language
 32     * @return bool|mixed|string
 33     */
 34    public function load(MyTextSanitizer &$ts, $source, $language)
 35    {
 36        $config = parent::loadConfig(dirname(__FILE__));
 37        if (empty($config['highlight'])) {
 38            return "<pre>{$source}</pre>";
 39        }
 40        $source = $ts->undoHtmlSpecialChars($source);
 41        $source = stripslashes($source);
 42        if ($config['highlight'] == 'geshi') {
 43            $language = str_replace('=', '', $language);
 44            $language = ($language) ? $language : $config['language'];
 45            $language = strtolower($language);
 46            if ($source2 = MytsSyntaxhighlight::geshi($source, $language)) {
 47                return $source2;
 48            }
 49        }
 50        $source = MytsSyntaxhighlight::php($source);
 51        return $source;
 52    }
 53
 54    /**
 55     * @param string $text
 56     * @return mixed|string
 57     */
 58    public function php($text)
 59    {
 60        $text = trim($text);
 61        $addedtag_open = 0;
 62        if (!strpos($text, "<?php") and (substr($text, 0, 5) != "<?php")) {
 63            $text = "<?php " . $text;
 64            $addedtag_open = 1;
 65        }
 66        $addedtag_close = 0;
 67        if (!strpos($text, "?>")) {
 68            $text .= "?>";
 69            $addedtag_close = 1;
 70        }
 71        $oldlevel = error_reporting(0);
 72
 73        //There is a bug in the highlight function(php < 5.3) that it doesn't render
 74        //backslashes properly like in \s. So here we replace any backslashes
 75        $text = str_replace("\\", "XxxX", $text);
 76
 77        $buffer = highlight_string($text, true); // Require PHP 4.20+
 78
 79        //Placing backspaces back again
 80        $buffer = str_replace("XxxX", "\\", $buffer);
 81
 82        error_reporting($oldlevel);
 83        $pos_open = $pos_close = 0;
 84        if ($addedtag_open) {
 85            $pos_open = strpos($buffer, '&lt;?php&nbsp;');
 86        }
 87        if ($addedtag_close) {
 88            $pos_close = strrpos($buffer, '?&gt;');
 89        }
 90
 91        $str_open = ($addedtag_open) ? substr($buffer, 0, $pos_open) : "";
 92        $str_close = ($pos_close) ? substr($buffer, $pos_close + 5) : "";
 93
 94        $length_open = ($addedtag_open) ? $pos_open + 14 : 0;
 95        $length_text = ($pos_close) ? $pos_close - $length_open : 0;
 96        $str_internal = ($length_text) ? substr($buffer, $length_open, $length_text) : substr($buffer, $length_open);
 97
 98        $buffer = $str_open . $str_internal . $str_close;
 99        return $buffer;
100    }
101
102    /**
103     * @param string $source
104     * @param string $language
105     * @return bool
106     */
107    public function geshi($source, $language)
108    {
109        if (!@XoopsLoad::load("geshi", "framework")) {
110            return false;
111        }
112
113        // Create the new XoopsGeshi object, passing relevant stuff
114        // XoopsGeshi should be extending geSHi in Frameworks/geshi/xoopsgeshi.php
115        $geshi = new XoopsGeshi($source, $language);
116
117        // Enclose the code in a <div>
118        $geshi->set_header_type(GESHI_HEADER_NONE);
119
120        // Sets the proper encoding charset other than "ISO-8859-1"
121        $geshi->set_encoding(_CHARSET);
122
123        $geshi->set_link_target("_blank");
124
125        // Parse the code
126        $code = $geshi->parse_code();
127
128        return $code;
129    }
130}