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