| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | use Civi\Cxn\Rpc\Message\AppMetasMessage; |
| 13 | use Civi\Cxn\Rpc\Message\GarbledMessage; |
| 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) { |
| 35 | $spec['appCert'] = [ |
| 36 | 'name' => 'appCert', |
| 37 | 'type' => CRM_Utils_Type::T_TEXT, |
| 38 | 'title' => ts('Certificate'), |
| 39 | 'description' => 'PEM-encoded certificate', |
| 40 | ]; |
| 41 | $spec['appId'] = [ |
| 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, |
| 48 | ]; |
| 49 | $spec['appUrl'] = [ |
| 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, |
| 56 | ]; |
| 57 | $spec['desc'] = [ |
| 58 | 'name' => 'desc', |
| 59 | 'type' => CRM_Utils_Type::T_TEXT, |
| 60 | 'title' => ts('Description'), |
| 61 | 'description' => 'Description', |
| 62 | ]; |
| 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) { |
| 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 | |
| 84 | list ($headers, $blob, $code) = CRM_Cxn_CiviCxnHttp::singleton()->send('GET', $url, ''); |
| 85 | if ($code != 200) { |
| 86 | throw new API_Exception("Failed to download application list."); |
| 87 | } |
| 88 | |
| 89 | $agent = new \Civi\Cxn\Rpc\Agent(NULL, NULL); |
| 90 | $agent->setCertValidator(CRM_Cxn_BAO_Cxn::createCertificateValidator()); |
| 91 | $message = $agent->decode([AppMetasMessage::NAME, GarbledMessage::NAME], $blob); |
| 92 | |
| 93 | if ($message instanceof AppMetasMessage) { |
| 94 | return _civicrm_api3_basic_array_get('CxnApp', $params, $message->getData(), 'appId', |
| 95 | ['appId', 'appUrl', 'desc', 'appCert', 'perm']); |
| 96 | } |
| 97 | elseif ($message instanceof GarbledMessage) { |
| 98 | return civicrm_api3_create_error('Received garbled response', [ |
| 99 | 'garbled_message' => $message->getData(), |
| 100 | ]); |
| 101 | } |
| 102 | else { |
| 103 | return civicrm_api3_create_error("Unrecognized message"); |
| 104 | } |
| 105 | } |