| 1 | <?php |
| 2 | /** |
| 3 | * Wrapper for json_encode and json_decode. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Json; |
| 24 | |
| 25 | use MediaWiki\Status\Status; |
| 26 | |
| 27 | /** |
| 28 | * JSON formatter wrapper class |
| 29 | */ |
| 30 | class FormatJson { |
| 31 | /** |
| 32 | * Skip escaping most characters above U+007F for readability and compactness. |
| 33 | * This encoding option saves 3 to 8 bytes (uncompressed) for each such character; |
| 34 | * however, it could break compatibility with systems that incorrectly handle UTF-8. |
| 35 | * |
| 36 | * @since 1.22 |
| 37 | */ |
| 38 | public const UTF8_OK = 1; |
| 39 | |
| 40 | /** |
| 41 | * Skip escaping the characters '<', '>', and '&', which have special meanings in |
| 42 | * HTML and XML. |
| 43 | * |
| 44 | * @warning Do not use this option for JSON that could end up in inline scripts. |
| 45 | * - HTML 5.2, §4.12.1.3 Restrictions for contents of script elements |
| 46 | * - XML 1.0 (5th Ed.), §2.4 Character Data and Markup |
| 47 | * |
| 48 | * @since 1.22 |
| 49 | */ |
| 50 | public const XMLMETA_OK = 2; |
| 51 | |
| 52 | /** |
| 53 | * Skip escaping as many characters as reasonably possible. |
| 54 | * |
| 55 | * @warning When generating inline script blocks, use FormatJson::UTF8_OK instead. |
| 56 | * |
| 57 | * @since 1.22 |
| 58 | */ |
| 59 | public const ALL_OK = self::UTF8_OK | self::XMLMETA_OK; |
| 60 | |
| 61 | /** |
| 62 | * If set, treat JSON objects '{...}' as associative arrays. Without this option, |
| 63 | * JSON objects will be converted to stdClass. |
| 64 | * |
| 65 | * @since 1.24 |
| 66 | */ |
| 67 | public const FORCE_ASSOC = 0x100; |
| 68 | |
| 69 | /** |
| 70 | * If set, attempt to fix invalid JSON. |
| 71 | * |
| 72 | * @since 1.24 |
| 73 | */ |
| 74 | public const TRY_FIXING = 0x200; |
| 75 | |
| 76 | /** |
| 77 | * If set, strip comments from input before parsing as JSON. |
| 78 | * |
| 79 | * @since 1.25 |
| 80 | */ |
| 81 | public const STRIP_COMMENTS = 0x400; |
| 82 | |
| 83 | /** |
| 84 | * Returns the JSON representation of a value. |
| 85 | * |
| 86 | * @note Empty arrays are encoded as numeric arrays, not as objects, so cast any associative |
| 87 | * array that might be empty to an object before encoding it. |
| 88 | * |
| 89 | * @note In pre-1.22 versions of MediaWiki, using this function for generating inline script |
| 90 | * blocks may result in an XSS vulnerability, and quite likely will in XML documents |
| 91 | * (cf. FormatJson::XMLMETA_OK). Use Xml::encodeJsVar() instead in such cases. |
| 92 | * |
| 93 | * @param mixed $value The value to encode. Can be any type except a resource. |
| 94 | * @param string|bool $pretty If a string, add non-significant whitespace to improve |
| 95 | * readability, using that string for indentation (must consist only of whitespace |
| 96 | * characters). If true, use the default indent string (four spaces). |
| 97 | * @param int $escaping Bitfield consisting of _OK class constants |
| 98 | * @return string|false String if successful; false upon failure |
| 99 | */ |
| 100 | public static function encode( $value, $pretty = false, $escaping = 0 ) { |
| 101 | // PHP escapes '/' to prevent breaking out of inline script blocks using '</script>', |
| 102 | // which is hardly useful when '<' and '>' are escaped (and inadequate), and such |
| 103 | // escaping negatively impacts the human readability of URLs and similar strings. |
| 104 | $options = JSON_UNESCAPED_SLASHES; |
| 105 | if ( $pretty || is_string( $pretty ) ) { |
| 106 | $options |= JSON_PRETTY_PRINT; |
| 107 | } |
| 108 | if ( $escaping & self::UTF8_OK ) { |
| 109 | $options |= JSON_UNESCAPED_UNICODE; |
| 110 | } |
| 111 | if ( !( $escaping & self::XMLMETA_OK ) ) { |
| 112 | $options |= JSON_HEX_TAG | JSON_HEX_AMP; |
| 113 | } |
| 114 | $json = json_encode( $value, $options ); |
| 115 | |
| 116 | if ( is_string( $pretty ) && $pretty !== ' ' && $json !== false ) { |
| 117 | // Change the four-space indent to the provided indent. |
| 118 | // The regex matches four spaces either at the start of a line or immediately |
| 119 | // after the previous match. $pretty should contain only whitespace characters, |
| 120 | // so there should be no need to call StringUtils::escapeRegexReplacement(). |
| 121 | $json = preg_replace( '/ {4}|.*+\n\K {4}/A', $pretty, $json ); |
| 122 | } |
| 123 | |
| 124 | return $json; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Decodes a JSON string. It is recommended to use FormatJson::parse(), |
| 129 | * which returns more comprehensive result in case of an error, and has |
| 130 | * more parsing options. |
| 131 | * |
| 132 | * In PHP versions before 7.1, decoding a JSON string containing an empty key |
| 133 | * without passing $assoc as true results in a return object with a property |
| 134 | * named "_empty_" (because true empty properties were not supported pre-PHP-7.1). |
| 135 | * Instead, consider passing $assoc as true to return an associative array. |
| 136 | * |
| 137 | * But be aware that in all supported PHP versions, decoding an empty JSON object |
| 138 | * with $assoc = true returns an array, not an object, breaking round-trip consistency. |
| 139 | * |
| 140 | * See https://phabricator.wikimedia.org/T206411 for more details on these quirks. |
| 141 | * |
| 142 | * @param string $value The JSON string being decoded |
| 143 | * @param bool $assoc When true, returned objects will be converted into associative arrays. |
| 144 | * |
| 145 | * @return mixed The value encoded in JSON in appropriate PHP type. |
| 146 | * `null` is returned if $value represented `null`, if $value could not be decoded, |
| 147 | * or if the encoded data was deeper than the recursion limit. |
| 148 | * Use FormatJson::parse() to distinguish between types of `null` and to get proper error code. |
| 149 | */ |
| 150 | public static function decode( $value, $assoc = false ) { |
| 151 | return json_decode( $value, $assoc ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Decodes a JSON string. |
| 156 | * Unlike FormatJson::decode(), if $value represents null value, it will be |
| 157 | * properly decoded as valid. |
| 158 | * |
| 159 | * @param string $value The JSON string being decoded |
| 160 | * @param int $options A bit field that allows FORCE_ASSOC, TRY_FIXING, |
| 161 | * STRIP_COMMENTS |
| 162 | * @return Status If valid JSON, the value is available in $result->getValue() |
| 163 | */ |
| 164 | public static function parse( $value, $options = 0 ) { |
| 165 | if ( $options & self::STRIP_COMMENTS ) { |
| 166 | $value = self::stripComments( $value ); |
| 167 | } |
| 168 | $assoc = ( $options & self::FORCE_ASSOC ) !== 0; |
| 169 | $result = json_decode( $value, $assoc ); |
| 170 | $code = json_last_error(); |
| 171 | |
| 172 | if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) { |
| 173 | // The most common error is the trailing comma in a list or an object. |
| 174 | // We cannot simply replace /,\s*[}\]]/ because it could be inside a string value. |
| 175 | // But we could use the fact that JSON does not allow multi-line string values, |
| 176 | // And remove trailing commas if they are et the end of a line. |
| 177 | // JSON only allows 4 control characters: [ \t\r\n]. So we must not use '\s' for matching. |
| 178 | // Regex match ,]<any non-quote chars>\n or ,\n] with optional spaces/tabs. |
| 179 | $count = 0; |
| 180 | $value = |
| 181 | preg_replace( '/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/', '$1', |
| 182 | $value, -1, $count ); |
| 183 | if ( $count > 0 ) { |
| 184 | $result = json_decode( $value, $assoc ); |
| 185 | if ( json_last_error() === JSON_ERROR_NONE ) { |
| 186 | // Report warning |
| 187 | $st = Status::newGood( $result ); |
| 188 | $st->warning( wfMessage( 'json-warn-trailing-comma' )->numParams( $count ) ); |
| 189 | return $st; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // JSON_ERROR_RECURSION, JSON_ERROR_INF_OR_NAN, JSON_ERROR_UNSUPPORTED_TYPE, |
| 195 | // are all encode errors that we don't need to care about here. |
| 196 | switch ( $code ) { |
| 197 | case JSON_ERROR_NONE: |
| 198 | return Status::newGood( $result ); |
| 199 | default: |
| 200 | return Status::newFatal( wfMessage( 'json-error-unknown' )->numParams( $code ) ); |
| 201 | case JSON_ERROR_DEPTH: |
| 202 | $msg = 'json-error-depth'; |
| 203 | break; |
| 204 | case JSON_ERROR_STATE_MISMATCH: |
| 205 | $msg = 'json-error-state-mismatch'; |
| 206 | break; |
| 207 | case JSON_ERROR_CTRL_CHAR: |
| 208 | $msg = 'json-error-ctrl-char'; |
| 209 | break; |
| 210 | case JSON_ERROR_SYNTAX: |
| 211 | $msg = 'json-error-syntax'; |
| 212 | break; |
| 213 | case JSON_ERROR_UTF8: |
| 214 | $msg = 'json-error-utf8'; |
| 215 | break; |
| 216 | case JSON_ERROR_INVALID_PROPERTY_NAME: |
| 217 | $msg = 'json-error-invalid-property-name'; |
| 218 | break; |
| 219 | case JSON_ERROR_UTF16: |
| 220 | $msg = 'json-error-utf16'; |
| 221 | break; |
| 222 | } |
| 223 | return Status::newFatal( $msg ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Remove multiline and single line comments from an otherwise valid JSON |
| 228 | * input string. This can be used as a preprocessor, to allow JSON |
| 229 | * formatted configuration files to contain comments. |
| 230 | * |
| 231 | * @param string $json |
| 232 | * @return string JSON with comments removed |
| 233 | */ |
| 234 | public static function stripComments( $json ) { |
| 235 | // Ensure we have a string |
| 236 | $str = (string)$json; |
| 237 | $buffer = ''; |
| 238 | $maxLen = strlen( $str ); |
| 239 | $mark = 0; |
| 240 | |
| 241 | $inString = false; |
| 242 | $inComment = false; |
| 243 | $multiline = false; |
| 244 | |
| 245 | for ( $idx = 0; $idx < $maxLen; $idx++ ) { |
| 246 | switch ( $str[$idx] ) { |
| 247 | case '"': |
| 248 | $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] : ''; |
| 249 | if ( !$inComment && $lookBehind !== '\\' ) { |
| 250 | // Either started or ended a string |
| 251 | $inString = !$inString; |
| 252 | } |
| 253 | break; |
| 254 | |
| 255 | case '/': |
| 256 | $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] : ''; |
| 257 | $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] : ''; |
| 258 | if ( $inString ) { |
| 259 | break; |
| 260 | |
| 261 | } elseif ( !$inComment && |
| 262 | ( $lookAhead === '/' || $lookAhead === '*' ) |
| 263 | ) { |
| 264 | // Transition into a comment |
| 265 | // Add characters seen to buffer |
| 266 | $buffer .= substr( $str, $mark, $idx - $mark ); |
| 267 | // Consume the look ahead character |
| 268 | $idx++; |
| 269 | // Track state |
| 270 | $inComment = true; |
| 271 | $multiline = $lookAhead === '*'; |
| 272 | |
| 273 | } elseif ( $multiline && $lookBehind === '*' ) { |
| 274 | // Found the end of the current comment |
| 275 | $mark = $idx + 1; |
| 276 | $inComment = false; |
| 277 | $multiline = false; |
| 278 | } |
| 279 | break; |
| 280 | |
| 281 | case "\n": |
| 282 | if ( $inComment && !$multiline ) { |
| 283 | // Found the end of the current comment |
| 284 | $mark = $idx + 1; |
| 285 | $inComment = false; |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | if ( $inComment ) { |
| 291 | // Comment ends with input |
| 292 | // Technically we should check to ensure that we aren't in |
| 293 | // a multiline comment that hasn't been properly ended, but this |
| 294 | // is a strip filter, not a validating parser. |
| 295 | $mark = $maxLen; |
| 296 | } |
| 297 | // Add final chunk to buffer before returning |
| 298 | return $buffer . substr( $str, $mark, $maxLen - $mark ); |
| 299 | } |
| 300 | } |
| 301 | /** @deprecated class alias since 1.43 */ |
| 302 | class_alias( FormatJson::class, 'FormatJson' ); |