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