CRM-15856 - Export list of required tokens to UI
[civicrm-core.git] / CRM / Utils / REST.php
index 09abcf5e3baf8804d89bd879e146903abede1be3..670de8012e7b13e1dcbb76caf2d4ca8947160e97 100644 (file)
@@ -36,7 +36,6 @@ class CRM_Utils_REST {
 
   /**
    * Number of seconds we should let a REST process idle
-   * @static
    */
   static $rest_timeout = 0;
 
@@ -64,7 +63,7 @@ class CRM_Utils_REST {
    * @param string $var
    *   The string to be echoed.
    *
-   * @return string       $var
+   * @return string
    */
   public static function ping($var = NULL) {
     $session = CRM_Core_Session::singleton();
@@ -73,8 +72,8 @@ class CRM_Utils_REST {
     return self::simple(array('message' => "PONG: $key"));
   }
 
-  // Generates values needed for error messages
   /**
+   * Generates values needed for error messages.
    * @param string $message
    *
    * @return array
@@ -87,8 +86,8 @@ class CRM_Utils_REST {
     return $values;
   }
 
-  // Generates values needed for non-error responses.
   /**
+   * Generates values needed for non-error responses.
    * @param array $params
    *
    * @return array
@@ -185,10 +184,10 @@ class CRM_Utils_REST {
    * @return string
    */
   public static function jsonFormated($json) {
-    $tabcount   = 0;
-    $result     = '';
-    $inquote    = FALSE;
-    $inarray    = FALSE;
+    $tabcount = 0;
+    $result = '';
+    $inquote = FALSE;
+    $inarray = FALSE;
     $ignorenext = FALSE;
 
     $tab = "\t";
@@ -278,7 +277,7 @@ class CRM_Utils_REST {
       $q = $r;
     }
     $entity = CRM_Utils_array::value('entity', $requestParams);
-    if ( empty($entity) && !empty($q)) {
+    if (empty($entity) && !empty($q)) {
       $args = explode('/', $q);
       // If the function isn't in the civicrm namespace, reject the request.
       if ($args[0] != 'civicrm') {
@@ -377,7 +376,7 @@ class CRM_Utils_REST {
       return $result;
     }
 
-    if ($_SERVER['REQUEST_METHOD'] == 'GET' && strtolower(substr( $args[2], 0, 3)) != 'get') {
+    if ($_SERVER['REQUEST_METHOD'] == 'GET' && strtolower(substr($args[2], 0, 3)) != 'get') {
       // get only valid for non destructive methods
       require_once 'api/v3/utils.php';
       return civicrm_api3_create_error("SECURITY: All requests that modify the database must be http POST, not GET.",
@@ -417,7 +416,7 @@ class CRM_Utils_REST {
       'action' => 1,
     );
 
-    if (array_key_exists('json', $requestParams) &&  $requestParams['json'][0] == "{") {
+    if (array_key_exists('json', $requestParams) && $requestParams['json'][0] == "{") {
       $params = json_decode($requestParams['json'], TRUE);
       if ($params === NULL) {
         CRM_Utils_JSON::output(array('is_error' => 1, 'error_message', 'Unable to decode supplied JSON.'));
@@ -456,34 +455,23 @@ class CRM_Utils_REST {
     CRM_Utils_System::civiExit();
   }
 
-  public static function APIDoc() {
-
-    CRM_Utils_System::setTitle("API Parameters");
-    $template = CRM_Core_Smarty::singleton();
-    return CRM_Utils_System::theme(
-      $template->fetch('CRM/Core/APIDoc.tpl')
-    );
-  }
-
-  public static function ajaxDoc() {
-    return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/api/explorer'));
-  }
-
-  /** used to load a template "inline", eg. for ajax, without having to build a menu for each template */
-  static  function loadTemplate () {
-    $request = CRM_Utils_Request::retrieve( 'q', 'String');
+  /**
+   * used to load a template "inline", eg. for ajax, without having to build a menu for each template
+   */
+  public static function loadTemplate() {
+    $request = CRM_Utils_Request::retrieve('q', 'String');
     if (FALSE !== strpos($request, '..')) {
       die ("SECURITY FATAL: the url can't contain '..'. Please report the issue on the forum at civicrm.org");
     }
 
-    $request = split('/', $request);
+    $request = explode('/', $request);
     $entity = _civicrm_api_get_camel_name($request[2]);
     $tplfile = _civicrm_api_get_camel_name($request[3]);
 
-    $tpl = 'CRM/'.$entity.'/Page/Inline/'.$tplfile.'.tpl';
-    $smarty = CRM_Core_Smarty::singleton( );
-    CRM_Utils_System::setTitle( "$entity::$tplfile inline $tpl" );
-    if (!$smarty->template_exists($tpl) ) {
+    $tpl = 'CRM/' . $entity . '/Page/Inline/' . $tplfile . '.tpl';
+    $smarty = CRM_Core_Smarty::singleton();
+    CRM_Utils_System::setTitle("$entity::$tplfile inline $tpl");
+    if (!$smarty->template_exists($tpl)) {
       header("Status: 404 Not Found");
       die ("Can't find the requested template file templates/$tpl");
     }
@@ -495,37 +483,39 @@ class CRM_Utils_REST {
     if ($pos !== FALSE) {
       die ("SECURITY FATAL: one of the param names contains <");
     }
-    $param = array_map( 'htmlentities', $_GET);
+    $param = array_map('htmlentities', $_GET);
     unset($param['q']);
     $smarty->assign_by_ref("request", $param);
 
-    if  ( ! array_key_exists( 'HTTP_X_REQUESTED_WITH', $_SERVER ) ||
-      $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest"  )  {
+    if (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) ||
+      $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest"
+    ) {
 
-      $smarty->assign( 'tplFile', $tpl );
+      $smarty->assign('tplFile', $tpl);
       $config = CRM_Core_Config::singleton();
-      $content = $smarty->fetch( 'CRM/common/'. strtolower($config->userFramework) .'.tpl' );
+      $content = $smarty->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
 
       if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
         CRM_Utils_System::addHTMLHead($region->render(''));
       }
-      CRM_Utils_System::appendTPLFile( $tpl, $content );
+      CRM_Utils_System::appendTPLFile($tpl, $content);
 
       return CRM_Utils_System::theme($content);
 
     }
     else {
       $content = "<!-- .tpl file embeded: $tpl -->\n";
-      CRM_Utils_System::appendTPLFile( $tpl, $content );
+      CRM_Utils_System::appendTPLFile($tpl, $content);
       echo $content . $smarty->fetch($tpl);
-      CRM_Utils_System::civiExit( );
+      CRM_Utils_System::civiExit();
     }
   }
 
-  /** This is a wrapper so you can call an api via json (it returns json too)
+  /**
+   * This is a wrapper so you can call an api via json (it returns json too)
    * http://example.org/civicrm/api/json?entity=Contact&action=Get"&json={"contact_type":"Individual","email.get.email":{}} to take all the emails from individuals
    * works for POST & GET (POST recommended)
-   **/
+   */
   public static function ajaxJson() {
     $requestParams = CRM_Utils_Request::exportValues();
 
@@ -533,7 +523,8 @@ class CRM_Utils_REST {
     // Why is $config undefined -- $config = CRM_Core_Config::singleton();
     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.api3().",
         array(
           'IP' => $_SERVER['REMOTE_ADDR'],
@@ -556,7 +547,10 @@ 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)) {
-      CRM_Utils_JSON::output(array('is_error' => 1, 'error_message', 'invalid json format: ?{"param_with_double_quote":"value"}'));
+      CRM_Utils_JSON::output(array(
+          'is_error' => 1,
+          'error_message' => 'invalid json format: ?{"param_with_double_quote":"value"}',
+        ));
     }
 
     $params['check_permissions'] = TRUE;
@@ -576,6 +570,11 @@ class CRM_Utils_REST {
     CRM_Utils_System::civiExit();
   }
 
+  /**
+   * Run ajax request.
+   *
+   * @return array
+   */
   public static function ajax() {
     $requestParams = CRM_Utils_Request::exportValues();
 
@@ -654,7 +653,8 @@ class CRM_Utils_REST {
   }
 
   /**
-   * @return array|NULL NULL if execution should proceed; array if the response is already known
+   * @return array|NULL
+   *   NULL if execution should proceed; array if the response is already known
    */
   public function loadCMSBootstrap() {
     $requestParams = CRM_Utils_Request::exportValues();
@@ -685,8 +685,8 @@ class CRM_Utils_REST {
 
     $uid = NULL;
     if (!$uid) {
-      $store      = NULL;
-      $api_key    = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
+      $store = NULL;
+      $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
       if (empty($api_key)) {
         return self::error("FATAL: mandatory param 'api_key' (user key) missing");
       }
@@ -704,4 +704,5 @@ class CRM_Utils_REST {
       return self::error('ERROR: No CMS user associated with given api-key');
     }
   }
+
 }