json-encode.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Provides a helper to properly encode HTML-safe JSON prior to PHP 5.3.0.
  5. */
  6. /**
  7. * Encodes a PHP variable to HTML-safe JSON for PHP versions below 5.3.0.
  8. *
  9. * @see drupal_json_encode()
  10. */
  11. function drupal_json_encode_helper($var) {
  12. switch (gettype($var)) {
  13. case 'boolean':
  14. return $var ? 'true' : 'false'; // Lowercase necessary!
  15. case 'integer':
  16. case 'double':
  17. return $var;
  18. case 'resource':
  19. case 'string':
  20. // Always use Unicode escape sequences (\u0022) over JSON escape
  21. // sequences (\") to prevent browsers interpreting these as
  22. // special characters.
  23. $replace_pairs = array(
  24. // ", \ and U+0000 - U+001F must be escaped according to RFC 4627.
  25. '\\' => '\u005C',
  26. '"' => '\u0022',
  27. "\x00" => '\u0000',
  28. "\x01" => '\u0001',
  29. "\x02" => '\u0002',
  30. "\x03" => '\u0003',
  31. "\x04" => '\u0004',
  32. "\x05" => '\u0005',
  33. "\x06" => '\u0006',
  34. "\x07" => '\u0007',
  35. "\x08" => '\u0008',
  36. "\x09" => '\u0009',
  37. "\x0a" => '\u000A',
  38. "\x0b" => '\u000B',
  39. "\x0c" => '\u000C',
  40. "\x0d" => '\u000D',
  41. "\x0e" => '\u000E',
  42. "\x0f" => '\u000F',
  43. "\x10" => '\u0010',
  44. "\x11" => '\u0011',
  45. "\x12" => '\u0012',
  46. "\x13" => '\u0013',
  47. "\x14" => '\u0014',
  48. "\x15" => '\u0015',
  49. "\x16" => '\u0016',
  50. "\x17" => '\u0017',
  51. "\x18" => '\u0018',
  52. "\x19" => '\u0019',
  53. "\x1a" => '\u001A',
  54. "\x1b" => '\u001B',
  55. "\x1c" => '\u001C',
  56. "\x1d" => '\u001D',
  57. "\x1e" => '\u001E',
  58. "\x1f" => '\u001F',
  59. // Prevent browsers from interpreting these as as special.
  60. "'" => '\u0027',
  61. '<' => '\u003C',
  62. '>' => '\u003E',
  63. '&' => '\u0026',
  64. // Prevent browsers from interpreting the solidus as special and
  65. // non-compliant JSON parsers from interpreting // as a comment.
  66. '/' => '\u002F',
  67. // While these are allowed unescaped according to ECMA-262, section
  68. // 15.12.2, they cause problems in some JSON parsers.
  69. "\xe2\x80\xa8" => '\u2028', // U+2028, Line Separator.
  70. "\xe2\x80\xa9" => '\u2029', // U+2029, Paragraph Separator.
  71. );
  72. return '"' . strtr($var, $replace_pairs) . '"';
  73. case 'array':
  74. // Arrays in JSON can't be associative. If the array is empty or if it
  75. // has sequential whole number keys starting with 0, it's not associative
  76. // so we can go ahead and convert it as an array.
  77. if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
  78. $output = array();
  79. foreach ($var as $v) {
  80. $output[] = drupal_json_encode_helper($v);
  81. }
  82. return '[ ' . implode(', ', $output) . ' ]';
  83. }
  84. // Otherwise, fall through to convert the array as an object.
  85. case 'object':
  86. $output = array();
  87. foreach ($var as $k => $v) {
  88. $output[] = drupal_json_encode_helper(strval($k)) . ':' . drupal_json_encode_helper($v);
  89. }
  90. return '{' . implode(', ', $output) . '}';
  91. default:
  92. return 'null';
  93. }
  94. }