diff --git a/src/applications/conduit/controller/PhabricatorConduitAPIController.php b/src/applications/conduit/controller/PhabricatorConduitAPIController.php --- a/src/applications/conduit/controller/PhabricatorConduitAPIController.php +++ b/src/applications/conduit/controller/PhabricatorConduitAPIController.php @@ -642,12 +642,15 @@ // entire param dictionary JSON encoded. $params_json = $request->getStr('params'); if (strlen($params_json)) { - $params = json_decode($params_json, true); - if (!is_array($params)) { - throw new Exception( - "Invalid parameter information was passed to method ". - "'{$method}', could not decode JSON serialization. Data: ". - $params_json); + $params = null; + try { + $params = phutil_json_decode($params_json); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht( + "Invalid parameter information was passed to method '%s'", + $method), + $ex); } $metadata = idx($params, '__conduit__', array()); diff --git a/src/applications/conduit/ssh/ConduitSSHWorkflow.php b/src/applications/conduit/ssh/ConduitSSHWorkflow.php --- a/src/applications/conduit/ssh/ConduitSSHWorkflow.php +++ b/src/applications/conduit/ssh/ConduitSSHWorkflow.php @@ -26,9 +26,11 @@ $method = head($methodv); $json = $this->readAllInput(); - $raw_params = json_decode($json, true); - if (!is_array($raw_params)) { - throw new Exception('Invalid JSON input.'); + $raw_params = null; + try { + $raw_params = phutil_json_decode($json); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException(pht('Invalid JSON input.'), $ex); } $params = idx($raw_params, 'params', '[]'); diff --git a/src/applications/console/controller/DarkConsoleDataController.php b/src/applications/console/controller/DarkConsoleDataController.php --- a/src/applications/console/controller/DarkConsoleDataController.php +++ b/src/applications/console/controller/DarkConsoleDataController.php @@ -33,9 +33,9 @@ return new Aphront400Response(); } - $result = json_decode($result, true); - - if (!is_array($result)) { + try { + $result = phutil_json_decode($result); + } catch (PhutilJSONParserException $ex) { return new Aphront400Response(); } diff --git a/src/applications/differential/__tests__/DifferentialParseRenderTestCase.php b/src/applications/differential/__tests__/DifferentialParseRenderTestCase.php --- a/src/applications/differential/__tests__/DifferentialParseRenderTestCase.php +++ b/src/applications/differential/__tests__/DifferentialParseRenderTestCase.php @@ -17,9 +17,12 @@ $opt_file = $dir.$file.'.options'; if (Filesystem::pathExists($opt_file)) { $options = Filesystem::readFile($opt_file); - $options = json_decode($options, true); - if (!is_array($options)) { - throw new Exception("Invalid options file: {$opt_file}."); + try { + $options = phutil_json_decode($options); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('Invalid options file: %s.', $opt_file), + $ex); } } else { $options = array(); diff --git a/src/applications/diffusion/DiffusionLintSaveRunner.php b/src/applications/diffusion/DiffusionLintSaveRunner.php --- a/src/applications/diffusion/DiffusionLintSaveRunner.php +++ b/src/applications/diffusion/DiffusionLintSaveRunner.php @@ -154,9 +154,11 @@ $files); foreach (new LinesOfALargeExecFuture($future) as $json) { - $paths = json_decode($json, true); - if (!is_array($paths)) { - fprintf(STDERR, "Invalid JSON: {$json}\n"); + $paths = null; + try { + $paths = phutil_json_decode($json); + } catch (PhutilJSONParserException $ex) { + fprintf(STDERR, pht("Invalid JSON: %s\n", $json)); continue; } diff --git a/src/applications/help/controller/PhabricatorHelpKeyboardShortcutController.php b/src/applications/help/controller/PhabricatorHelpKeyboardShortcutController.php --- a/src/applications/help/controller/PhabricatorHelpKeyboardShortcutController.php +++ b/src/applications/help/controller/PhabricatorHelpKeyboardShortcutController.php @@ -12,8 +12,9 @@ $user = $request->getUser(); $keys = $request->getStr('keys'); - $keys = json_decode($keys, true); - if (!is_array($keys)) { + try { + $keys = phutil_json_decode($keys); + } catch (PhutilJSONParserException $ex) { return new Aphront400Response(); } diff --git a/src/applications/herald/adapter/HeraldAdapter.php b/src/applications/herald/adapter/HeraldAdapter.php --- a/src/applications/herald/adapter/HeraldAdapter.php +++ b/src/applications/herald/adapter/HeraldAdapter.php @@ -633,14 +633,16 @@ // dictionary. The first regexp must match the dictionary key, and the // second regexp must match the dictionary value. If any key/value pair // in the dictionary matches both regexps, the condition is satisfied. - $regexp_pair = json_decode($condition_value, true); - if (!is_array($regexp_pair)) { + $regexp_pair = null; + try { + $regexp_pair = phutil_json_decode($condition_value); + } catch (PhutilJSONParserException $ex) { throw new HeraldInvalidConditionException( - 'Regular expression pair is not valid JSON!'); + pht('Regular expression pair is not valid JSON!')); } if (count($regexp_pair) != 2) { throw new HeraldInvalidConditionException( - 'Regular expression pair is not a pair!'); + pht('Regular expression pair is not a pair!')); } $key_regexp = array_shift($regexp_pair); @@ -705,8 +707,10 @@ } break; case self::CONDITION_REGEXP_PAIR: - $json = json_decode($condition_value, true); - if (!is_array($json)) { + $json = null; + try { + $json = phutil_json_decode($condition_value); + } catch (PhutilJSONParserException $ex) { throw new HeraldInvalidConditionException( pht( 'The regular expression pair "%s" is not valid JSON. Enter a '. diff --git a/src/applications/metamta/adapter/PhabricatorMailImplementationMailgunAdapter.php b/src/applications/metamta/adapter/PhabricatorMailImplementationMailgunAdapter.php --- a/src/applications/metamta/adapter/PhabricatorMailImplementationMailgunAdapter.php +++ b/src/applications/metamta/adapter/PhabricatorMailImplementationMailgunAdapter.php @@ -120,9 +120,13 @@ list($body) = $future->resolvex(); - $response = json_decode($body, true); - if (!is_array($response)) { - throw new Exception("Failed to JSON decode response: {$body}"); + $response = null; + try { + $response = phutil_json_decode($body); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('Failed to JSON decode response.'), + $ex); } if (!idx($response, 'id')) { diff --git a/src/applications/phame/skins/PhameSkinSpecification.php b/src/applications/phame/skins/PhameSkinSpecification.php --- a/src/applications/phame/skins/PhameSkinSpecification.php +++ b/src/applications/phame/skins/PhameSkinSpecification.php @@ -93,15 +93,18 @@ } private static function loadSkinSpecification($path) { - $config_path = $path.DIRECTORY_SEPARATOR.'skin.json'; $config = array(); if (Filesystem::pathExists($config_path)) { $config = Filesystem::readFile($config_path); - $config = json_decode($config, true); - if (!is_array($config)) { - throw new Exception( - "Skin configuration file '{$config_path}' is not a valid JSON file."); + try { + $config = phutil_json_decode($config); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht( + "Skin configuration file '%s' is not a valid JSON file.", + $config_path), + $ex); } $type = idx($config, 'type', self::TYPE_BASIC); } else { diff --git a/src/applications/phortune/controller/PhortunePaymentMethodCreateController.php b/src/applications/phortune/controller/PhortunePaymentMethodCreateController.php --- a/src/applications/phortune/controller/PhortunePaymentMethodCreateController.php +++ b/src/applications/phortune/controller/PhortunePaymentMethodCreateController.php @@ -215,8 +215,10 @@ $errors = array(); - $client_errors = json_decode($client_errors_raw, true); - if (!is_array($client_errors)) { + $client_errors = null; + try { + $client_errors = phutil_json_decode($client_errors_raw); + } catch (PhutilJSONParserException $ex) { $errors[] = pht( 'There was an error decoding error information submitted by the '. 'client. Expected a JSON-encoded list of error codes, received: %s.', diff --git a/src/applications/policy/controller/PhabricatorPolicyEditController.php b/src/applications/policy/controller/PhabricatorPolicyEditController.php --- a/src/applications/policy/controller/PhabricatorPolicyEditController.php +++ b/src/applications/policy/controller/PhabricatorPolicyEditController.php @@ -52,9 +52,12 @@ $errors = array(); if ($request->isFormPost()) { $data = $request->getStr('rules'); - $data = @json_decode($data, true); - if (!is_array($data)) { - throw new Exception('Failed to JSON decode rule data!'); + try { + $data = phutil_json_decode($data); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('Failed to JSON decode rule data!'), + $ex); } $rule_data = array(); diff --git a/src/applications/search/engine/PhabricatorSearchEngineElastic.php b/src/applications/search/engine/PhabricatorSearchEngineElastic.php --- a/src/applications/search/engine/PhabricatorSearchEngineElastic.php +++ b/src/applications/search/engine/PhabricatorSearchEngineElastic.php @@ -397,12 +397,13 @@ return null; } - $body = json_decode($body, true); - if (!is_array($body)) { - throw new Exception('elasticsearch server returned invalid JSON!'); + try { + return phutil_json_decode($body); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('ElasticSearch server returned invalid JSON!'), + $ex); } - - return $body; } } diff --git a/src/applications/xhprof/controller/PhabricatorXHProfProfileController.php b/src/applications/xhprof/controller/PhabricatorXHProfProfileController.php --- a/src/applications/xhprof/controller/PhabricatorXHProfProfileController.php +++ b/src/applications/xhprof/controller/PhabricatorXHProfProfileController.php @@ -21,9 +21,12 @@ } $data = $file->loadFileData(); - $data = @json_decode($data, true); - if (!$data) { - throw new Exception('Failed to unserialize XHProf profile!'); + try { + $data = phutil_json_decode($data); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('Failed to unserialize XHProf profile!'), + $ex); } $symbol = $request->getStr('symbol'); diff --git a/src/infrastructure/daemon/bot/PhabricatorBot.php b/src/infrastructure/daemon/bot/PhabricatorBot.php --- a/src/infrastructure/daemon/bot/PhabricatorBot.php +++ b/src/infrastructure/daemon/bot/PhabricatorBot.php @@ -23,9 +23,12 @@ } $json_raw = Filesystem::readFile($argv[0]); - $config = json_decode($json_raw, true); - if (!is_array($config)) { - throw new Exception("File '{$argv[0]}' is not valid JSON!"); + try { + $config = phutil_json_decode($json_raw); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht("File '%s' is not valid JSON!", $argv[0]), + $ex); } $nick = idx($config, 'nick', 'phabot');