X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FREST.php;h=010cfe8156b0c69759895083d89c3de1af7a50be;hb=6a2fe65b124c9977dff8e006280624ad599a59e0;hp=2111f6356555bb736bee45a6fe7b97a2826d1b1f;hpb=7dee6a13d167a1dd2b4bf1075252bcb7e324c7e5;p=civicrm-core.git diff --git a/CRM/Utils/REST.php b/CRM/Utils/REST.php index 2111f63565..010cfe8156 100644 --- a/CRM/Utils/REST.php +++ b/CRM/Utils/REST.php @@ -49,7 +49,7 @@ class CRM_Utils_REST { * Class constructor. This caches the real user framework class locally, * so we can use it for authentication and validation. * - * @param string $uf The userframework class + * @internal param string $uf The userframework class */ public function __construct() { // any external program which call Rest Server is responsible for @@ -74,6 +74,11 @@ class CRM_Utils_REST { } // Generates values needed for error messages + /** + * @param string $message + * + * @return array + */ static function error($message = 'Unknown Error') { $values = array( 'error_message' => $message, @@ -83,17 +88,28 @@ class CRM_Utils_REST { } // Generates values needed for non-error responses. + /** + * @param $params + * + * @return array + */ static function simple($params) { $values = array('is_error' => 0); $values += $params; return $values; } + /** + * @return string + */ function run() { $result = self::handle(); return self::output($result); } + /** + * @return string + */ function bootAndRun() { $response = $this->loadCMSBootstrap(); if (is_array($response)) { @@ -102,6 +118,11 @@ class CRM_Utils_REST { return $this->run(); } + /** + * @param $result + * + * @return string + */ static function output(&$result) { $requestParams = CRM_Utils_Request::exportValues(); @@ -127,7 +148,7 @@ class CRM_Utils_REST { if (CRM_Utils_Array::value('json', $requestParams)) { header('Content-Type: text/javascript'); $json = json_encode(array_merge($result)); - if (CRM_Utils_Array::value('debug', $requestParams)) { + if (CRM_Utils_Array::value('prettyprint', $requestParams)) { return self::jsonFormated($json); } return $json; @@ -160,6 +181,11 @@ class CRM_Utils_REST { return $xml; } + /** + * @param $json + * + * @return string + */ static function jsonFormated($json) { $tabcount = 0; $result = ''; @@ -238,6 +264,9 @@ class CRM_Utils_REST { return $result; } + /** + * @return array|int + */ static function handle() { $requestParams = CRM_Utils_Request::exportValues(); @@ -248,7 +277,8 @@ class CRM_Utils_REST { if (!empty($r)) { $q = $r; } - if (!empty($q)) { + $entity = CRM_Utils_array::value('entity', $requestParams); + if ( empty($entity) && !empty($q)) { $args = explode('/', $q); // If the function isn't in the civicrm namespace, reject the request. if ($args[0] != 'civicrm') { @@ -308,6 +338,12 @@ class CRM_Utils_REST { return self::process($args, self::buildParamList()); } + /** + * @param $args + * @param $params + * + * @return array|int + */ static function process(&$args, $params) { $params['check_permissions'] = TRUE; $fnName = $apiFile = NULL; @@ -366,6 +402,9 @@ class CRM_Utils_REST { return $result; } + /** + * @return array|mixed|null + */ static function &buildParamList() { $requestParams = CRM_Utils_Request::exportValues(); $params = array(); @@ -382,8 +421,7 @@ class CRM_Utils_REST { if (array_key_exists('json', $requestParams) && $requestParams['json'][0] == "{") { $params = json_decode($requestParams['json'], TRUE); if($params === NULL) { - echo json_encode(array('is_error' => 1, 'error_message', 'Unable to decode supplied JSON.')); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output(array('is_error' => 1, 'error_message', 'Unable to decode supplied JSON.')); } } foreach ($requestParams as $n => $v) { @@ -397,6 +435,9 @@ class CRM_Utils_REST { return $params; } + /** + * @param $pearError + */ static function fatal($pearError) { header('Content-Type: text/xml'); $error = array(); @@ -491,7 +532,7 @@ class CRM_Utils_REST { if (!$config->debug && (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" )) { - $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api().", + $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3().", array( 'IP' => $_SERVER['REMOTE_ADDR'], 'level' => 'security', @@ -499,16 +540,13 @@ class CRM_Utils_REST { 'reason' => 'CSRF suspected', ) ); - echo json_encode($error); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output($error); } if (empty($requestParams['entity'])) { - echo json_encode(civicrm_api3_create_error('missing entity param')); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output(civicrm_api3_create_error('missing entity param')); } if (empty($requestParams['entity'])) { - echo json_encode(civicrm_api3_create_error('missing entity entity')); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output(civicrm_api3_create_error('missing entity entity')); } if (!empty($requestParams['json'])) { $params = json_decode($requestParams['json'], TRUE); @@ -516,8 +554,7 @@ class CRM_Utils_REST { $entity = CRM_Utils_String::munge(CRM_Utils_Array::value('entity', $requestParams)); $action = CRM_Utils_String::munge(CRM_Utils_Array::value('action', $requestParams)); if (!is_array($params)) { - echo json_encode(array('is_error' => 1, 'error_message', 'invalid json format: ?{"param_with_double_quote":"value"}')); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output(array('is_error' => 1, 'error_message', 'invalid json format: ?{"param_with_double_quote":"value"}')); } $params['check_permissions'] = TRUE; @@ -558,8 +595,7 @@ class CRM_Utils_REST { 'reason' => 'CSRF suspected', ) ); - echo json_encode($error); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output($error); } $q = CRM_Utils_Array::value('fnName', $requestParams);