Commit | Line | Data |
---|---|---|
a066deea TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
a066deea | 5 | | | |
a30c801b TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
a066deea TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
7c31ae57 SL |
12 | use Civi\Cxn\Rpc\Message\AppMetasMessage; |
13 | use Civi\Cxn\Rpc\Message\GarbledMessage; | |
a066deea TO |
14 | |
15 | /** | |
16 | * The CxnApp API provides a pseudo-entity for exploring the list | |
17 | * of published applications. It is a read-only API which | |
18 | * downloads and validates the application list from civicrm.org. | |
19 | * | |
20 | * At time of writing, this API only supports simple filtering on | |
21 | * equality. If you'd like more advanced filters, consider updating | |
22 | * _civicrm_api3_basic_array_get() and api_v3_UtilsTest::testBasicArrayGet. | |
23 | * | |
24 | * NOTE: SyntaxConformanceTest is disabled for CxnApp. As a rough | |
25 | * equivalent, see api_v3_UtilsTest::testBasicArrayGet. | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Adjust metadata for "register" action. | |
30 | * | |
31 | * @param array $spec | |
32 | * List of fields. | |
33 | */ | |
34 | function _civicrm_api3_cxn_app_get_spec(&$spec) { | |
cf8f0fff | 35 | $spec['appCert'] = [ |
a066deea TO |
36 | 'name' => 'appCert', |
37 | 'type' => CRM_Utils_Type::T_TEXT, | |
38 | 'title' => ts('Certificate'), | |
39 | 'description' => 'PEM-encoded certificate', | |
cf8f0fff CW |
40 | ]; |
41 | $spec['appId'] = [ | |
a066deea TO |
42 | 'name' => 'appId', |
43 | 'type' => CRM_Utils_Type::T_STRING, | |
44 | 'title' => ts('Application GUID'), | |
45 | 'description' => 'Application GUID', | |
46 | 'maxlength' => 128, | |
47 | 'size' => CRM_Utils_Type::HUGE, | |
cf8f0fff CW |
48 | ]; |
49 | $spec['appUrl'] = [ | |
a066deea TO |
50 | 'name' => 'appUrl', |
51 | 'type' => CRM_Utils_Type::T_STRING, | |
52 | 'title' => ts('Registration URL'), | |
53 | 'description' => 'An endpoint to notify when performing registration', | |
54 | 'maxlength' => 255, | |
55 | 'size' => CRM_Utils_Type::HUGE, | |
cf8f0fff CW |
56 | ]; |
57 | $spec['desc'] = [ | |
a066deea TO |
58 | 'name' => 'desc', |
59 | 'type' => CRM_Utils_Type::T_TEXT, | |
60 | 'title' => ts('Description'), | |
61 | 'description' => 'Description', | |
cf8f0fff | 62 | ]; |
a066deea TO |
63 | //$spec['perm'] = array( |
64 | // 'name' => 'perm', | |
65 | // 'type' => CRM_Utils_Type::T_TEXT, | |
66 | // 'title' => ts('Permissions'), | |
67 | // 'description' => 'Permissions expected for the service (struct)', | |
68 | //); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Get a list of applications available for connections. | |
73 | * | |
74 | * @param array $params | |
75 | * @return array | |
76 | * @throws API_Exception | |
77 | * @throws CRM_Core_Exception | |
78 | * @throws \Civi\Cxn\Rpc\Exception\InvalidMessageException | |
79 | */ | |
80 | function civicrm_api3_cxn_app_get($params) { | |
8e88239c TO |
81 | // You should not change CIVICRM_CXN_APPS_URL in production; this is for local development. |
82 | $url = defined('CIVICRM_CXN_APPS_URL') ? CIVICRM_CXN_APPS_URL : \Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL; | |
83 | ||
5063e355 TO |
84 | list ($headers, $blob, $code) = CRM_Cxn_CiviCxnHttp::singleton()->send('GET', $url, ''); |
85 | if ($code != 200) { | |
a066deea TO |
86 | throw new API_Exception("Failed to download application list."); |
87 | } | |
8e88239c | 88 | |
fc736b89 TO |
89 | $agent = new \Civi\Cxn\Rpc\Agent(NULL, NULL); |
90 | $agent->setCertValidator(CRM_Cxn_BAO_Cxn::createCertificateValidator()); | |
cf8f0fff | 91 | $message = $agent->decode([AppMetasMessage::NAME, GarbledMessage::NAME], $blob); |
8e88239c | 92 | |
a066deea TO |
93 | if ($message instanceof AppMetasMessage) { |
94 | return _civicrm_api3_basic_array_get('CxnApp', $params, $message->getData(), 'appId', | |
cf8f0fff | 95 | ['appId', 'appUrl', 'desc', 'appCert', 'perm']); |
a066deea TO |
96 | } |
97 | elseif ($message instanceof GarbledMessage) { | |
cf8f0fff | 98 | return civicrm_api3_create_error('Received garbled response', [ |
a066deea | 99 | 'garbled_message' => $message->getData(), |
cf8f0fff | 100 | ]); |
a066deea TO |
101 | } |
102 | else { | |
103 | return civicrm_api3_create_error("Unrecognized message"); | |
104 | } | |
105 | } |