Merge pull request #22639 from civicrm/5.46
[civicrm-core.git] / CRM / Utils / Token.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 use Civi\Token\TokenProcessor;
13
14 /**
15 *
16 * @package CRM
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19
20 /**
21 * Class to abstract token replacement.
22 */
23 class CRM_Utils_Token {
24 public static $_requiredTokens = NULL;
25
26 public static $_tokens = [
27 'action' => [
28 'forward',
29 'optOut',
30 'optOutUrl',
31 'reply',
32 'unsubscribe',
33 'unsubscribeUrl',
34 'resubscribe',
35 'resubscribeUrl',
36 'subscribeUrl',
37 ],
38 'mailing' => [
39 'id',
40 'key',
41 'name',
42 'group',
43 'subject',
44 'viewUrl',
45 'editUrl',
46 'scheduleUrl',
47 'approvalStatus',
48 'approvalNote',
49 'approveUrl',
50 'creator',
51 'creatorEmail',
52 ],
53 'user' => [
54 // we extract the stuff after the role / permission and return the
55 // civicrm email addresses of all users with that role / permission
56 // useful with rules integration
57 'permission:',
58 'role:',
59 ],
60 // populate this dynamically
61 'contact' => NULL,
62 // populate this dynamically
63 'contribution' => NULL,
64 'domain' => [
65 'name',
66 'phone',
67 'address',
68 'email',
69 'id',
70 'description',
71 ],
72 'subscribe' => ['group'],
73 'unsubscribe' => ['group'],
74 'resubscribe' => ['group'],
75 'welcome' => ['group'],
76 ];
77
78 /**
79 * @deprecated
80 * This is used by CiviMail but will be made redundant by FlexMailer.
81 * @return array
82 */
83 public static function getRequiredTokens() {
84 if (self::$_requiredTokens == NULL) {
85 self::$_requiredTokens = [
86 'domain.address' => ts("Domain address - displays your organization's postal address."),
87 'action.optOutUrl or action.unsubscribeUrl' => [
88 'action.optOut' => ts("'Opt out via email' - displays an email address for recipients to opt out of receiving emails from your organization."),
89 'action.optOutUrl' => ts("'Opt out via web page' - creates a link for recipients to click if they want to opt out of receiving emails from your organization. Alternatively, you can include the 'Opt out via email' token."),
90 'action.unsubscribe' => ts("'Unsubscribe via email' - displays an email address for recipients to unsubscribe from the specific mailing list used to send this message."),
91 'action.unsubscribeUrl' => ts("'Unsubscribe via web page' - creates a link for recipients to unsubscribe from the specific mailing list used to send this message. Alternatively, you can include the 'Unsubscribe via email' token or one of the Opt-out tokens."),
92 ],
93 ];
94 }
95 return self::$_requiredTokens;
96 }
97
98 /**
99 * Check a string (mailing body) for required tokens.
100 *
101 * @param string $str
102 * The message.
103 *
104 * @return bool|array
105 * true if all required tokens are found,
106 * else an array of the missing tokens
107 */
108 public static function requiredTokens(&$str) {
109 // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail.
110 $requiredTokens = defined('CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS') ? Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS, []) : CRM_Utils_Token::getRequiredTokens();
111
112 $missing = [];
113 foreach ($requiredTokens as $token => $value) {
114 if (!is_array($value)) {
115 if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
116 $missing[$token] = $value;
117 }
118 }
119 else {
120 $present = FALSE;
121 $desc = NULL;
122 foreach ($value as $t => $d) {
123 $desc = $d;
124 if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) {
125 $present = TRUE;
126 }
127 }
128 if (!$present) {
129 $missing[$token] = $desc;
130 }
131 }
132 }
133
134 if (empty($missing)) {
135 return TRUE;
136 }
137 return $missing;
138 }
139
140 /**
141 * Wrapper for token matching.
142 *
143 * @param string $type
144 * The token type (domain,mailing,contact,action).
145 * @param string $var
146 * The token variable.
147 * @param string $str
148 * The string to search.
149 *
150 * @return bool
151 * Was there a match
152 */
153 public static function token_match($type, $var, &$str) {
154 $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
155 return preg_match("/(^|[^\{])$token/", $str);
156 }
157
158 /**
159 * Wrapper for token replacing.
160 *
161 * @param string $type
162 * The token type.
163 * @param string $var
164 * The token variable.
165 * @param string $value
166 * The value to substitute for the token.
167 * @param string $str (reference) The string to replace in
168 *
169 * @param bool $escapeSmarty
170 *
171 * @return string
172 * The processed string
173 */
174 public static function token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
175 $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
176 if (!$value) {
177 $value = '$3';
178 }
179 if ($escapeSmarty) {
180 $value = self::tokenEscapeSmarty($value);
181 }
182 $str = preg_replace("/([^\{])?$token/", "\${1}$value", $str);
183 return $str;
184 }
185
186 /**
187 * Get the regex for token replacement
188 *
189 * @param string $token_type
190 * A string indicating the the type of token to be used in the expression.
191 *
192 * @return string
193 * regular expression suitable for using in preg_replace
194 */
195 private static function tokenRegex(string $token_type) {
196 return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(:|\.)?\w*(\-[\w\s]+)?)\}(?!\})/';
197 }
198
199 /**
200 * Escape the string so a malicious user cannot inject smarty code into the template.
201 *
202 * @param string $string
203 * A string that needs to be escaped from smarty parsing.
204 *
205 * @return string
206 * the escaped string
207 */
208 public static function tokenEscapeSmarty($string) {
209 // need to use negative look-behind, as both str_replace() and preg_replace() are sequential
210 return preg_replace(['/{/', '/(?<!{ldelim)}/'], ['{ldelim}', '{rdelim}'], $string);
211 }
212
213 /**
214 * Replace all the domain-level tokens in $str
215 *
216 * @deprecated
217 *
218 * @param string $str
219 * The string with tokens to be replaced.
220 * @param object $domain
221 * The domain BAO.
222 * @param bool $html
223 * Replace tokens with HTML or plain text.
224 *
225 * @param null $knownTokens
226 * @param bool $escapeSmarty
227 *
228 * @return string
229 * The processed string
230 */
231 public static function replaceDomainTokens(
232 $str,
233 $domain,
234 $html = FALSE,
235 $knownTokens = NULL,
236 $escapeSmarty = FALSE
237 ) {
238 $key = 'domain';
239 if (
240 !$knownTokens || empty($knownTokens[$key])
241 ) {
242 return $str;
243 }
244
245 $str = preg_replace_callback(
246 self::tokenRegex($key),
247 function ($matches) use ($domain, $html, $escapeSmarty) {
248 return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
249 },
250 $str
251 );
252 return $str;
253 }
254
255 /**
256 * Do not use.
257 *
258 * @deprecated
259 *
260 * @param string $token
261 * @param CRM_Core_BAO_Domain $domain
262 * @param bool $html
263 * @param bool $escapeSmarty
264 *
265 * @return null|string
266 */
267 public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?string {
268 $tokens = CRM_Core_DomainTokens::getDomainTokenValues($domain->id, $html);
269 $value = $tokens[$token] ?? "{domain.$token}";
270 if ($escapeSmarty) {
271 $value = self::tokenEscapeSmarty($value);
272 }
273 return $value;
274 }
275
276 /**
277 * Replace all the org-level tokens in $str
278 *
279 * @fixme: This function appears to be broken, as it depended on
280 * nonexistant method: CRM_Core_BAO_CustomValue::getContactValues()
281 * Marking as deprecated until this is clarified.
282 *
283 * @deprecated
284 * - the above hard-breakage was there from 2015 to 2021 and
285 * no error was ever reported on it -does that mean
286 * 1) the code is never hit because the only function that
287 * calls this function is never called or
288 * 2) it was called but never required to resolve any tokens
289 * or more specifically custom field tokens
290 *
291 * The handling for custom fields with the removed token has
292 * now been removed.
293 *
294 * @param string $str
295 * The string with tokens to be replaced.
296 * @param object $org
297 * Associative array of org properties.
298 * @param bool $html
299 * Replace tokens with HTML or plain text.
300 *
301 * @param bool $escapeSmarty
302 *
303 * @return string
304 * The processed string
305 */
306 public static function replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
307 CRM_Core_Error::deprecatedFunctionWarning('token processor');
308 self::$_tokens['org']
309 = array_merge(
310 array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')),
311 ['address', 'display_name', 'checksum', 'contact_id']
312 );
313
314 foreach (self::$_tokens['org'] as $token) {
315 // print "Getting token value for $token<br/><br/>";
316 if ($token === '') {
317 continue;
318 }
319
320 // If the string doesn't contain this token, skip it.
321
322 if (!self::token_match('org', $token, $str)) {
323 continue;
324 }
325
326 // Construct value from $token and $contact
327
328 $value = NULL;
329
330 if ($token === 'checksum') {
331 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']);
332 $value = "cs={$cs}";
333 }
334 elseif ($token === 'address') {
335 // Build the location values array
336
337 $loc = [];
338 $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name');
339 $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address');
340 $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city');
341 $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province');
342 $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code');
343
344 // Construct the address token
345
346 $value = CRM_Utils_Address::format($loc);
347 if ($html) {
348 $value = str_replace("\n", '<br />', $value);
349 }
350 }
351 else {
352 $value = CRM_Utils_Array::retrieveValueRecursive($org, $token);
353 }
354
355 self::token_replace('org', $token, $value, $str, $escapeSmarty);
356 }
357
358 return $str;
359 }
360
361 /**
362 * Replace all mailing tokens in $str
363 *
364 * @param string $str
365 * The string with tokens to be replaced.
366 * @param object $mailing
367 * The mailing BAO, or null for validation.
368 * @param bool $html
369 * Replace tokens with HTML or plain text.
370 *
371 * @param null $knownTokens
372 * @param bool $escapeSmarty
373 *
374 * @return string
375 * The processed string
376 *
377 * @deprecated
378 */
379 public static function &replaceMailingTokens(
380 $str,
381 &$mailing,
382 $html = FALSE,
383 $knownTokens = NULL,
384 $escapeSmarty = FALSE
385 ) {
386 $key = 'mailing';
387 if (!$knownTokens || !isset($knownTokens[$key])) {
388 return $str;
389 }
390
391 $str = preg_replace_callback(
392 self::tokenRegex($key),
393 function ($matches) use (&$mailing, $escapeSmarty) {
394 return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
395 },
396 $str
397 );
398 return $str;
399 }
400
401 /**
402 * @param $token
403 * @param $mailing
404 * @param bool $escapeSmarty
405 *
406 * @return string
407 */
408 public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) {
409 $value = '';
410 switch ($token) {
411 // CRM-7663
412
413 case 'id':
414 $value = $mailing ? $mailing->id : 'undefined';
415 break;
416
417 // Key is the ID, or the hash when the hash URLs setting is enabled
418 case 'key':
419 $value = $mailing->id;
420 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($value)) {
421 $value = $hash;
422 }
423 break;
424
425 case 'name':
426 $value = $mailing ? $mailing->name : 'Mailing Name';
427 break;
428
429 case 'group':
430 $groups = $mailing ? $mailing->getGroupNames() : ['Mailing Groups'];
431 $value = implode(', ', $groups);
432 break;
433
434 case 'subject':
435 $value = $mailing->subject;
436 break;
437
438 case 'viewUrl':
439 $mailingKey = $mailing->id;
440 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
441 $mailingKey = $hash;
442 }
443 $value = CRM_Utils_System::url('civicrm/mailing/view',
444 "reset=1&id={$mailingKey}",
445 TRUE, NULL, FALSE, TRUE
446 );
447 break;
448
449 case 'editUrl':
450 case 'scheduleUrl':
451 // Note: editUrl and scheduleUrl used to be different, but now there's
452 // one screen which can adapt based on permissions (in workflow mode).
453 $value = CRM_Utils_System::url('civicrm/mailing/send',
454 "reset=1&mid={$mailing->id}&continue=true",
455 TRUE, NULL, FALSE, TRUE
456 );
457 break;
458
459 case 'html':
460 $page = new CRM_Mailing_Page_View();
461 $value = $page->run($mailing->id, NULL, FALSE, TRUE);
462 break;
463
464 case 'approvalStatus':
465 $value = CRM_Core_PseudoConstant::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id);
466 break;
467
468 case 'approvalNote':
469 $value = $mailing->approval_note;
470 break;
471
472 case 'approveUrl':
473 $value = CRM_Utils_System::url('civicrm/mailing/approve',
474 "reset=1&mid={$mailing->id}",
475 TRUE, NULL, FALSE, TRUE
476 );
477 break;
478
479 case 'creator':
480 $value = CRM_Contact_BAO_Contact::displayName($mailing->created_id);
481 break;
482
483 case 'creatorEmail':
484 $value = CRM_Contact_BAO_Contact::getPrimaryEmail($mailing->created_id);
485 break;
486
487 default:
488 $value = "{mailing.$token}";
489 break;
490 }
491
492 if ($escapeSmarty) {
493 $value = self::tokenEscapeSmarty($value);
494 }
495 return $value;
496 }
497
498 /**
499 * Replace all action tokens in $str
500 *
501 * @param string $str
502 * The string with tokens to be replaced.
503 * @param array $addresses
504 * Assoc. array of VERP event addresses.
505 * @param array $urls
506 * Assoc. array of action URLs.
507 * @param bool $html
508 * Replace tokens with HTML or plain text.
509 * @param array $knownTokens
510 * A list of tokens that are known to exist in the email body.
511 *
512 * @param bool $escapeSmarty
513 *
514 * @return string
515 * The processed string
516 */
517 public static function &replaceActionTokens(
518 $str,
519 &$addresses,
520 &$urls,
521 $html = FALSE,
522 $knownTokens = NULL,
523 $escapeSmarty = FALSE
524 ) {
525 $key = 'action';
526 // here we intersect with the list of pre-configured valid tokens
527 // so that we remove anything we do not recognize
528 // I hope to move this step out of here soon and
529 // then we will just iterate on a list of tokens that are passed to us
530 if (!$knownTokens || empty($knownTokens[$key])) {
531 return $str;
532 }
533
534 $str = preg_replace_callback(
535 self::tokenRegex($key),
536 function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) {
537 return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
538 },
539 $str
540 );
541 return $str;
542 }
543
544 /**
545 * @deprecated
546 *
547 * @param $token
548 * @param $addresses
549 * @param $urls
550 * @param bool $html
551 * @param bool $escapeSmarty
552 *
553 * @return mixed|string
554 */
555 public static function getActionTokenReplacement(
556 $token,
557 &$addresses,
558 &$urls,
559 $html = FALSE,
560 $escapeSmarty = FALSE
561 ) {
562 // If the token is an email action, use it. Otherwise, find the
563 // appropriate URL.
564
565 if (!in_array($token, self::$_tokens['action'])) {
566 $value = "{action.$token}";
567 }
568 else {
569 $value = $addresses[$token] ?? NULL;
570
571 if ($value == NULL) {
572 $value = $urls[$token] ?? NULL;
573 }
574
575 if ($value && $html) {
576 // fix for CRM-2318
577 if ((substr($token, -3) != 'Url') && ($token != 'forward')) {
578 $value = "mailto:$value";
579 }
580 }
581 elseif ($value && !$html) {
582 $value = str_replace('&amp;', '&', $value);
583 }
584 }
585
586 if ($escapeSmarty) {
587 $value = self::tokenEscapeSmarty($value);
588 }
589 return $value;
590 }
591
592 /**
593 * Replace all the contact-level tokens in $str with information from
594 * $contact.
595 *
596 * @param string $str
597 * The string with tokens to be replaced.
598 * @param array $contact
599 * Associative array of contact properties.
600 * @param bool $html
601 * Replace tokens with HTML or plain text.
602 * @param array $knownTokens
603 * A list of tokens that are known to exist in the email body.
604 * @param bool $returnBlankToken
605 * Return unevaluated token if value is null.
606 *
607 * @deprecated
608 *
609 * @param bool $escapeSmarty
610 *
611 * @return string
612 * The processed string
613 */
614 public static function replaceContactTokens(
615 $str,
616 &$contact,
617 $html = FALSE,
618 $knownTokens = NULL,
619 $returnBlankToken = FALSE,
620 $escapeSmarty = FALSE
621 ) {
622 // Refresh contact tokens in case they have changed. There is heavy caching
623 // in exportable fields so there is no benefit in doing this conditionally.
624 self::$_tokens['contact'] = array_merge(
625 array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
626 ['checksum', 'contact_id']
627 );
628
629 $key = 'contact';
630 // here we intersect with the list of pre-configured valid tokens
631 // so that we remove anything we do not recognize
632 // I hope to move this step out of here soon and
633 // then we will just iterate on a list of tokens that are passed to us
634 if (!$knownTokens || empty($knownTokens[$key])) {
635 return $str;
636 }
637
638 $str = preg_replace_callback(
639 self::tokenRegex($key),
640 function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) {
641 return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
642 },
643 $str
644 );
645
646 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
647 return $str;
648 }
649
650 /**
651 * Do Not use.
652 *
653 * Only core usage is from a deprecated unused function and
654 * from deprecated BAO_Mailing code (to be replaced by flexmailer).
655 *
656 * @deprecated
657 *
658 * @param $token
659 * @param $contact
660 * @param bool $html
661 * @param bool $returnBlankToken
662 * @param bool $escapeSmarty
663 *
664 * @return bool|mixed|null|string
665 */
666 public static function getContactTokenReplacement(
667 $token,
668 &$contact,
669 $html = FALSE,
670 $returnBlankToken = FALSE,
671 $escapeSmarty = FALSE
672 ) {
673 if (self::$_tokens['contact'] == NULL) {
674 /* This should come from UF */
675
676 self::$_tokens['contact']
677 = array_merge(
678 array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
679 ['checksum', 'contact_id']
680 );
681 }
682
683 // Construct value from $token and $contact
684
685 $value = NULL;
686 $noReplace = FALSE;
687
688 // Support legacy tokens
689 $token = CRM_Utils_Array::value($token, self::legacyContactTokens(), $token);
690
691 // check if the token we were passed is valid
692 // we have to do this because this function is
693 // called only when we find a token in the string
694
695 if (!in_array(str_replace(':label', '', $token), self::$_tokens['contact'])) {
696 $noReplace = TRUE;
697 }
698 elseif ($token == 'checksum') {
699 $hash = $contact['hash'] ?? NULL;
700 $contactID = CRM_Utils_Array::retrieveValueRecursive($contact, 'contact_id');
701 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID,
702 NULL,
703 NULL,
704 $hash
705 );
706 $value = "cs={$cs}";
707 }
708 else {
709 $value = (array) CRM_Utils_Array::retrieveValueRecursive($contact, str_replace(':label', '', $token));
710
711 foreach ($value as $index => $item) {
712 $value[$index] = self::convertPseudoConstantsUsingMetadata($value[$index], str_replace(':label', '', $token));
713 }
714 $value = implode(', ', $value);
715 }
716
717 if (!$html) {
718 $value = str_replace('&amp;', '&', $value);
719 }
720
721 // if null then return actual token
722 if ($returnBlankToken && !$value) {
723 $noReplace = TRUE;
724 }
725
726 if ($noReplace) {
727 $value = "{contact.$token}";
728 }
729
730 if ($escapeSmarty
731 && !($returnBlankToken && $noReplace)
732 ) {
733 // $returnBlankToken means the caller wants to do further attempts at
734 // processing unreplaced tokens -- so don't escape them yet in this case.
735 $value = self::tokenEscapeSmarty($value);
736 }
737
738 return $value;
739 }
740
741 /**
742 * Do not use - unused in core.
743 *
744 * Replace all the hook tokens in $str with information from
745 * $contact.
746 *
747 * @deprecated
748 *
749 * @param string $str
750 * The string with tokens to be replaced.
751 * @param array $contact
752 * Associative array of contact properties (including hook token values).
753 * @param $categories
754 * @param bool $html
755 * Replace tokens with HTML or plain text.
756 *
757 * @param bool $escapeSmarty
758 *
759 * @return string
760 * The processed string
761 */
762 public static function &replaceHookTokens(
763 $str,
764 &$contact,
765 $categories = NULL,
766 $html = FALSE,
767 $escapeSmarty = FALSE
768 ) {
769 if (!$categories) {
770 $categories = self::getTokenCategories();
771 }
772 foreach ($categories as $key) {
773 $str = preg_replace_callback(
774 self::tokenRegex($key),
775 function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
776 return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
777 },
778 $str
779 );
780 }
781 return $str;
782 }
783
784 /**
785 * Get the categories required for rendering tokens.
786 *
787 * @return array
788 */
789 public static function getTokenCategories(): array {
790 if (!isset(\Civi::$statics[__CLASS__]['token_categories'])) {
791 $tokens = [];
792 \CRM_Utils_Hook::tokens($tokens);
793 \Civi::$statics[__CLASS__]['token_categories'] = array_keys($tokens);
794 }
795 return \Civi::$statics[__CLASS__]['token_categories'];
796 }
797
798 /**
799 * Parse html through Smarty resolving any smarty functions.
800 * @param string $tokenHtml
801 * @param array $entity
802 * @param string $entityType
803 * @return string
804 * html parsed through smarty
805 * @deprecated
806 */
807 public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
808 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
809 $smarty = CRM_Core_Smarty::singleton();
810 // also add the tokens to the template
811 $smarty->assign_by_ref($entityType, $entity);
812 $tokenHtml = $smarty->fetch("string:$tokenHtml");
813 }
814 return $tokenHtml;
815 }
816
817 /**
818 * Do not use, unused in core.
819 *
820 * @deprecated
821 *
822 * @param $token
823 * @param $contact
824 * @param $category
825 * @param bool $html
826 * @param bool $escapeSmarty
827 *
828 * @return mixed|string
829 */
830 public static function getHookTokenReplacement(
831 $token,
832 &$contact,
833 $category,
834 $html = FALSE,
835 $escapeSmarty = FALSE
836 ) {
837 $value = $contact["{$category}.{$token}"] ?? NULL;
838
839 if ($value && !$html) {
840 $value = str_replace('&amp;', '&', $value);
841 }
842
843 if ($escapeSmarty) {
844 $value = self::tokenEscapeSmarty($value);
845 }
846
847 return $value;
848 }
849
850 /**
851 * unescapeTokens removes any characters that caused the replacement routines to skip token replacement
852 * for example {{token}} or \{token} will result in {token} in the final email
853 *
854 * this routine will remove the extra backslashes and braces
855 *
856 * @deprecated
857 *
858 * @param string $str ref to the string that will be scanned and modified
859 */
860 public static function unescapeTokens(&$str) {
861 $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
862 }
863
864 /**
865 * Replace unsubscribe tokens.
866 *
867 * @param string $str
868 * The string with tokens to be replaced.
869 * @param object $domain
870 * The domain BAO.
871 * @param array $groups
872 * The groups (if any) being unsubscribed.
873 * @param bool $html
874 * Replace tokens with html or plain text.
875 * @param int $contact_id
876 * The contact ID.
877 * @param string $hash The security hash of the unsub event
878 *
879 * @return string
880 * The processed string
881 */
882 public static function &replaceUnsubscribeTokens(
883 $str,
884 &$domain,
885 &$groups,
886 $html,
887 $contact_id,
888 $hash
889 ) {
890 if (self::token_match('unsubscribe', 'group', $str)) {
891 if (!empty($groups)) {
892 $config = CRM_Core_Config::singleton();
893 $base = CRM_Utils_System::baseURL();
894
895 // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented
896 $dao = new CRM_Contact_DAO_Group();
897 $dao->find();
898 while ($dao->fetch()) {
899 if (substr($dao->visibility, 0, 6) == 'Public') {
900 $visibleGroups[] = $dao->id;
901 }
902 }
903 $value = implode(', ', $groups);
904 self::token_replace('unsubscribe', 'group', $value, $str);
905 }
906 }
907 return $str;
908 }
909
910 /**
911 * Replace resubscribe tokens.
912 *
913 * @param string $str
914 * The string with tokens to be replaced.
915 * @param object $domain
916 * The domain BAO.
917 * @param array $groups
918 * The groups (if any) being resubscribed.
919 * @param bool $html
920 * Replace tokens with html or plain text.
921 * @param int $contact_id
922 * The contact ID.
923 * @param string $hash The security hash of the resub event
924 *
925 * @return string
926 * The processed string
927 */
928 public static function &replaceResubscribeTokens(
929 $str, &$domain, &$groups, $html,
930 $contact_id, $hash
931 ) {
932 if (self::token_match('resubscribe', 'group', $str)) {
933 if (!empty($groups)) {
934 $value = implode(', ', $groups);
935 self::token_replace('resubscribe', 'group', $value, $str);
936 }
937 }
938 return $str;
939 }
940
941 /**
942 * Replace subscription-confirmation-request tokens
943 *
944 * @param string $str
945 * The string with tokens to be replaced.
946 * @param string $group
947 * The name of the group being subscribed.
948 * @param $url
949 * @param bool $html
950 * Replace tokens with html or plain text.
951 *
952 * @return string
953 * The processed string
954 */
955 public static function &replaceSubscribeTokens($str, $group, $url, $html) {
956 if (self::token_match('subscribe', 'group', $str)) {
957 self::token_replace('subscribe', 'group', $group, $str);
958 }
959 if (self::token_match('subscribe', 'url', $str)) {
960 self::token_replace('subscribe', 'url', $url, $str);
961 }
962 return $str;
963 }
964
965 /**
966 * Replace subscription-invitation tokens
967 *
968 * @param string $str
969 * The string with tokens to be replaced.
970 *
971 * @return string
972 * The processed string
973 */
974 public static function &replaceSubscribeInviteTokens($str) {
975 if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
976 $url = CRM_Utils_System::url('civicrm/mailing/subscribe',
977 'reset=1',
978 TRUE, NULL, FALSE, TRUE
979 );
980 $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str);
981 }
982
983 if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) {
984 foreach ($matches as $key => $value) {
985 $gid = substr($value, 21, -1);
986 $url = CRM_Utils_System::url('civicrm/mailing/subscribe',
987 "reset=1&gid={$gid}",
988 TRUE, NULL, FALSE, TRUE
989 );
990 $str = preg_replace('/' . preg_quote($value) . '/', $url, $str);
991 }
992 }
993
994 if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
995 foreach ($matches as $key => $value) {
996 $gid = substr($value, 18, -1);
997 $config = CRM_Core_Config::singleton();
998 $domain = CRM_Core_BAO_MailSettings::defaultDomain();
999 $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
1000 // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
1001 $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
1002 }
1003 }
1004 return $str;
1005 }
1006
1007 /**
1008 * Replace welcome/confirmation tokens
1009 *
1010 * @param string $str
1011 * The string with tokens to be replaced.
1012 * @param string $group
1013 * The name of the group being subscribed.
1014 * @param bool $html
1015 * Replace tokens with html or plain text.
1016 *
1017 * @return string
1018 * The processed string
1019 */
1020 public static function &replaceWelcomeTokens($str, $group, $html) {
1021 if (self::token_match('welcome', 'group', $str)) {
1022 self::token_replace('welcome', 'group', $group, $str);
1023 }
1024 return $str;
1025 }
1026
1027 /**
1028 * Find unprocessed tokens (call this last)
1029 *
1030 * @param string $str
1031 * The string to search.
1032 *
1033 * @return array
1034 * Array of tokens that weren't replaced
1035 */
1036 public static function &unmatchedTokens(&$str) {
1037 //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
1038 preg_match_all('/\{(\w+\.\w+)\}/', $str, $match);
1039 return $match[1];
1040 }
1041
1042 /**
1043 * Find and replace tokens for each component.
1044 *
1045 * @param string $str
1046 * The string to search.
1047 * @param array $contact
1048 * Associative array of contact properties.
1049 * @param array $components
1050 * A list of tokens that are known to exist in the email body.
1051 *
1052 * @param bool $escapeSmarty
1053 * @param bool $returnEmptyToken
1054 *
1055 * @return string
1056 * The processed string
1057 *
1058 * @deprecated
1059 */
1060 public static function replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
1061 CRM_Core_Error::deprecatedFunctionWarning('use the token processor');
1062 if (!is_array($components) || empty($contact)) {
1063 return $str;
1064 }
1065
1066 foreach ($components as $name => $tokens) {
1067 if (!is_array($tokens) || empty($tokens)) {
1068 continue;
1069 }
1070
1071 foreach ($tokens as $token) {
1072 if (self::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) {
1073 self::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty);
1074 }
1075 elseif (!$returnEmptyToken) {
1076 //replacing empty token
1077 self::token_replace($name, $token, "", $str, $escapeSmarty);
1078 }
1079 }
1080 }
1081 return $str;
1082 }
1083
1084 /**
1085 * Get array of string tokens.
1086 *
1087 * @param string $string
1088 * The input string to parse for tokens.
1089 *
1090 * @return array
1091 * array of tokens mentioned in field
1092 */
1093 public static function getTokens($string) {
1094 $matches = [];
1095 $tokens = [];
1096 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+(:|.)?\w*)\}(?!\})/',
1097 $string,
1098 $matches,
1099 PREG_PATTERN_ORDER
1100 );
1101
1102 if ($matches[1]) {
1103 foreach ($matches[1] as $token) {
1104 $parts = explode('.', $token, 3);
1105 $type = $parts[0];
1106 $name = $parts[1];
1107 $suffix = !empty($parts[2]) ? ('.' . $parts[2]) : '';
1108 if ($name && $type) {
1109 if (!isset($tokens[$type])) {
1110 $tokens[$type] = [];
1111 }
1112 $tokens[$type][] = $name . $suffix;
1113 }
1114 }
1115 }
1116 return $tokens;
1117 }
1118
1119 /**
1120 * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
1121 * and getTokens appears to be historical rather than intentional and should be reviewed
1122 * @param $string
1123 * @return array
1124 * fields to pass in as return properties when populating token
1125 */
1126 public static function getReturnProperties(&$string) {
1127 $returnProperties = [];
1128 $matches = [];
1129 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
1130 $string,
1131 $matches,
1132 PREG_PATTERN_ORDER
1133 );
1134 if ($matches[1]) {
1135 foreach ($matches[1] as $token) {
1136 [$type, $name] = preg_split('/\./', $token, 2);
1137 if ($name) {
1138 $returnProperties["{$name}"] = 1;
1139 }
1140 }
1141 }
1142
1143 return $returnProperties;
1144 }
1145
1146 /**
1147 * Do not use this function.
1148 *
1149 * Gives required details of contacts in an indexed array format so we
1150 * can iterate in a nice loop and do token evaluation
1151 *
1152 * @param array $contactIDs
1153 * @param array $returnProperties
1154 * Of required properties.
1155 * @param bool $skipOnHold Don't return on_hold contact info also.
1156 * Don't return on_hold contact info also.
1157 * @param bool $skipDeceased Don't return deceased contact info.
1158 * Don't return deceased contact info.
1159 * @param array $extraParams
1160 * Extra params - DEPRECATED
1161 * @param array $tokens
1162 * The list of tokens we've extracted from the content.
1163 * @param string|null $className
1164 * @param int|null $jobID
1165 * The mailing list jobID - this is a legacy param.
1166 *
1167 * @deprecated
1168 *
1169 * @return array - e.g [[1 => ['first_name' => 'bob'...], 34 => ['first_name' => 'fred'...]]]
1170 */
1171 public static function getTokenDetails(
1172 $contactIDs,
1173 $returnProperties = NULL,
1174 $skipOnHold = TRUE,
1175 $skipDeceased = TRUE,
1176 $extraParams = NULL,
1177 $tokens = [],
1178 $className = NULL,
1179 $jobID = NULL
1180 ) {
1181 $params = [];
1182 foreach ($contactIDs as $contactID) {
1183 $params[] = [
1184 CRM_Core_Form::CB_PREFIX . $contactID,
1185 '=',
1186 1,
1187 0,
1188 0,
1189 ];
1190 }
1191
1192 // fix for CRM-2613
1193 if ($skipDeceased) {
1194 $params[] = ['is_deceased', '=', 0, 0, 0];
1195 }
1196
1197 //fix for CRM-3798
1198 if ($skipOnHold) {
1199 $params[] = ['on_hold', '=', 0, 0, 0];
1200 }
1201
1202 if ($extraParams) {
1203 CRM_Core_Error::deprecatedWarning('Passing $extraParams to getTokenDetails() is not supported and will be removed in a future version');
1204 $params = array_merge($params, $extraParams);
1205 }
1206
1207 // if return properties are not passed then get all return properties
1208 if (empty($returnProperties)) {
1209 $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()),
1210 ['display_name', 'checksum', 'contact_id']
1211 );
1212 foreach ($fields as $val) {
1213 // The unavailable fields are not available as tokens, do not have a one-2-one relationship
1214 // with contacts and are expensive to resolve.
1215 // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
1216 // and upstream calls to this should populate return properties.
1217 $unavailableFields = ['group', 'tag'];
1218 if (!in_array($val, $unavailableFields)) {
1219 $returnProperties[$val] = 1;
1220 }
1221 }
1222 }
1223
1224 $custom = [];
1225 foreach ($returnProperties as $name => $dontCare) {
1226 $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
1227 if ($cfID) {
1228 $custom[] = $cfID;
1229 }
1230 }
1231
1232 [$contactDetails] = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs), TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, TRUE);
1233
1234 foreach ($contactIDs as $contactID) {
1235 if (array_key_exists($contactID, $contactDetails)) {
1236 if (!empty($contactDetails[$contactID]['preferred_communication_method'])
1237 ) {
1238 $communicationPreferences = [];
1239 foreach ((array) $contactDetails[$contactID]['preferred_communication_method'] as $val) {
1240 if ($val) {
1241 $communicationPreferences[$val] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
1242 }
1243 }
1244 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $communicationPreferences);
1245 }
1246
1247 foreach ($custom as $cfID) {
1248 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
1249 $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID);
1250 }
1251 }
1252
1253 // special case for greeting replacement
1254 foreach ([
1255 'email_greeting',
1256 'postal_greeting',
1257 'addressee',
1258 ] as $val) {
1259 if (!empty($contactDetails[$contactID][$val])) {
1260 $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
1261 }
1262 }
1263 }
1264 }
1265
1266 // $contactDetails = &$details[0] = is an array of [ contactID => contactDetails ]
1267 // also call a hook and get token details
1268 CRM_Utils_Hook::tokenValues($contactDetails,
1269 $contactIDs,
1270 $jobID,
1271 $tokens,
1272 $className
1273 );
1274 return [$contactDetails];
1275 }
1276
1277 /**
1278 * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact
1279 * specific tokens to be rendered
1280 *
1281 * @param array $contactIDs
1282 * This should always be array(0) or its not anonymous - left to keep signature same.
1283 * as main fn
1284 * @param string $returnProperties
1285 * @param bool $skipOnHold
1286 * @param bool $skipDeceased
1287 * @param string $extraParams
1288 * @param array $tokens
1289 * @param string $className
1290 * Sent as context to the hook.
1291 * @param string $jobID
1292 * @return array
1293 * contactDetails with hooks swapped out
1294 *
1295 * @deprecated
1296 */
1297 public static function getAnonymousTokenDetails($contactIDs = [0],
1298 $returnProperties = NULL,
1299 $skipOnHold = TRUE,
1300 $skipDeceased = TRUE,
1301 $extraParams = NULL,
1302 $tokens = [],
1303 $className = NULL,
1304 $jobID = NULL) {
1305 $details = [0 => []];
1306 // also call a hook and get token details
1307 CRM_Utils_Hook::tokenValues($details[0],
1308 $contactIDs,
1309 $jobID,
1310 $tokens,
1311 $className
1312 );
1313 return $details;
1314 }
1315
1316 /**
1317 * Get Membership Token Details.
1318 * @param array $membershipIDs
1319 * Array of membership IDS.
1320 *
1321 * @deprecated
1322 */
1323 public static function getMembershipTokenDetails($membershipIDs) {
1324 $memberships = civicrm_api3('membership', 'get', [
1325 'options' => ['limit' => 0],
1326 'membership_id' => ['IN' => (array) $membershipIDs],
1327 ]);
1328 return $memberships['values'];
1329 }
1330
1331 /**
1332 * Replace existing greeting tokens in message/subject.
1333 *
1334 * This function operates by reference, modifying the first parameter. Other
1335 * methods for token replacement in this class return the modified string.
1336 * This leads to inconsistency in how these methods must be applied.
1337 *
1338 * @TODO Remove that inconsistency in usage.
1339 *
1340 * @param string $tokenString
1341 * @param array $contactDetails
1342 * @param int $contactId
1343 * @param string $className
1344 * @param bool $escapeSmarty
1345 */
1346 public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
1347
1348 if (!$contactDetails && !$contactId) {
1349 return;
1350 }
1351 // check if there are any tokens
1352 $greetingTokens = self::getTokens($tokenString);
1353 $context = $contactId ? ['contactId' => $contactId] : [];
1354 if ($contactDetails) {
1355 $context['contact'] = isset($contactDetails[0]) ? reset($contactDetails[0]) : $contactDetails;
1356 }
1357 $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
1358 'smarty' => FALSE,
1359 'class' => $className,
1360 ]);
1361 $tokenProcessor->addRow($context);
1362 $tokenProcessor->addMessage('greeting', $tokenString, 'text/plain');
1363 $tokenProcessor->evaluate();
1364 foreach ($tokenProcessor->getRows() as $row) {
1365 $tokenString = $row->render('greeting');
1366 }
1367 }
1368
1369 /**
1370 * At this point, $contactDetails has loaded the contact from the DAO. Any
1371 * (non-custom) missing fields are null. By removing them, we can avoid
1372 * expensive calls to CRM_Contact_BAO_Query.
1373 *
1374 * @deprecated unused in core
1375 *
1376 * @param string $tokenString
1377 * @param array $contactDetails
1378 * @param array $greetingTokens
1379 */
1380 private static function removeNullContactTokens(&$tokenString, $contactDetails, &$greetingTokens) {
1381
1382 // Only applies to contact tokens
1383 if (!array_key_exists('contact', $greetingTokens)) {
1384 return;
1385 }
1386
1387 $greetingTokensOriginal = $greetingTokens;
1388 $contactFieldList = CRM_Contact_DAO_Contact::fields();
1389 // Sometimes contactDetails are in a multidemensional array, sometimes a
1390 // single-dimension array.
1391 if (array_key_exists(0, $contactDetails) && is_array($contactDetails[0])) {
1392 $contactDetails = current($contactDetails[0]);
1393 }
1394 $nullFields = array_keys(array_diff_key($contactFieldList, $contactDetails));
1395
1396 // Handle legacy tokens
1397 foreach (self::legacyContactTokens() as $oldToken => $newToken) {
1398 if (CRM_Utils_Array::key($newToken, $nullFields)) {
1399 $nullFields[] = $oldToken;
1400 }
1401 }
1402
1403 // Remove null contact fields from $greetingTokens
1404 $greetingTokens['contact'] = array_diff($greetingTokens['contact'], $nullFields);
1405
1406 // Also remove them from $tokenString
1407 $removedTokens = array_diff($greetingTokensOriginal['contact'], $greetingTokens['contact']);
1408 // Handle legacy tokens again, sigh
1409 if (!empty($removedTokens)) {
1410 foreach ($removedTokens as $token) {
1411 if (CRM_Utils_Array::value($token, self::legacyContactTokens()) !== NULL) {
1412 $removedTokens[] = CRM_Utils_Array::value($token, self::legacyContactTokens());
1413 }
1414 }
1415 foreach ($removedTokens as $token) {
1416 $tokenString = str_replace("{contact.$token}", '', $tokenString);
1417 }
1418 }
1419 }
1420
1421 /**
1422 * @param $tokens
1423 *
1424 * @return array
1425 */
1426 public static function flattenTokens(&$tokens) {
1427 $flattenTokens = [];
1428
1429 foreach ([
1430 'html',
1431 'text',
1432 'subject',
1433 ] as $prop) {
1434 if (!isset($tokens[$prop])) {
1435 continue;
1436 }
1437 foreach ($tokens[$prop] as $type => $names) {
1438 if (!isset($flattenTokens[$type])) {
1439 $flattenTokens[$type] = [];
1440 }
1441 foreach ($names as $name) {
1442 $flattenTokens[$type][$name] = 1;
1443 }
1444 }
1445 }
1446
1447 return $flattenTokens;
1448 }
1449
1450 /**
1451 * Replace all user tokens in $str
1452 *
1453 * @param string $str
1454 * The string with tokens to be replaced.
1455 *
1456 * @param null $knownTokens
1457 * @param bool $escapeSmarty
1458 *
1459 * @return string
1460 * The processed string
1461 */
1462 public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
1463 $key = 'user';
1464 if (!$knownTokens ||
1465 !isset($knownTokens[$key])
1466 ) {
1467 return $str;
1468 }
1469
1470 $str = preg_replace_callback(
1471 self::tokenRegex($key),
1472 function ($matches) use ($escapeSmarty) {
1473 return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
1474 },
1475 $str
1476 );
1477 return $str;
1478 }
1479
1480 /**
1481 * @param $token
1482 * @param bool $escapeSmarty
1483 *
1484 * @return string
1485 */
1486 public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) {
1487 $value = '';
1488
1489 [$objectName, $objectValue] = explode('-', $token, 2);
1490
1491 switch ($objectName) {
1492 case 'permission':
1493 $value = CRM_Core_Permission::permissionEmails($objectValue);
1494 break;
1495
1496 case 'role':
1497 $value = CRM_Core_Permission::roleEmails($objectValue);
1498 break;
1499 }
1500
1501 if ($escapeSmarty) {
1502 $value = self::tokenEscapeSmarty($value);
1503 }
1504
1505 return $value;
1506 }
1507
1508 /**
1509 * @deprecated
1510 *
1511 * Do not use this function - it still needs full removal from active code
1512 * in CRM_Contribute_Form_Task_PDFLetter.
1513 */
1514 protected static function _buildContributionTokens() {
1515 $key = 'contribution';
1516
1517 if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$key])) {
1518 $tokens = array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
1519 ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []],
1520 self::getCustomFieldTokens('Contribution'),
1521 [
1522 'financial_type_id:label',
1523 'financial_type_id:name',
1524 'contribution_page_id:label',
1525 'contribution_page_id:name',
1526 'payment_instrument_id:label',
1527 'payment_instrument_id:name',
1528 'is_test:label',
1529 'is_pay_later:label',
1530 'contribution_status_id:label',
1531 'contribution_status_id:name',
1532 'is_template:label',
1533 'campaign_id:label',
1534 'campaign_id:name',
1535 ]
1536 );
1537 foreach ($tokens as $token) {
1538 if (!empty($token['name'])) {
1539 $tokens[$token['name']] = [];
1540 }
1541 elseif (is_string($token) && strpos($token, ':') !== FALSE) {
1542 $tokens[$token] = [];
1543 }
1544 }
1545 Civi::$statics[__CLASS__][__FUNCTION__][$key] = array_keys($tokens);
1546 }
1547 self::$_tokens[$key] = Civi::$statics[__CLASS__][__FUNCTION__][$key];
1548 }
1549
1550 /**
1551 * Do not use.
1552 *
1553 * @deprecated
1554 *
1555 * Replace tokens for an entity.
1556 * @param string $entity
1557 * @param array $entityArray
1558 * (e.g. in format from api).
1559 * @param string $str
1560 * String to replace in.
1561 * @param array $knownTokens
1562 * Array of tokens present.
1563 * @param bool $escapeSmarty
1564 * @return string
1565 * string with replacements made
1566 */
1567 public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1568 if (!$knownTokens || empty($knownTokens[$entity])) {
1569 return $str;
1570 }
1571
1572 $fn = 'get' . ucfirst($entity) . 'TokenReplacement';
1573 $fn = is_callable(['CRM_Utils_Token', $fn]) ? $fn : 'getApiTokenReplacement';
1574 // since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
1575 foreach ($knownTokens[$entity] as $token) {
1576 // We are now supporting the syntax case_type_id:label
1577 // so strip anything after the ':'
1578 // (we aren't supporting 'name' at this stage, so we can assume 'label'
1579 // test cover in TokenConsistencyTest.
1580 $parts = explode(':', $token);
1581 $replacement = self::$fn($entity, $parts[0], $entityArray);
1582 if ($escapeSmarty) {
1583 $replacement = self::tokenEscapeSmarty($replacement);
1584 }
1585 $str = str_replace('{' . $entity . '.' . $token . '}', $replacement, $str);
1586 }
1587 return preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1588 }
1589
1590 /**
1591 * @deprecated
1592 *
1593 * @param int $caseId
1594 * @param string $str
1595 * @param array $knownTokens
1596 * @param bool $escapeSmarty
1597 * @return string
1598 * @throws \CiviCRM_API3_Exception
1599 */
1600 public static function replaceCaseTokens($caseId, $str, $knownTokens = NULL, $escapeSmarty = FALSE): string {
1601 if (strpos($str, '{case.') === FALSE) {
1602 return $str;
1603 }
1604 if (!$knownTokens) {
1605 $knownTokens = self::getTokens($str);
1606 }
1607 $case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
1608 return self::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
1609 }
1610
1611 /**
1612 * Generic function for formatting token replacement for an api field
1613 *
1614 * @deprecated
1615 *
1616 * @param string $entity
1617 * @param string $token
1618 * @param array $entityArray
1619 * @return string
1620 * @throws \CiviCRM_API3_Exception
1621 */
1622 public static function getApiTokenReplacement($entity, $token, $entityArray) {
1623 if (!isset($entityArray[$token])) {
1624 return '';
1625 }
1626 $field = civicrm_api3($entity, 'getfield', ['action' => 'get', 'name' => $token, 'get_options' => 'get']);
1627 $field = $field['values'];
1628 $fieldType = $field['type'] ?? NULL;
1629 // Boolean fields
1630 if ($fieldType == CRM_Utils_Type::T_BOOLEAN && empty($field['options'])) {
1631 $field['options'] = [ts('No'), ts('Yes')];
1632 }
1633 // Match pseudoconstants
1634 if (!empty($field['options'])) {
1635 $ret = [];
1636 foreach ((array) $entityArray[$token] as $val) {
1637 $ret[] = $field['options'][$val];
1638 }
1639 return implode(', ', $ret);
1640 }
1641 // Format date fields
1642 elseif ($entityArray[$token] && in_array($fieldType, [CRM_Utils_Type::T_DATE, CRM_Utils_Type::T_TIMESTAMP, (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)])) {
1643 return CRM_Utils_Date::customFormat($entityArray[$token]);
1644 }
1645 return implode(', ', (array) $entityArray[$token]);
1646 }
1647
1648 /**
1649 * Do not use - unused in core.
1650 *
1651 * Replace Contribution tokens in html.
1652 *
1653 * @param string $str
1654 * @param array $contribution
1655 * @param bool|string $html
1656 * @param string $knownTokens
1657 * @param bool|string $escapeSmarty
1658 *
1659 * @deprecated
1660 *
1661 * @return mixed
1662 */
1663 public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1664 $key = 'contribution';
1665 if (!$knownTokens || empty($knownTokens[$key])) {
1666 //early return
1667 return $str;
1668 }
1669
1670 // here we intersect with the list of pre-configured valid tokens
1671 // so that we remove anything we do not recognize
1672 // I hope to move this step out of here soon and
1673 // then we will just iterate on a list of tokens that are passed to us
1674
1675 $str = preg_replace_callback(
1676 self::tokenRegex($key),
1677 function ($matches) use (&$contribution, $html, $escapeSmarty) {
1678 return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
1679 },
1680 $str
1681 );
1682
1683 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1684 return $str;
1685 }
1686
1687 /**
1688 * We have a situation where we are rendering more than one token in each field because we are combining
1689 * tokens from more than one contribution when pdf thank you letters are grouped (CRM-14367)
1690 *
1691 * The replaceContributionToken doesn't handle receive_date correctly in this scenario because of the formatting
1692 * it applies (other tokens are OK including date fields)
1693 *
1694 * So we sort this out & then call the main function. Note that we are not escaping smarty on this fields like the main function
1695 * does - but the fields is already being formatted through a date function
1696 *
1697 * @param string $separator
1698 * @param string $str
1699 * @param array $contributions
1700 * @param array $knownTokens
1701 *
1702 * @deprecated
1703 *
1704 * @return string
1705 */
1706 public static function replaceMultipleContributionTokens(string $separator, string $str, array $contributions, array $knownTokens): string {
1707 CRM_Core_Error::deprecatedFunctionWarning('no alternative');
1708 foreach ($knownTokens['contribution'] ?? [] as $token) {
1709 $resolvedTokens = [];
1710 foreach ($contributions as $contribution) {
1711 $resolvedTokens[] = self::replaceContributionTokens('{contribution.' . $token . '}', $contribution, FALSE, $knownTokens);
1712 }
1713 $str = self::token_replace('contribution', $token, implode($separator, $resolvedTokens), $str);
1714 }
1715 return $str;
1716 }
1717
1718 /**
1719 * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
1720 * - this is used by the pdfLetter task from membership search
1721 *
1722 * This is called via replaceEntityTokens.
1723 *
1724 * In the near term it will not be called at all from core as
1725 * the pdf letter task is updated to use the processor.
1726 *
1727 * @deprecated
1728 *
1729 * @param string $entity
1730 * should always be "membership"
1731 * @param string $token
1732 * field name
1733 * @param array $membership
1734 * An api result array for a single membership.
1735 * @return string token replacement
1736 */
1737 public static function getMembershipTokenReplacement($entity, $token, $membership) {
1738 $supportedTokens = [
1739 'id',
1740 'status',
1741 'status_id',
1742 'type',
1743 'membership_type_id',
1744 'start_date',
1745 'join_date',
1746 'end_date',
1747 'fee',
1748 ];
1749 switch ($token) {
1750 case 'type':
1751 // membership_type_id would only be requested if the calling
1752 // class is mapping it to '{membership:membership_type_id:label'}
1753 case 'membership_type_id':
1754 $value = $membership['membership_name'];
1755 break;
1756
1757 case 'status':
1758 // status_id would only be requested if the calling
1759 // class is mapping it to '{membership:status_id:label'}
1760 case 'status_id':
1761 $statuses = CRM_Member_BAO_Membership::buildOptions('status_id');
1762 $value = $statuses[$membership['status_id']];
1763 break;
1764
1765 case 'fee':
1766 try {
1767 $value = civicrm_api3('membership_type', 'getvalue', [
1768 'id' => $membership['membership_type_id'],
1769 'return' => 'minimum_fee',
1770 ]);
1771 $value = CRM_Utils_Money::format($value, NULL, NULL, TRUE);
1772 }
1773 catch (CiviCRM_API3_Exception $e) {
1774 // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
1775 // api handles NULL (4.4)
1776 $value = 0;
1777 }
1778 break;
1779
1780 default:
1781 if (in_array($token, $supportedTokens)) {
1782 $value = $membership[$token];
1783 if (CRM_Utils_String::endsWith($token, '_date')) {
1784 $value = CRM_Utils_Date::customFormat($value);
1785 }
1786 }
1787 else {
1788 // ie unchanged
1789 $value = "{$entity}.{$token}";
1790 }
1791 break;
1792 }
1793
1794 return $value;
1795 }
1796
1797 /**
1798 * Do not use - unused in core.
1799 *
1800 * @param $token
1801 * @param $contribution
1802 * @param bool $html
1803 * @param bool $escapeSmarty
1804 *
1805 * @deprecated
1806 *
1807 * @return mixed|string
1808 * @throws \CRM_Core_Exception
1809 */
1810 public static function getContributionTokenReplacement($token, $contribution, $html = FALSE, $escapeSmarty = FALSE) {
1811 self::_buildContributionTokens();
1812
1813 switch ($token) {
1814 case 'total_amount':
1815 case 'net_amount':
1816 case 'fee_amount':
1817 case 'non_deductible_amount':
1818 // FIXME: Is this ever a multi-dimensional array? Why use retrieveValueRecursive()?
1819 $amount = CRM_Utils_Array::retrieveValueRecursive($contribution, $token);
1820 $currency = CRM_Utils_Array::retrieveValueRecursive($contribution, 'currency');
1821 $value = CRM_Utils_Money::format($amount, $currency);
1822 break;
1823
1824 case 'receive_date':
1825 case 'receipt_date':
1826 $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token);
1827 $config = CRM_Core_Config::singleton();
1828 $value = CRM_Utils_Date::customFormat($value, $config->dateformatDatetime);
1829 break;
1830
1831 case 'source':
1832 $value = CRM_Utils_Array::retrieveValueRecursive($contribution, 'contribution_source');
1833 break;
1834
1835 default:
1836 if (!in_array($token, self::$_tokens['contribution'])) {
1837 $value = "{contribution.$token}";
1838 }
1839 else {
1840 $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token);
1841 }
1842 break;
1843 }
1844
1845 if ($escapeSmarty) {
1846 $value = self::tokenEscapeSmarty($value);
1847 }
1848 return $value;
1849 }
1850
1851 /**
1852 * @deprecated
1853 *
1854 * Only used from deprecated functions not called by core.
1855 *
1856 * @return array
1857 * [legacy_token => new_token]
1858 */
1859 public static function legacyContactTokens() {
1860 return [
1861 'individual_prefix' => 'prefix_id',
1862 'individual_suffix' => 'suffix_id',
1863 'gender' => 'gender_id',
1864 'communication_style' => 'communication_style_id',
1865 ];
1866 }
1867
1868 /**
1869 * Get all custom field tokens of $entity
1870 *
1871 * @deprecated
1872 *
1873 * @param string $entity
1874 * @return array
1875 * return custom field tokens in array('custom_N' => 'label') format
1876 */
1877 public static function getCustomFieldTokens($entity) {
1878 $customTokens = [];
1879 foreach (CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) {
1880 $customTokens['custom_' . $id] = $info['label'] . ' :: ' . $info['groupTitle'];
1881 }
1882 return $customTokens;
1883 }
1884
1885 /**
1886 * Formats a token list for the select2 widget
1887 *
1888 * @param $tokens
1889 * @return array
1890 */
1891 public static function formatTokensForDisplay($tokens) {
1892 $sorted = $output = [];
1893
1894 // Sort in ascending order by ignoring word case
1895 natcasesort($tokens);
1896
1897 // Attempt to place tokens into optgroups
1898 // @todo These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice.
1899 foreach ($tokens as $k => $v) {
1900 // Check to see if this token is already in a group e.g. for custom fields
1901 $split = explode(' :: ', $v);
1902 if (!empty($split[1])) {
1903 $sorted[$split[1]][] = ['id' => $k, 'text' => $split[0]];
1904 }
1905 // Group by entity
1906 else {
1907 $split = explode('.', trim($k, '{}'));
1908 if (isset($split[1])) {
1909 $entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucwords(str_replace('_', ' ', $split[0]));
1910 }
1911 else {
1912 $entity = 'Contact';
1913 }
1914 $sorted[ts($entity)][] = ['id' => $k, 'text' => $v];
1915 }
1916 }
1917
1918 ksort($sorted);
1919 foreach ($sorted as $k => $v) {
1920 $output[] = ['text' => $k, 'children' => $v];
1921 }
1922
1923 return $output;
1924 }
1925
1926 /**
1927 * @param $value
1928 * @param $token
1929 *
1930 * @return bool|int|mixed|string|null
1931 */
1932 protected static function convertPseudoConstantsUsingMetadata($value, $token) {
1933 // Convert pseudoconstants using metadata
1934 if ($value && is_numeric($value)) {
1935 $allFields = CRM_Contact_BAO_Contact::exportableFields('All');
1936 if (!empty($allFields[$token]['pseudoconstant'])) {
1937 $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value);
1938 }
1939 }
1940 elseif ($value && CRM_Utils_String::endsWith($token, '_date')) {
1941 $value = CRM_Utils_Date::customFormat($value);
1942 }
1943 return $value;
1944 }
1945
1946 /**
1947 * Get token deprecation information.
1948 *
1949 * @return array
1950 */
1951 public static function getTokenDeprecations(): array {
1952 return [
1953 'WorkFlowMessageTemplates' => [
1954 'contribution_invoice_receipt' => [
1955 '$display_name' => 'contact.display_name',
1956 ],
1957 'contribution_online_receipt' => [
1958 '$contributeMode' => 'no longer available / relevant',
1959 '$first_name' => 'contact.first_name',
1960 '$last_name' => 'contact.last_name',
1961 '$displayName' => 'contact.display_name',
1962 ],
1963 'pledge_acknowledgement' => [
1964 '$domain' => 'no longer available / relevant',
1965 '$contact' => 'no longer available / relevant',
1966 ],
1967 'pledge_reminder' => [
1968 '$domain' => 'no longer available / relevant',
1969 '$contact' => 'no longer available / relevant',
1970 ],
1971 ],
1972 ];
1973 }
1974
1975 }