Merge pull request #22391 from eileenmcnaughton/legacy
[civicrm-core.git] / api / v3 / utils.php
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 /**
13 * CiviCRM APIv3 utility functions.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Initialize CiviCRM - should be run at the start of each API function.
20 */
21 function _civicrm_api3_initialize() {
22 require_once 'CRM/Core/ClassLoader.php';
23 CRM_Core_ClassLoader::singleton()->register();
24 CRM_Core_Config::singleton();
25 }
26
27 /**
28 * Wrapper Function for civicrm_verify_mandatory to make it simple to pass either / or fields for checking.
29 *
30 * @param array $params
31 * Array of fields to check.
32 * @param array $daoName
33 * String DAO to check for required fields (create functions only).
34 * @param array $keyoptions
35 * List of required fields options. One of the options is required.
36 *
37 * @throws \API_Exception
38 */
39 function civicrm_api3_verify_one_mandatory($params, $daoName = NULL, $keyoptions = []) {
40 $keys = [[]];
41 foreach ($keyoptions as $key) {
42 $keys[0][] = $key;
43 }
44 civicrm_api3_verify_mandatory($params, $daoName, $keys);
45 }
46
47 /**
48 * Check mandatory fields are included.
49 *
50 * @param array $params
51 * Array of fields to check.
52 * @param array $daoName
53 * String DAO to check for required fields (create functions only).
54 * @param array $keys
55 * List of required fields. A value can be an array denoting that either this or that is required.
56 * @param bool $verifyDAO
57 *
58 * @throws \API_Exception
59 */
60 function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = [], $verifyDAO = TRUE) {
61 $unmatched = [];
62
63 if (!empty($params['id'])) {
64 $keys = ['version'];
65 }
66 else {
67 if (!in_array('version', $keys)) {
68 // required from v3 onwards
69 $keys[] = 'version';
70 }
71 }
72 foreach ($keys as $key) {
73 if (is_array($key)) {
74 $match = 0;
75 $optionset = [];
76 foreach ($key as $subkey) {
77 if (!array_key_exists($subkey, $params) || empty($params[$subkey])) {
78 $optionset[] = $subkey;
79 }
80 else {
81 // As long as there is one match we don't need to return anything.
82 $match = 1;
83 }
84 }
85 if (empty($match) && !empty($optionset)) {
86 $unmatched[] = "one of (" . implode(", ", $optionset) . ")";
87 }
88 }
89 else {
90 // Disallow empty values except for the number zero.
91 // TODO: create a utility for this since it's needed in many places.
92 if (!array_key_exists($key, $params) || (empty($params[$key]) && $params[$key] !== 0.0 && $params[$key] !== 0 && $params[$key] !== '0')) {
93 $unmatched[] = $key;
94 }
95 }
96 }
97 if (!empty($unmatched)) {
98 throw new API_Exception('Mandatory key(s) missing from params array: ' . implode(", ", $unmatched), 'mandatory_missing', ["fields" => $unmatched]);
99 }
100 }
101
102 /**
103 * Create error array.
104 *
105 * @param string $msg
106 * @param array $data
107 *
108 * @return array
109 */
110 function civicrm_api3_create_error($msg, $data = []) {
111 $data['is_error'] = 1;
112 $data['error_message'] = $msg;
113
114 // we will show sql to privileged user only (not sure of a specific
115 // security hole here but seems sensible - perhaps should apply to the trace as well?)
116 if (isset($data['sql'])) {
117 if (CRM_Core_Permission::check('Administer CiviCRM') || CIVICRM_UF === 'UnitTests') {
118 // Isn't this redundant?
119 $data['debug_information'] = $data['sql'];
120 }
121 else {
122 unset($data['sql']);
123 }
124 }
125 return $data;
126 }
127
128 /**
129 * Format array in result output style.
130 *
131 * @param array|int $values values generated by API operation (the result)
132 * @param array $params
133 * Parameters passed into API call.
134 * @param string $entity
135 * The entity being acted on.
136 * @param string $action
137 * The action passed to the API.
138 * @param object $dao
139 * DAO object to be freed here.
140 * @param array $extraReturnValues
141 * Additional values to be added to top level of result array(.
142 * - this param is currently used for legacy behaviour support
143 *
144 * @return array
145 * @throws \CiviCRM_API3_Exception
146 */
147 function civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL, &$dao = NULL, $extraReturnValues = []) {
148 $result = [];
149 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
150 // TODO: This shouldn't be necessary but this fn sometimes gets called with lowercase entity
151 $entity = _civicrm_api_get_camel_name($entity);
152 $result['is_error'] = 0;
153 //lets set the ['id'] field if it's not set & we know what the entity is
154 if (is_array($values) && $entity && $action !== 'getfields') {
155 foreach ($values as $key => $item) {
156 if (empty($item['id']) && !empty($item[$lowercase_entity . '_id'])) {
157 $values[$key]['id'] = $item[$lowercase_entity . '_id'];
158 }
159 if (!empty($item['financial_type_id'])) {
160 // 4.3 legacy handling.
161 $values[$key]['contribution_type_id'] = $item['financial_type_id'];
162 }
163 if (!empty($item['contribution_cancel_date'])) {
164 // 5.16 legacy handling.
165 $values[$key]['cancel_date'] = $item['contribution_cancel_date'];
166 }
167 if (!empty($item['next_sched_contribution_date'])) {
168 // 4.4 legacy handling
169 $values[$key]['next_sched_contribution'] = $item['next_sched_contribution_date'];
170 }
171 }
172 }
173
174 if (is_array($params) && $entity && !empty($params['debug'])) {
175 if (is_string($action) && $action !== 'getfields') {
176 $apiFields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => $action] + $params);
177 }
178 elseif ($action !== 'getfields') {
179 $apiFields = civicrm_api($entity, 'getfields', ['version' => 3] + $params);
180 }
181 else {
182 $apiFields = FALSE;
183 }
184
185 $allFields = [];
186 if ($action !== 'getfields' && isset($apiFields['values']) && is_array($apiFields['values'])) {
187 $allFields = array_keys($apiFields['values']);
188 }
189 $paramFields = array_keys($params);
190 $undefined = array_diff($paramFields, $allFields, array_keys($_COOKIE), [
191 'action',
192 'entity',
193 'debug',
194 'version',
195 'check_permissions',
196 'IDS_request_uri',
197 'IDS_user_agent',
198 'return',
199 'sequential',
200 'rowCount',
201 'option_offset',
202 'option_limit',
203 'custom',
204 'option_sort',
205 'options',
206 'prettyprint',
207 ]);
208 if ($undefined) {
209 $result['undefined_fields'] = array_merge($undefined);
210 }
211 }
212
213 $result['version'] = 3;
214 if (is_array($values)) {
215 $result['count'] = (int) count($values);
216
217 // Convert value-separated strings to array
218 if ($action !== 'getfields') {
219 _civicrm_api3_separate_values($values);
220 }
221
222 if ($result['count'] == 1) {
223 list($result['id']) = array_keys($values);
224 }
225 elseif (!empty($values['id']) && is_int($values['id'])) {
226 $result['id'] = $values['id'];
227 }
228 }
229 else {
230 $result['count'] = !empty($values) ? 1 : 0;
231 }
232
233 if (is_array($values) && isset($params['sequential']) &&
234 $params['sequential'] == 1
235 ) {
236 $result['values'] = array_values($values);
237 }
238 else {
239 $result['values'] = $values;
240 }
241 if (!empty($params['options']['metadata'])) {
242 // We've made metadata an array but only supporting 'fields' atm.
243 if (in_array('fields', (array) $params['options']['metadata']) && $action !== 'getfields') {
244 $fields = civicrm_api3($entity, 'getfields', [
245 'action' => substr($action, 0, 3) == 'get' ? 'get' : 'create',
246 ]);
247 $result['metadata']['fields'] = $fields['values'];
248 }
249 }
250 // Report deprecations.
251 $deprecated = _civicrm_api3_deprecation_check($entity, $result);
252 // Always report "setvalue" action as deprecated.
253 if (!is_string($deprecated) && ($action === 'getactions' || $action === 'setvalue')) {
254 $deprecated = ((array) $deprecated) + ['setvalue' => 'The "setvalue" action is deprecated. Use "create" with an id instead.'];
255 }
256 // Always report "update" action as deprecated.
257 if (!is_string($deprecated) && ($action === 'getactions' || $action === 'update')) {
258 $deprecated = ((array) $deprecated) + ['update' => 'The "update" action is deprecated. Use "create" with an id instead.'];
259 }
260 if ($deprecated) {
261 // Metadata-level deprecations or wholesale entity deprecations.
262 if ($entity === 'Entity' || $action === 'getactions' || is_string($deprecated)) {
263 $result['deprecated'] = $deprecated;
264 }
265 // Action-specific deprecations
266 elseif (!empty($deprecated[$action])) {
267 $result['deprecated'] = $deprecated[$action];
268 }
269 }
270 return array_merge($result, $extraReturnValues);
271 }
272
273 /**
274 * Load the DAO of the entity.
275 *
276 * @param $entity
277 *
278 * @return bool
279 */
280 function _civicrm_api3_load_DAO($entity) {
281 $dao = _civicrm_api3_get_DAO($entity);
282 if (empty($dao)) {
283 return FALSE;
284 }
285 $d = new $dao();
286 return $d;
287 }
288
289 /**
290 * Return the DAO of the function or Entity.
291 *
292 * @param string $name
293 * Either a function of the api (civicrm_{entity}_create or the entity name.
294 * return the DAO name to manipulate this function
295 * eg. "civicrm_api3_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
296 *
297 * @return CRM_Core_DAO|string
298 */
299 function _civicrm_api3_get_DAO($name) {
300 if (strpos($name, 'civicrm_api3') !== FALSE) {
301 $last = strrpos($name, '_');
302 // len ('civicrm_api3_') == 13
303 $name = substr($name, 13, $last - 13);
304 }
305
306 $name = _civicrm_api_get_camel_name($name);
307
308 if ($name === 'Individual' || $name === 'Household' || $name === 'Organization') {
309 $name = 'Contact';
310 }
311
312 // hack to deal with incorrectly named BAO/DAO - see CRM-10859
313
314 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingEventQueue
315 if ($name === 'MailingEventQueue') {
316 return 'CRM_Mailing_Event_DAO_Queue';
317 }
318 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingRecipients
319 // but am not confident mailing_recipients is tested so have not tackled.
320 if ($name === 'MailingRecipients') {
321 return 'CRM_Mailing_DAO_Recipients';
322 }
323 if ($name === 'AclRole') {
324 return 'CRM_ACL_DAO_ACLEntityRole';
325 }
326 // FIXME: DAO should be renamed CRM_SMS_DAO_SmsProvider
327 // But this would impact SMS extensions so need to coordinate
328 // Probably best approach is to migrate them to use the api and decouple them from core BAOs
329 if ($name === 'SmsProvider') {
330 return 'CRM_SMS_DAO_Provider';
331 }
332 // Entity was renamed to CRM_Dedupe_DAO_DedupeRule for APIv4
333 if ($name === 'Rule') {
334 return 'CRM_Dedupe_DAO_DedupeRule';
335 }
336 // Entity was renamed to CRM_Dedupe_DAO_DedupeRuleGroup for APIv4
337 if ($name === 'RuleGroup') {
338 return 'CRM_Dedupe_DAO_DedupeRuleGroup';
339 }
340 // Entity was renamed to CRM_Dedupe_DAO_DedupeException for APIv4
341 if ($name === 'Exception') {
342 return 'CRM_Dedupe_DAO_DedupeException';
343 }
344 // FIXME: DAO names should follow CamelCase convention
345 if ($name === 'Im' || $name === 'Acl' || $name === 'Pcp') {
346 $name = strtoupper($name);
347 }
348
349 $dao = CRM_Core_DAO_AllCoreTables::getFullName($name);
350 if ($dao || !$name) {
351 return $dao;
352 }
353
354 // Really weird apis can declare their own DAO name. Not sure if this is a good idea...
355 if (file_exists("api/v3/$name.php")) {
356 include_once "api/v3/$name.php";
357 }
358
359 $daoFn = '_civicrm_api3_' . _civicrm_api_get_entity_name_from_camel($name) . '_DAO';
360 if (function_exists($daoFn)) {
361 return $daoFn();
362 }
363
364 return NULL;
365 }
366
367 /**
368 * Return the BAO name of the function or Entity.
369 *
370 * @param string $name
371 * Is either a function of the api (civicrm_{entity}_create or the entity name.
372 * return the DAO name to manipulate this function
373 * eg. "civicrm_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
374 *
375 * @return string|null
376 */
377 function _civicrm_api3_get_BAO($name) {
378 // FIXME: DAO should be renamed CRM_Badge_DAO_BadgeLayout
379 if ($name === 'PrintLabel') {
380 return 'CRM_Badge_BAO_Layout';
381 }
382 if ($name === 'Order') {
383 // Order basically maps to contribution at the top level but
384 // has enhanced access to other entities.
385 $name = 'Contribution';
386 }
387 if ($name === 'Dedupe') {
388 // Dedupe is a pseudoentity for PrevNextCache - but accessing dedupe related info
389 // not the other cache info like search results (which could in fact be in Redis or another cache engine)
390 $name = 'PrevNextCache';
391 }
392 if ($name === 'Payment') {
393 $name = 'FinancialTrxn';
394 }
395 $dao = _civicrm_api3_get_DAO($name);
396 if (!$dao) {
397 return NULL;
398 }
399 $bao = str_replace("DAO", "BAO", $dao);
400 $file = strtr($bao, '_', '/') . '.php';
401 // Check if this entity actually has a BAO. Fall back on the DAO if not.
402 return stream_resolve_include_path($file) ? $bao : $dao;
403 }
404
405 /**
406 * Recursive function to explode value-separated strings into arrays.
407 *
408 * @param $values
409 */
410 function _civicrm_api3_separate_values(&$values) {
411 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
412 foreach ($values as $key => & $value) {
413 if (is_array($value)) {
414 _civicrm_api3_separate_values($value);
415 }
416 elseif (is_string($value)) {
417 // This is to honor the way case API was originally written.
418 if ($key === 'case_type_id') {
419 $value = trim(str_replace($sp, ',', $value), ',');
420 }
421 elseif (strpos($value, $sp) !== FALSE) {
422 $value = explode($sp, trim($value, $sp));
423 }
424 }
425 }
426 }
427
428 /**
429 * This is a legacy wrapper for api_store_values.
430 *
431 * It checks suitable fields using getfields rather than DAO->fields.
432 *
433 * Getfields has handling for how to deal with unique names which dao->fields doesn't
434 *
435 * Note this is used by BAO type create functions - eg. contribution
436 *
437 * @param string $entity
438 * @param array $params
439 * @param array $values
440 */
441 function _civicrm_api3_filter_fields_for_bao($entity, &$params, &$values) {
442 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'create']);
443 $fields = $fields['values'];
444 _civicrm_api3_store_values($fields, $params, $values);
445 }
446
447 /**
448 * Store values.
449 *
450 * @param array $fields
451 * @param array $params
452 * @param array $values
453 *
454 * @return Bool
455 */
456 function _civicrm_api3_store_values(&$fields, &$params, &$values) {
457 $valueFound = FALSE;
458
459 $keys = array_intersect_key($params, $fields);
460 foreach ($keys as $name => $value) {
461 if ($name !== 'id') {
462 $values[$name] = $value;
463 $valueFound = TRUE;
464 }
465 }
466 return $valueFound;
467 }
468
469 /**
470 * Returns field names of the given entity fields.
471 *
472 * @deprecated
473 * @param array $fields
474 * Fields array to retrieve the field names for.
475 * @return array
476 */
477 function _civicrm_api3_field_names($fields) {
478 CRM_Core_Error::deprecatedFunctionWarning('array_column');
479 $result = [];
480 foreach ($fields as $key => $value) {
481 if (!empty($value['name'])) {
482 $result[] = $value['name'];
483 }
484 }
485 return $result;
486 }
487
488 /**
489 * Get function for query object api.
490 *
491 * The API supports 2 types of get request. The more complex uses the BAO query object.
492 * This is a generic function for those functions that call it
493 *
494 * At the moment only called by contact we should extend to contribution &
495 * others that use the query object. Note that this function passes permission information in.
496 * The others don't
497 *
498 * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the
499 * 2 variants call
500 *
501 * @param $entity
502 * @param array $params
503 * As passed into api get or getcount function.
504 * @param array $additional_options
505 * Array of options (so we can modify the filter).
506 * @param bool $getCount
507 * Are we just after the count.
508 * @param int $mode
509 * This basically correlates to the component.
510 * @param null|array $defaultReturnProperties
511 * Default return properties for the entity
512 * (used if return not set - but don't do that - set return!).
513 *
514 * @return array
515 * @throws API_Exception
516 */
517 function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = [], $getCount = NULL, $mode = 1, $defaultReturnProperties = NULL) {
518 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
519 // Convert id to e.g. contact_id
520 if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) {
521 $params[$lowercase_entity . '_id'] = $params['id'];
522 }
523 unset($params['id']);
524
525 $options = _civicrm_api3_get_options_from_params($params, TRUE);
526
527 $inputParams = array_merge(
528 CRM_Utils_Array::value('input_params', $options, []),
529 CRM_Utils_Array::value('input_params', $additional_options, [])
530 );
531 $returnProperties = array_merge(
532 CRM_Utils_Array::value('return', $options, []),
533 CRM_Utils_Array::value('return', $additional_options, [])
534 );
535 if (empty($returnProperties)) {
536 $returnProperties = $defaultReturnProperties;
537 }
538 if (!empty($params['check_permissions'])) {
539 // we will filter query object against getfields
540 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'get']);
541 // we need to add this in as earlier in this function 'id' was unset in favour of $entity_id
542 $fields['values'][$lowercase_entity . '_id'] = [];
543 $varsToFilter = ['returnProperties', 'inputParams'];
544 foreach ($varsToFilter as $varToFilter) {
545 if (!is_array($$varToFilter)) {
546 continue;
547 }
548 //I was going to throw an exception rather than silently filter out - but
549 //would need to diff out of exceptions arr other keys like 'options', 'return', 'api. etcetc
550 //so we are silently ignoring parts of their request
551 //$exceptionsArr = array_diff(array_keys($$varToFilter), array_keys($fields['values']));
552 $$varToFilter = array_intersect_key($$varToFilter, $fields['values']);
553 }
554 }
555 $options = array_merge($options, $additional_options);
556 $sort = $options['sort'] ?? NULL;
557 $offset = $options['offset'] ?? NULL;
558 $limit = $options['limit'] ?? NULL;
559 $smartGroupCache = $params['smartGroupCache'] ?? NULL;
560
561 if ($getCount) {
562 $limit = NULL;
563 $returnProperties = NULL;
564 }
565
566 if (substr($sort, 0, 2) == 'id') {
567 $sort = $lowercase_entity . "_" . $sort;
568 }
569
570 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
571
572 $skipPermissions = !empty($params['check_permissions']) ? 0 : 1;
573
574 list($entities) = CRM_Contact_BAO_Query::apiQuery(
575 $newParams,
576 $returnProperties,
577 NULL,
578 $sort,
579 $offset,
580 $limit,
581 $smartGroupCache,
582 $getCount,
583 $skipPermissions,
584 $mode,
585 $entity,
586 TRUE
587 );
588
589 return $entities;
590 }
591
592 /**
593 * Get dao query object based on input params.
594 *
595 * Ideally this would be merged with _civicrm_get_using_query_object but we need to resolve differences in what the
596 * 2 variants call
597 *
598 * @param array $params
599 * @param string $mode
600 * @param string $entity
601 *
602 * @return array
603 * [CRM_Core_DAO|CRM_Contact_BAO_Query]
604 */
605 function _civicrm_api3_get_query_object($params, $mode, $entity) {
606 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
607 $sort = $options['sort'] ?? NULL;
608 $offset = $options['offset'] ?? NULL;
609 $rowCount = $options['limit'] ?? NULL;
610 $inputParams = CRM_Utils_Array::value('input_params', $options, []);
611 $returnProperties = $options['return'] ?? NULL;
612 if (empty($returnProperties)) {
613 $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode);
614 }
615
616 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams, 0, FALSE, $entity);
617 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
618 FALSE, FALSE, $mode,
619 empty($params['check_permissions']),
620 TRUE, TRUE, NULL, 'AND', 'NULL', TRUE
621 );
622 list($select, $from, $where, $having) = $query->query();
623
624 $sql = "$select $from $where $having";
625
626 if (!empty($sort)) {
627 $sort = CRM_Utils_Type::escape($sort, 'MysqlOrderBy');
628 $sql .= " ORDER BY $sort ";
629 }
630 if (!empty($rowCount)) {
631 $sql .= " LIMIT $offset, $rowCount ";
632 }
633 $dao = CRM_Core_DAO::executeQuery($sql);
634 return [$dao, $query];
635 }
636
637 /**
638 * Function transfers the filters being passed into the DAO onto the params object.
639 *
640 * @deprecated DAO based retrieval is being phased out.
641 *
642 * @param CRM_Core_DAO $dao
643 * @param array $params
644 * @param bool $unique
645 * @param array $extraSql
646 * API specific queries eg for event isCurrent would be converted to
647 * $extraSql['where'] = array('civicrm_event' => array('(start_date >= CURDATE() || end_date >= CURDATE())'));
648 *
649 * @throws API_Exception
650 * @throws Exception
651 */
652 function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $extraSql = []) {
653 $entity = _civicrm_api_get_entity_name_from_dao($dao);
654 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
655 if (!empty($params[$lowercase_entity . "_id"]) && empty($params['id'])) {
656 //if entity_id is set then treat it as ID (will be overridden by id if set)
657 $params['id'] = $params[$lowercase_entity . "_id"];
658 }
659 $allfields = _civicrm_api3_build_fields_array($dao, $unique);
660 $fields = array_intersect(array_keys($allfields), array_keys($params));
661
662 $options = _civicrm_api3_get_options_from_params($params);
663 //apply options like sort
664 _civicrm_api3_apply_options_to_dao($params, $dao, $entity);
665
666 //accept filters like filter.activity_date_time_high
667 // std is now 'filters' => ..
668 if (strstr(implode(',', array_keys($params)), 'filter')) {
669 if (isset($params['filters']) && is_array($params['filters'])) {
670 foreach ($params['filters'] as $paramkey => $paramvalue) {
671 _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao);
672 }
673 }
674 else {
675 foreach ($params as $paramkey => $paramvalue) {
676 if (strstr($paramkey, 'filter')) {
677 _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao);
678 }
679 }
680 }
681 }
682 if (!$fields) {
683 $fields = [];
684 }
685
686 foreach ($fields as $field) {
687 if (is_array($params[$field])) {
688 //get the actual fieldname from db
689 $fieldName = $allfields[$field]['name'];
690 $where = CRM_Core_DAO::createSqlFilter($fieldName, $params[$field], 'String');
691 if (!empty($where)) {
692 $dao->whereAdd($where);
693 }
694 }
695 else {
696 if ($unique) {
697 $daoFieldName = $allfields[$field]['name'];
698 if (empty($daoFieldName)) {
699 throw new API_Exception("Failed to determine field name for \"$field\"");
700 }
701 $dao->{$daoFieldName} = $params[$field];
702 }
703 else {
704 $dao->$field = $params[$field];
705 }
706 }
707 }
708 if (!empty($extraSql['where'])) {
709 foreach ($extraSql['where'] as $table => $sqlWhere) {
710 foreach ($sqlWhere as $where) {
711 $dao->whereAdd($where);
712 }
713 }
714 }
715 if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) {
716 $dao->selectAdd();
717 // Ensure 'id' is included.
718 $options['return']['id'] = TRUE;
719 $allfields = _civicrm_api3_get_unique_name_array($dao);
720 $returnMatched = array_intersect(array_keys($options['return']), $allfields);
721 foreach ($returnMatched as $returnValue) {
722 $dao->selectAdd($returnValue);
723 }
724
725 // Not already matched on the field names.
726 $unmatchedFields = array_diff(
727 array_keys($options['return']),
728 $returnMatched
729 );
730
731 $returnUniqueMatched = array_intersect(
732 $unmatchedFields,
733 // But a match for the field keys.
734 array_flip($allfields)
735 );
736 foreach ($returnUniqueMatched as $uniqueVal) {
737 $dao->selectAdd($allfields[$uniqueVal]);
738 }
739 }
740 $dao->setApiFilter($params);
741 }
742
743 /**
744 * Apply filters (e.g. high, low) to DAO object (prior to find).
745 *
746 * @param string $filterField
747 * Field name of filter.
748 * @param string $filterValue
749 * Field value of filter.
750 * @param object $dao
751 * DAO object.
752 */
753 function _civicrm_api3_apply_filters_to_dao($filterField, $filterValue, &$dao) {
754 if (strstr($filterField, 'high')) {
755 $fieldName = substr($filterField, 0, -5);
756 $dao->whereAdd("($fieldName <= $filterValue )");
757 }
758 if (strstr($filterField, 'low')) {
759 $fieldName = substr($filterField, 0, -4);
760 $dao->whereAdd("($fieldName >= $filterValue )");
761 }
762 if ($filterField == 'is_current' && $filterValue == 1) {
763 $todayStart = date('Ymd000000', strtotime('now'));
764 $todayEnd = date('Ymd235959', strtotime('now'));
765 $dao->whereAdd("(start_date <= '$todayStart' OR start_date IS NULL) AND (end_date >= '$todayEnd' OR end_date IS NULL)");
766 if (property_exists($dao, 'is_active')) {
767 $dao->whereAdd('is_active = 1');
768 }
769 }
770 }
771
772 /**
773 * Get sort, limit etc options from the params - supporting old & new formats.
774 *
775 * Get returnProperties for legacy
776 *
777 * @param array $params
778 * Params array as passed into civicrm_api.
779 * @param bool $queryObject
780 * Is this supporting a queryObject api (e.g contact) - if so we support more options.
781 * for legacy report & return a unique fields array
782 *
783 * @param string $entity
784 * @param string $action
785 *
786 * @throws API_Exception
787 * @return array
788 * options extracted from params
789 */
790 function _civicrm_api3_get_options_from_params($params, $queryObject = FALSE, $entity = '', $action = '') {
791 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
792 $is_count = FALSE;
793
794 // dear PHP thought it would be a good idea to transform a.b into a_b in the get/post
795 $sort = $params['option_sort'] ?? $params['option.sort'] ?? $params['sort'] ?? 0;
796 $offset = $params['option_offset'] ?? $params['option.offset'] ?? $params['offset'] ?? 0;
797
798 $limit = CRM_Utils_Array::value('rowCount', $params, 25);
799 $limit = CRM_Utils_Array::value('option.limit', $params, $limit);
800 $limit = CRM_Utils_Array::value('option_limit', $params, $limit);
801
802 if (isset($params['options']) && is_array($params['options'])) {
803 // is count is set by generic getcount not user
804 $is_count = $params['options']['is_count'] ?? FALSE;
805 $offset = $params['options']['offset'] ?? $offset;
806 $limit = CRM_Utils_Array::value('limit', $params['options'], $limit);
807 $sort = $params['options']['sort'] ?? $sort;
808 }
809
810 $returnProperties = [];
811 // handle the format return =sort_name,display_name...
812 if (array_key_exists('return', $params)) {
813 if (is_array($params['return'])) {
814 $returnProperties = array_fill_keys($params['return'], 1);
815 }
816 else {
817 $returnProperties = explode(',', str_replace(' ', '', $params['return']));
818 $returnProperties = array_fill_keys($returnProperties, 1);
819 }
820 }
821 if ($entity && $action == 'get') {
822 if (!empty($returnProperties['id'])) {
823 $returnProperties[$lowercase_entity . '_id'] = 1;
824 unset($returnProperties['id']);
825 }
826 }
827
828 $options = [
829 'offset' => CRM_Utils_Rule::integer($offset) ? $offset : NULL,
830 'limit' => (!$is_count && CRM_Utils_Rule::integer($limit)) ? $limit : NULL,
831 'is_count' => $is_count,
832 'return' => !empty($returnProperties) ? $returnProperties : [],
833 ];
834
835 $finalSort = [];
836 $options['sort'] = NULL;
837 if (!empty($sort)) {
838 if (!is_array($sort)) {
839 $sort = array_map('trim', explode(',', $sort));
840 }
841 foreach ($sort as $s) {
842 if ($s === '(1)' || CRM_Utils_Rule::mysqlOrderBy($s)) {
843 if ($entity && $action === 'get') {
844 switch (trim(strtolower($s))) {
845 case 'id':
846 case 'id desc':
847 case 'id asc':
848 $s = str_replace('id', $lowercase_entity . '_id', $s);
849 }
850 }
851 $finalSort[] = $s;
852 }
853 else {
854 throw new API_Exception("Unknown field specified for sort. Cannot order by '$s'");
855 }
856 }
857 $options['sort'] = implode(', ', $finalSort);
858 }
859
860 if ($options['sort'] && stristr($options['sort'], 'SELECT')) {
861 throw new API_Exception('invalid string in sort options');
862 }
863
864 if (!$queryObject) {
865 return $options;
866 }
867 //here comes the legacy support for $returnProperties, $inputParams e.g for contat_get
868 // if the query object is being used this should be used
869 $inputParams = [];
870 $legacyreturnProperties = [];
871 $otherVars = [
872 'sort', 'offset', 'rowCount', 'options', 'return',
873 'version', 'prettyprint', 'check_permissions', 'sequential',
874 ];
875 foreach ($params as $n => $v) {
876 if (substr($n, 0, 7) === 'return.') {
877 $legacyreturnProperties[substr($n, 7)] = $v;
878 }
879 elseif ($n === 'id') {
880 $inputParams[$lowercase_entity . '_id'] = $v;
881 }
882 elseif (!in_array($n, $otherVars)) {
883 $inputParams[$n] = $v;
884 if ($v && !is_array($v) && stristr($v, 'SELECT')) {
885 throw new API_Exception('invalid string');
886 }
887 }
888 }
889 $options['return'] = array_merge($returnProperties, $legacyreturnProperties);
890 $options['input_params'] = $inputParams;
891 return $options;
892 }
893
894 /**
895 * Apply options (e.g. sort, limit, order by) to DAO object (prior to find).
896 *
897 * @param array $params
898 * Params array as passed into civicrm_api.
899 * @param object $dao
900 * DAO object.
901 * @param $entity
902 *
903 * @throws \API_Exception
904 * @throws \CRM_Core_Exception
905 */
906 function _civicrm_api3_apply_options_to_dao(&$params, &$dao, $entity) {
907
908 $options = _civicrm_api3_get_options_from_params($params, FALSE, $entity);
909 if (!$options['is_count']) {
910 if (!empty($options['limit'])) {
911 $dao->limit((int) $options['offset'], (int) $options['limit']);
912 }
913 if (!empty($options['sort'])) {
914 $options['sort'] = CRM_Utils_Type::escape($options['sort'], 'MysqlOrderBy');
915 $dao->orderBy($options['sort']);
916 }
917 }
918 }
919
920 /**
921 * Build fields array.
922 *
923 * This is the array of fields as it relates to the given DAO
924 * returns unique fields as keys by default but if set but can return by DB fields
925 *
926 * @param CRM_Core_DAO $bao
927 * @param bool $unique
928 *
929 * @return array
930 */
931 function _civicrm_api3_build_fields_array(&$bao, $unique = TRUE) {
932 $fields = $bao->fields();
933 if ($unique) {
934 if (empty($fields['id'])) {
935 $lowercase_entity = _civicrm_api_get_entity_name_from_camel(_civicrm_api_get_entity_name_from_dao($bao));
936 if (isset($fields[$lowercase_entity . '_id'])) {
937 $fields['id'] = $fields[$lowercase_entity . '_id'];
938 unset($fields[$lowercase_entity . '_id']);
939 }
940 }
941 return $fields;
942 }
943
944 foreach ($fields as $field) {
945 $dbFields[$field['name']] = $field;
946 }
947 return $dbFields;
948 }
949
950 /**
951 * Build fields array.
952 *
953 * This is the array of fields as it relates to the given DAO
954 * returns unique fields as keys by default but if set but can return by DB fields
955 *
956 * @param CRM_Core_DAO $bao
957 *
958 * @return array
959 */
960 function _civicrm_api3_get_unique_name_array(&$bao) {
961 $fields = $bao->fields();
962 foreach ($fields as $field => $values) {
963 $uniqueFields[$field] = CRM_Utils_Array::value('name', $values, $field);
964 }
965 return $uniqueFields;
966 }
967
968 /**
969 * Converts an DAO object to an array.
970 *
971 * @param CRM_Core_DAO $dao
972 * Object to convert.
973 * @param array $params
974 * @param bool $uniqueFields
975 * @param string $entity
976 * @param bool $autoFind
977 *
978 * @return array
979 *
980 * @throws \API_Exception
981 *
982 * @deprecated - DAO based retrieval is being phased out.
983 *
984 */
985 function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, $entity = "", $autoFind = TRUE) {
986 $result = [];
987 if (isset($params['options']) && !empty($params['options']['is_count'])) {
988 return $dao->count();
989 }
990 if (empty($dao)) {
991 return [];
992 }
993 if ($autoFind && !$dao->find()) {
994 return [];
995 }
996
997 if (isset($dao->count)) {
998 return $dao->count;
999 }
1000
1001 $fields = array_keys(_civicrm_api3_build_fields_array($dao, FALSE));
1002 while ($dao->fetch()) {
1003 $tmp = [];
1004 foreach ($fields as $key) {
1005 if (property_exists($dao, $key)) {
1006 // not sure on that one
1007 if ($dao->$key !== NULL) {
1008 $tmp[$key] = $dao->$key;
1009 }
1010 }
1011 }
1012 $result[$dao->id] = $tmp;
1013
1014 if (_civicrm_api3_custom_fields_are_required($entity, $params)) {
1015 _civicrm_api3_custom_data_get($result[$dao->id], $params['check_permissions'], $entity, $dao->id);
1016 }
1017 }
1018
1019 return $result;
1020 }
1021
1022 /**
1023 * Determine if custom fields need to be retrieved.
1024 *
1025 * We currently retrieve all custom fields or none at this level so if we know the entity
1026 * && it can take custom fields & there is the string 'custom' in their return request we get them all, they are filtered on the way out
1027 *
1028 * @param string $entity
1029 * Entity name in CamelCase.
1030 * @param array $params
1031 *
1032 * @return bool
1033 * @throws \API_Exception
1034 *
1035 * @todo filter so only required fields are queried
1036 */
1037 function _civicrm_api3_custom_fields_are_required($entity, $params) {
1038 if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery::$extendsMap)) {
1039 return FALSE;
1040 }
1041 $options = _civicrm_api3_get_options_from_params($params);
1042 // We check for possibility of 'custom' => 1 as well as specific custom fields.
1043 $returnString = implode('', $options['return']) . implode('', array_keys($options['return']));
1044 if (stristr($returnString, 'custom')) {
1045 return TRUE;
1046 }
1047 }
1048
1049 /**
1050 * Converts an object to an array.
1051 *
1052 * @param object $dao
1053 * (reference) object to convert.
1054 * @param array $values
1055 * (reference) array.
1056 * @param array|bool $uniqueFields
1057 */
1058 function _civicrm_api3_object_to_array(&$dao, &$values, $uniqueFields = FALSE) {
1059
1060 $fields = _civicrm_api3_build_fields_array($dao, $uniqueFields);
1061 foreach ($fields as $key => $value) {
1062 if (property_exists($dao, $key)) {
1063 $values[$key] = $dao->$key ?? NULL;
1064 }
1065 }
1066 }
1067
1068 /**
1069 * Wrapper for _civicrm_object_to_array when api supports unique fields.
1070 *
1071 * @param $dao
1072 * @param $values
1073 *
1074 * @return array
1075 */
1076 function _civicrm_api3_object_to_array_unique_fields(&$dao, &$values) {
1077 return _civicrm_api3_object_to_array($dao, $values, TRUE);
1078 }
1079
1080 /**
1081 * Format custom parameters.
1082 *
1083 * @param array $params
1084 * @param array $values
1085 * @param string $extends
1086 * Entity that this custom field extends (e.g. contribution, event, contact).
1087 * @param string $entityId
1088 * ID of entity per $extends.
1089 */
1090 function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) {
1091 if (!empty($params['custom']) && empty($params['check_permissions'])) {
1092 // The Import class does the formatting first - ideally it wouldn't but this early return
1093 // provides transitional support.
1094 return;
1095 }
1096 $values['custom'] = [];
1097 $checkCheckBoxField = FALSE;
1098 $entity = $extends;
1099 if (in_array($extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) {
1100 $entity = 'Contact';
1101 }
1102
1103 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'create']);
1104 if (!$fields['is_error']) {
1105 // not sure if fields could be error - maybe change to using civicrm_api3 wrapper later - this is conservative
1106 $fields = $fields['values'];
1107 $checkCheckBoxField = TRUE;
1108 }
1109
1110 foreach ($params as $key => $value) {
1111 list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($key, TRUE);
1112 if ($customFieldID && (!is_null($value))) {
1113 if ($checkCheckBoxField && !empty($fields['custom_' . $customFieldID]) && $fields['custom_' . $customFieldID]['html_type'] == 'CheckBox') {
1114 formatCheckBoxField($value, 'custom_' . $customFieldID, $entity);
1115 }
1116
1117 CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $values['custom'],
1118 $value, $extends, $customValueID, $entityId, FALSE, !empty($params['check_permissions']), TRUE
1119 );
1120 }
1121 }
1122 }
1123
1124 /**
1125 * Format parameters for create action.
1126 *
1127 * @param array $params
1128 * @param $entity
1129 */
1130 function _civicrm_api3_format_params_for_create(&$params, $entity) {
1131 $nonGenericEntities = ['Contact', 'Individual', 'Household', 'Organization'];
1132
1133 $customFieldEntities = array_diff_key(CRM_Core_SelectValues::customGroupExtends(), array_fill_keys($nonGenericEntities, 1));
1134 if (!array_key_exists($entity, $customFieldEntities)) {
1135 return;
1136 }
1137 $values = [];
1138 _civicrm_api3_custom_format_params($params, $values, $entity);
1139 $params = array_merge($params, $values);
1140 }
1141
1142 /**
1143 * We can't rely on downstream to add separators to checkboxes so we'll check here.
1144 *
1145 * We should look at pushing to BAO function
1146 * and / or validate function but this is a safe place for now as it has massive test coverage & we can keep the change very specific
1147 * note that this is specifically tested in the GRANT api test case so later refactoring should use that as a checking point
1148 *
1149 * We will only alter the value if we are sure that changing it will make it correct - if it appears wrong but does not appear to have a clear fix we
1150 * don't touch - lots of very cautious code in here
1151 *
1152 * The resulting array should look like
1153 * array(
1154 * 'key' => 1,
1155 * 'key1' => 1,
1156 * );
1157 *
1158 * OR one or more keys wrapped in a CRM_Core_DAO::VALUE_SEPARATOR - either it accepted by the receiving function
1159 *
1160 * @todo - we are probably skipping handling disabled options as presumably getoptions is not giving us them. This should be non-regressive but might
1161 * be fixed in future
1162 *
1163 * @param mixed $checkboxFieldValue
1164 * @param string $customFieldLabel
1165 * @param string $entity
1166 */
1167 function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) {
1168
1169 if (is_string($checkboxFieldValue) && stristr($checkboxFieldValue, CRM_Core_DAO::VALUE_SEPARATOR)) {
1170 // We can assume it's pre-formatted.
1171 return;
1172 }
1173 $options = civicrm_api($entity, 'getoptions', ['field' => $customFieldLabel, 'version' => 3]);
1174 if (!empty($options['is_error'])) {
1175 // The check is precautionary - can probably be removed later.
1176 return;
1177 }
1178
1179 $options = $options['values'];
1180 $validValue = TRUE;
1181 if (is_array($checkboxFieldValue)) {
1182 foreach ($checkboxFieldValue as $key => $value) {
1183 if (!array_key_exists($key, $options)) {
1184 $validValue = FALSE;
1185 }
1186 }
1187 if ($validValue) {
1188 // we have been passed an array that is already in the 'odd' custom field format
1189 return;
1190 }
1191 }
1192
1193 // so we either have an array that is not keyed by the value or we have a string that doesn't hold separators
1194 // if the array only has one item we'll treat it like any other string
1195 if (is_array($checkboxFieldValue) && count($checkboxFieldValue) == 1) {
1196 $possibleValue = reset($checkboxFieldValue);
1197 }
1198 if (is_string($checkboxFieldValue)) {
1199 $possibleValue = $checkboxFieldValue;
1200 }
1201 if (isset($possibleValue) && array_key_exists($possibleValue, $options)) {
1202 $checkboxFieldValue = CRM_Core_DAO::VALUE_SEPARATOR . $possibleValue . CRM_Core_DAO::VALUE_SEPARATOR;
1203 return;
1204 }
1205 elseif (is_array($checkboxFieldValue)) {
1206 // so this time around we are considering the values in the array
1207 $possibleValues = $checkboxFieldValue;
1208 $formatValue = TRUE;
1209 }
1210 elseif (stristr($checkboxFieldValue, ',')) {
1211 $formatValue = TRUE;
1212 //lets see if we should separate it - we do this near the end so we
1213 // ensure we have already checked that the comma is not part of a legitimate match
1214 // and of course, we don't make any changes if we don't now have matches
1215 $possibleValues = explode(',', $checkboxFieldValue);
1216 }
1217 else {
1218 // run out of ideas as to what the format might be - if it's a string it doesn't match with or without the ','
1219 return;
1220 }
1221
1222 foreach ($possibleValues as $index => $possibleValue) {
1223 if (array_key_exists($possibleValue, $options)) {
1224 // do nothing - we will leave formatValue set to true unless another value is not found (which would cause us to ignore the whole value set)
1225 }
1226 elseif (array_key_exists(trim($possibleValue), $options)) {
1227 $possibleValues[$index] = trim($possibleValue);
1228 }
1229 else {
1230 $formatValue = FALSE;
1231 }
1232 }
1233 if ($formatValue) {
1234 $checkboxFieldValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $possibleValues) . CRM_Core_DAO::VALUE_SEPARATOR;
1235 }
1236 }
1237
1238 /**
1239 * Function to do a 'standard' api get - when the api is only doing a $bao->find then use this.
1240 *
1241 * @param string|CRM_Core_DAO $bao_name
1242 * Name of BAO.
1243 * @param array $params
1244 * Params from api.
1245 * @param bool $returnAsSuccess
1246 * Return in api success format.
1247 * @param string $entity
1248 * @param CRM_Utils_SQL_Select|NULL $sql
1249 * Extra SQL bits to add to the query. For filtering current events, this might be:
1250 * CRM_Utils_SQL_Select::fragment()->where('(start_date >= CURDATE() || end_date >= CURDATE())');
1251 * @param bool $uniqueFields
1252 * Should unique field names be returned (for backward compatibility)
1253 *
1254 * @return array
1255 */
1256 function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) {
1257 $entity = $entity ?: CRM_Core_DAO_AllCoreTables::getBriefName($bao_name);
1258 $options = _civicrm_api3_get_options_from_params($params);
1259
1260 // Skip query if table doesn't exist yet due to pending upgrade
1261 if (!$bao_name::tableHasBeenAdded()) {
1262 \Civi::log()->warning("Could not read from {$entity} before table has been added. Upgrade required.", ['civi.tag' => 'upgrade_needed']);
1263 $result = [];
1264 }
1265 else {
1266 $query = new \Civi\API\Api3SelectQuery($entity, $params['check_permissions'] ?? FALSE);
1267 $query->where = $params;
1268 if ($options['is_count']) {
1269 $query->select = ['count_rows'];
1270 }
1271 else {
1272 $query->select = array_keys(array_filter($options['return']));
1273 $query->orderBy = $options['sort'];
1274 $query->isFillUniqueFields = $uniqueFields;
1275 }
1276 $query->limit = $options['limit'];
1277 $query->offset = $options['offset'];
1278 $query->merge($sql);
1279 $result = $query->run();
1280 }
1281
1282 if ($returnAsSuccess) {
1283 return civicrm_api3_create_success($result, $params, $entity, 'get');
1284 }
1285 return $result;
1286 }
1287
1288 /**
1289 * Function to do a 'standard' api create - when the api is only doing a $bao::create then use this.
1290 *
1291 * @param string $bao_name
1292 * Name of BAO Class.
1293 * @param array $params
1294 * Parameters passed into the api call.
1295 * @param string $entity
1296 * Entity - pass in if entity is non-standard & required $ids array.
1297 *
1298 * @throws API_Exception
1299 * @throws \Civi\API\Exception\UnauthorizedException
1300 * @return array
1301 */
1302 function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
1303 _civicrm_api3_check_edit_permissions($bao_name, $params);
1304 _civicrm_api3_format_params_for_create($params, $entity);
1305 $args = array(&$params);
1306 if ($entity) {
1307 $ids = [$entity => CRM_Utils_Array::value('id', $params)];
1308 $args[] = &$ids;
1309 }
1310
1311 if (method_exists($bao_name, 'create')) {
1312 $fct = 'create';
1313 $fct_name = $bao_name . '::' . $fct;
1314 $bao = call_user_func_array([$bao_name, $fct], $args);
1315 }
1316 elseif (method_exists($bao_name, 'add')) {
1317 $fct = 'add';
1318 $fct_name = $bao_name . '::' . $fct;
1319 $bao = call_user_func_array([$bao_name, $fct], $args);
1320 }
1321 else {
1322 $fct_name = '_civicrm_api3_basic_create_fallback';
1323 $bao = _civicrm_api3_basic_create_fallback($bao_name, $params);
1324 }
1325
1326 if (is_null($bao)) {
1327 return civicrm_api3_create_error('Entity not created (' . $fct_name . ')');
1328 }
1329 elseif (is_a($bao, 'CRM_Core_Error')) {
1330 //some weird circular thing means the error takes itself as an argument
1331 $msg = $bao->getMessages($bao);
1332 // the api deals with entities on a one-by-one basis. However, the contribution bao pushes entities
1333 // onto the error object - presumably because the contribution import is not handling multiple errors correctly
1334 // so we need to reset the error object here to avoid getting concatenated errors
1335 //@todo - the mulitple error handling should be moved out of the contribution object to the import / multiple entity processes
1336 CRM_Core_Error::singleton()->reset();
1337 throw new API_Exception($msg);
1338 }
1339 else {
1340 // If we have custom fields the BAO may have taken care of it or we may have to.
1341 // $extendsMap provides a pretty good hard-coded list of BAOs that take care of the custom data.
1342 if (isset($params['custom']) && empty(CRM_Core_BAO_CustomQuery::$extendsMap[$entity])) {
1343 CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id);
1344 }
1345 $values = [];
1346 _civicrm_api3_object_to_array($bao, $values[$bao->id]);
1347 return civicrm_api3_create_success($values, $params, $entity, 'create', $bao);
1348 }
1349 }
1350
1351 /**
1352 * For BAO's which don't have a create() or add() functions, use this fallback implementation.
1353 *
1354 * @param string|CRM_Core_DAO $bao_name
1355 * @param array $params
1356 *
1357 * @throws API_Exception
1358 *
1359 * @return CRM_Core_DAO|NULL
1360 * An instance of the BAO
1361 */
1362 function _civicrm_api3_basic_create_fallback($bao_name, $params) {
1363 return $bao_name::writeRecord($params);
1364 }
1365
1366 /**
1367 * Function to do a 'standard' api del.
1368 *
1369 * When the api is only doing a $bao::del then use this if api::del doesn't exist it will try DAO delete method.
1370 *
1371 * @param string|CRM_Core_DAO $bao_name
1372 * @param array $params
1373 *
1374 * @return array
1375 * API result array
1376 *
1377 * @throws API_Exception
1378 * @throws \Civi\API\Exception\UnauthorizedException
1379 * @throws \CiviCRM_API3_Exception
1380 */
1381 function _civicrm_api3_basic_delete($bao_name, &$params) {
1382 civicrm_api3_verify_mandatory($params, NULL, ['id']);
1383 _civicrm_api3_check_edit_permissions($bao_name, ['id' => $params['id']]);
1384 if (method_exists($bao_name, 'del')) {
1385 $args = [&$params['id']];
1386 $dao = new $bao_name();
1387 $dao->id = $params['id'];
1388 if ($dao->find()) {
1389 $bao = call_user_func_array([$bao_name, 'del'], $args);
1390 if ($bao !== FALSE) {
1391 return civicrm_api3_create_success();
1392 }
1393 throw new API_Exception('Could not delete entity id ' . $params['id']);
1394 }
1395 throw new API_Exception('Could not delete entity id ' . $params['id']);
1396 }
1397 else {
1398 $bao_name::deleteRecord($params);
1399 return civicrm_api3_create_success();
1400 }
1401 }
1402
1403 /**
1404 * Get custom data for the given entity & Add it to the returnArray.
1405 *
1406 * This looks like 'custom_123' = 'custom string' AND
1407 * 'custom_123_1' = 'custom string'
1408 * Where 123 is field value & 1 is the id within the custom group data table (value ID)
1409 *
1410 * @param array $returnArray
1411 * Array to append custom data too - generally $result[4] where 4 is the entity id.
1412 * @param $checkPermission
1413 * @param string $entity
1414 * E.g membership, event.
1415 * @param int $entity_id
1416 * @param int $groupID
1417 * Per CRM_Core_BAO_CustomGroup::getTree.
1418 * @param int $subType
1419 * E.g. membership_type_id where custom data doesn't apply to all membership types.
1420 * @param string $subName
1421 * Subtype of entity.
1422 *
1423 * @throws \CRM_Core_Exception
1424 */
1425 function _civicrm_api3_custom_data_get(&$returnArray, $checkPermission, $entity, $entity_id, $groupID = NULL, $subType = NULL, $subName = NULL) {
1426 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entity,
1427 NULL,
1428 $entity_id,
1429 $groupID,
1430 NULL,
1431 $subName,
1432 TRUE,
1433 NULL,
1434 TRUE,
1435 $checkPermission ? CRM_Core_Permission::VIEW : FALSE
1436 );
1437 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1);
1438 $customValues = [];
1439 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $customValues);
1440 $fieldInfo = [];
1441 foreach ($groupTree as $set) {
1442 $fieldInfo += $set['fields'];
1443 }
1444 if (!empty($customValues)) {
1445 foreach ($customValues as $key => $val) {
1446 // per standard - return custom_fieldID
1447 $id = CRM_Core_BAO_CustomField::getKeyID($key);
1448 $returnArray['custom_' . $id] = $val;
1449
1450 //not standard - but some api did this so guess we should keep - cheap as chips
1451 $returnArray[$key] = $val;
1452
1453 // Shim to restore legacy behavior of ContactReference custom fields
1454 if (!empty($fieldInfo[$id]) && $fieldInfo[$id]['data_type'] === 'ContactReference') {
1455 $returnArray['custom_' . $id . '_id'] = $returnArray[$key . '_id'] = $val;
1456 $returnArray['custom_' . $id] = $returnArray[$key] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $val, 'sort_name');
1457 }
1458 }
1459 }
1460 }
1461
1462 /**
1463 * Used by the Validate API.
1464 *
1465 * @param string $entity
1466 * @param string $action
1467 * @param array $params
1468 *
1469 * @return array $errors
1470 * @throws \CiviCRM_API3_Exception
1471 */
1472 function _civicrm_api3_validate($entity, $action, $params) {
1473 $errors = [];
1474 $fields = civicrm_api3($entity, 'getfields', ['sequential' => 1, 'api_action' => $action]);
1475 $fields = $fields['values'];
1476
1477 // Check for required fields.
1478 foreach ($fields as $values) {
1479 if (!empty($values['api.required']) && empty($params[$values['name']])) {
1480 $errors[$values['name']] = [
1481 'message' => 'Mandatory key(s) missing from params array: ' . $values['name'],
1482 'code' => 'mandatory_missing',
1483 ];
1484 }
1485 }
1486
1487 // Select only the fields which have been input as a param.
1488 $finalfields = [];
1489 foreach ($fields as $values) {
1490 if (array_key_exists($values['name'], $params)) {
1491 $finalfields[] = $values;
1492 }
1493 }
1494
1495 // This derives heavily from the function "_civicrm_api3_validate_fields".
1496 // However, the difference is that try-catch blocks are nested in the loop, making it
1497 // possible for us to get all errors in one go.
1498 foreach ($finalfields as $fieldInfo) {
1499 $fieldName = $fieldInfo['name'];
1500 try {
1501 _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params, $action);
1502 }
1503 catch (Exception $e) {
1504 $errors[$fieldName] = [
1505 'message' => $e->getMessage(),
1506 'code' => 'incorrect_value',
1507 ];
1508 }
1509 }
1510
1511 return [$errors];
1512 }
1513
1514 /**
1515 * Used by the Validate API.
1516 * @param $fieldName
1517 * @param array $fieldInfo
1518 * @param string $entity
1519 * @param array $params
1520 * @param string $action
1521 *
1522 * @throws API_Exception
1523 * @throws Exception
1524 */
1525 function _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params, $action) {
1526 switch (CRM_Utils_Array::value('type', $fieldInfo)) {
1527 case CRM_Utils_Type::T_INT:
1528 _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
1529 break;
1530
1531 case CRM_Utils_Type::T_DATE:
1532 case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
1533 case CRM_Utils_Type::T_TIMESTAMP:
1534 //field is of type date or datetime
1535 _civicrm_api3_validate_date($params, $fieldName, $fieldInfo);
1536 break;
1537
1538 case CRM_Utils_Type::T_TEXT:
1539 case CRM_Utils_Type::T_STRING:
1540 _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity, $action);
1541 break;
1542
1543 case CRM_Utils_Type::T_MONEY:
1544 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1545
1546 foreach ((array) $fieldValue as $fieldvalue) {
1547 if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
1548 throw new Exception($fieldName . ' is not a valid amount: ' . $params[$fieldName]);
1549 }
1550 }
1551 break;
1552 }
1553 }
1554
1555 /**
1556 * Validate fields being passed into API.
1557 *
1558 * This function relies on the getFields function working accurately
1559 * for the given API.
1560 *
1561 * As of writing only date was implemented.
1562 *
1563 * @param string $entity
1564 * @param string $action
1565 * @param array $params
1566 * -.
1567 * @param array $fields
1568 * Response from getfields all variables are the same as per civicrm_api.
1569 *
1570 * @throws Exception
1571 */
1572 function _civicrm_api3_validate_fields($entity, $action, &$params, $fields) {
1573 //CRM-15792 handle datetime for custom fields below code handles chain api call
1574 $chainApikeys = array_flip(preg_grep("/^api./", array_keys($params)));
1575 if (!empty($chainApikeys) && is_array($chainApikeys)) {
1576 foreach ($chainApikeys as $key => $value) {
1577 if (is_array($params[$key])) {
1578 $chainApiParams = array_intersect_key($fields, $params[$key]);
1579 $customFields = array_fill_keys(array_keys($params[$key]), $key);
1580 }
1581 }
1582 }
1583 $fields = array_intersect_key($fields, $params);
1584 if (!empty($chainApiParams)) {
1585 $fields = array_merge($fields, $chainApiParams);
1586 }
1587 foreach ($fields as $fieldName => $fieldInfo) {
1588 switch (CRM_Utils_Array::value('type', $fieldInfo)) {
1589 case CRM_Utils_Type::T_INT:
1590 //field is of type integer
1591 _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
1592 break;
1593
1594 case CRM_Utils_Type::T_DATE:
1595 case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
1596 case CRM_Utils_Type::T_TIMESTAMP:
1597 //field is of type date or datetime
1598 if (!empty($customFields) && array_key_exists($fieldName, $customFields)) {
1599 $dateParams = &$params[$customFields[$fieldName]];
1600 }
1601 else {
1602 $dateParams = &$params;
1603 }
1604 _civicrm_api3_validate_date($dateParams, $fieldName, $fieldInfo);
1605 break;
1606
1607 case CRM_Utils_Type::T_TEXT:
1608 case CRM_Utils_Type::T_STRING:
1609 _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity, $action);
1610 break;
1611
1612 case CRM_Utils_Type::T_MONEY:
1613 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1614 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1615 break;
1616 }
1617 foreach ((array) $fieldValue as $fieldvalue) {
1618 if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
1619 throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]);
1620 }
1621 }
1622 break;
1623 }
1624 }
1625 }
1626
1627 /**
1628 * Validate foreign key values of fields being passed into API.
1629 *
1630 * This function relies on the getFields function working accurately
1631 * for the given API.
1632 *
1633 * @param string $entity
1634 * @param string $action
1635 * @param array $params
1636 *
1637 * @param array $fields
1638 * Response from getfields all variables are the same as per civicrm_api.
1639 *
1640 * @throws Exception
1641 */
1642 function _civicrm_api3_validate_foreign_keys($entity, $action, &$params, $fields) {
1643 // intensive checks - usually only called after DB level fail
1644 foreach ($fields as $fieldName => $fieldInfo) {
1645 if (!empty($fieldInfo['FKClassName'])) {
1646 if (!empty($params[$fieldName])) {
1647 foreach ((array) $params[$fieldName] as $fieldValue) {
1648 _civicrm_api3_validate_constraint($fieldValue, $fieldName, $fieldInfo, $entity);
1649 }
1650 }
1651 elseif (!empty($fieldInfo['required'])) {
1652 throw new Exception("DB Constraint Violation - $fieldName should possibly be marked as mandatory for $entity,$action API. If so, please raise a bug report.");
1653 }
1654 }
1655 if (!empty($fieldInfo['api.unique'])) {
1656 $params['entity'] = $entity;
1657 _civicrm_api3_validate_unique_key($params, $fieldName);
1658 }
1659 }
1660 }
1661
1662 /**
1663 * Validate date fields being passed into API.
1664 *
1665 * It currently converts both unique fields and DB field names to a mysql date.
1666 * @todo - probably the unique field handling & the if exists handling is now done before this
1667 * function is reached in the wrapper - can reduce this code down to assume we
1668 * are only checking the passed in field
1669 *
1670 * It also checks against the RULE:date function. This is a centralisation of code that was scattered and
1671 * may not be the best thing to do. There is no code level documentation on the existing functions to work off
1672 *
1673 * @param array $params
1674 * Params from civicrm_api.
1675 * @param string $fieldName
1676 * Uniquename of field being checked.
1677 * @param array $fieldInfo
1678 * Array of fields from getfields function.
1679 *
1680 * @throws Exception
1681 */
1682 function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) {
1683 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1684 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1685 return;
1686 }
1687
1688 if ($fieldValue === 'null' && empty($fieldInfo['api.required'])) {
1689 // This is the wierd & wonderful way PEAR sets null.
1690 return;
1691 }
1692
1693 //should we check first to prevent it from being copied if they have passed in sql friendly format?
1694 if (!empty($params[$fieldInfo['name']])) {
1695 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldInfo['name'], $fieldInfo['type']);
1696 }
1697 if ((CRM_Utils_Array::value('name', $fieldInfo) != $fieldName) && !empty($fieldValue)) {
1698 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldName, $fieldInfo['type']);
1699 }
1700
1701 if (!empty($op)) {
1702 $params[$fieldName][$op] = $fieldValue;
1703 }
1704 else {
1705 $params[$fieldName] = $fieldValue;
1706 }
1707 }
1708
1709 /**
1710 * Convert date into BAO friendly date.
1711 *
1712 * We accept 'whatever strtotime accepts'
1713 *
1714 * @param string $dateValue
1715 * @param string $fieldName
1716 * @param $fieldType
1717 *
1718 * @throws Exception
1719 * @return mixed
1720 */
1721 function _civicrm_api3_getValidDate($dateValue, $fieldName, $fieldType) {
1722 if (is_array($dateValue)) {
1723 foreach ($dateValue as $key => $value) {
1724 $dateValue[$key] = _civicrm_api3_getValidDate($value, $fieldName, $fieldType);
1725 }
1726 return $dateValue;
1727 }
1728 if (strtotime($dateValue) === FALSE) {
1729 throw new Exception($fieldName . " is not a valid date: " . $dateValue);
1730 }
1731 $format = ($fieldType == CRM_Utils_Type::T_DATE) ? 'Ymd000000' : 'YmdHis';
1732 return CRM_Utils_Date::processDate($dateValue, NULL, FALSE, $format);
1733 }
1734
1735 /**
1736 * Validate foreign constraint fields being passed into API.
1737 *
1738 * @param mixed $fieldValue
1739 * @param string $fieldName
1740 * Unique name of field being checked.
1741 * @param array $fieldInfo
1742 * Array of fields from getfields function.
1743 * @param string $entity
1744 *
1745 * @throws \API_Exception
1746 */
1747 function _civicrm_api3_validate_constraint($fieldValue, $fieldName, $fieldInfo, $entity) {
1748 $daoName = $fieldInfo['FKClassName'];
1749 $fieldInfo = [$fieldName => $fieldInfo];
1750 $params = [$fieldName => $fieldValue];
1751 _civicrm_api3_validate_fields($entity, NULL, $params, $fieldInfo);
1752 /* @var CRM_Core_DAO $dao*/
1753 $dao = new $daoName();
1754 $dao->id = $params[$fieldName];
1755 $dao->selectAdd();
1756 $dao->selectAdd('id');
1757 if (!$dao->find()) {
1758 throw new API_Exception("$fieldName is not valid : " . $fieldValue);
1759 }
1760 }
1761
1762 /**
1763 * Validate foreign constraint fields being passed into API.
1764 *
1765 * @param array $params
1766 * Params from civicrm_api.
1767 * @param string $fieldName
1768 * Uniquename of field being checked.
1769 *
1770 * @throws Exception
1771 */
1772 function _civicrm_api3_validate_unique_key(&$params, &$fieldName) {
1773 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1774 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1775 return;
1776 }
1777 $existing = civicrm_api($params['entity'], 'get', [
1778 'version' => $params['version'],
1779 $fieldName => $fieldValue,
1780 ]);
1781 // an entry already exists for this unique field
1782 if ($existing['count'] == 1) {
1783 // question - could this ever be a security issue?
1784 throw new API_Exception("Field: `$fieldName` must be unique. An conflicting entity already exists - id: " . $existing['id']);
1785 }
1786 }
1787
1788 /**
1789 * Generic implementation of the "replace" action.
1790 *
1791 * Replace the old set of entities (matching some given keys) with a new set of
1792 * entities (matching the same keys).
1793 *
1794 * @note This will verify that 'values' is present, but it does not directly verify
1795 * any other parameters.
1796 *
1797 * @param string $entity
1798 * Entity name.
1799 * @param array $params
1800 * Params from civicrm_api, including:.
1801 * - 'values': an array of records to save
1802 * - all other items: keys which identify new/pre-existing records.
1803 *
1804 * @return array|int
1805 */
1806 function _civicrm_api3_generic_replace($entity, $params) {
1807
1808 $transaction = new CRM_Core_Transaction();
1809 try {
1810 if (!is_array($params['values'])) {
1811 throw new Exception("Mandatory key(s) missing from params array: values");
1812 }
1813
1814 // Extract the keys -- somewhat scary, don't think too hard about it
1815 $baseParams = _civicrm_api3_generic_replace_base_params($params);
1816
1817 // Lookup pre-existing records
1818 $preexisting = civicrm_api($entity, 'get', $baseParams, $params);
1819 if (civicrm_error($preexisting)) {
1820 $transaction->rollback();
1821 return $preexisting;
1822 }
1823
1824 // Save the new/updated records
1825 $creates = [];
1826 foreach ($params['values'] as $replacement) {
1827 // Sugar: Don't force clients to duplicate the 'key' data
1828 $replacement = array_merge($baseParams, $replacement);
1829 $action = (isset($replacement['id']) || isset($replacement[$entity . '_id'])) ? 'update' : 'create';
1830 $create = civicrm_api($entity, $action, $replacement);
1831 if (civicrm_error($create)) {
1832 $transaction->rollback();
1833 return $create;
1834 }
1835 foreach ($create['values'] as $entity_id => $entity_value) {
1836 $creates[$entity_id] = $entity_value;
1837 }
1838 }
1839
1840 // Remove stale records
1841 $staleIDs = array_diff(
1842 array_keys($preexisting['values']),
1843 array_keys($creates)
1844 );
1845 foreach ($staleIDs as $staleID) {
1846 $delete = civicrm_api($entity, 'delete', [
1847 'version' => $params['version'],
1848 'id' => $staleID,
1849 ]);
1850 if (civicrm_error($delete)) {
1851 $transaction->rollback();
1852 return $delete;
1853 }
1854 }
1855
1856 return civicrm_api3_create_success($creates, $params);
1857 }
1858 catch (PEAR_Exception $e) {
1859 $transaction->rollback();
1860 return civicrm_api3_create_error($e->getMessage());
1861 }
1862 catch (Exception $e) {
1863 $transaction->rollback();
1864 return civicrm_api3_create_error($e->getMessage());
1865 }
1866 }
1867
1868 /**
1869 * Replace base parameters.
1870 *
1871 * @param array $params
1872 *
1873 * @return array
1874 */
1875 function _civicrm_api3_generic_replace_base_params($params) {
1876 $baseParams = $params;
1877 unset($baseParams['values']);
1878 unset($baseParams['sequential']);
1879 unset($baseParams['options']);
1880 $baseParams['options']['limit'] = 0;
1881 return $baseParams;
1882 }
1883
1884 /**
1885 * Returns fields allowable by api.
1886 *
1887 * @param $entity
1888 * String Entity to query.
1889 * @param bool $unique
1890 * Index by unique fields?.
1891 * @param array $params
1892 *
1893 * @return array
1894 */
1895 function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = []) {
1896 $unsetIfEmpty = [
1897 'dataPattern',
1898 'headerPattern',
1899 'default',
1900 'export',
1901 'import',
1902 ];
1903 $dao = _civicrm_api3_get_DAO($entity);
1904 if (empty($dao)) {
1905 return [];
1906 }
1907 $fields = $dao::fields();
1908 $supportedFields = $dao::getSupportedFields();
1909
1910 foreach ($fields as $name => $field) {
1911 // Denote as core field
1912 $fields[$name]['is_core_field'] = TRUE;
1913 // Set html attributes for text fields
1914 if (isset($field['html'])) {
1915 $fields[$name]['html'] += (array) $dao::makeAttribute($field);
1916 }
1917 // Delete field if not supported by current db schema (prevents errors when there are pending db updates)
1918 if (!isset($supportedFields[$field['name']])) {
1919 unset($fields[$name]);
1920 }
1921 }
1922
1923 // replace uniqueNames by the normal names as the key
1924 if (empty($unique)) {
1925 foreach ($fields as $name => &$field) {
1926 //getting rid of unused attributes
1927 foreach ($unsetIfEmpty as $attr) {
1928 if (empty($field[$attr])) {
1929 unset($field[$attr]);
1930 }
1931 }
1932 if ($name == $field['name']) {
1933 continue;
1934 }
1935 if (array_key_exists($field['name'], $fields)) {
1936 $field['error'] = 'name conflict';
1937 // it should never happen, but better safe than sorry
1938 continue;
1939 }
1940 $fields[$field['name']] = $field;
1941 $fields[$field['name']]['uniqueName'] = $name;
1942 unset($fields[$name]);
1943 }
1944 }
1945 // Translate FKClassName to the corresponding api
1946 foreach ($fields as $name => &$field) {
1947 if (!empty($field['FKClassName'])) {
1948 $FKApi = CRM_Core_DAO_AllCoreTables::getBriefName($field['FKClassName']);
1949 if ($FKApi) {
1950 $field['FKApiName'] = $FKApi;
1951 }
1952 }
1953 }
1954 $fields += _civicrm_api_get_custom_fields($entity, $params);
1955 return $fields;
1956 }
1957
1958 /**
1959 * Return an array of fields for a given entity.
1960 *
1961 * This is the same as the BAO function but fields are prefixed with 'custom_' to represent api params.
1962 *
1963 * @param $entity
1964 * @param array $params
1965 *
1966 * @return array
1967 */
1968 function _civicrm_api_get_custom_fields($entity, &$params) {
1969 $entity = _civicrm_api_get_camel_name($entity);
1970 if ($entity == 'Contact') {
1971 // Use sub-type if available, otherwise "NULL" to fetch from all contact types
1972 $entity = $params['contact_type'] ?? NULL;
1973 }
1974 $customfields = CRM_Core_BAO_CustomField::getFields($entity,
1975 FALSE,
1976 FALSE,
1977 // we could / should probably test for other subtypes here - e.g. activity_type_id
1978 CRM_Utils_Array::value('contact_sub_type', $params),
1979 NULL,
1980 FALSE,
1981 FALSE,
1982 FALSE
1983 );
1984
1985 $ret = [];
1986
1987 foreach ($customfields as $key => $value) {
1988 // Regular fields have a 'name' property
1989 $value['name'] = 'custom_' . $key;
1990 $value['title'] = $value['label'];
1991 if ($value['data_type'] == 'Date' && CRM_Utils_Array::value('time_format', $value, 0) > 0) {
1992 $value['data_type'] = 'DateTime';
1993 }
1994 $value['type'] = CRM_Utils_Array::value($value['data_type'], CRM_Core_BAO_CustomField::dataToType());
1995 $ret['custom_' . $key] = $value;
1996 }
1997 return $ret;
1998 }
1999
2000 /**
2001 * Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't.
2002 *
2003 * If multiple aliases the last takes precedence
2004 *
2005 * Function also swaps unique fields for non-unique fields & vice versa.
2006 *
2007 * @param $apiRequest
2008 * @param $fields
2009 */
2010 function _civicrm_api3_swap_out_aliases(&$apiRequest, $fields) {
2011 foreach ($fields as $field => $values) {
2012 $uniqueName = $values['uniqueName'] ?? NULL;
2013 if (!empty($values['api.aliases'])) {
2014 // if aliased field is not set we try to use field alias
2015 if (!isset($apiRequest['params'][$field])) {
2016 foreach ($values['api.aliases'] as $alias) {
2017 if (isset($apiRequest['params'][$alias])) {
2018 $apiRequest['params'][$field] = $apiRequest['params'][$alias];
2019 }
2020 //unset original field nb - need to be careful with this as it may bring inconsistencies
2021 // out of the woodwork but will be implementing only as _spec function extended
2022 unset($apiRequest['params'][$alias]);
2023 }
2024 }
2025 }
2026 if (!isset($apiRequest['params'][$field]) && !empty($values['name']) && $field != $values['name']
2027 && isset($apiRequest['params'][$values['name']])
2028 ) {
2029 $apiRequest['params'][$field] = $apiRequest['params'][$values['name']];
2030 // note that it would make sense to unset the original field here but tests need to be in place first
2031 if ($field != 'domain_version') {
2032 unset($apiRequest['params'][$values['name']]);
2033 }
2034 }
2035 if (!isset($apiRequest['params'][$field])
2036 && $uniqueName
2037 && $field != $uniqueName
2038 && array_key_exists($uniqueName, $apiRequest['params'])
2039 ) {
2040 $apiRequest['params'][$field] = $apiRequest['params'][$values['uniqueName']] ?? NULL;
2041 // note that it would make sense to unset the original field here but tests need to be in place first
2042 }
2043 }
2044
2045 }
2046
2047 /**
2048 * Validate integer fields being passed into API.
2049 *
2050 * It currently converts the incoming value 'user_contact_id' into the id of the currently logged in user.
2051 *
2052 * @param array $params
2053 * Params from civicrm_api.
2054 * @param string $fieldName
2055 * Uniquename of field being checked.
2056 * @param array $fieldInfo
2057 * Array of fields from getfields function.
2058 * @param string $entity
2059 *
2060 * @throws API_Exception
2061 */
2062 function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $entity) {
2063 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2064 if ($fieldName === 'auto_renew' && $fieldValue === TRUE) {
2065 // https://lab.civicrm.org/dev/rc/-/issues/14
2066 $fieldValue = 1;
2067 }
2068 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
2069 return;
2070 }
2071
2072 if (!empty($fieldValue) || $fieldValue === '0' || $fieldValue === 0) {
2073 // if value = 'user_contact_id' (or similar), replace value with contact id
2074 if (!is_numeric($fieldValue) && is_scalar($fieldValue)) {
2075 $realContactId = _civicrm_api3_resolve_contactID($fieldValue);
2076 if ('unknown-user' === $realContactId) {
2077 throw new API_Exception("\"$fieldName\" \"{$fieldValue}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldName, "type" => "integer"]);
2078 }
2079 elseif (is_numeric($realContactId)) {
2080 $fieldValue = $realContactId;
2081 }
2082 elseif (is_null($realContactId) && empty($fieldInfo['api.required']) && $fieldValue === 'user_contact_id') {
2083 // If not mandatory this will be OK. If mandatory it should fail.
2084 $fieldValue = NULL;
2085 }
2086 }
2087 if (
2088 (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options']) || $fieldName === 'campaign_id')
2089 // if it is already numeric AND it is an FK field we don't need to validate because
2090 // sql will do that for us on insert (this also saves a big lookup)
2091 && (!is_numeric($fieldValue) || empty($fieldInfo['FKClassName']))
2092 ) {
2093 $additional_lookup_params = [];
2094 if (strtolower($entity) === 'address' && $fieldName == 'state_province_id') {
2095 $country_id = _civicrm_api3_resolve_country_id($params);
2096 if (!empty($country_id)) {
2097 $additional_lookup_params = ['country_id' => $country_id];
2098 }
2099 }
2100 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op, $additional_lookup_params);
2101 }
2102
2103 // After swapping options, ensure we have an integer(s)
2104 foreach ((array) ($fieldValue) as $value) {
2105 if ($value && !is_numeric($value) && $value !== 'null' && $value !== NULL && !is_array($value)) {
2106 throw new API_Exception("$fieldName is not a valid integer", 2001, ['error_field' => $fieldName, "type" => "integer"]);
2107 }
2108 }
2109
2110 // Check our field length
2111 if (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen($fieldValue) > $fieldInfo['maxlength']
2112 ) {
2113 throw new API_Exception($fieldValue . " is " . strlen($fieldValue) . " characters - longer than $fieldName length" . $fieldInfo['maxlength'] . ' characters',
2114 2100, ['field' => $fieldName, "max_length" => $fieldInfo['maxlength']]
2115 );
2116 }
2117 }
2118
2119 if (!empty($op)) {
2120 $params[$fieldName][$op] = $fieldValue;
2121 }
2122 else {
2123 $params[$fieldName] = $fieldValue;
2124 }
2125 }
2126
2127 /**
2128 * Helper function to determine country_id given the myriad of values for country_id or country that are supported
2129 * @param $params
2130 *
2131 * @return int|null
2132 */
2133 function _civicrm_api3_resolve_country_id($params) {
2134 if (!empty($params['country_id'])) {
2135 if (is_numeric($params['country_id'])) {
2136 $country_id = $params['country_id'];
2137 }
2138 else {
2139 $country = new CRM_Core_DAO_Country();
2140 $country->name = $params['country_id'];
2141 if (!$country->find(TRUE)) {
2142 $country->name = NULL;
2143 $country->iso_code = $params['country_id'];
2144 $country->find(TRUE);
2145 }
2146 if (!empty($country->id)) {
2147 $country_id = $country->id;
2148 }
2149 }
2150 }
2151 elseif (!empty($params['country'])) {
2152 if (is_numeric($params['country'])) {
2153 $country_id = $params['country'];
2154 }
2155 else {
2156 $country = new CRM_Core_DAO_Country();
2157 $country->name = $params['country'];
2158 if (!$country->find(TRUE)) {
2159 $country->name = NULL;
2160 $country->iso_code = $params['country'];
2161 $country->find(TRUE);
2162 }
2163 if (!empty($country->id)) {
2164 $country_id = $country->id;
2165 }
2166 }
2167 }
2168 return !empty($country_id) ? $country_id : NULL;
2169 }
2170
2171 /**
2172 * Determine a contact ID using a string expression.
2173 *
2174 * @param string $contactIdExpr
2175 * E.g. "user_contact_id" or "@user:username".
2176 *
2177 * @return int|null|'unknown-user'
2178 * @throws \CRM_Core_Exception
2179 */
2180 function _civicrm_api3_resolve_contactID($contactIdExpr) {
2181 // If value = 'user_contact_id' replace value with logged in user id.
2182 if ($contactIdExpr == "user_contact_id") {
2183 return CRM_Core_Session::getLoggedInContactID();
2184 }
2185 elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) {
2186 $config = CRM_Core_Config::singleton();
2187
2188 $ufID = $config->userSystem->getUfId($matches[1]);
2189 if (!$ufID) {
2190 return 'unknown-user';
2191 }
2192
2193 $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID);
2194 if (!$contactID) {
2195 return 'unknown-user';
2196 }
2197
2198 return $contactID;
2199 }
2200 return NULL;
2201 }
2202
2203 /**
2204 * Validate html (check for scripting attack).
2205 *
2206 * @param array $params
2207 * @param string $fieldName
2208 * @param array $fieldInfo
2209 *
2210 * @throws API_Exception
2211 */
2212 function _civicrm_api3_validate_html(&$params, &$fieldName, $fieldInfo) {
2213 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2214 if (strpos($op, 'NULL') || strpos($op, 'EMPTY')) {
2215 return;
2216 }
2217 }
2218
2219 /**
2220 * Validate string fields being passed into API.
2221 *
2222 * @param array $params
2223 * Params from civicrm_api.
2224 * @param string $fieldName
2225 * Uniquename of field being checked.
2226 * @param array $fieldInfo
2227 * Array of fields from getfields function.
2228 * @param string $entity
2229 * @param string $action
2230 *
2231 * @throws API_Exception
2232 * @throws Exception
2233 */
2234 function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $entity, $action) {
2235 $isGet = substr($action, 0, 3) === 'get';
2236 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName, 'String');
2237 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE || CRM_Utils_System::isNull($fieldValue)) {
2238 return;
2239 }
2240
2241 if (!is_array($fieldValue)) {
2242 $fieldValue = (string) $fieldValue;
2243 }
2244
2245 if ($fieldValue) {
2246 foreach ((array) $fieldValue as $key => $value) {
2247 if ($fieldName == 'currency') {
2248 //When using IN operator $fieldValue is a array of currency codes
2249 if (!CRM_Utils_Rule::currencyCode($value)) {
2250 throw new Exception("Currency not a valid code: $value");
2251 }
2252 }
2253 }
2254 }
2255 if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
2256 try {
2257 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op);
2258 }
2259 catch (API_Exception $e) {
2260 // For get operations, allow any string
2261 if (!$isGet) {
2262 throw $e;
2263 }
2264 }
2265 }
2266 // Check our field length
2267 elseif (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen(utf8_decode($fieldValue)) > $fieldInfo['maxlength']) {
2268 throw new API_Exception("Value for $fieldName is " . strlen(utf8_decode($value)) . " characters - This field has a maxlength of {$fieldInfo['maxlength']} characters.",
2269 2100, ['field' => $fieldName]
2270 );
2271 }
2272
2273 if (!empty($op)) {
2274 $params[$fieldName][$op] = $fieldValue;
2275 }
2276 else {
2277 $params[$fieldName] = $fieldValue;
2278 }
2279 }
2280
2281 /**
2282 * Validate & swap out any pseudoconstants / options.
2283 *
2284 * @param mixed $fieldValue
2285 * @param string $entity : api entity name
2286 * @param string $fieldName : field name used in api call (not necessarily the canonical name)
2287 * @param array $fieldInfo : getfields meta-data
2288 * @param string $op
2289 * @param array $additional_lookup_params
2290 *
2291 * @throws \API_Exception
2292 */
2293 function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo, $op = '=', $additional_lookup_params = []) {
2294 if (in_array($op, ['>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'])) {
2295 return;
2296 }
2297
2298 $options = $fieldInfo['options'] ?? NULL;
2299
2300 if (!$options) {
2301 if (strtolower($entity) == 'profile' && !empty($fieldInfo['entity'])) {
2302 // We need to get the options from the entity the field relates to.
2303 $entity = $fieldInfo['entity'];
2304 }
2305 $options_lookup_params = [
2306 'version' => 3,
2307 'field' => $fieldInfo['name'],
2308 'context' => 'validate',
2309 ];
2310 if (!empty($additional_lookup_params)) {
2311 $options_lookup_params = array_merge($additional_lookup_params, $options_lookup_params);
2312 }
2313 $options = civicrm_api($entity, 'getoptions', $options_lookup_params);
2314
2315 $options = CRM_Utils_Array::value('values', $options, []);
2316 }
2317
2318 if (is_string($fieldValue) && strpos($fieldValue, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
2319 $fieldValue = CRM_Utils_Array::explodePadded($fieldValue);
2320 }
2321 // If passed multiple options, validate each.
2322 if (is_array($fieldValue)) {
2323 foreach ($fieldValue as &$value) {
2324 if (!is_array($value)) {
2325 _civicrm_api3_api_match_pseudoconstant_value($value, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
2326 }
2327 }
2328 }
2329 else {
2330 _civicrm_api3_api_match_pseudoconstant_value($fieldValue, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
2331 }
2332 }
2333
2334 /**
2335 * Validate & swap a single option value for a field.
2336 *
2337 * @param string $value field value
2338 * @param array $options array of options for this field
2339 * @param string $fieldName field name used in api call (not necessarily the canonical name)
2340 * @param bool $isRequired
2341 * Is this a required field or is 'null' an acceptable option. We allow 'null' last
2342 * in case we have the weird situation of it being a valid option in which case our
2343 * brains will probably explode.
2344 *
2345 * @throws API_Exception
2346 */
2347 function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName, $isRequired) {
2348 // If option is a key, no need to translate
2349 // or if no options are avaiable for pseudoconstant 'table' property
2350 if (array_key_exists($value, $options) || !$options) {
2351 return;
2352 }
2353
2354 // Hack for Profile formatting fields
2355 if ($fieldName === 'field_name' && (strpos($value, 'formatting') === 0)) {
2356 return;
2357 }
2358
2359 // Legacy handling of tag used_for values, see https://github.com/civicrm/civicrm-core/pull/20573
2360 if ($fieldName === 'used_for') {
2361 $legacyTagUsedFor = [
2362 'Activities' => 'Activity',
2363 'Contacts' => 'Contact',
2364 'Cases' => 'Case',
2365 // Attachements [sic] was the original spelling
2366 'Attachements' => 'File',
2367 ];
2368 $value = $legacyTagUsedFor[$value] ?? $value;
2369 }
2370
2371 // Translate value into key
2372 // Cast $value to string to avoid a bug in array_search
2373 $newValue = array_search((string) $value, $options);
2374 if ($newValue !== FALSE) {
2375 $value = $newValue;
2376 return;
2377 }
2378 // Case-insensitive matching
2379 $newValue = strtolower($value);
2380 $options = array_map("strtolower", $options);
2381 $newValue = array_search($newValue, $options);
2382 if ($newValue === FALSE) {
2383 if ($value === 'null' && !$isRequired) {
2384 // CiviMagic syntax for Nulling out the field - let it through.
2385 return;
2386 }
2387 // Legacy support for custom fields: If matching failed by name, fallback to label
2388 // @see https://lab.civicrm.org/dev/core/-/issues/1816
2389 if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldName)) {
2390 $field = new CRM_Core_BAO_CustomField();
2391 $field->id = $customFieldId;
2392 $options = array_map("strtolower", $field->getOptions());
2393 $newValue = array_search(strtolower($value), $options);
2394 }
2395 }
2396 if ($newValue === FALSE) {
2397 throw new API_Exception("'$value' is not a valid option for field $fieldName", 2001, ['error_field' => $fieldName]);
2398 }
2399 $value = $newValue;
2400 }
2401
2402 /**
2403 * Returns the canonical name of a field.
2404 *
2405 * @param $entity
2406 * api entity name (string should already be standardized - no camelCase).
2407 * @param $fieldName
2408 * any variation of a field's name (name, unique_name, api.alias).
2409 *
2410 * @param string $action
2411 *
2412 * @return bool|string
2413 * FieldName or FALSE if the field does not exist
2414 */
2415 function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create') {
2416 if (!$fieldName) {
2417 return FALSE;
2418 }
2419 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
2420 return $fieldName;
2421 }
2422 if ($fieldName === (CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($entity) . '_id')) {
2423 return 'id';
2424 }
2425 $result = civicrm_api($entity, 'getfields', [
2426 'version' => 3,
2427 'action' => $action,
2428 ]);
2429 $meta = $result['values'];
2430 if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
2431 $fieldName = $fieldName . '_id';
2432 }
2433 if (isset($meta[$fieldName])) {
2434 return $meta[$fieldName]['name'];
2435 }
2436 foreach ($meta as $info) {
2437 if ($fieldName == $info['name'] || $fieldName == CRM_Utils_Array::value('uniqueName', $info)) {
2438 return $info['name'];
2439 }
2440 if (array_search($fieldName, CRM_Utils_Array::value('api.aliases', $info, [])) !== FALSE) {
2441 return $info['name'];
2442 }
2443 }
2444 // Create didn't work, try with get
2445 if ($action == 'create') {
2446 return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get');
2447 }
2448 return FALSE;
2449 }
2450
2451 /**
2452 * Check if the function is deprecated.
2453 *
2454 * @param string $entity
2455 * @param array $result
2456 *
2457 * @return string|array|null
2458 */
2459 function _civicrm_api3_deprecation_check($entity, $result = []) {
2460 if ($entity) {
2461 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
2462 $fnName = "_civicrm_api3_{$lowercase_entity}_deprecation";
2463 if (function_exists($fnName)) {
2464 return $fnName($result);
2465 }
2466 }
2467 }
2468
2469 /**
2470 * Get the actual field value.
2471 *
2472 * In some case $params[$fieldName] holds Array value in this format Array([operator] => [value])
2473 * So this function returns the actual field value.
2474 *
2475 * @param array $params
2476 * @param string $fieldName
2477 * @param string $type
2478 *
2479 * @return mixed
2480 */
2481 function _civicrm_api3_field_value_check(&$params, $fieldName, $type = NULL) {
2482 $fieldValue = $params[$fieldName] ?? NULL;
2483 $op = NULL;
2484
2485 if (!empty($fieldValue) && is_array($fieldValue) &&
2486 (array_search(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators()) ||
2487 $type == 'String' && strstr(key($fieldValue), 'EMPTY'))
2488 ) {
2489 $op = key($fieldValue);
2490 $fieldValue = $fieldValue[$op] ?? NULL;
2491 }
2492 return [$fieldValue, $op];
2493 }
2494
2495 /**
2496 * A generic "get" API based on simple array data. This is comparable to
2497 * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for
2498 * small/mid-size data loaded from external JSON or XML documents.
2499 *
2500 * @param $entity
2501 * @param array $params
2502 * API parameters.
2503 * @param array $records
2504 * List of all records.
2505 * @param string $idCol
2506 * The property which defines the ID of a record
2507 * @param array $filterableFields
2508 * List of filterable fields.
2509 *
2510 * @return array
2511 * @throws \API_Exception
2512 */
2513 function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $filterableFields) {
2514 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
2515 $offset = $options['offset'] ?? NULL;
2516 $limit = $options['limit'] ?? NULL;
2517
2518 $sort = !empty($options['sort']) ? explode(', ', $options['sort']) : NULL;
2519 if ($sort) {
2520 usort($records, function($a, $b) use ($sort) {
2521 foreach ($sort as $field) {
2522 [$field, $dir] = array_pad(explode(' ', $field), 2, 'asc');
2523 $modifier = strtolower($dir) == 'asc' ? 1 : -1;
2524 if (isset($a[$field]) && isset($b[$field])) {
2525 if ($a[$field] == $b[$field]) {
2526 continue;
2527 }
2528 return (strnatcasecmp($a[$field], $b[$field]) * $modifier);
2529 }
2530 elseif (isset($a[$field]) || isset($b[$field])) {
2531 return ((isset($a[$field]) ? 1 : -1) * $modifier);
2532 }
2533 }
2534 return 0;
2535 });
2536 }
2537
2538 $matches = [];
2539
2540 $isMatch = function($recordVal, $searchVal) {
2541 $operator = '=';
2542 if (is_array($searchVal) && count($searchVal) === 1 && in_array(array_keys($searchVal)[0], CRM_Core_DAO::acceptedSQLOperators())) {
2543 $operator = array_keys($searchVal)[0];
2544 $searchVal = array_values($searchVal)[0];
2545 }
2546 switch ($operator) {
2547 case '=':
2548 case '!=':
2549 case '<>':
2550 return ($recordVal == $searchVal) == ($operator == '=');
2551
2552 case 'IS NULL':
2553 case 'IS NOT NULL':
2554 return is_null($recordVal) == ($operator == 'IS NULL');
2555
2556 case '>':
2557 return $recordVal > $searchVal;
2558
2559 case '>=':
2560 return $recordVal >= $searchVal;
2561
2562 case '<':
2563 return $recordVal < $searchVal;
2564
2565 case '<=':
2566 return $recordVal <= $searchVal;
2567
2568 case 'BETWEEN':
2569 case 'NOT BETWEEN':
2570 $between = ($recordVal >= $searchVal[0] && $recordVal <= $searchVal[1]);
2571 return $between == ($operator == 'BETWEEN');
2572
2573 case 'LIKE':
2574 case 'NOT LIKE':
2575 $pattern = '/^' . str_replace('%', '.*', preg_quote($searchVal, '/')) . '$/i';
2576 return !preg_match($pattern, $recordVal) == ($operator != 'LIKE');
2577
2578 case 'IN':
2579 case 'NOT IN':
2580 return in_array($recordVal, $searchVal) == ($operator == 'IN');
2581
2582 default:
2583 throw new API_Exception("Unsupported operator: '$operator' cannot be used with array data");
2584 }
2585 };
2586
2587 $currentOffset = 0;
2588 foreach ($records as $record) {
2589 if ($idCol != 'id') {
2590 $record['id'] = $record[$idCol];
2591 }
2592 $match = TRUE;
2593 foreach ($params as $k => $v) {
2594 if ($k == 'id') {
2595 $k = $idCol;
2596 }
2597 if (in_array($k, $filterableFields) && !$isMatch($record[$k] ?? NULL, $v)) {
2598 $match = FALSE;
2599 break;
2600 }
2601 }
2602 if ($match) {
2603 if ($currentOffset >= $offset) {
2604 $matches[$record[$idCol]] = $record;
2605 }
2606 if ($limit && count($matches) >= $limit) {
2607 break;
2608 }
2609 $currentOffset++;
2610 }
2611 }
2612
2613 $return = CRM_Utils_Array::value('return', $options, []);
2614 if (!empty($return)) {
2615 $return['id'] = 1;
2616 $matches = CRM_Utils_Array::filterColumns($matches, array_keys($return));
2617 }
2618
2619 return civicrm_api3_create_success($matches, $params);
2620 }
2621
2622 /**
2623 * @param string $bao_name
2624 * @param array $params
2625 * @throws \Civi\API\Exception\UnauthorizedException
2626 */
2627 function _civicrm_api3_check_edit_permissions($bao_name, $params) {
2628 // For lack of something more clever, here's a whitelist of entities whos permissions
2629 // are inherited from a contact record.
2630 // Note, when adding here, also remember to modify _civicrm_api3_permissions()
2631 $contactEntities = [
2632 'CRM_Core_BAO_Email',
2633 'CRM_Core_BAO_Phone',
2634 'CRM_Core_BAO_Address',
2635 'CRM_Core_BAO_IM',
2636 'CRM_Core_BAO_Website',
2637 'CRM_Core_BAO_OpenID',
2638 ];
2639 if (!empty($params['check_permissions']) && in_array($bao_name, $contactEntities)) {
2640 $cid = !empty($params['contact_id']) ? $params['contact_id'] : CRM_Core_DAO::getFieldValue($bao_name, $params['id'], 'contact_id');
2641 if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) {
2642 throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record');
2643 }
2644 }
2645 }
2646
2647 /**
2648 * Check if an entity has been modified since the last known modified_date
2649 *
2650 * @param string $modifiedDate Last knowm modified_date
2651 * @param int $id Id of record to check
2652 * @param string $entity API Entity
2653 *
2654 * @return bool
2655 * @throws \CiviCRM_API3_Exception
2656 */
2657 function _civicrm_api3_compare_timestamps($modifiedDate, $id, $entity) {
2658 $currentDbInfo = civicrm_api3($entity, 'getsingle', ['id' => $id]);
2659 if (strtotime($currentDbInfo['modified_date']) <= strtotime($modifiedDate)) {
2660 return TRUE;
2661 }
2662 return FALSE;
2663 }