XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
syntaxhighlight.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
23 {
24  function load(&$ts, $source, $language)
25  {
26  $config = parent::loadConfig(dirname(__FILE__));
27  if (empty($config['highlight'])) {
28  return "<pre>{$source}</pre>";
29  }
30  $source = $ts->undoHtmlSpecialChars($source);
31  $source = stripslashes($source);
32  if ($config['highlight'] == 'geshi') {
33  $language = str_replace('=', '', $language);
34  $language = ($language) ? $language : $config['language'];
35  $language = strtolower($language);
36  if ($source2 = MytsSyntaxhighlight::geshi($source, $language)) {
37  return $source2;
38  }
39  }
40  $source = MytsSyntaxhighlight::php($source);
41  return $source;
42  }
43 
44  function php($text)
45  {
46  $text = trim($text);
47  $addedtag_open = 0;
48  if (!strpos($text, "<?php") and (substr($text, 0, 5) != "<?php")) {
49  $text = "<?php " . $text;
50  $addedtag_open = 1;
51  }
52  $addedtag_close = 0;
53  if (!strpos($text, "?>")) {
54  $text .= "?>";
55  $addedtag_close = 1;
56  }
57  $oldlevel = error_reporting(0);
58 
59  //There is a bug in the highlight function(php < 5.3) that it doesn't render
60  //backslashes properly like in \s. So here we replace any backslashes
61  $text = str_replace("\\", "XxxX", $text);
62 
63  $buffer = highlight_string($text, true); // Require PHP 4.20+
64 
65  //Placing backspaces back again
66  $buffer = str_replace("XxxX", "\\", $buffer);
67 
68  error_reporting($oldlevel);
69  $pos_open = $pos_close = 0;
70  if ($addedtag_open) {
71  $pos_open = strpos($buffer, '&lt;?php&nbsp;');
72  }
73  if ($addedtag_close) {
74  $pos_close = strrpos($buffer, '?&gt;');
75  }
76 
77  $str_open = ($addedtag_open) ? substr($buffer, 0, $pos_open) : "";
78  $str_close = ($pos_close) ? substr($buffer, $pos_close + 5) : "";
79 
80  $length_open = ($addedtag_open) ? $pos_open + 14 : 0;
81  $length_text = ($pos_close) ? $pos_close - $length_open : 0;
82  $str_internal = ($length_text) ? substr($buffer, $length_open, $length_text) : substr($buffer, $length_open);
83 
84  $buffer = $str_open . $str_internal . $str_close;
85  return $buffer;
86  }
87 
88  function geshi($source, $language)
89  {
90  if (!@xoops_load("geshi", "framework")) {
91  return false;
92  }
93 
94  // Create the new XoopsGeshi object, passing relevant stuff
95  // XoopsGeshi should be extending geSHi in Frameworks/geshi/xoopsgeshi.php
96  $geshi = new XoopsGeshi($source, $language);
97 
98  // Enclose the code in a <div>
99  $geshi->set_header_type(GESHI_HEADER_NONE);
100 
101  // Sets the proper encoding charset other than "ISO-8859-1"
102  $geshi->set_encoding(_CHARSET);
103 
104  $geshi->set_link_target("_blank");
105 
106  // Parse the code
107  $code = $geshi->parse_code();
108 
109  return $code;
110  }
111 }
112 ?>