3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 use Civi\API\Event\AuthorizeEvent
;
30 use Civi\API\Event\PrepareEvent
;
31 use Civi\API\Event\ExceptionEvent
;
32 use Civi\API\Event\RespondEvent
;
38 * @copyright CiviCRM LLC (c) 2004-2013
44 * @var \Symfony\Component\EventDispatcher\EventDispatcher
46 protected $dispatcher;
49 * @var array<APIProviderInterface>
51 protected $apiProviders;
53 function __construct($dispatcher, $apiProviders = array()) {
54 $this->apiProviders
= $apiProviders;
55 $this->dispatcher
= $dispatcher;
59 * @param string $entity
60 * type of entities to deal with
61 * @param string $action
62 * create, get, delete or some special action name.
63 * @param array $params
64 * array to be passed to function
69 public function run($entity, $action, $params, $extra) {
70 $apiRequest = array();
71 $apiRequest['entity'] = \CRM_Utils_String
::munge($entity);
72 $apiRequest['action'] = \CRM_Utils_String
::munge($action);
73 $apiRequest['version'] = civicrm_get_api_version($params);
74 $apiRequest['params'] = $params;
75 $apiRequest['extra'] = $extra;
77 /** @var $apiWrappers array<\API_Wrapper> */
79 \CRM_Utils_API_HTMLInputCoder
::singleton(),
80 \CRM_Utils_API_NullOutputCoder
::singleton(),
81 \CRM_Utils_API_ReloadOption
::singleton(),
82 \CRM_Utils_API_MatchOption
::singleton(),
84 \CRM_Utils_Hook
::apiWrappers($apiWrappers, $apiRequest);
87 if (!is_array($params)) {
88 throw new \
API_Exception('Input variable `params` is not an array', 2000);
92 $errorScope = \CRM_Core_TemporaryErrorScope
::useException();
94 // look up function, file, is_generic
95 $apiRequest +
= _civicrm_api_resolve($apiRequest);
97 if (! $this->dispatcher
->dispatch(Events
::AUTHORIZE
, new AuthorizeEvent(NULL, $apiRequest))->isAuthorized()) {
98 throw new \
API_Exception("Authorization failed");
101 $apiRequest = $this->dispatcher
->dispatch(Events
::PREPARE
, new PrepareEvent(NULL, $apiRequest))->getApiRequest();
103 // support multi-lingual requests
104 if ($language = \CRM_Utils_Array
::value('option.language', $params)) {
105 _civicrm_api_set_locale($language);
108 _civicrm_api3_api_check_permission($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']);
109 $fields = _civicrm_api3_api_getfields($apiRequest);
110 // we do this before we
111 _civicrm_api3_swap_out_aliases($apiRequest, $fields);
112 if (strtolower($action) != 'getfields') {
113 if (empty($apiRequest['params']['id'])) {
114 $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest, $fields), $apiRequest['params']);
116 //if 'id' is set then only 'version' will be checked but should still be checked for consistency
117 civicrm_api3_verify_mandatory($apiRequest['params'], NULL, _civicrm_api3_getrequired($apiRequest, $fields));
120 // For input filtering, process $apiWrappers in forward order
121 foreach ($apiWrappers as $apiWrapper) {
122 $apiRequest = $apiWrapper->fromApiInput($apiRequest);
125 $function = $apiRequest['function'];
126 if ($apiRequest['function'] && $apiRequest['is_generic']) {
127 // Unlike normal API implementations, generic implementations require explicit
128 // knowledge of the entity and action (as well as $params). Bundle up these bits
129 // into a convenient data structure.
130 $result = $function($apiRequest);
132 elseif ($apiRequest['function'] && !$apiRequest['is_generic']) {
133 _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $fields);
135 $result = isset($extra) ?
$function($apiRequest['params'], $extra) : $function($apiRequest['params']);
138 return civicrm_api3_create_error("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
141 // For output filtering, process $apiWrappers in reverse order
142 foreach (array_reverse($apiWrappers) as $apiWrapper) {
143 $result = $apiWrapper->toApiOutput($apiRequest, $result);
146 if (\CRM_Utils_Array
::value('format.is_success', $apiRequest['params']) == 1) {
147 if ($result['is_error'] === 0) {
156 if (!empty($apiRequest['params']['format.only_id']) && isset($result['id'])) {
158 return $result['id'];
161 if (\CRM_Utils_Array
::value('is_error', $result, 0) == 0) {
162 _civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']);
165 $responseEvent = $this->dispatcher
->dispatch(Events
::RESPOND
, new RespondEvent(NULL, $apiRequest, $result));
166 $result = $responseEvent->getResponse();
170 catch (\Exception
$e) {
171 $this->dispatcher
->dispatch(Events
::EXCEPTION
, new ExceptionEvent($e, NULL, $apiRequest));
173 if (isset($apiRequest, $apiRequest['params'], $apiRequest['params']['format.is_success']) && $apiRequest['params']['format.is_success'] == 1) {
177 if ($e instanceof \PEAR_Exception
) {
178 $err = $this->formatPearException($e, $apiRequest);
179 } elseif ($e instanceof \API_Exception
) {
180 $err = $this->formatApiException($e, $apiRequest);
182 $err = $this->formatException($e, $apiRequest);
190 public function boot() {
191 require_once ('api/v3/utils.php');
192 require_once 'api/Exception.php';
193 _civicrm_api3_initialize();
197 * @param \Exception $e
198 * @param array $apiRequest
199 * @return array (API response)
201 public function formatException($e, $apiRequest) {
203 if (!empty($apiRequest['params']['debug'])) {
204 $data['trace'] = $e->getTraceAsString();
206 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode());
210 * @param \API_Exception $e
211 * @param array $apiRequest
212 * @return array (API response)
214 public function formatApiException($e, $apiRequest) {
215 $data = $e->getExtraParams();
216 $data['entity'] = \CRM_Utils_Array
::value('entity', $apiRequest);
217 $data['action'] = \CRM_Utils_Array
::value('action', $apiRequest);
219 if (\CRM_Utils_Array
::value('debug', \CRM_Utils_Array
::value('params', $apiRequest))
220 && empty($data['trace']) // prevent recursion
222 $data['trace'] = $e->getTraceAsString();
225 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode());
229 * @param \PEAR_Exception $e
230 * @param array $apiRequest
231 * @return array (API response)
233 public function formatPearException($e, $apiRequest) {
235 $error = $e->getCause();
236 if ($error instanceof \DB_Error
) {
237 $data["error_code"] = \DB
::errorMessage($error->getCode());
238 $data["sql"] = $error->getDebugInfo();
240 if (!empty($apiRequest['params']['debug'])) {
241 if (method_exists($e, 'getUserInfo')) {
242 $data['debug_info'] = $error->getUserInfo();
244 if (method_exists($e, 'getExtraData')) {
245 $data['debug_info'] = $data +
$error->getExtraData();
247 $data['trace'] = $e->getTraceAsString();
250 $data['tip'] = "add debug=1 to your API call to have more info about the error";
253 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest);