Merge pull request #6397 from colemanw/CRM-14078
[civicrm-core.git] / api / v3 / Cxn.php
index 4c0d29df90405c2b7b5dc94e14ebe573fab6a887..cf1a1710f20c7f03557903260d8da970ec3f5643 100644 (file)
 function _civicrm_api3_cxn_register_spec(&$spec) {
   $daoFields = CRM_Cxn_DAO_Cxn::fields();
   $spec['app_guid'] = $daoFields['app_guid'];
-
-  if (!CRM_Cxn_BAO_Cxn::isAppMetaVerified()) {
-    $spec['app_meta_url'] = array(
-      'name' => 'app_meta_url',
-      'type' => CRM_Utils_Type::T_STRING,
-      'title' => ts('Application Metadata URL'),
-      'description' => 'Application Metadata URL',
-      'maxlength' => 255,
-      'size' => CRM_Utils_Type::HUGE,
-    );
-  }
+  $spec['app_meta_url'] = array(
+    'name' => 'app_meta_url',
+    'type' => CRM_Utils_Type::T_STRING,
+    'title' => ts('Application Metadata URL'),
+    'description' => 'Application Metadata URL',
+    'maxlength' => 255,
+    'size' => CRM_Utils_Type::HUGE,
+  );
 }
 
 /**
@@ -72,18 +69,13 @@ function _civicrm_api3_cxn_register_spec(&$spec) {
  */
 function civicrm_api3_cxn_register($params) {
   if (!empty($params['app_meta_url'])) {
-    if (!CRM_Cxn_BAO_Cxn::isAppMetaVerified()) {
-      list ($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
-      if (CRM_Utils_HttpClient::STATUS_OK != $status) {
-        throw new API_Exception("Failed to download appMeta.");
-      }
-      $appMeta = json_decode($json, TRUE);
+    list ($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
+    if (CRM_Utils_HttpClient::STATUS_OK != $status) {
+      throw new API_Exception("Failed to download appMeta. (Bad HTTP response)");
     }
-    else {
-      // Note: The metadata includes a cert, but the details aren't signed.
-      // This is very useful in testing/development. In ordinary usage, we
-      // rely on civicrm.org to sign the metadata for all apps en masse.
-      throw new API_Exception('This site is configured to only connect to applications with verified metadata.');
+    $appMeta = json_decode($json, TRUE);
+    if (empty($appMeta)) {
+      throw new API_Exception("Failed to download appMeta. (Malformed)");
     }
   }
   elseif (!empty($params['app_guid'])) {
@@ -137,6 +129,25 @@ function _civicrm_api3_cxn_unregister_spec(&$spec) {
  * @return array
  */
 function civicrm_api3_cxn_unregister($params) {
+  $cxnId = _civicrm_api3_cxn_parseCxnId($params);
+  $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
+
+  /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
+  $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
+  list($cxnId, $result) = $client->unregister($appMeta, CRM_Utils_Array::value('force', $params, FALSE));
+
+  return $result;
+}
+
+/**
+ * @param array $params
+ *   An array with cxn_guid and/or app_guid.
+ * @return string
+ *   The CxnId. (If not available, then an exception is thrown.)
+ *
+ * @throws API_Exception
+ */
+function _civicrm_api3_cxn_parseCxnId($params) {
   $cxnId = NULL;
 
   if (!empty($params['cxn_guid'])) {
@@ -153,14 +164,12 @@ function civicrm_api3_cxn_unregister($params) {
   if (!$cxnId) {
     throw new API_Exception('Missing required parameter: cxn_guid');
   }
+  return $cxnId;
+}
 
-  $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
-
-  /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
-  $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
-  list($cxnId, $result) = $client->unregister($appMeta, CRM_Utils_Array::value('force', $params, FALSE));
-
-  return $result;
+function _civicrm_api3_cxn_get_spec(&$spec) {
+  // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
+  unset($spec['secret']);
 }
 
 /**
@@ -173,6 +182,9 @@ function civicrm_api3_cxn_unregister($params) {
  *   API result array.
  */
 function civicrm_api3_cxn_get($params) {
+  // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
+  unset($params['secret']);
+
   $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
   if (is_array($result['values'])) {
     foreach (array_keys($result['values']) as $i) {
@@ -182,7 +194,54 @@ function civicrm_api3_cxn_get($params) {
       if (!empty($result['values'][$i]['perm'])) {
         $result['values'][$i]['perm'] = json_decode($result['values'][$i]['perm'], TRUE);
       }
+      // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
+      unset($result['values'][$i]['secret']);
     }
   }
+
   return $result;
 }
+
+/**
+ * Adjust metadata for "getlink" action.
+ *
+ * @param array $spec
+ *   List of fields.
+ */
+function _civicrm_api3_cxn_getlink_spec(&$spec) {
+  $daoFields = CRM_Cxn_DAO_Cxn::fields();
+  $spec['app_guid'] = $daoFields['app_guid'];
+  $spec['cxn_guid'] = $daoFields['cxn_guid'];
+  $spec['page'] = array(
+    'name' => 'page',
+    'type' => CRM_Utils_Type::T_STRING,
+    'title' => ts('Page Type'),
+    'description' => 'The type of page (eg "settings")',
+    'maxlength' => 63,
+    'size' => CRM_Utils_Type::HUGE,
+  );
+}
+
+/**
+ *
+ * @param array $params
+ *   Array with keys:
+ *   - cxn_guid OR app_guid: string.
+ *   - page: string.
+ * @return array
+ * @throws Exception
+ */
+function civicrm_api3_cxn_getlink($params) {
+  $cxnId = _civicrm_api3_cxn_parseCxnId($params);
+  $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
+
+  if (empty($params['page']) || !is_string($params['page'])) {
+    throw new API_Exception("Invalid page");
+  }
+
+  /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
+  $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
+  return $client->call($appMeta, 'Cxn', 'getlink', array(
+    'page' => $params['page'],
+  ));
+}