[php] MyCode parsing returns empty string in PHP 8.3+ due to HTML validation failure
#1
File: inc/class_parser.php

  The Bug:
  In PHP 8.3, the internal output of the native highlight_string() function was changed. It no
  longer generates the exact same <span style="color: ..."> and <code> HTML structure as older
  PHP versions.

  Because mycode_parse_php() uses hardcoded preg_replace patterns that expect the old PHP 8.2-
  HTML structure, these regex replacements now create broken/unbalanced HTML tags. At the end of
  the parsing process, MyBB's output_allowed() and validate_output() methods detect this invalid
  HTML and completely abort, returning an empty string.

  The Code Causing It (inc/class_parser.php inside mycode_parse_php):

 
1 $code = @highlight_string($str, true);
  2
  3 // These replacements fail to correctly balance tags on PHP 8.3+
  4 $code = preg_replace('#<pre><code style="color: \#000000">#i', "<code>", $code);
  5 $code = preg_replace('#<code>\s*<span style="color: \#000000">\s*#i', "<code>", $code);
  6 $code = preg_replace("#</span>\s*</code>#", "</code>", $code);
  7 $code = preg_replace("#</code>\s*</pre>#", "</code>", $code);
  8 $code = preg_replace("#</span>(\r\n?|\n?)</code>#", "</span></code>", $code);


  The Fix:
  Update the string replacement logic in mycode_parse_php() to account for PHP 8.3+'s modern
  highlight_string() wrapper format.

  Find this block in inc/class_parser.php:


    1        if($added_end_tag)
    2        {
    3            $code = str_replace("?&gt;</span></code>", "</span></code>", $code);
    4            // Wait a minute. It fails highlighting? Stupid highlighter.
    5            $code = str_replace("?&gt;</code>", "</code>", $code);
    6        }
    7
    8        $code = preg_replace("#<span style=\"color: \#([A-Z0-9]{6})\"></span>#", "",
      $code);
    9        $code = str_replace("<code>", "<div dir=\"ltr\"><code>", $code);
  10        $code = str_replace("</code>", "</code></div>", $code);


  Replace with:

    1        if($added_end_tag)
    2        {
    3            $code = str_replace("?&gt;</span></code>", "</span></code>", $code);
    4            $code = str_replace("?&gt;</code>", "</code>", $code);
    5            $code = str_replace("?&gt;</span></pre>", "</span></pre>", $code);
    6        }
    7
    8        $code = preg_replace("#<span style=\"color: \#([A-Z0-9]{6})\"></span>#", "",
      $code);
    9
  10        // PHP 8.3+ compatibility: remove stray wrapping <pre> tags leftover from new
      highlight_string format
  11        $code = str_replace("<pre>", "", $code);
  12        $code = str_replace("</pre>", "", $code);
  13
  14        $code = str_replace("<code>", "<div dir=\"ltr\"><code>", $code);
  15        $code = str_replace("</code>", "</code></div>", $code);
Reply
#2
Related: https://github.com/mybb/mybb/issues/5005
devilshakerz.com/pgp (DF3A 34D9 A627 42E5 BC6A 6750 1F2F B8AA 28FF E1BC) ▪ keybase.io/devilshakerz
Reply
#3
https://github.com/mybb/mybb/pull/5384
[MyBB 1.8 Czech translation] [MyBB 1.8 plugins]: Prune old PMs + optimize DB plugin --- Thank you/like system
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)