crmCxnManageCtrl - Show links for settings, docs, support, logs
[civicrm-core.git] / CRM / Cxn / BAO / Cxn.php
CommitLineData
5d5d3b35
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright (C) 2011 Marty Wright |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
9ae2d27b
TO
29use Civi\Cxn\Rpc\Constants;
30
5d5d3b35
TO
31/**
32 *
33 * @package CRM
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * $Id$
36 *
37 */
38
39/**
40 * This class helps to manage connections to third-party apps.
41 */
42class CRM_Cxn_BAO_Cxn extends CRM_Cxn_DAO_Cxn {
0849804a
TO
43
44 /**
45 * Determine the current site's callback URL.
46 *
47 * @return string
48 */
5d5d3b35
TO
49 public static function getSiteCallbackUrl() {
50 $config = CRM_Core_Config::singleton();
3e6b8905 51
5d5d3b35
TO
52 if (preg_match('/^(http|https):/', $config->resourceBase)) {
53 $civiUrl = $config->resourceBase;
54 }
55 else {
56 $civiUrl = rtrim(CRM_Utils_System::baseURL(), '/') . '/' . ltrim($config->resourceBase, '/');
57 }
3e6b8905
TO
58
59 // In practice, this may not be necessary, but we want to prevent
60 // edge-cases that downgrade security-level below system policy.
61 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL')) {
62 $civiUrl = preg_replace('/^http:/', 'https:', $civiUrl);
63 }
64
5d5d3b35
TO
65 return rtrim($civiUrl, '/') . '/extern/cxn.php';
66 }
67
0849804a
TO
68 /**
69 * Update the AppMeta for any existing connections.
70 *
71 * @param array $appMeta
72 * @throws \Civi\Cxn\Rpc\Exception\CxnException
73 */
5d5d3b35
TO
74 public static function updateAppMeta($appMeta) {
75 \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
39151786 76 CRM_Core_DAO::executeQuery('UPDATE civicrm_cxn SET app_meta = %1 WHERE app_guid = %2', array(
5d5d3b35
TO
77 1 => array(json_encode($appMeta), 'String'),
78 2 => array($appMeta['appId'], 'String'),
79 ));
80 }
0efb07c0 81
0849804a
TO
82 /**
83 * Get the AppMeta for an existing connection.
84 *
85 * @param string $cxnId
86 * @return array
87 * @throws \Civi\Cxn\Rpc\Exception\CxnException
88 */
0efb07c0 89 public static function getAppMeta($cxnId) {
39151786 90 $appMetaJson = CRM_Core_DAO::getFieldValue('CRM_Cxn_DAO_Cxn', $cxnId, 'app_meta', 'cxn_guid', TRUE);
0efb07c0
TO
91 $appMeta = json_decode($appMetaJson, TRUE);
92 \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
93 return $appMeta;
94 }
95
9ae2d27b
TO
96 /**
97 * Parse the CIVICRM_CXN_CA constant. It may have the following
98 * values:
99 * - 'CiviRootCA'|undefined -- Use the production civicrm.org root CA
100 * - 'CiviTestRootCA' -- Use the test civicrm.org root CA
101 * - 'none' -- Do not perform any certificate verification.
102 *
103 * This constant is emphatically *not* exposed through Civi's "Settings"
104 * system (or any other runtime-editable datastore). Manipulating
105 * this setting can expose the system to man-in-the-middle attacks,
106 * and allowing runtime manipulation would create a new vector
107 * for escalating privileges. This setting must only be manipulated
108 * by developers and sysadmins who already have full privileges
109 * to edit the source.
110 *
111 * @return string|NULL
112 * The PEM-encoded root certificate. NULL if verification is disabled.
113 * @throws CRM_Core_Exception
114 */
13afc1a9 115 public static function getCACert() {
9ae2d27b
TO
116 if (!defined('CIVICRM_CXN_CA') || CIVICRM_CXN_CA === 'CiviRootCA') {
117 $file = Constants::getCert();
118 }
119 elseif (CIVICRM_CXN_CA === 'CiviTestRootCA') {
120 $file = Constants::getTestCert();
121 }
122 elseif (CIVICRM_CXN_CA === 'none') {
123 return NULL;
124 }
125 else {
126 throw new \CRM_Core_Exception("CIVICRM_CXN_CA is invalid.");
127 }
128
129 $content = file_get_contents($file);
130 if (empty($content)) {
131 // Fail hard. Returning an empty value is not acceptable.
132 throw new \CRM_Core_Exception("Error loading CA certificate: $file");
133 }
134 return $content;
135 }
136
137 /**
138 * Determine if this site's security policy allows connecting
139 * to apps based on untrusted metadata.
140 *
141 * @return bool
142 * TRUE if application metadata must be verified.
143 */
144 public static function isAppMetaVerified() {
8e88239c
TO
145 if (defined('CIVICRM_CXN_APPS_VERIFY')) {
146 return CIVICRM_CXN_APPS_VERIFY;
9ae2d27b
TO
147 }
148 elseif (!defined('CIVICRM_CXN_CA')) {
149 return TRUE;
150 }
151 else {
152 return !in_array(CIVICRM_CXN_CA, array('CiviTestRootCA', 'none'));
153 }
154 }
155
0849804a
TO
156 /**
157 * Construct a client for performing registration actions.
158 *
159 * @return \Civi\Cxn\Rpc\RegistrationClient
160 * @throws CRM_Core_Exception
161 */
9ae2d27b
TO
162 public static function createRegistrationClient() {
163 $cxnStore = new \CRM_Cxn_CiviCxnStore();
13afc1a9 164 $client = new \Civi\Cxn\Rpc\RegistrationClient(self::getCACert(), $cxnStore, \CRM_Cxn_BAO_Cxn::getSiteCallbackUrl());
9ae2d27b
TO
165 $client->setLog(new \CRM_Utils_SystemLogger());
166 return $client;
167 }
168
5d5d3b35 169}