Short array syntax - auto-convert api dir
[civicrm-core.git] / api / v3 / Cxn.php
CommitLineData
5d5d3b35
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
5d5d3b35 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
5d5d3b35
TO
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'];
cf8f0fff 46 $spec['app_meta_url'] = [
08c79574
TO
47 'name' => 'app_meta_url',
48 'type' => CRM_Utils_Type::T_STRING,
49 'title' => ts('Application Metadata URL'),
50 'description' => 'Application Metadata URL',
51 'maxlength' => 255,
52 'size' => CRM_Utils_Type::HUGE,
cf8f0fff 53 ];
39151786
TO
54}
55
56/**
57 * Register with a remote application and create a new connection.
58 *
59 * One should generally identify an application using the app_guid.
60 * However, if you need to test a new/experimental application, then
61 * disable CIVICRM_CXN_CA and specify app_meta_url.
62 *
5d5d3b35
TO
63 * @param array $params
64 * Array with keys:
39151786
TO
65 * - app_guid: The unique identifer of the target application.
66 * - app_meta_url: The URL for the application's metadata.
5d5d3b35 67 * @return array
39151786 68 * @throws Exception
5d5d3b35
TO
69 */
70function civicrm_api3_cxn_register($params) {
39151786 71 if (!empty($params['app_meta_url'])) {
08c79574
TO
72 list ($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
73 if (CRM_Utils_HttpClient::STATUS_OK != $status) {
74 throw new API_Exception("Failed to download appMeta. (Bad HTTP response)");
5d5d3b35 75 }
08c79574
TO
76 $appMeta = json_decode($json, TRUE);
77 if (empty($appMeta)) {
78 throw new API_Exception("Failed to download appMeta. (Malformed)");
5d5d3b35
TO
79 }
80 }
39151786 81 elseif (!empty($params['app_guid'])) {
cf8f0fff 82 $appMeta = civicrm_api3('CxnApp', 'getsingle', [
6fb60f90 83 'appId' => $params['app_guid'],
cf8f0fff 84 ]);
39151786 85 }
5d5d3b35 86
39151786
TO
87 if (empty($appMeta) || !is_array($appMeta)) {
88 throw new API_Exception("Missing expected parameter: app_guid");
5d5d3b35 89 }
39151786 90 \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
5d5d3b35 91
5d5d3b35 92 try {
7b4bbb34 93 /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
048222df 94 $client = \Civi::service('cxn_reg_client');
39151786
TO
95 list($cxnId, $result) = $client->register($appMeta);
96 CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
5d5d3b35
TO
97 }
98 catch (Exception $e) {
39151786 99 CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
5d5d3b35
TO
100 throw $e;
101 }
102
0efb07c0
TO
103 return $result;
104}
105
70599df6 106/**
107 * Adjust metadata for cxn unregister.
108 *
109 * @param array $spec
110 */
39151786
TO
111function _civicrm_api3_cxn_unregister_spec(&$spec) {
112 $daoFields = CRM_Cxn_DAO_Cxn::fields();
113 $spec['cxn_guid'] = $daoFields['cxn_guid'];
114 $spec['app_guid'] = $daoFields['app_guid'];
cf8f0fff 115 $spec['force'] = [
39151786
TO
116 'name' => 'force',
117 'type' => CRM_Utils_Type::T_BOOLEAN,
118 'title' => ts('Force'),
119 'description' => 'Destroy connection even if the remote application is non-responsive.',
120 'default' => '0',
cf8f0fff 121 ];
39151786
TO
122}
123
0efb07c0 124/**
39151786
TO
125 * Unregister with a remote application; destroy an existing connection.
126 *
127 * Specify app_guid XOR cxn_guid.
128 *
0efb07c0
TO
129 * @param array $params
130 * Array with keys:
39151786
TO
131 * - cxn_guid: string
132 * - app_guid: string
133 * - force: bool
0efb07c0
TO
134 * @return array
135 */
136function civicrm_api3_cxn_unregister($params) {
a8b2e33f
TO
137 $cxnId = _civicrm_api3_cxn_parseCxnId($params);
138 $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
139
140 /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
048222df 141 $client = \Civi::service('cxn_reg_client');
a8b2e33f
TO
142 list($cxnId, $result) = $client->unregister($appMeta, CRM_Utils_Array::value('force', $params, FALSE));
143
144 return $result;
145}
146
147/**
148 * @param array $params
149 * An array with cxn_guid and/or app_guid.
150 * @return string
151 * The CxnId. (If not available, then an exception is thrown.)
152 *
153 * @throws API_Exception
154 */
155function _civicrm_api3_cxn_parseCxnId($params) {
39151786
TO
156 $cxnId = NULL;
157
158 if (!empty($params['cxn_guid'])) {
159 $cxnId = $params['cxn_guid'];
160 }
161 elseif (!empty($params['app_guid'])) {
cf8f0fff
CW
162 $cxnId = CRM_Core_DAO::singleValueQuery('SELECT cxn_guid FROM civicrm_cxn WHERE app_guid = %1', [
163 1 => [$params['app_guid'], 'String'],
164 ]);
39151786
TO
165 if (!$cxnId) {
166 throw new API_Exception("The app_guid does not correspond to an active connection.");
167 }
168 }
169 if (!$cxnId) {
170 throw new API_Exception('Missing required parameter: cxn_guid');
5d5d3b35 171 }
a8b2e33f 172 return $cxnId;
5d5d3b35 173}
39151786 174
70599df6 175/**
176 * Adjust metadata for cxn get action.
177 *
178 * @param array $spec
179 */
52079b6d
TO
180function _civicrm_api3_cxn_get_spec(&$spec) {
181 // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
182 unset($spec['secret']);
183}
184
39151786
TO
185/**
186 * Returns an array of Cxn records.
187 *
188 * @param array $params
189 * Array of one or more valid property_name=>value pairs.
190 *
191 * @return array
192 * API result array.
193 */
194function civicrm_api3_cxn_get($params) {
52079b6d
TO
195 // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
196 unset($params['secret']);
197
098de400
TO
198 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
199 if (is_array($result['values'])) {
200 foreach (array_keys($result['values']) as $i) {
201 if (!empty($result['values'][$i]['app_meta'])) {
202 $result['values'][$i]['app_meta'] = json_decode($result['values'][$i]['app_meta'], TRUE);
203 }
204 if (!empty($result['values'][$i]['perm'])) {
205 $result['values'][$i]['perm'] = json_decode($result['values'][$i]['perm'], TRUE);
206 }
52079b6d
TO
207 // Don't trust AJAX callers or other external code to modify, filter, or return the secret.
208 unset($result['values'][$i]['secret']);
098de400
TO
209 }
210 }
52079b6d 211
098de400 212 return $result;
39151786 213}
a8b2e33f
TO
214
215/**
216 * Adjust metadata for "getlink" action.
217 *
218 * @param array $spec
219 * List of fields.
220 */
221function _civicrm_api3_cxn_getlink_spec(&$spec) {
222 $daoFields = CRM_Cxn_DAO_Cxn::fields();
223 $spec['app_guid'] = $daoFields['app_guid'];
224 $spec['cxn_guid'] = $daoFields['cxn_guid'];
cf8f0fff 225 $spec['page_name'] = [
b5552396 226 'name' => 'page_name',
a8b2e33f
TO
227 'type' => CRM_Utils_Type::T_STRING,
228 'title' => ts('Page Type'),
229 'description' => 'The type of page (eg "settings")',
230 'maxlength' => 63,
231 'size' => CRM_Utils_Type::HUGE,
cf8f0fff
CW
232 'api.aliases' => ['page'],
233 ];
a8b2e33f
TO
234}
235
236/**
237 *
238 * @param array $params
239 * Array with keys:
240 * - cxn_guid OR app_guid: string.
241 * - page: string.
242 * @return array
243 * @throws Exception
244 */
245function civicrm_api3_cxn_getlink($params) {
246 $cxnId = _civicrm_api3_cxn_parseCxnId($params);
247 $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
248
b5552396 249 if (empty($params['page_name']) || !is_string($params['page_name'])) {
a8b2e33f
TO
250 throw new API_Exception("Invalid page");
251 }
252
253 /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
048222df 254 $client = \Civi::service('cxn_reg_client');
cf8f0fff 255 return $client->call($appMeta, 'Cxn', 'getlink', [
b5552396 256 'page' => $params['page_name'],
cf8f0fff 257 ]);
a8b2e33f 258}
0b5469cd
C
259
260/**
261 *
262 * @param array $params
263 * @return array
264 * @throws Exception
265 */
266function civicrm_api3_cxn_getcfg($params) {
cf8f0fff 267 $result = [
0b5469cd
C
268 'CIVICRM_CXN_CA' => defined('CIVICRM_CXN_CA') ? CIVICRM_CXN_CA : NULL,
269 'CIVICRM_CXN_VIA' => defined('CIVICRM_CXN_VIA') ? CIVICRM_CXN_VIA : NULL,
270 'CIVICRM_CXN_APPS_URL' => defined('CIVICRM_CXN_APPS_URL') ? CIVICRM_CXN_APPS_URL : NULL,
271 'siteCallbackUrl' => CRM_Cxn_BAO_Cxn::getSiteCallbackUrl(),
cf8f0fff 272 ];
0b5469cd 273 return civicrm_api3_create_success($result);
0b5469cd 274}
e3c37dee
C
275
276/**
277 * Creates or modifies a Cxn row.
278 *
279 * @param array $params
280 * Array with keys:
281 * - id, cxn_guid OR app_guid: string.
282 * - is_active: boolean.
283 * - options: JSON
284 * @return page
285 * @throws Exception
286 */
287function civicrm_api3_cxn_create($params) {
288 $result = "";
289
290 try {
291 // get the ID
292 if (!empty($params['id'])) {
293 $cxnId = $params['id'];
294 }
295 else {
296 $cxnId = _civicrm_api3_cxn_parseCxnId($params);
297 }
298
299 // see if it's sth to update
300 if (isset($params['options']) || isset($params['is_active'])) {
301
302 $dao = new CRM_Cxn_DAO_Cxn();
303 $dao->id = $cxnId;
304
305 if ($dao->find()) {
306 if (isset($params['is_active'])) {
307 $dao->is_active = (int) $params['is_active'];
308 }
309 if (isset($params['options'])) {
310 $dao->options = $params['options'];
311 }
312
313 $result = $dao->save();
314 }
315
316 }
317 return civicrm_api3_create_success($result, $params, 'Cxn', 'create');
318
319 }
320 catch(Exception $ex){
321 throw $ex;
322 }
323}