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