3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class to abstract token replacement.
21 class CRM_Utils_Token
{
22 public static $_requiredTokens = NULL;
24 public static $_tokens = [
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
58 // populate this dynamically
60 // populate this dynamically
61 'contribution' => NULL,
70 'subscribe' => ['group'],
71 'unsubscribe' => ['group'],
72 'resubscribe' => ['group'],
73 'welcome' => ['group'],
78 * This is used by CiviMail but will be made redundant by FlexMailer.
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."),
93 return self
::$_requiredTokens;
97 * Check a string (mailing body) for required tokens.
103 * true if all required tokens are found,
104 * else an array of the missing tokens
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();
111 foreach ($requiredTokens as $token => $value) {
112 if (!is_array($value)) {
113 if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
114 $missing[$token] = $value;
120 foreach ($value as $t => $d) {
122 if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) {
127 $missing[$token] = $desc;
132 if (empty($missing)) {
139 * Wrapper for token matching.
141 * @param string $type
142 * The token type (domain,mailing,contact,action).
144 * The token variable.
146 * The string to search.
151 public static function token_match($type, $var, &$str) {
152 $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
153 return preg_match("/(^|[^\{])$token/", $str);
157 * Wrapper for token replacing.
159 * @param string $type
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
167 * @param bool $escapeSmarty
170 * The processed string
172 public static function token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
173 $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
178 $value = self
::tokenEscapeSmarty($value);
180 $str = preg_replace("/([^\{])?$token/", "\${1}$value", $str);
185 * Get the regex for token replacement
187 * @param string $token_type
188 * A string indicating the the type of token to be used in the expression.
191 * regular expression suitable for using in preg_replace
193 private static function tokenRegex(string $token_type) {
194 return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(:|\.)?\w*(\-[\w\s]+)?)\}(?!\})/';
198 * Escape the string so a malicious user cannot inject smarty code into the template.
200 * @param string $string
201 * A string that needs to be escaped from smarty parsing.
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);
212 * Replace all the domain-level tokens in $str
215 * The string with tokens to be replaced.
216 * @param object $domain
219 * Replace tokens with HTML or plain text.
221 * @param null $knownTokens
222 * @param bool $escapeSmarty
225 * The processed string
227 public static function replaceDomainTokens(
232 $escapeSmarty = FALSE
236 !$knownTokens ||
empty($knownTokens[$key])
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);
252 * @param string $token
253 * @param CRM_Core_BAO_Domain $domain
255 * @param bool $escapeSmarty
257 * @return null|string
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
264 $loc = $domain->getLocationValues();
266 if (!in_array($token, self
::$_tokens['domain'])) {
267 $value = "{domain.$token}";
269 elseif ($token === 'address') {
270 $cacheKey = __CLASS__
. 'address_token_cache' . CRM_Core_Config
::domainID();
271 $addressCache = Civi
::cache()->has($cacheKey) ? Civi
::cache()->get($cacheKey) : [];
273 $fieldKey = $html ?
'address-html' : 'address-text';
274 if (array_key_exists($fieldKey, $addressCache)) {
275 return $addressCache[$fieldKey];
279 // Construct the address token
281 if (!empty($loc[$token])) {
283 $value = str_replace("\n", '<br />', $loc[$token][1]['display']);
286 $value = $loc[$token][1]['display_text'];
288 Civi
::cache()->set($cacheKey, $addressCache);
291 elseif ($token === 'name' ||
$token === 'id' ||
$token === 'description') {
292 $value = $domain->$token;
294 elseif ($token === 'phone' ||
$token === 'email') {
295 // Construct the phone and email tokens
298 if (!empty($loc[$token])) {
299 foreach ($loc[$token] as $index => $entity) {
300 $value = $entity[$token];
307 $value = self
::tokenEscapeSmarty($value);
314 * Replace all the org-level tokens in $str
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.
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
328 * The handling for custom fields with the removed token has
332 * The string with tokens to be replaced.
334 * Associative array of org properties.
336 * Replace tokens with HTML or plain text.
338 * @param bool $escapeSmarty
341 * The processed string
343 public static function replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
344 self
::$_tokens['org']
346 array_keys(CRM_Contact_BAO_Contact
::importableFields('Organization')),
347 ['address', 'display_name', 'checksum', 'contact_id']
350 foreach (self
::$_tokens['org'] as $token) {
351 // print "Getting token value for $token<br/><br/>";
356 // If the string doesn't contain this token, skip it.
358 if (!self
::token_match('org', $token, $str)) {
362 // Construct value from $token and $contact
366 if ($token === 'checksum') {
367 $cs = CRM_Contact_BAO_Contact_Utils
::generateChecksum($org['contact_id']);
370 elseif ($token === 'address') {
371 // Build the location values array
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');
380 // Construct the address token
382 $value = CRM_Utils_Address
::format($loc);
384 $value = str_replace("\n", '<br />', $value);
388 $value = CRM_Utils_Array
::retrieveValueRecursive($org, $token);
391 self
::token_replace('org', $token, $value, $str, $escapeSmarty);
398 * Replace all mailing tokens in $str
401 * The string with tokens to be replaced.
402 * @param object $mailing
403 * The mailing BAO, or null for validation.
405 * Replace tokens with HTML or plain text.
407 * @param null $knownTokens
408 * @param bool $escapeSmarty
411 * The processed string
413 public static function &replaceMailingTokens(
418 $escapeSmarty = FALSE
421 if (!$knownTokens ||
!isset($knownTokens[$key])) {
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);
438 * @param bool $escapeSmarty
442 public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) {
448 $value = $mailing ?
$mailing->id
: 'undefined';
451 // Key is the ID, or the hash when the hash URLs setting is enabled
453 $value = $mailing->id
;
454 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($value)) {
460 $value = $mailing ?
$mailing->name
: 'Mailing Name';
464 $groups = $mailing ?
$mailing->getGroupNames() : ['Mailing Groups'];
465 $value = implode(', ', $groups);
469 $value = $mailing->subject
;
473 $mailingKey = $mailing->id
;
474 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
477 $value = CRM_Utils_System
::url('civicrm/mailing/view',
478 "reset=1&id={$mailingKey}",
479 TRUE, NULL, FALSE, TRUE
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
494 $page = new CRM_Mailing_Page_View();
495 $value = $page->run($mailing->id
, NULL, FALSE, TRUE);
498 case 'approvalStatus':
499 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id
);
503 $value = $mailing->approval_note
;
507 $value = CRM_Utils_System
::url('civicrm/mailing/approve',
508 "reset=1&mid={$mailing->id}",
509 TRUE, NULL, FALSE, TRUE
514 $value = CRM_Contact_BAO_Contact
::displayName($mailing->created_id
);
518 $value = CRM_Contact_BAO_Contact
::getPrimaryEmail($mailing->created_id
);
522 $value = "{mailing.$token}";
527 $value = self
::tokenEscapeSmarty($value);
533 * Replace all action tokens in $str
536 * The string with tokens to be replaced.
537 * @param array $addresses
538 * Assoc. array of VERP event addresses.
540 * Assoc. array of action URLs.
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.
546 * @param bool $escapeSmarty
549 * The processed string
551 public static function &replaceActionTokens(
557 $escapeSmarty = FALSE
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])) {
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);
583 * @param bool $escapeSmarty
585 * @return mixed|string
587 public static function getActionTokenReplacement(
592 $escapeSmarty = FALSE
594 // If the token is an email action, use it. Otherwise, find the
597 if (!in_array($token, self
::$_tokens['action'])) {
598 $value = "{action.$token}";
601 $value = $addresses[$token] ??
NULL;
603 if ($value == NULL) {
604 $value = $urls[$token] ??
NULL;
607 if ($value && $html) {
609 if ((substr($token, -3) != 'Url') && ($token != 'forward')) {
610 $value = "mailto:$value";
613 elseif ($value && !$html) {
614 $value = str_replace('&', '&', $value);
619 $value = self
::tokenEscapeSmarty($value);
625 * Replace all the contact-level tokens in $str with information from
629 * The string with tokens to be replaced.
630 * @param array $contact
631 * Associative array of contact properties.
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.
639 * @param bool $escapeSmarty
642 * The processed string
644 public static function replaceContactTokens(
649 $returnBlankToken = FALSE,
650 $escapeSmarty = FALSE
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']
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])) {
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);
676 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
684 * @param bool $returnBlankToken
685 * @param bool $escapeSmarty
687 * @return bool|mixed|null|string
689 public static function getContactTokenReplacement(
693 $returnBlankToken = FALSE,
694 $escapeSmarty = FALSE
696 if (self
::$_tokens['contact'] == NULL) {
697 /* This should come from UF */
699 self
::$_tokens['contact']
701 array_keys(CRM_Contact_BAO_Contact
::exportableFields('All')),
702 ['checksum', 'contact_id']
706 // Construct value from $token and $contact
711 // Support legacy tokens
712 $token = CRM_Utils_Array
::value($token, self
::legacyContactTokens(), $token);
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
718 if (!in_array($token, self
::$_tokens['contact'])) {
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,
732 $value = (array) CRM_Utils_Array
::retrieveValueRecursive($contact, $token);
734 foreach ($value as $index => $item) {
735 $value[$index] = self
::convertPseudoConstantsUsingMetadata($value[$index], $token);
737 $value = implode(', ', $value);
741 $value = str_replace('&', '&', $value);
744 // if null then return actual token
745 if ($returnBlankToken && !$value) {
750 $value = "{contact.$token}";
754 && !($returnBlankToken && $noReplace)
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);
765 * Replace all the hook tokens in $str with information from
769 * The string with tokens to be replaced.
770 * @param array $contact
771 * Associative array of contact properties (including hook token values).
774 * Replace tokens with HTML or plain text.
776 * @param bool $escapeSmarty
779 * The processed string
781 public static function &replaceHookTokens(
786 $escapeSmarty = FALSE
789 $categories = self
::getTokenCategories();
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);
804 * Get the categories required for rendering tokens.
808 public static function getTokenCategories(): array {
809 if (!isset(\Civi
::$statics[__CLASS__
]['token_categories'])) {
811 \CRM_Utils_Hook
::tokens($tokens);
812 \Civi
::$statics[__CLASS__
]['token_categories'] = array_keys($tokens);
814 return \Civi
::$statics[__CLASS__
]['token_categories'];
818 * Parse html through Smarty resolving any smarty functions.
819 * @param string $tokenHtml
820 * @param array $entity
821 * @param string $entityType
823 * html parsed through smarty
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");
840 * @param bool $escapeSmarty
842 * @return mixed|string
844 public static function getHookTokenReplacement(
849 $escapeSmarty = FALSE
851 $value = $contact["{$category}.{$token}"] ??
NULL;
853 if ($value && !$html) {
854 $value = str_replace('&', '&', $value);
858 $value = self
::tokenEscapeSmarty($value);
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
868 * this routine will remove the extra backslashes and braces
870 * @param $str ref to the string that will be scanned and modified
872 public static function unescapeTokens(&$str) {
873 $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
877 * Replace unsubscribe tokens.
880 * The string with tokens to be replaced.
881 * @param object $domain
883 * @param array $groups
884 * The groups (if any) being unsubscribed.
886 * Replace tokens with html or plain text.
887 * @param int $contact_id
889 * @param string $hash The security hash of the unsub event
892 * The processed string
894 public static function &replaceUnsubscribeTokens(
902 if (self
::token_match('unsubscribe', 'group', $str)) {
903 if (!empty($groups)) {
904 $config = CRM_Core_Config
::singleton();
905 $base = CRM_Utils_System
::baseURL();
907 // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented
908 $dao = new CRM_Contact_DAO_Group();
910 while ($dao->fetch()) {
911 if (substr($dao->visibility
, 0, 6) == 'Public') {
912 $visibleGroups[] = $dao->id
;
915 $value = implode(', ', $groups);
916 self
::token_replace('unsubscribe', 'group', $value, $str);
923 * Replace resubscribe tokens.
926 * The string with tokens to be replaced.
927 * @param object $domain
929 * @param array $groups
930 * The groups (if any) being resubscribed.
932 * Replace tokens with html or plain text.
933 * @param int $contact_id
935 * @param string $hash The security hash of the resub event
938 * The processed string
940 public static function &replaceResubscribeTokens(
941 $str, &$domain, &$groups, $html,
944 if (self
::token_match('resubscribe', 'group', $str)) {
945 if (!empty($groups)) {
946 $value = implode(', ', $groups);
947 self
::token_replace('resubscribe', 'group', $value, $str);
954 * Replace subscription-confirmation-request tokens
957 * The string with tokens to be replaced.
958 * @param string $group
959 * The name of the group being subscribed.
962 * Replace tokens with html or plain text.
965 * The processed string
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);
971 if (self
::token_match('subscribe', 'url', $str)) {
972 self
::token_replace('subscribe', 'url', $url, $str);
978 * Replace subscription-invitation tokens
981 * The string with tokens to be replaced.
984 * The processed string
986 public static function &replaceSubscribeInviteTokens($str) {
987 if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
988 $url = CRM_Utils_System
::url('civicrm/mailing/subscribe',
990 TRUE, NULL, FALSE, TRUE
992 $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str);
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
1002 $str = preg_replace('/' . preg_quote($value) . '/', $url, $str);
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);
1020 * Replace welcome/confirmation tokens
1022 * @param string $str
1023 * The string with tokens to be replaced.
1024 * @param string $group
1025 * The name of the group being subscribed.
1027 * Replace tokens with html or plain text.
1030 * The processed string
1032 public static function &replaceWelcomeTokens($str, $group, $html) {
1033 if (self
::token_match('welcome', 'group', $str)) {
1034 self
::token_replace('welcome', 'group', $group, $str);
1040 * Find unprocessed tokens (call this last)
1042 * @param string $str
1043 * The string to search.
1046 * Array of tokens that weren't replaced
1048 public static function &unmatchedTokens(&$str) {
1049 //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
1050 preg_match_all('/\{(\w+\.\w+)\}/', $str, $match);
1055 * Find and replace tokens for each component.
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.
1064 * @param bool $escapeSmarty
1065 * @param bool $returnEmptyToken
1068 * The processed string
1070 public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
1071 if (!is_array($components) ||
empty($contact)) {
1075 foreach ($components as $name => $tokens) {
1076 if (!is_array($tokens) ||
empty($tokens)) {
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);
1084 elseif (!$returnEmptyToken) {
1085 //replacing empty token
1086 self
::token_replace($name, $token, "", $str, $escapeSmarty);
1094 * Get array of string tokens.
1096 * @param string $string
1097 * The input string to parse for tokens.
1100 * array of tokens mentioned in field
1102 public static function getTokens($string) {
1105 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+(:|.)?\w*)\}(?!\})/',
1112 foreach ($matches[1] as $token) {
1113 $parts = explode('.', $token, 3);
1116 $suffix = !empty($parts[2]) ?
('.' . $parts[2]) : '';
1117 if ($name && $type) {
1118 if (!isset($tokens[$type])) {
1119 $tokens[$type] = [];
1121 $tokens[$type][] = $name . $suffix;
1129 * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
1130 * and getTokens appears to be historical rather than intentional and should be reviewed
1133 * fields to pass in as return properties when populating token
1135 public static function getReturnProperties(&$string) {
1136 $returnProperties = [];
1138 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
1144 foreach ($matches[1] as $token) {
1145 [$type, $name] = preg_split('/\./', $token, 2);
1147 $returnProperties["{$name}"] = 1;
1152 return $returnProperties;
1156 * Gives required details of contacts in an indexed array format so we
1157 * can iterate in a nice loop and do token evaluation
1159 * @param array $contactIDs
1160 * @param array $returnProperties
1161 * Of required properties.
1162 * @param bool $skipOnHold Don't return on_hold contact info also.
1163 * Don't return on_hold contact info also.
1164 * @param bool $skipDeceased Don't return deceased contact info.
1165 * Don't return deceased contact info.
1166 * @param array $extraParams
1167 * Extra params - DEPRECATED
1168 * @param array $tokens
1169 * The list of tokens we've extracted from the content.
1170 * @param string|null $className
1171 * @param int|null $jobID
1172 * The mailing list jobID - this is a legacy param.
1174 * @return array - e.g [[1 => ['first_name' => 'bob'...], 34 => ['first_name' => 'fred'...]]]
1176 public static function getTokenDetails(
1178 $returnProperties = NULL,
1180 $skipDeceased = TRUE,
1181 $extraParams = NULL,
1188 foreach ($contactIDs as $contactID) {
1190 CRM_Core_Form
::CB_PREFIX
. $contactID,
1199 if ($skipDeceased) {
1200 $params[] = ['is_deceased', '=', 0, 0, 0];
1205 $params[] = ['on_hold', '=', 0, 0, 0];
1209 CRM_Core_Error
::deprecatedWarning('Passing $extraParams to getTokenDetails() is not supported and will be removed in a future version');
1210 $params = array_merge($params, $extraParams);
1213 // if return properties are not passed then get all return properties
1214 if (empty($returnProperties)) {
1215 $fields = array_merge(array_keys(CRM_Contact_BAO_Contact
::exportableFields()),
1216 ['display_name', 'checksum', 'contact_id']
1218 foreach ($fields as $val) {
1219 // The unavailable fields are not available as tokens, do not have a one-2-one relationship
1220 // with contacts and are expensive to resolve.
1221 // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
1222 // and upstream calls to this should populate return properties.
1223 $unavailableFields = ['group', 'tag'];
1224 if (!in_array($val, $unavailableFields)) {
1225 $returnProperties[$val] = 1;
1231 foreach ($returnProperties as $name => $dontCare) {
1232 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
1238 [$contactDetails] = CRM_Contact_BAO_Query
::apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs), TRUE, FALSE, TRUE, CRM_Contact_BAO_Query
::MODE_CONTACTS
, NULL, TRUE);
1240 foreach ($contactIDs as $contactID) {
1241 if (array_key_exists($contactID, $contactDetails)) {
1242 if (!empty($contactDetails[$contactID]['preferred_communication_method'])
1244 $communicationPreferences = [];
1245 foreach ((array) $contactDetails[$contactID]['preferred_communication_method'] as $val) {
1247 $communicationPreferences[$val] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
1250 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $communicationPreferences);
1253 foreach ($custom as $cfID) {
1254 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
1255 $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField
::displayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID);
1259 // special case for greeting replacement
1265 if (!empty($contactDetails[$contactID][$val])) {
1266 $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
1272 // $contactDetails = &$details[0] = is an array of [ contactID => contactDetails ]
1273 // also call a hook and get token details
1274 CRM_Utils_Hook
::tokenValues($contactDetails,
1280 return [$contactDetails];
1284 * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact
1285 * specific tokens to be rendered
1287 * @param array $contactIDs
1288 * This should always be array(0) or its not anonymous - left to keep signature same.
1290 * @param string $returnProperties
1291 * @param bool $skipOnHold
1292 * @param bool $skipDeceased
1293 * @param string $extraParams
1294 * @param array $tokens
1295 * @param string $className
1296 * Sent as context to the hook.
1297 * @param string $jobID
1299 * contactDetails with hooks swapped out
1301 public static function getAnonymousTokenDetails($contactIDs = [0],
1302 $returnProperties = NULL,
1304 $skipDeceased = TRUE,
1305 $extraParams = NULL,
1309 $details = [0 => []];
1310 // also call a hook and get token details
1311 CRM_Utils_Hook
::tokenValues($details[0],
1321 * Get Membership Token Details.
1322 * @param array $membershipIDs
1323 * Array of membership IDS.
1325 public static function getMembershipTokenDetails($membershipIDs) {
1326 $memberships = civicrm_api3('membership', 'get', [
1327 'options' => ['limit' => 0],
1328 'membership_id' => ['IN' => (array) $membershipIDs],
1330 return $memberships['values'];
1334 * Replace existing greeting tokens in message/subject.
1336 * This function operates by reference, modifying the first parameter. Other
1337 * methods for token replacement in this class return the modified string.
1338 * This leads to inconsistency in how these methods must be applied.
1340 * @TODO Remove that inconsistency in usage.
1342 * ::replaceContactTokens() may need to be called after this method, to
1343 * replace tokens supplied from this method.
1345 * @param string $tokenString
1346 * @param array $contactDetails
1347 * @param int $contactId
1348 * @param string $className
1349 * @param bool $escapeSmarty
1351 public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
1353 if (!$contactDetails && !$contactId) {
1357 // check if there are any tokens
1358 $greetingTokens = self
::getTokens($tokenString);
1360 if (!empty($greetingTokens)) {
1361 // first use the existing contact object for token replacement
1362 if (!empty($contactDetails)) {
1363 $tokenString = CRM_Utils_Token
::replaceContactTokens($tokenString, $contactDetails, TRUE, $greetingTokens, TRUE, $escapeSmarty);
1366 self
::removeNullContactTokens($tokenString, $contactDetails, $greetingTokens);
1367 // check if there are any unevaluated tokens
1368 $greetingTokens = self
::getTokens($tokenString);
1370 // $greetingTokens not empty, means there are few tokens which are not
1371 // evaluated, like custom data etc
1372 // so retrieve it from database
1373 if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) {
1374 $greetingsReturnProperties = array_flip(CRM_Utils_Array
::value('contact', $greetingTokens));
1375 $greetingsReturnProperties = array_fill_keys(array_keys($greetingsReturnProperties), 1);
1376 $contactParams = ['contact_id' => $contactId];
1378 $greetingDetails = self
::getTokenDetails($contactParams,
1379 $greetingsReturnProperties,
1385 // again replace tokens
1386 $tokenString = CRM_Utils_Token
::replaceContactTokens($tokenString,
1395 // check if there are still any unevaluated tokens
1396 $remainingTokens = self
::getTokens($tokenString);
1398 // $greetingTokens not empty, there are customized or hook tokens to replace
1399 if (!empty($remainingTokens)) {
1400 // Fill the return properties array
1401 $greetingTokens = $remainingTokens;
1402 reset($greetingTokens);
1403 $greetingsReturnProperties = [];
1404 foreach ($greetingTokens as $value) {
1405 $props = array_flip($value);
1406 $props = array_fill_keys(array_keys($props), 1);
1407 $greetingsReturnProperties = $greetingsReturnProperties +
$props;
1409 $contactParams = ['contact_id' => $contactId];
1410 $greetingDetails = self
::getTokenDetails($contactParams,
1411 $greetingsReturnProperties,
1416 // Prepare variables for calling replaceHookTokens
1417 $categories = array_keys($greetingTokens);
1418 [$contact] = $greetingDetails;
1419 // Replace tokens defined in Hooks.
1420 $tokenString = CRM_Utils_Token
::replaceHookTokens($tokenString, $contact[$contactId], $categories);
1426 * At this point, $contactDetails has loaded the contact from the DAO. Any
1427 * (non-custom) missing fields are null. By removing them, we can avoid
1428 * expensive calls to CRM_Contact_BAO_Query.
1430 * @param string $tokenString
1431 * @param array $contactDetails
1432 * @param array $greetingTokens
1434 private static function removeNullContactTokens(&$tokenString, $contactDetails, &$greetingTokens) {
1436 // Only applies to contact tokens
1437 if (!array_key_exists('contact', $greetingTokens)) {
1441 $greetingTokensOriginal = $greetingTokens;
1442 $contactFieldList = CRM_Contact_DAO_Contact
::fields();
1443 // Sometimes contactDetails are in a multidemensional array, sometimes a
1444 // single-dimension array.
1445 if (array_key_exists(0, $contactDetails) && is_array($contactDetails[0])) {
1446 $contactDetails = current($contactDetails[0]);
1448 $nullFields = array_keys(array_diff_key($contactFieldList, $contactDetails));
1450 // Handle legacy tokens
1451 foreach (self
::legacyContactTokens() as $oldToken => $newToken) {
1452 if (CRM_Utils_Array
::key($newToken, $nullFields)) {
1453 $nullFields[] = $oldToken;
1457 // Remove null contact fields from $greetingTokens
1458 $greetingTokens['contact'] = array_diff($greetingTokens['contact'], $nullFields);
1460 // Also remove them from $tokenString
1461 $removedTokens = array_diff($greetingTokensOriginal['contact'], $greetingTokens['contact']);
1462 // Handle legacy tokens again, sigh
1463 if (!empty($removedTokens)) {
1464 foreach ($removedTokens as $token) {
1465 if (CRM_Utils_Array
::value($token, self
::legacyContactTokens()) !== NULL) {
1466 $removedTokens[] = CRM_Utils_Array
::value($token, self
::legacyContactTokens());
1469 foreach ($removedTokens as $token) {
1470 $tokenString = str_replace("{contact.$token}", '', $tokenString);
1480 public static function flattenTokens(&$tokens) {
1481 $flattenTokens = [];
1488 if (!isset($tokens[$prop])) {
1491 foreach ($tokens[$prop] as $type => $names) {
1492 if (!isset($flattenTokens[$type])) {
1493 $flattenTokens[$type] = [];
1495 foreach ($names as $name) {
1496 $flattenTokens[$type][$name] = 1;
1501 return $flattenTokens;
1505 * Replace all user tokens in $str
1507 * @param string $str
1508 * The string with tokens to be replaced.
1510 * @param null $knownTokens
1511 * @param bool $escapeSmarty
1514 * The processed string
1516 public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
1518 if (!$knownTokens ||
1519 !isset($knownTokens[$key])
1524 $str = preg_replace_callback(
1525 self
::tokenRegex($key),
1526 function ($matches) use ($escapeSmarty) {
1527 return CRM_Utils_Token
::getUserTokenReplacement($matches[1], $escapeSmarty);
1536 * @param bool $escapeSmarty
1540 public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) {
1543 [$objectName, $objectValue] = explode('-', $token, 2);
1545 switch ($objectName) {
1547 $value = CRM_Core_Permission
::permissionEmails($objectValue);
1551 $value = CRM_Core_Permission
::roleEmails($objectValue);
1555 if ($escapeSmarty) {
1556 $value = self
::tokenEscapeSmarty($value);
1562 protected static function _buildContributionTokens() {
1563 $key = 'contribution';
1565 if (!isset(Civi
::$statics[__CLASS__
][__FUNCTION__
][$key])) {
1566 $processor = new CRM_Contribute_Tokens();
1567 $tokens = array_merge(CRM_Contribute_BAO_Contribution
::exportableFields('All'),
1568 ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []],
1569 self
::getCustomFieldTokens('Contribution'),
1570 $processor->getPseudoTokens()
1572 foreach ($tokens as $token) {
1573 if (!empty($token['name'])) {
1574 $tokens[$token['name']] = [];
1577 Civi
::$statics[__CLASS__
][__FUNCTION__
][$key] = array_keys($tokens);
1579 self
::$_tokens[$key] = Civi
::$statics[__CLASS__
][__FUNCTION__
][$key];
1583 * Store membership tokens on the static _tokens array.
1585 protected static function _buildMembershipTokens() {
1586 $key = 'membership';
1587 if (!isset(self
::$_tokens[$key]) || self
::$_tokens[$key] == NULL) {
1588 $membershipTokens = [];
1589 $tokens = CRM_Core_SelectValues
::membershipTokens();
1590 foreach ($tokens as $token => $dontCare) {
1591 $membershipTokens[] = substr($token, (strpos($token, '.') +
1), -1);
1593 self
::$_tokens[$key] = $membershipTokens;
1598 * Replace tokens for an entity.
1599 * @param string $entity
1600 * @param array $entityArray
1601 * (e.g. in format from api).
1602 * @param string $str
1603 * String to replace in.
1604 * @param array $knownTokens
1605 * Array of tokens present.
1606 * @param bool $escapeSmarty
1608 * string with replacements made
1610 public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1611 if (!$knownTokens ||
empty($knownTokens[$entity])) {
1615 $fn = 'get' . ucfirst($entity) . 'TokenReplacement';
1616 $fn = is_callable(['CRM_Utils_Token', $fn]) ?
$fn : 'getApiTokenReplacement';
1617 // since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
1618 foreach ($knownTokens[$entity] as $token) {
1619 $replacement = self
::$fn($entity, $token, $entityArray);
1620 if ($escapeSmarty) {
1621 $replacement = self
::tokenEscapeSmarty($replacement);
1623 $str = str_replace('{' . $entity . '.' . $token . '}', $replacement, $str);
1625 return preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1629 * @param int $caseId
1630 * @param string $str
1631 * @param array $knownTokens
1632 * @param bool $escapeSmarty
1634 * @throws \CiviCRM_API3_Exception
1636 public static function replaceCaseTokens($caseId, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1637 if (!$knownTokens ||
empty($knownTokens['case'])) {
1640 $case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
1641 return self
::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
1645 * Generic function for formatting token replacement for an api field
1647 * @param string $entity
1648 * @param string $token
1649 * @param array $entityArray
1651 * @throws \CiviCRM_API3_Exception
1653 public static function getApiTokenReplacement($entity, $token, $entityArray) {
1654 if (!isset($entityArray[$token])) {
1657 $field = civicrm_api3($entity, 'getfield', ['action' => 'get', 'name' => $token, 'get_options' => 'get']);
1658 $field = $field['values'];
1659 $fieldType = $field['type'] ??
NULL;
1661 if ($fieldType == CRM_Utils_Type
::T_BOOLEAN
&& empty($field['options'])) {
1662 $field['options'] = [ts('No'), ts('Yes')];
1664 // Match pseudoconstants
1665 if (!empty($field['options'])) {
1667 foreach ((array) $entityArray[$token] as $val) {
1668 $ret[] = $field['options'][$val];
1670 return implode(', ', $ret);
1672 // Format date fields
1673 elseif ($entityArray[$token] && $fieldType == CRM_Utils_Type
::T_DATE
) {
1674 return CRM_Utils_Date
::customFormat($entityArray[$token]);
1676 return implode(', ', (array) $entityArray[$token]);
1680 * Replace Contribution tokens in html.
1682 * @param string $str
1683 * @param array $contribution
1684 * @param bool|string $html
1685 * @param string $knownTokens
1686 * @param bool|string $escapeSmarty
1690 public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1691 $key = 'contribution';
1692 if (!$knownTokens ||
empty($knownTokens[$key])) {
1697 // here we intersect with the list of pre-configured valid tokens
1698 // so that we remove anything we do not recognize
1699 // I hope to move this step out of here soon and
1700 // then we will just iterate on a list of tokens that are passed to us
1702 $str = preg_replace_callback(
1703 self
::tokenRegex($key),
1704 function ($matches) use (&$contribution, $html, $escapeSmarty) {
1705 return CRM_Utils_Token
::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
1710 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1715 * We have a situation where we are rendering more than one token in each field because we are combining
1716 * tokens from more than one contribution when pdf thank you letters are grouped (CRM-14367)
1718 * The replaceContributionToken doesn't handle receive_date correctly in this scenario because of the formatting
1719 * it applies (other tokens are OK including date fields)
1721 * So we sort this out & then call the main function. Note that we are not escaping smarty on this fields like the main function
1722 * does - but the fields is already being formatted through a date function
1724 * @param string $separator
1725 * @param string $str
1726 * @param array $contributions
1727 * @param array $knownTokens
1731 public static function replaceMultipleContributionTokens(string $separator, string $str, array $contributions, array $knownTokens): string {
1732 foreach ($knownTokens['contribution'] ??
[] as $token) {
1733 $resolvedTokens = [];
1734 foreach ($contributions as $contribution) {
1735 $resolvedTokens[] = self
::replaceContributionTokens('{contribution.' . $token . '}', $contribution, FALSE, $knownTokens);
1737 $str = self
::token_replace('contribution', $token, implode($separator, $resolvedTokens), $str);
1743 * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
1744 * - this is used by the pdfLetter task from membership search
1745 * @param string $entity
1746 * should always be "membership"
1747 * @param string $token
1749 * @param array $membership
1750 * An api result array for a single membership.
1751 * @return string token replacement
1753 public static function getMembershipTokenReplacement($entity, $token, $membership) {
1754 self
::_buildMembershipTokens();
1757 $value = $membership['membership_name'];
1761 $statuses = CRM_Member_BAO_Membership
::buildOptions('status_id');
1762 $value = $statuses[$membership['status_id']];
1767 $value = civicrm_api3('membership_type', 'getvalue', [
1768 'id' => $membership['membership_type_id'],
1769 'return' => 'minimum_fee',
1771 $value = CRM_Utils_Money
::format($value, NULL, NULL, TRUE);
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)
1781 if (in_array($token, self
::$_tokens[$entity])) {
1782 $value = $membership[$token];
1783 if (CRM_Utils_String
::endsWith($token, '_date')) {
1784 $value = CRM_Utils_Date
::customFormat($value);
1789 $value = "{$entity}.{$token}";
1799 * @param $contribution
1801 * @param bool $escapeSmarty
1803 * @return mixed|string
1804 * @throws \CRM_Core_Exception
1806 public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) {
1807 self
::_buildContributionTokens();
1810 case 'total_amount':
1813 case 'non_deductible_amount':
1814 // FIXME: Is this ever a multi-dimensional array? Why use retrieveValueRecursive()?
1815 $amount = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1816 $currency = CRM_Utils_Array
::retrieveValueRecursive($contribution, 'currency');
1817 $value = CRM_Utils_Money
::format($amount, $currency);
1820 case 'receive_date':
1821 case 'receipt_date':
1822 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1823 $config = CRM_Core_Config
::singleton();
1824 $value = CRM_Utils_Date
::customFormat($value, $config->dateformatDatetime
);
1828 if (!in_array($token, self
::$_tokens['contribution'])) {
1829 $value = "{contribution.$token}";
1832 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1837 if ($escapeSmarty) {
1838 $value = self
::tokenEscapeSmarty($value);
1845 * [legacy_token => new_token]
1847 public static function legacyContactTokens() {
1849 'individual_prefix' => 'prefix_id',
1850 'individual_suffix' => 'suffix_id',
1851 'gender' => 'gender_id',
1852 'communication_style' => 'communication_style_id',
1857 * Get all custom field tokens of $entity
1859 * @param string $entity
1860 * @param bool $usedForTokenWidget
1863 * return custom field tokens in array('custom_N' => 'label') format
1865 public static function getCustomFieldTokens($entity, $usedForTokenWidget = FALSE) {
1867 $tokenName = $usedForTokenWidget ?
"{contribution.custom_%d}" : "custom_%d";
1868 foreach (CRM_Core_BAO_CustomField
::getFields($entity) as $id => $info) {
1869 $customTokens[sprintf($tokenName, $id)] = $info['label'];
1872 return $customTokens;
1876 * Formats a token list for the select2 widget
1881 public static function formatTokensForDisplay($tokens) {
1882 $sorted = $output = [];
1884 // Sort in ascending order by ignoring word case
1885 natcasesort($tokens);
1887 // Attempt to place tokens into optgroups
1888 // @todo These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice.
1889 foreach ($tokens as $k => $v) {
1890 // Check to see if this token is already in a group e.g. for custom fields
1891 $split = explode(' :: ', $v);
1892 if (!empty($split[1])) {
1893 $sorted[$split[1]][] = ['id' => $k, 'text' => $split[0]];
1897 $split = explode('.', trim($k, '{}'));
1898 if (isset($split[1])) {
1899 $entity = array_key_exists($split[1], CRM_Core_DAO_Address
::export()) ?
'Address' : ucwords(str_replace('_', ' ', $split[0]));
1902 $entity = 'Contact';
1904 $sorted[ts($entity)][] = ['id' => $k, 'text' => $v];
1909 foreach ($sorted as $k => $v) {
1910 $output[] = ['text' => $k, 'children' => $v];
1920 * @return bool|int|mixed|string|null
1922 protected static function convertPseudoConstantsUsingMetadata($value, $token) {
1923 // Convert pseudoconstants using metadata
1924 if ($value && is_numeric($value)) {
1925 $allFields = CRM_Contact_BAO_Contact
::exportableFields('All');
1926 if (!empty($allFields[$token]['pseudoconstant'])) {
1927 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $token, $value);
1930 elseif ($value && CRM_Utils_String
::endsWith($token, '_date')) {
1931 $value = CRM_Utils_Date
::customFormat($value);