| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | |
| 28 | use \Civi\Cxn\Rpc\Message\AppMetasMessage; |
| 29 | use \Civi\Cxn\Rpc\Message\GarbledMessage; |
| 30 | |
| 31 | /** |
| 32 | * The CxnApp API provides a pseudo-entity for exploring the list |
| 33 | * of published applications. It is a read-only API which |
| 34 | * downloads and validates the application list from civicrm.org. |
| 35 | * |
| 36 | * At time of writing, this API only supports simple filtering on |
| 37 | * equality. If you'd like more advanced filters, consider updating |
| 38 | * _civicrm_api3_basic_array_get() and api_v3_UtilsTest::testBasicArrayGet. |
| 39 | * |
| 40 | * NOTE: SyntaxConformanceTest is disabled for CxnApp. As a rough |
| 41 | * equivalent, see api_v3_UtilsTest::testBasicArrayGet. |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * Adjust metadata for "register" action. |
| 46 | * |
| 47 | * @param array $spec |
| 48 | * List of fields. |
| 49 | */ |
| 50 | function _civicrm_api3_cxn_app_get_spec(&$spec) { |
| 51 | $spec['appCert'] = array( |
| 52 | 'name' => 'appCert', |
| 53 | 'type' => CRM_Utils_Type::T_TEXT, |
| 54 | 'title' => ts('Certificate'), |
| 55 | 'description' => 'PEM-encoded certificate', |
| 56 | ); |
| 57 | $spec['appId'] = array( |
| 58 | 'name' => 'appId', |
| 59 | 'type' => CRM_Utils_Type::T_STRING, |
| 60 | 'title' => ts('Application GUID'), |
| 61 | 'description' => 'Application GUID', |
| 62 | 'maxlength' => 128, |
| 63 | 'size' => CRM_Utils_Type::HUGE, |
| 64 | ); |
| 65 | $spec['appUrl'] = array( |
| 66 | 'name' => 'appUrl', |
| 67 | 'type' => CRM_Utils_Type::T_STRING, |
| 68 | 'title' => ts('Registration URL'), |
| 69 | 'description' => 'An endpoint to notify when performing registration', |
| 70 | 'maxlength' => 255, |
| 71 | 'size' => CRM_Utils_Type::HUGE, |
| 72 | ); |
| 73 | $spec['desc'] = array( |
| 74 | 'name' => 'desc', |
| 75 | 'type' => CRM_Utils_Type::T_TEXT, |
| 76 | 'title' => ts('Description'), |
| 77 | 'description' => 'Description', |
| 78 | ); |
| 79 | //$spec['perm'] = array( |
| 80 | // 'name' => 'perm', |
| 81 | // 'type' => CRM_Utils_Type::T_TEXT, |
| 82 | // 'title' => ts('Permissions'), |
| 83 | // 'description' => 'Permissions expected for the service (struct)', |
| 84 | //); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get a list of applications available for connections. |
| 89 | * |
| 90 | * @param array $params |
| 91 | * @return array |
| 92 | * @throws API_Exception |
| 93 | * @throws CRM_Core_Exception |
| 94 | * @throws \Civi\Cxn\Rpc\Exception\InvalidMessageException |
| 95 | */ |
| 96 | function civicrm_api3_cxn_app_get($params) { |
| 97 | // You should not change CIVICRM_CXN_APPS_URL in production; this is for local development. |
| 98 | $url = defined('CIVICRM_CXN_APPS_URL') ? CIVICRM_CXN_APPS_URL : \Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL; |
| 99 | |
| 100 | list ($headers, $blob, $code) = CRM_Cxn_CiviCxnHttp::singleton()->send('GET', $url, ''); |
| 101 | if ($code != 200) { |
| 102 | throw new API_Exception("Failed to download application list."); |
| 103 | } |
| 104 | |
| 105 | $agent = new \Civi\Cxn\Rpc\Agent(NULL, NULL); |
| 106 | $agent->setCertValidator(CRM_Cxn_BAO_Cxn::createCertificateValidator()); |
| 107 | $message = $agent->decode(array(AppMetasMessage::NAME, GarbledMessage::NAME), $blob); |
| 108 | |
| 109 | if ($message instanceof AppMetasMessage) { |
| 110 | return _civicrm_api3_basic_array_get('CxnApp', $params, $message->getData(), 'appId', |
| 111 | array('appId', 'appUrl', 'desc', 'appCert', 'perm')); |
| 112 | } |
| 113 | elseif ($message instanceof GarbledMessage) { |
| 114 | return civicrm_api3_create_error('Received garbled response', array( |
| 115 | 'garbled_message' => $message->getData(), |
| 116 | )); |
| 117 | } |
| 118 | else { |
| 119 | return civicrm_api3_create_error("Unrecognized message"); |
| 120 | } |
| 121 | } |