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 +--------------------------------------------------------------------+
12 use Civi\Token\TokenProcessor
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Class to abstract token replacement.
23 class CRM_Utils_Token
{
24 public static $_requiredTokens = NULL;
26 public static $_tokens = [
54 // we extract the stuff after the role / permission and return the
55 // civicrm email addresses of all users with that role / permission
56 // useful with rules integration
60 // populate this dynamically
62 // populate this dynamically
63 'contribution' => NULL,
72 'subscribe' => ['group'],
73 'unsubscribe' => ['group'],
74 'resubscribe' => ['group'],
75 'welcome' => ['group'],
80 * This is used by CiviMail but will be made redundant by FlexMailer.
83 public static function getRequiredTokens() {
84 if (self
::$_requiredTokens == NULL) {
85 self
::$_requiredTokens = [
86 'domain.address' => ts("Domain address - displays your organization's postal address."),
87 'action.optOutUrl or action.unsubscribeUrl' => [
88 'action.optOut' => ts("'Opt out via email' - displays an email address for recipients to opt out of receiving emails from your organization."),
89 'action.optOutUrl' => ts("'Opt out via web page' - creates a link for recipients to click if they want to opt out of receiving emails from your organization. Alternatively, you can include the 'Opt out via email' token."),
90 'action.unsubscribe' => ts("'Unsubscribe via email' - displays an email address for recipients to unsubscribe from the specific mailing list used to send this message."),
91 'action.unsubscribeUrl' => ts("'Unsubscribe via web page' - creates a link for recipients to unsubscribe from the specific mailing list used to send this message. Alternatively, you can include the 'Unsubscribe via email' token or one of the Opt-out tokens."),
95 return self
::$_requiredTokens;
99 * Check a string (mailing body) for required tokens.
105 * true if all required tokens are found,
106 * else an array of the missing tokens
108 public static function requiredTokens(&$str) {
109 // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail.
110 $requiredTokens = defined('CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS') ? Civi\Core\Resolver
::singleton()->call(CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS
, []) : CRM_Utils_Token
::getRequiredTokens();
113 foreach ($requiredTokens as $token => $value) {
114 if (!is_array($value)) {
115 if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
116 $missing[$token] = $value;
122 foreach ($value as $t => $d) {
124 if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) {
129 $missing[$token] = $desc;
134 if (empty($missing)) {
141 * Wrapper for token matching.
143 * @param string $type
144 * The token type (domain,mailing,contact,action).
146 * The token variable.
148 * The string to search.
153 public static function token_match($type, $var, &$str) {
154 $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
155 return preg_match("/(^|[^\{])$token/", $str);
159 * Wrapper for token replacing.
161 * @param string $type
164 * The token variable.
165 * @param string $value
166 * The value to substitute for the token.
167 * @param string $str (reference) The string to replace in
169 * @param bool $escapeSmarty
172 * The processed string
174 public static function token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
175 $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
180 $value = self
::tokenEscapeSmarty($value);
182 $str = preg_replace("/([^\{])?$token/", "\${1}$value", $str);
187 * Get the regex for token replacement
189 * @param string $token_type
190 * A string indicating the the type of token to be used in the expression.
193 * regular expression suitable for using in preg_replace
195 private static function tokenRegex(string $token_type) {
196 return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(:|\.)?\w*(\-[\w\s]+)?)\}(?!\})/';
200 * Escape the string so a malicious user cannot inject smarty code into the template.
202 * @param string $string
203 * A string that needs to be escaped from smarty parsing.
208 public static function tokenEscapeSmarty($string) {
209 // need to use negative look-behind, as both str_replace() and preg_replace() are sequential
210 return preg_replace(['/{/', '/(?<!{ldelim)}/'], ['{ldelim}', '{rdelim}'], $string);
214 * Replace all the domain-level tokens in $str
219 * The string with tokens to be replaced.
220 * @param object $domain
223 * Replace tokens with HTML or plain text.
225 * @param null $knownTokens
226 * @param bool $escapeSmarty
229 * The processed string
231 public static function replaceDomainTokens(
236 $escapeSmarty = FALSE
240 !$knownTokens ||
empty($knownTokens[$key])
245 $str = preg_replace_callback(
246 self
::tokenRegex($key),
247 function ($matches) use ($domain, $html, $escapeSmarty) {
248 return CRM_Utils_Token
::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
260 * @param string $token
261 * @param CRM_Core_BAO_Domain $domain
263 * @param bool $escapeSmarty
265 * @return null|string
267 public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?
string {
268 $tokens = CRM_Core_DomainTokens
::getDomainTokenValues($domain->id
, $html);
269 $value = $tokens[$token] ??
"{domain.$token}";
271 $value = self
::tokenEscapeSmarty($value);
277 * Replace all the org-level tokens in $str
279 * @fixme: This function appears to be broken, as it depended on
280 * nonexistant method: CRM_Core_BAO_CustomValue::getContactValues()
281 * Marking as deprecated until this is clarified.
284 * - the above hard-breakage was there from 2015 to 2021 and
285 * no error was ever reported on it -does that mean
286 * 1) the code is never hit because the only function that
287 * calls this function is never called or
288 * 2) it was called but never required to resolve any tokens
289 * or more specifically custom field tokens
291 * The handling for custom fields with the removed token has
295 * The string with tokens to be replaced.
297 * Associative array of org properties.
299 * Replace tokens with HTML or plain text.
301 * @param bool $escapeSmarty
304 * The processed string
306 public static function replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
307 CRM_Core_Error
::deprecatedFunctionWarning('token processor');
308 self
::$_tokens['org']
310 array_keys(CRM_Contact_BAO_Contact
::importableFields('Organization')),
311 ['address', 'display_name', 'checksum', 'contact_id']
314 foreach (self
::$_tokens['org'] as $token) {
315 // print "Getting token value for $token<br/><br/>";
320 // If the string doesn't contain this token, skip it.
322 if (!self
::token_match('org', $token, $str)) {
326 // Construct value from $token and $contact
330 if ($token === 'checksum') {
331 $cs = CRM_Contact_BAO_Contact_Utils
::generateChecksum($org['contact_id']);
334 elseif ($token === 'address') {
335 // Build the location values array
338 $loc['display_name'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'display_name');
339 $loc['street_address'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'street_address');
340 $loc['city'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'city');
341 $loc['state_province'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'state_province');
342 $loc['postal_code'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'postal_code');
344 // Construct the address token
346 $value = CRM_Utils_Address
::format($loc);
348 $value = str_replace("\n", '<br />', $value);
352 $value = CRM_Utils_Array
::retrieveValueRecursive($org, $token);
355 self
::token_replace('org', $token, $value, $str, $escapeSmarty);
362 * Replace all mailing tokens in $str
365 * The string with tokens to be replaced.
366 * @param object $mailing
367 * The mailing BAO, or null for validation.
369 * Replace tokens with HTML or plain text.
371 * @param null $knownTokens
372 * @param bool $escapeSmarty
375 * The processed string
379 public static function &replaceMailingTokens(
384 $escapeSmarty = FALSE
387 if (!$knownTokens ||
!isset($knownTokens[$key])) {
391 $str = preg_replace_callback(
392 self
::tokenRegex($key),
393 function ($matches) use (&$mailing, $escapeSmarty) {
394 return CRM_Utils_Token
::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
404 * @param bool $escapeSmarty
408 public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) {
414 $value = $mailing ?
$mailing->id
: 'undefined';
417 // Key is the ID, or the hash when the hash URLs setting is enabled
419 $value = $mailing->id
;
420 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($value)) {
426 $value = $mailing ?
$mailing->name
: 'Mailing Name';
430 $groups = $mailing ?
$mailing->getGroupNames() : ['Mailing Groups'];
431 $value = implode(', ', $groups);
435 $value = $mailing->subject
;
439 $mailingKey = $mailing->id
;
440 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
443 $value = CRM_Utils_System
::url('civicrm/mailing/view',
444 "reset=1&id={$mailingKey}",
445 TRUE, NULL, FALSE, TRUE
451 // Note: editUrl and scheduleUrl used to be different, but now there's
452 // one screen which can adapt based on permissions (in workflow mode).
453 $value = CRM_Utils_System
::url('civicrm/mailing/send',
454 "reset=1&mid={$mailing->id}&continue=true",
455 TRUE, NULL, FALSE, TRUE
460 $page = new CRM_Mailing_Page_View();
461 $value = $page->run($mailing->id
, NULL, FALSE, TRUE);
464 case 'approvalStatus':
465 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id
);
469 $value = $mailing->approval_note
;
473 $value = CRM_Utils_System
::url('civicrm/mailing/approve',
474 "reset=1&mid={$mailing->id}",
475 TRUE, NULL, FALSE, TRUE
480 $value = CRM_Contact_BAO_Contact
::displayName($mailing->created_id
);
484 $value = CRM_Contact_BAO_Contact
::getPrimaryEmail($mailing->created_id
);
488 $value = "{mailing.$token}";
493 $value = self
::tokenEscapeSmarty($value);
499 * Replace all action tokens in $str
502 * The string with tokens to be replaced.
503 * @param array $addresses
504 * Assoc. array of VERP event addresses.
506 * Assoc. array of action URLs.
508 * Replace tokens with HTML or plain text.
509 * @param array $knownTokens
510 * A list of tokens that are known to exist in the email body.
512 * @param bool $escapeSmarty
515 * The processed string
517 public static function &replaceActionTokens(
523 $escapeSmarty = FALSE
526 // here we intersect with the list of pre-configured valid tokens
527 // so that we remove anything we do not recognize
528 // I hope to move this step out of here soon and
529 // then we will just iterate on a list of tokens that are passed to us
530 if (!$knownTokens ||
empty($knownTokens[$key])) {
534 $str = preg_replace_callback(
535 self
::tokenRegex($key),
536 function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) {
537 return CRM_Utils_Token
::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
551 * @param bool $escapeSmarty
553 * @return mixed|string
555 public static function getActionTokenReplacement(
560 $escapeSmarty = FALSE
562 // If the token is an email action, use it. Otherwise, find the
565 if (!in_array($token, self
::$_tokens['action'])) {
566 $value = "{action.$token}";
569 $value = $addresses[$token] ??
NULL;
571 if ($value == NULL) {
572 $value = $urls[$token] ??
NULL;
575 if ($value && $html) {
577 if ((substr($token, -3) != 'Url') && ($token != 'forward')) {
578 $value = "mailto:$value";
581 elseif ($value && !$html) {
582 $value = str_replace('&', '&', $value);
587 $value = self
::tokenEscapeSmarty($value);
593 * Replace all the contact-level tokens in $str with information from
597 * The string with tokens to be replaced.
598 * @param array $contact
599 * Associative array of contact properties.
601 * Replace tokens with HTML or plain text.
602 * @param array $knownTokens
603 * A list of tokens that are known to exist in the email body.
604 * @param bool $returnBlankToken
605 * Return unevaluated token if value is null.
609 * @param bool $escapeSmarty
612 * The processed string
614 public static function replaceContactTokens(
619 $returnBlankToken = FALSE,
620 $escapeSmarty = FALSE
622 // Refresh contact tokens in case they have changed. There is heavy caching
623 // in exportable fields so there is no benefit in doing this conditionally.
624 self
::$_tokens['contact'] = array_merge(
625 array_keys(CRM_Contact_BAO_Contact
::exportableFields('All')),
626 ['checksum', 'contact_id']
630 // here we intersect with the list of pre-configured valid tokens
631 // so that we remove anything we do not recognize
632 // I hope to move this step out of here soon and
633 // then we will just iterate on a list of tokens that are passed to us
634 if (!$knownTokens ||
empty($knownTokens[$key])) {
638 $str = preg_replace_callback(
639 self
::tokenRegex($key),
640 function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) {
641 return CRM_Utils_Token
::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
646 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
653 * Only core usage is from a deprecated unused function and
654 * from deprecated BAO_Mailing code (to be replaced by flexmailer).
661 * @param bool $returnBlankToken
662 * @param bool $escapeSmarty
664 * @return bool|mixed|null|string
666 public static function getContactTokenReplacement(
670 $returnBlankToken = FALSE,
671 $escapeSmarty = FALSE
673 if (self
::$_tokens['contact'] == NULL) {
674 /* This should come from UF */
676 self
::$_tokens['contact']
678 array_keys(CRM_Contact_BAO_Contact
::exportableFields('All')),
679 ['checksum', 'contact_id']
683 // Construct value from $token and $contact
688 // Support legacy tokens
689 $token = CRM_Utils_Array
::value($token, self
::legacyContactTokens(), $token);
691 // check if the token we were passed is valid
692 // we have to do this because this function is
693 // called only when we find a token in the string
695 if (!in_array(str_replace(':label', '', $token), self
::$_tokens['contact'])) {
698 elseif ($token == 'checksum') {
699 $hash = $contact['hash'] ??
NULL;
700 $contactID = CRM_Utils_Array
::retrieveValueRecursive($contact, 'contact_id');
701 $cs = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID,
709 $value = (array) CRM_Utils_Array
::retrieveValueRecursive($contact, str_replace(':label', '', $token));
711 foreach ($value as $index => $item) {
712 $value[$index] = self
::convertPseudoConstantsUsingMetadata($value[$index], str_replace(':label', '', $token));
714 $value = implode(', ', $value);
718 $value = str_replace('&', '&', $value);
721 // if null then return actual token
722 if ($returnBlankToken && !$value) {
727 $value = "{contact.$token}";
731 && !($returnBlankToken && $noReplace)
733 // $returnBlankToken means the caller wants to do further attempts at
734 // processing unreplaced tokens -- so don't escape them yet in this case.
735 $value = self
::tokenEscapeSmarty($value);
742 * Do not use - unused in core.
744 * Replace all the hook tokens in $str with information from
750 * The string with tokens to be replaced.
751 * @param array $contact
752 * Associative array of contact properties (including hook token values).
755 * Replace tokens with HTML or plain text.
757 * @param bool $escapeSmarty
760 * The processed string
762 public static function &replaceHookTokens(
767 $escapeSmarty = FALSE
770 $categories = self
::getTokenCategories();
772 foreach ($categories as $key) {
773 $str = preg_replace_callback(
774 self
::tokenRegex($key),
775 function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
776 return CRM_Utils_Token
::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
785 * Get the categories required for rendering tokens.
789 public static function getTokenCategories(): array {
790 if (!isset(\Civi
::$statics[__CLASS__
]['token_categories'])) {
792 \CRM_Utils_Hook
::tokens($tokens);
793 \Civi
::$statics[__CLASS__
]['token_categories'] = array_keys($tokens);
795 return \Civi
::$statics[__CLASS__
]['token_categories'];
799 * Parse html through Smarty resolving any smarty functions.
800 * @param string $tokenHtml
801 * @param array $entity
802 * @param string $entityType
804 * html parsed through smarty
807 public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
808 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
809 $smarty = CRM_Core_Smarty
::singleton();
810 // also add the tokens to the template
811 $smarty->assign_by_ref($entityType, $entity);
812 $tokenHtml = $smarty->fetch("string:$tokenHtml");
818 * Do not use, unused in core.
826 * @param bool $escapeSmarty
828 * @return mixed|string
830 public static function getHookTokenReplacement(
835 $escapeSmarty = FALSE
837 $value = $contact["{$category}.{$token}"] ??
NULL;
839 if ($value && !$html) {
840 $value = str_replace('&', '&', $value);
844 $value = self
::tokenEscapeSmarty($value);
851 * unescapeTokens removes any characters that caused the replacement routines to skip token replacement
852 * for example {{token}} or \{token} will result in {token} in the final email
854 * this routine will remove the extra backslashes and braces
858 * @param string $str ref to the string that will be scanned and modified
860 public static function unescapeTokens(&$str) {
861 $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
865 * Replace unsubscribe tokens.
868 * The string with tokens to be replaced.
869 * @param object $domain
871 * @param array $groups
872 * The groups (if any) being unsubscribed.
874 * Replace tokens with html or plain text.
875 * @param int $contact_id
877 * @param string $hash The security hash of the unsub event
880 * The processed string
882 public static function &replaceUnsubscribeTokens(
890 if (self
::token_match('unsubscribe', 'group', $str)) {
891 if (!empty($groups)) {
892 $config = CRM_Core_Config
::singleton();
893 $base = CRM_Utils_System
::baseURL();
895 // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented
896 $dao = new CRM_Contact_DAO_Group();
898 while ($dao->fetch()) {
899 if (substr($dao->visibility
, 0, 6) == 'Public') {
900 $visibleGroups[] = $dao->id
;
903 $value = implode(', ', $groups);
904 self
::token_replace('unsubscribe', 'group', $value, $str);
911 * Replace resubscribe tokens.
914 * The string with tokens to be replaced.
915 * @param object $domain
917 * @param array $groups
918 * The groups (if any) being resubscribed.
920 * Replace tokens with html or plain text.
921 * @param int $contact_id
923 * @param string $hash The security hash of the resub event
926 * The processed string
928 public static function &replaceResubscribeTokens(
929 $str, &$domain, &$groups, $html,
932 if (self
::token_match('resubscribe', 'group', $str)) {
933 if (!empty($groups)) {
934 $value = implode(', ', $groups);
935 self
::token_replace('resubscribe', 'group', $value, $str);
942 * Replace subscription-confirmation-request tokens
945 * The string with tokens to be replaced.
946 * @param string $group
947 * The name of the group being subscribed.
950 * Replace tokens with html or plain text.
953 * The processed string
955 public static function &replaceSubscribeTokens($str, $group, $url, $html) {
956 if (self
::token_match('subscribe', 'group', $str)) {
957 self
::token_replace('subscribe', 'group', $group, $str);
959 if (self
::token_match('subscribe', 'url', $str)) {
960 self
::token_replace('subscribe', 'url', $url, $str);
966 * Replace subscription-invitation tokens
969 * The string with tokens to be replaced.
972 * The processed string
974 public static function &replaceSubscribeInviteTokens($str) {
975 if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
976 $url = CRM_Utils_System
::url('civicrm/mailing/subscribe',
978 TRUE, NULL, FALSE, TRUE
980 $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str);
983 if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) {
984 foreach ($matches as $key => $value) {
985 $gid = substr($value, 21, -1);
986 $url = CRM_Utils_System
::url('civicrm/mailing/subscribe',
987 "reset=1&gid={$gid}",
988 TRUE, NULL, FALSE, TRUE
990 $str = preg_replace('/' . preg_quote($value) . '/', $url, $str);
994 if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
995 foreach ($matches as $key => $value) {
996 $gid = substr($value, 18, -1);
997 $config = CRM_Core_Config
::singleton();
998 $domain = CRM_Core_BAO_MailSettings
::defaultDomain();
999 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
1000 // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
1001 $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
1008 * Replace welcome/confirmation tokens
1010 * @param string $str
1011 * The string with tokens to be replaced.
1012 * @param string $group
1013 * The name of the group being subscribed.
1015 * Replace tokens with html or plain text.
1018 * The processed string
1020 public static function &replaceWelcomeTokens($str, $group, $html) {
1021 if (self
::token_match('welcome', 'group', $str)) {
1022 self
::token_replace('welcome', 'group', $group, $str);
1028 * Find unprocessed tokens (call this last)
1030 * @param string $str
1031 * The string to search.
1034 * Array of tokens that weren't replaced
1036 public static function &unmatchedTokens(&$str) {
1037 //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
1038 preg_match_all('/\{(\w+\.\w+)\}/', $str, $match);
1043 * Find and replace tokens for each component.
1045 * @param string $str
1046 * The string to search.
1047 * @param array $contact
1048 * Associative array of contact properties.
1049 * @param array $components
1050 * A list of tokens that are known to exist in the email body.
1052 * @param bool $escapeSmarty
1053 * @param bool $returnEmptyToken
1056 * The processed string
1060 public static function replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
1061 CRM_Core_Error
::deprecatedFunctionWarning('use the token processor');
1062 if (!is_array($components) ||
empty($contact)) {
1066 foreach ($components as $name => $tokens) {
1067 if (!is_array($tokens) ||
empty($tokens)) {
1071 foreach ($tokens as $token) {
1072 if (self
::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) {
1073 self
::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty);
1075 elseif (!$returnEmptyToken) {
1076 //replacing empty token
1077 self
::token_replace($name, $token, "", $str, $escapeSmarty);
1085 * Get array of string tokens.
1087 * @param string $string
1088 * The input string to parse for tokens.
1091 * array of tokens mentioned in field
1093 public static function getTokens($string) {
1096 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+(:|.)?\w*)\}(?!\})/',
1103 foreach ($matches[1] as $token) {
1104 $parts = explode('.', $token, 3);
1107 $suffix = !empty($parts[2]) ?
('.' . $parts[2]) : '';
1108 if ($name && $type) {
1109 if (!isset($tokens[$type])) {
1110 $tokens[$type] = [];
1112 $tokens[$type][] = $name . $suffix;
1120 * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
1121 * and getTokens appears to be historical rather than intentional and should be reviewed
1124 * fields to pass in as return properties when populating token
1126 public static function getReturnProperties(&$string) {
1127 $returnProperties = [];
1129 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
1135 foreach ($matches[1] as $token) {
1136 [$type, $name] = preg_split('/\./', $token, 2);
1138 $returnProperties["{$name}"] = 1;
1143 return $returnProperties;
1147 * Do not use this function.
1149 * Gives required details of contacts in an indexed array format so we
1150 * can iterate in a nice loop and do token evaluation
1152 * @param array $contactIDs
1153 * @param array $returnProperties
1154 * Of required properties.
1155 * @param bool $skipOnHold Don't return on_hold contact info also.
1156 * Don't return on_hold contact info also.
1157 * @param bool $skipDeceased Don't return deceased contact info.
1158 * Don't return deceased contact info.
1159 * @param array $extraParams
1160 * Extra params - DEPRECATED
1161 * @param array $tokens
1162 * The list of tokens we've extracted from the content.
1163 * @param string|null $className
1164 * @param int|null $jobID
1165 * The mailing list jobID - this is a legacy param.
1169 * @return array - e.g [[1 => ['first_name' => 'bob'...], 34 => ['first_name' => 'fred'...]]]
1171 public static function getTokenDetails(
1173 $returnProperties = NULL,
1175 $skipDeceased = TRUE,
1176 $extraParams = NULL,
1181 CRM_Core_Error
::deprecatedFunctionWarning('If you hit this in mailing code you should use flexmailer - otherwise use the token processor');
1183 foreach ($contactIDs as $contactID) {
1185 CRM_Core_Form
::CB_PREFIX
. $contactID,
1194 if ($skipDeceased) {
1195 $params[] = ['is_deceased', '=', 0, 0, 0];
1200 $params[] = ['on_hold', '=', 0, 0, 0];
1204 CRM_Core_Error
::deprecatedWarning('Passing $extraParams to getTokenDetails() is not supported and will be removed in a future version');
1205 $params = array_merge($params, $extraParams);
1208 // if return properties are not passed then get all return properties
1209 if (empty($returnProperties)) {
1210 $fields = array_merge(array_keys(CRM_Contact_BAO_Contact
::exportableFields()),
1211 ['display_name', 'checksum', 'contact_id']
1213 foreach ($fields as $val) {
1214 // The unavailable fields are not available as tokens, do not have a one-2-one relationship
1215 // with contacts and are expensive to resolve.
1216 // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
1217 // and upstream calls to this should populate return properties.
1218 $unavailableFields = ['group', 'tag'];
1219 if (!in_array($val, $unavailableFields)) {
1220 $returnProperties[$val] = 1;
1226 foreach ($returnProperties as $name => $dontCare) {
1227 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
1233 [$contactDetails] = CRM_Contact_BAO_Query
::apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs), TRUE, FALSE, TRUE, CRM_Contact_BAO_Query
::MODE_CONTACTS
, NULL, TRUE);
1235 foreach ($contactIDs as $contactID) {
1236 if (array_key_exists($contactID, $contactDetails)) {
1237 if (!empty($contactDetails[$contactID]['preferred_communication_method'])
1239 $communicationPreferences = [];
1240 foreach ((array) $contactDetails[$contactID]['preferred_communication_method'] as $val) {
1242 $communicationPreferences[$val] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
1245 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $communicationPreferences);
1248 foreach ($custom as $cfID) {
1249 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
1250 $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField
::displayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID);
1254 // special case for greeting replacement
1260 if (!empty($contactDetails[$contactID][$val])) {
1261 $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
1267 // $contactDetails = &$details[0] = is an array of [ contactID => contactDetails ]
1268 // also call a hook and get token details
1269 CRM_Utils_Hook
::tokenValues($contactDetails,
1275 return [$contactDetails];
1279 * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact
1280 * specific tokens to be rendered
1282 * @param array $contactIDs
1283 * This should always be array(0) or its not anonymous - left to keep signature same.
1285 * @param string $returnProperties
1286 * @param bool $skipOnHold
1287 * @param bool $skipDeceased
1288 * @param string $extraParams
1289 * @param array $tokens
1290 * @param string $className
1291 * Sent as context to the hook.
1292 * @param string $jobID
1294 * contactDetails with hooks swapped out
1298 public static function getAnonymousTokenDetails($contactIDs = [0],
1299 $returnProperties = NULL,
1301 $skipDeceased = TRUE,
1302 $extraParams = NULL,
1306 $details = [0 => []];
1307 // also call a hook and get token details
1308 CRM_Utils_Hook
::tokenValues($details[0],
1318 * Get Membership Token Details.
1319 * @param array $membershipIDs
1320 * Array of membership IDS.
1324 public static function getMembershipTokenDetails($membershipIDs) {
1325 $memberships = civicrm_api3('membership', 'get', [
1326 'options' => ['limit' => 0],
1327 'membership_id' => ['IN' => (array) $membershipIDs],
1329 return $memberships['values'];
1333 * Replace existing greeting tokens in message/subject.
1335 * This function operates by reference, modifying the first parameter. Other
1336 * methods for token replacement in this class return the modified string.
1337 * This leads to inconsistency in how these methods must be applied.
1339 * @TODO Remove that inconsistency in usage.
1341 * @param string $tokenString
1342 * @param array $contactDetails
1343 * @param int $contactId
1344 * @param string $className
1345 * @param bool $escapeSmarty
1347 public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
1349 if (!$contactDetails && !$contactId) {
1352 // check if there are any tokens
1353 $greetingTokens = self
::getTokens($tokenString);
1354 $context = $contactId ?
['contactId' => $contactId] : [];
1355 if ($contactDetails) {
1356 $context['contact'] = isset($contactDetails[0]) ?
reset($contactDetails[0]) : $contactDetails;
1358 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
1360 'class' => $className,
1362 $tokenProcessor->addRow($context);
1363 $tokenProcessor->addMessage('greeting', $tokenString, 'text/plain');
1364 $tokenProcessor->evaluate();
1365 foreach ($tokenProcessor->getRows() as $row) {
1366 $tokenString = $row->render('greeting');
1371 * At this point, $contactDetails has loaded the contact from the DAO. Any
1372 * (non-custom) missing fields are null. By removing them, we can avoid
1373 * expensive calls to CRM_Contact_BAO_Query.
1375 * @deprecated unused in core
1377 * @param string $tokenString
1378 * @param array $contactDetails
1379 * @param array $greetingTokens
1381 private static function removeNullContactTokens(&$tokenString, $contactDetails, &$greetingTokens) {
1383 // Only applies to contact tokens
1384 if (!array_key_exists('contact', $greetingTokens)) {
1388 $greetingTokensOriginal = $greetingTokens;
1389 $contactFieldList = CRM_Contact_DAO_Contact
::fields();
1390 // Sometimes contactDetails are in a multidemensional array, sometimes a
1391 // single-dimension array.
1392 if (array_key_exists(0, $contactDetails) && is_array($contactDetails[0])) {
1393 $contactDetails = current($contactDetails[0]);
1395 $nullFields = array_keys(array_diff_key($contactFieldList, $contactDetails));
1397 // Handle legacy tokens
1398 foreach (self
::legacyContactTokens() as $oldToken => $newToken) {
1399 if (CRM_Utils_Array
::key($newToken, $nullFields)) {
1400 $nullFields[] = $oldToken;
1404 // Remove null contact fields from $greetingTokens
1405 $greetingTokens['contact'] = array_diff($greetingTokens['contact'], $nullFields);
1407 // Also remove them from $tokenString
1408 $removedTokens = array_diff($greetingTokensOriginal['contact'], $greetingTokens['contact']);
1409 // Handle legacy tokens again, sigh
1410 if (!empty($removedTokens)) {
1411 foreach ($removedTokens as $token) {
1412 if (CRM_Utils_Array
::value($token, self
::legacyContactTokens()) !== NULL) {
1413 $removedTokens[] = CRM_Utils_Array
::value($token, self
::legacyContactTokens());
1416 foreach ($removedTokens as $token) {
1417 $tokenString = str_replace("{contact.$token}", '', $tokenString);
1427 public static function flattenTokens(&$tokens) {
1428 $flattenTokens = [];
1435 if (!isset($tokens[$prop])) {
1438 foreach ($tokens[$prop] as $type => $names) {
1439 if (!isset($flattenTokens[$type])) {
1440 $flattenTokens[$type] = [];
1442 foreach ($names as $name) {
1443 $flattenTokens[$type][$name] = 1;
1448 return $flattenTokens;
1452 * Replace all user tokens in $str
1454 * @param string $str
1455 * The string with tokens to be replaced.
1457 * @param null $knownTokens
1458 * @param bool $escapeSmarty
1461 * The processed string
1463 public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
1465 if (!$knownTokens ||
1466 !isset($knownTokens[$key])
1471 $str = preg_replace_callback(
1472 self
::tokenRegex($key),
1473 function ($matches) use ($escapeSmarty) {
1474 return CRM_Utils_Token
::getUserTokenReplacement($matches[1], $escapeSmarty);
1483 * @param bool $escapeSmarty
1487 public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) {
1490 [$objectName, $objectValue] = explode('-', $token, 2);
1492 switch ($objectName) {
1494 $value = CRM_Core_Permission
::permissionEmails($objectValue);
1498 $value = CRM_Core_Permission
::roleEmails($objectValue);
1502 if ($escapeSmarty) {
1503 $value = self
::tokenEscapeSmarty($value);
1512 * Do not use this function - it still needs full removal from active code
1513 * in CRM_Contribute_Form_Task_PDFLetter.
1515 protected static function _buildContributionTokens() {
1516 $key = 'contribution';
1518 if (!isset(Civi
::$statics[__CLASS__
][__FUNCTION__
][$key])) {
1519 $tokens = array_merge(CRM_Contribute_BAO_Contribution
::exportableFields('All'),
1520 ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []],
1521 self
::getCustomFieldTokens('Contribution'),
1523 'financial_type_id:label',
1524 'financial_type_id:name',
1525 'contribution_page_id:label',
1526 'contribution_page_id:name',
1527 'payment_instrument_id:label',
1528 'payment_instrument_id:name',
1530 'is_pay_later:label',
1531 'contribution_status_id:label',
1532 'contribution_status_id:name',
1533 'is_template:label',
1534 'campaign_id:label',
1538 foreach ($tokens as $token) {
1539 if (!empty($token['name'])) {
1540 $tokens[$token['name']] = [];
1542 elseif (is_string($token) && strpos($token, ':') !== FALSE) {
1543 $tokens[$token] = [];
1546 Civi
::$statics[__CLASS__
][__FUNCTION__
][$key] = array_keys($tokens);
1548 self
::$_tokens[$key] = Civi
::$statics[__CLASS__
][__FUNCTION__
][$key];
1556 * Replace tokens for an entity.
1557 * @param string $entity
1558 * @param array $entityArray
1559 * (e.g. in format from api).
1560 * @param string $str
1561 * String to replace in.
1562 * @param array $knownTokens
1563 * Array of tokens present.
1564 * @param bool $escapeSmarty
1566 * string with replacements made
1568 public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1569 if (!$knownTokens ||
empty($knownTokens[$entity])) {
1573 $fn = 'get' . ucfirst($entity) . 'TokenReplacement';
1574 $fn = is_callable(['CRM_Utils_Token', $fn]) ?
$fn : 'getApiTokenReplacement';
1575 // since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
1576 foreach ($knownTokens[$entity] as $token) {
1577 // We are now supporting the syntax case_type_id:label
1578 // so strip anything after the ':'
1579 // (we aren't supporting 'name' at this stage, so we can assume 'label'
1580 // test cover in TokenConsistencyTest.
1581 $parts = explode(':', $token);
1582 $replacement = self
::$fn($entity, $parts[0], $entityArray);
1583 if ($escapeSmarty) {
1584 $replacement = self
::tokenEscapeSmarty($replacement);
1586 $str = str_replace('{' . $entity . '.' . $token . '}', $replacement, $str);
1588 return preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1594 * @param int $caseId
1595 * @param string $str
1596 * @param array $knownTokens
1597 * @param bool $escapeSmarty
1599 * @throws \CiviCRM_API3_Exception
1601 public static function replaceCaseTokens($caseId, $str, $knownTokens = NULL, $escapeSmarty = FALSE): string {
1602 if (strpos($str, '{case.') === FALSE) {
1605 if (!$knownTokens) {
1606 $knownTokens = self
::getTokens($str);
1608 $case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
1609 return self
::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
1613 * Generic function for formatting token replacement for an api field
1617 * @param string $entity
1618 * @param string $token
1619 * @param array $entityArray
1621 * @throws \CiviCRM_API3_Exception
1623 public static function getApiTokenReplacement($entity, $token, $entityArray) {
1624 if (!isset($entityArray[$token])) {
1627 $field = civicrm_api3($entity, 'getfield', ['action' => 'get', 'name' => $token, 'get_options' => 'get']);
1628 $field = $field['values'];
1629 $fieldType = $field['type'] ??
NULL;
1631 if ($fieldType == CRM_Utils_Type
::T_BOOLEAN
&& empty($field['options'])) {
1632 $field['options'] = [ts('No'), ts('Yes')];
1634 // Match pseudoconstants
1635 if (!empty($field['options'])) {
1637 foreach ((array) $entityArray[$token] as $val) {
1638 $ret[] = $field['options'][$val];
1640 return implode(', ', $ret);
1642 // Format date fields
1643 elseif ($entityArray[$token] && in_array($fieldType, [CRM_Utils_Type
::T_DATE
, CRM_Utils_Type
::T_TIMESTAMP
, (CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
)])) {
1644 return CRM_Utils_Date
::customFormat($entityArray[$token]);
1646 return implode(', ', (array) $entityArray[$token]);
1650 * Do not use - unused in core.
1652 * Replace Contribution tokens in html.
1654 * @param string $str
1655 * @param array $contribution
1656 * @param bool|string $html
1657 * @param string $knownTokens
1658 * @param bool|string $escapeSmarty
1664 public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1665 $key = 'contribution';
1666 if (!$knownTokens ||
empty($knownTokens[$key])) {
1671 // here we intersect with the list of pre-configured valid tokens
1672 // so that we remove anything we do not recognize
1673 // I hope to move this step out of here soon and
1674 // then we will just iterate on a list of tokens that are passed to us
1676 $str = preg_replace_callback(
1677 self
::tokenRegex($key),
1678 function ($matches) use (&$contribution, $html, $escapeSmarty) {
1679 return CRM_Utils_Token
::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
1684 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1689 * We have a situation where we are rendering more than one token in each field because we are combining
1690 * tokens from more than one contribution when pdf thank you letters are grouped (CRM-14367)
1692 * The replaceContributionToken doesn't handle receive_date correctly in this scenario because of the formatting
1693 * it applies (other tokens are OK including date fields)
1695 * So we sort this out & then call the main function. Note that we are not escaping smarty on this fields like the main function
1696 * does - but the fields is already being formatted through a date function
1698 * @param string $separator
1699 * @param string $str
1700 * @param array $contributions
1701 * @param array $knownTokens
1707 public static function replaceMultipleContributionTokens(string $separator, string $str, array $contributions, array $knownTokens): string {
1708 CRM_Core_Error
::deprecatedFunctionWarning('no alternative');
1709 foreach ($knownTokens['contribution'] ??
[] as $token) {
1710 $resolvedTokens = [];
1711 foreach ($contributions as $contribution) {
1712 $resolvedTokens[] = self
::replaceContributionTokens('{contribution.' . $token . '}', $contribution, FALSE, $knownTokens);
1714 $str = self
::token_replace('contribution', $token, implode($separator, $resolvedTokens), $str);
1720 * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
1721 * - this is used by the pdfLetter task from membership search
1723 * This is called via replaceEntityTokens.
1725 * In the near term it will not be called at all from core as
1726 * the pdf letter task is updated to use the processor.
1730 * @param string $entity
1731 * should always be "membership"
1732 * @param string $token
1734 * @param array $membership
1735 * An api result array for a single membership.
1736 * @return string token replacement
1738 public static function getMembershipTokenReplacement($entity, $token, $membership) {
1739 $supportedTokens = [
1744 'membership_type_id',
1752 // membership_type_id would only be requested if the calling
1753 // class is mapping it to '{membership:membership_type_id:label'}
1754 case 'membership_type_id':
1755 $value = $membership['membership_name'];
1759 // status_id would only be requested if the calling
1760 // class is mapping it to '{membership:status_id:label'}
1762 $statuses = CRM_Member_BAO_Membership
::buildOptions('status_id');
1763 $value = $statuses[$membership['status_id']];
1768 $value = civicrm_api3('membership_type', 'getvalue', [
1769 'id' => $membership['membership_type_id'],
1770 'return' => 'minimum_fee',
1772 $value = CRM_Utils_Money
::format($value, NULL, NULL, TRUE);
1774 catch (CiviCRM_API3_Exception
$e) {
1775 // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
1776 // api handles NULL (4.4)
1782 if (in_array($token, $supportedTokens)) {
1783 $value = $membership[$token];
1784 if (CRM_Utils_String
::endsWith($token, '_date')) {
1785 $value = CRM_Utils_Date
::customFormat($value);
1790 $value = "{$entity}.{$token}";
1799 * Do not use - unused in core.
1802 * @param $contribution
1804 * @param bool $escapeSmarty
1808 * @return mixed|string
1809 * @throws \CRM_Core_Exception
1811 public static function getContributionTokenReplacement($token, $contribution, $html = FALSE, $escapeSmarty = FALSE) {
1812 self
::_buildContributionTokens();
1815 case 'total_amount':
1818 case 'non_deductible_amount':
1819 // FIXME: Is this ever a multi-dimensional array? Why use retrieveValueRecursive()?
1820 $amount = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1821 $currency = CRM_Utils_Array
::retrieveValueRecursive($contribution, 'currency');
1822 $value = CRM_Utils_Money
::format($amount, $currency);
1825 case 'receive_date':
1826 case 'receipt_date':
1827 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1828 $config = CRM_Core_Config
::singleton();
1829 $value = CRM_Utils_Date
::customFormat($value, $config->dateformatDatetime
);
1833 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, 'contribution_source');
1837 if (!in_array($token, self
::$_tokens['contribution'])) {
1838 $value = "{contribution.$token}";
1841 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1846 if ($escapeSmarty) {
1847 $value = self
::tokenEscapeSmarty($value);
1855 * Only used from deprecated functions not called by core.
1858 * [legacy_token => new_token]
1860 public static function legacyContactTokens() {
1862 'individual_prefix' => 'prefix_id',
1863 'individual_suffix' => 'suffix_id',
1864 'gender' => 'gender_id',
1865 'communication_style' => 'communication_style_id',
1870 * Get all custom field tokens of $entity
1874 * @param string $entity
1876 * return custom field tokens in array('custom_N' => 'label') format
1878 public static function getCustomFieldTokens($entity) {
1880 foreach (CRM_Core_BAO_CustomField
::getFields($entity) as $id => $info) {
1881 $customTokens['custom_' . $id] = $info['label'] . ' :: ' . $info['groupTitle'];
1883 return $customTokens;
1887 * Formats a token list for the select2 widget
1892 public static function formatTokensForDisplay($tokens) {
1893 $sorted = $output = [];
1895 // Sort in ascending order by ignoring word case
1896 natcasesort($tokens);
1898 // Attempt to place tokens into optgroups
1899 // @todo These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice.
1900 foreach ($tokens as $k => $v) {
1901 // Check to see if this token is already in a group e.g. for custom fields
1902 $split = explode(' :: ', $v);
1903 if (!empty($split[1])) {
1904 $sorted[$split[1]][] = ['id' => $k, 'text' => $split[0]];
1908 $split = explode('.', trim($k, '{}'));
1909 if (isset($split[1])) {
1910 $entity = array_key_exists($split[1], CRM_Core_DAO_Address
::export()) ?
'Address' : ucwords(str_replace('_', ' ', $split[0]));
1913 $entity = 'Contact';
1915 $sorted[ts($entity)][] = ['id' => $k, 'text' => $v];
1920 foreach ($sorted as $k => $v) {
1921 $output[] = ['text' => $k, 'children' => $v];
1931 * @return bool|int|mixed|string|null
1933 protected static function convertPseudoConstantsUsingMetadata($value, $token) {
1934 // Convert pseudoconstants using metadata
1935 if ($value && is_numeric($value)) {
1936 $allFields = CRM_Contact_BAO_Contact
::exportableFields('All');
1937 if (!empty($allFields[$token]['pseudoconstant'])) {
1938 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $token, $value);
1941 elseif ($value && CRM_Utils_String
::endsWith($token, '_date')) {
1942 $value = CRM_Utils_Date
::customFormat($value);
1948 * Get token deprecation information.
1952 public static function getTokenDeprecations(): array {
1954 'WorkFlowMessageTemplates' => [
1955 'contribution_invoice_receipt' => [
1956 '$display_name' => 'contact.display_name',
1958 'contribution_online_receipt' => [
1959 '$contributeMode' => 'no longer available / relevant',
1960 '$first_name' => 'contact.first_name',
1961 '$last_name' => 'contact.last_name',
1962 '$displayName' => 'contact.display_name',
1964 'membership_offline_receipt' => [
1965 // receipt_text_renewal appears to be long gone.
1966 'receipt_text_renewal' => 'receipt_text',
1968 'pledge_acknowledgement' => [
1969 '$domain' => 'no longer available / relevant',
1970 '$contact' => 'no longer available / relevant',
1972 'pledge_reminder' => [
1973 '$domain' => 'no longer available / relevant',
1974 '$contact' => 'no longer available / relevant',