CRM-16173 - Cxn API - Add "get". Fix GUID fields and API metadata.
[civicrm-core.git] / api / v3 / Cxn.php
CommitLineData
5d5d3b35
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
5d5d3b35 28/**
39151786
TO
29 * The Cxn API allows a Civi site to initiate a connection to a
30 * remote application. There are three primary actions:
31 *
32 * - register: Establish a new connection.
33 * - unregister: Destroy an existing connection.
34 * - get: Get a list of existing connections.
35 */
36
37/**
38 * Adjust metadata for "register" action.
39 *
40 * @param array $spec
41 * List of fields.
42 */
43function _civicrm_api3_cxn_register_spec(&$spec) {
44 $daoFields = CRM_Cxn_DAO_Cxn::fields();
45 $spec['app_guid'] = $daoFields['app_guid'];
46
47 if (!CRM_Cxn_BAO_Cxn::isAppMetaVerified()) {
48 $spec['app_meta_url'] = array(
49 'name' => 'app_meta_url',
50 'type' => CRM_Utils_Type::T_STRING,
51 'title' => ts('Application Metadata URL'),
52 'description' => 'Application Metadata URL',
53 'maxlength' => 255,
54 'size' => CRM_Utils_Type::HUGE,
55 );
56 }
57}
58
59/**
60 * Register with a remote application and create a new connection.
61 *
62 * One should generally identify an application using the app_guid.
63 * However, if you need to test a new/experimental application, then
64 * disable CIVICRM_CXN_CA and specify app_meta_url.
65 *
5d5d3b35
TO
66 * @param array $params
67 * Array with keys:
39151786
TO
68 * - app_guid: The unique identifer of the target application.
69 * - app_meta_url: The URL for the application's metadata.
5d5d3b35 70 * @return array
39151786 71 * @throws Exception
5d5d3b35
TO
72 */
73function civicrm_api3_cxn_register($params) {
39151786 74 if (!empty($params['app_meta_url'])) {
9ae2d27b 75 if (!CRM_Cxn_BAO_Cxn::isAppMetaVerified()) {
39151786 76 list ($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
5d5d3b35
TO
77 if (CRM_Utils_HttpClient::STATUS_OK != $status) {
78 throw new API_Exception("Failed to download appMeta.");
79 }
39151786 80 $appMeta = json_decode($json, TRUE);
5d5d3b35
TO
81 }
82 else {
83 // Note: The metadata includes a cert, but the details aren't signed.
84 // This is very useful in testing/development. In ordinary usage, we
9ae2d27b
TO
85 // rely on civicrm.org to sign the metadata for all apps en masse.
86 throw new API_Exception('This site is configured to only connect to applications with verified metadata.');
5d5d3b35
TO
87 }
88 }
39151786
TO
89 elseif (!empty($params['app_guid'])) {
90 $appMeta = civicrm_api3('CxnApp', 'getsingle', array(
91 'app_guid' => $params['app_guid'],
92 ));
93 }
5d5d3b35 94
39151786
TO
95 if (empty($appMeta) || !is_array($appMeta)) {
96 throw new API_Exception("Missing expected parameter: app_guid");
5d5d3b35 97 }
39151786 98 \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
5d5d3b35 99
5d5d3b35 100 try {
7b4bbb34
TO
101 /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
102 $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
39151786
TO
103 list($cxnId, $result) = $client->register($appMeta);
104 CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
5d5d3b35
TO
105 }
106 catch (Exception $e) {
39151786 107 CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
5d5d3b35
TO
108 throw $e;
109 }
110
0efb07c0
TO
111 return $result;
112}
113
39151786
TO
114function _civicrm_api3_cxn_unregister_spec(&$spec) {
115 $daoFields = CRM_Cxn_DAO_Cxn::fields();
116 $spec['cxn_guid'] = $daoFields['cxn_guid'];
117 $spec['app_guid'] = $daoFields['app_guid'];
118 $spec['force'] = array(
119 'name' => 'force',
120 'type' => CRM_Utils_Type::T_BOOLEAN,
121 'title' => ts('Force'),
122 'description' => 'Destroy connection even if the remote application is non-responsive.',
123 'default' => '0',
124 );
125}
126
0efb07c0 127/**
39151786
TO
128 * Unregister with a remote application; destroy an existing connection.
129 *
130 * Specify app_guid XOR cxn_guid.
131 *
0efb07c0
TO
132 * @param array $params
133 * Array with keys:
39151786
TO
134 * - cxn_guid: string
135 * - app_guid: string
136 * - force: bool
0efb07c0
TO
137 * @return array
138 */
139function civicrm_api3_cxn_unregister($params) {
39151786
TO
140 $cxnId = NULL;
141
142 if (!empty($params['cxn_guid'])) {
143 $cxnId = $params['cxn_guid'];
144 }
145 elseif (!empty($params['app_guid'])) {
146 $cxnId = CRM_Core_DAO::singleValueQuery('SELECT cxn_guid FROM civicrm_cxn WHERE app_guid = %1', array(
147 1 => array($params['app_guid'], 'String'),
148 ));
149 if (!$cxnId) {
150 throw new API_Exception("The app_guid does not correspond to an active connection.");
151 }
152 }
153 if (!$cxnId) {
154 throw new API_Exception('Missing required parameter: cxn_guid');
5d5d3b35 155 }
0efb07c0 156
39151786 157 $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
0efb07c0
TO
158
159 /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
160 $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
161 list($cxnId, $result) = $client->unregister($appMeta, CRM_Utils_Array::value('force', $params, FALSE));
162
163 return $result;
5d5d3b35 164}
39151786
TO
165
166/**
167 * Returns an array of Cxn records.
168 *
169 * @param array $params
170 * Array of one or more valid property_name=>value pairs.
171 *
172 * @return array
173 * API result array.
174 */
175function civicrm_api3_cxn_get($params) {
176 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
177}