XOOPS  2.6.0
syntaxhighlight.php
Go to the documentation of this 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 
25 {
32  public function load(MyTextSanitizer &$ts, $source, $language)
33  {
34  $config = parent::loadConfig(__DIR__);
35  if (empty($config['highlight'])) {
36  return "<pre>{$source}</pre>";
37  }
38  $source = $ts->undoHtmlSpecialChars($source);
39  $source = stripslashes($source);
40  if ($config['highlight'] == 'geshi') {
41  $language = str_replace('=', '', $language);
42  $language = ($language) ? $language : $config['language'];
43  $language = strtolower($language);
44  if ($source2 = MytsSyntaxhighlight::geshi($source, $language)) {
45  return $source2;
46  }
47  }
48  $source = MytsSyntaxhighlight::php($source);
49  return $source;
50  }
51 
56  public function php($text)
57  {
58  $text = trim($text);
59  $addedtag_open = 0;
60  if (!strpos($text, "<?php") and (substr($text, 0, 5) != "<?php")) {
61  $text = "<?php " . $text;
62  $addedtag_open = 1;
63  }
64  $addedtag_close = 0;
65  if (!strpos($text, "?>")) {
66  $text .= "?>";
67  $addedtag_close = 1;
68  }
69  $oldlevel = error_reporting(0);
70 
71  //There is a bug in the highlight function(php < 5.3) that it doesn't render
72  //backslashes properly like in \s. So here we replace any backslashes
73  $text = str_replace("\\", "XxxX", $text);
74 
75  $buffer = highlight_string($text, true); // Require PHP 4.20+
76 
77  //Placing backspaces back again
78  $buffer = str_replace("XxxX", "\\", $buffer);
79 
80  error_reporting($oldlevel);
81  $pos_open = $pos_close = 0;
82  if ($addedtag_open) {
83  $pos_open = strpos($buffer, '&lt;?php&nbsp;');
84  }
85  if ($addedtag_close) {
86  $pos_close = strrpos($buffer, '?&gt;');
87  }
88 
89  $str_open = ($addedtag_open) ? substr($buffer, 0, $pos_open) : "";
90  $str_close = ($pos_close) ? substr($buffer, $pos_close + 5) : "";
91 
92  $length_open = ($addedtag_open) ? $pos_open + 14 : 0;
93  $length_text = ($pos_close) ? $pos_close - $length_open : 0;
94  $str_internal = ($length_text) ? substr($buffer, $length_open, $length_text) : substr($buffer, $length_open);
95 
96  $buffer = $str_open . $str_internal . $str_close;
97  return $buffer;
98  }
99 
105  public function geshi($source, $language)
106  {
107  if (!@XoopsLoad::load("geshi", "framework")) {
108  return false;
109  }
110 
111  // Create the new XoopsGeshi object, passing relevant stuff
112  // XoopsGeshi should be extending geSHi in Frameworks/geshi/xoopsgeshi.php
113  $geshi = new XoopsGeshi($source, $language);
114 
115  // Enclose the code in a <div>
116  $geshi->set_header_type(GESHI_HEADER_NONE);
117 
118  // Sets the proper encoding charset other than "ISO-8859-1"
119  $geshi->set_encoding(XoopsLocale::getCharset());
120 
121  $geshi->set_link_target("_blank");
122 
123  // Parse the code
124  $code = $geshi->parse_code();
125 
126  return $code;
127  }
128 }
$text
Definition: qrrender.php:27
load(MyTextSanitizer &$ts, $source, $language)
static load($name, $type="core")
Definition: xoopsload.php:65
geshi($source, $language)
$language
$code
Definition: lostpass.php:48