XH_JSON::encode() errors on ASCII control characters
Posted: Wed Nov 26, 2014 12:07 am
Hello Community,
thanks to marcus a bug has been found in XH_JSON::encode(), where ASCII control characters have been escaped to \u00, which is obviously invalid. In the case of Pagemanager the language strings are converted to JSON, what results in a script error when the fallback to XH_JSON::encode() is required (usually for PHP < 5.2), so the tree view won't get populated. This bug had already been reported by Archie900 a while ago, but due to lack of feedback it has not been resolved.
I suggest the following patch:
A respective unit test should be added as well.
Christoph
PS: See http://cmsimpleforum.com/viewtopic.php?f=10&t=8257 regarding a follow-up bug and the fix.
thanks to marcus a bug has been found in XH_JSON::encode(), where ASCII control characters have been escaped to \u00, which is obviously invalid. In the case of Pagemanager the language strings are converted to JSON, what results in a script error when the fallback to XH_JSON::encode() is required (usually for PHP < 5.2), so the tree view won't get populated. This bug had already been reported by Archie900 a while ago, but due to lack of feedback it has not been resolved.
I suggest the following patch:
Code: Select all
Index: JSON.php
===================================================================
--- JSON.php (revision 3)
+++ JSON.php (working copy)
@@ -197,11 +197,27 @@
function quote($string)
{
$string = addcslashes($string, "\"\\/");
- $string = preg_replace('/[\x00-\x1f]/s', '\u00$1', $string);
+ $string = preg_replace_callback(
+ '/[\x00-\x1f]/', array($this, 'escapeControlChar'), $string
+ );
return $string;
}
/**
+ * Escapes an ASCII control character for use in a JSON string.
+ *
+ * @param string $matches An array of matches with a single element.
+ *
+ * @return string
+ *
+ * @access protected
+ */
+ function escapeControlChar($matches)
+ {
+ return sprintf('\\u%04X', ord($matches[0]));
+ }
+
+ /**
* Returns UTF-8 chars for JSON unicode escape sequence.
*
* @param array $matches Matches from the previous preg_match().
Christoph
PS: See http://cmsimpleforum.com/viewtopic.php?f=10&t=8257 regarding a follow-up bug and the fix.