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 sutiable for using in preg_replace
193 private static function tokenRegex($token_type) {
194 return '/(?<!\{|\\\\)\{' . $token_type . '\.([\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);
255 * @param bool $escapeSmarty
257 * @return mixed|null|string
259 public static function getDomainTokenReplacement($token, &$domain, $html = FALSE, $escapeSmarty = FALSE) {
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 static $addressCache = [];
272 $cache_key = $html ?
'address-html' : 'address-text';
273 if (array_key_exists($cache_key, $addressCache)) {
274 return $addressCache[$cache_key];
278 // Construct the address token
280 if (!empty($loc[$token])) {
282 $value = $loc[$token][1]['display'];
283 $value = str_replace("\n", '<br />', $value);
286 $value = $loc[$token][1]['display_text'];
288 $addressCache[$cache_key] = $value;
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 depends on
317 * nonexistant method: CRM_Core_BAO_CustomValue::getContactValues()
318 * Marking as deprecated until this is fixed
322 * The string with tokens to be replaced.
324 * Associative array of org properties.
326 * Replace tokens with HTML or plain text.
328 * @param bool $escapeSmarty
331 * The processed string
333 public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
334 self
::$_tokens['org']
336 array_keys(CRM_Contact_BAO_Contact
::importableFields('Organization')),
337 ['address', 'display_name', 'checksum', 'contact_id']
341 foreach (self
::$_tokens['org'] as $token) {
342 // print "Getting token value for $token<br/><br/>";
347 // If the string doesn't contain this token, skip it.
349 if (!self
::token_match('org', $token, $str)) {
353 // Construct value from $token and $contact
357 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($token)) {
358 // only generate cv if we need it
360 $cv = CRM_Core_BAO_CustomValue
::getContactValues($org['contact_id']);
362 foreach ($cv as $cvFieldID => $value) {
363 if ($cvFieldID == $cfID) {
364 $value = CRM_Core_BAO_CustomField
::displayValue($value, $cfID);
369 elseif ($token == 'checksum') {
370 $cs = CRM_Contact_BAO_Contact_Utils
::generateChecksum($org['contact_id']);
373 elseif ($token == 'address') {
374 // Build the location values array
377 $loc['display_name'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'display_name');
378 $loc['street_address'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'street_address');
379 $loc['city'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'city');
380 $loc['state_province'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'state_province');
381 $loc['postal_code'] = CRM_Utils_Array
::retrieveValueRecursive($org, 'postal_code');
383 // Construct the address token
385 $value = CRM_Utils_Address
::format($loc);
387 $value = str_replace("\n", '<br />', $value);
391 $value = CRM_Utils_Array
::retrieveValueRecursive($org, $token);
394 self
::token_replace('org', $token, $value, $str, $escapeSmarty);
401 * Replace all mailing tokens in $str
404 * The string with tokens to be replaced.
405 * @param object $mailing
406 * The mailing BAO, or null for validation.
408 * Replace tokens with HTML or plain text.
410 * @param null $knownTokens
411 * @param bool $escapeSmarty
414 * The processed string
416 public static function &replaceMailingTokens(
421 $escapeSmarty = FALSE
424 if (!$knownTokens ||
!isset($knownTokens[$key])) {
428 $str = preg_replace_callback(
429 self
::tokenRegex($key),
430 function ($matches) use (&$mailing, $escapeSmarty) {
431 return CRM_Utils_Token
::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
441 * @param bool $escapeSmarty
445 public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) {
451 $value = $mailing ?
$mailing->id
: 'undefined';
454 // Key is the ID, or the hash when the hash URLs setting is enabled
456 $value = $mailing->id
;
457 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($value)) {
463 $value = $mailing ?
$mailing->name
: 'Mailing Name';
467 $groups = $mailing ?
$mailing->getGroupNames() : ['Mailing Groups'];
468 $value = implode(', ', $groups);
472 $value = $mailing->subject
;
476 $mailingKey = $mailing->id
;
477 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
480 $value = CRM_Utils_System
::url('civicrm/mailing/view',
481 "reset=1&id={$mailingKey}",
482 TRUE, NULL, FALSE, TRUE
488 // Note: editUrl and scheduleUrl used to be different, but now there's
489 // one screen which can adapt based on permissions (in workflow mode).
490 $value = CRM_Utils_System
::url('civicrm/mailing/send',
491 "reset=1&mid={$mailing->id}&continue=true",
492 TRUE, NULL, FALSE, TRUE
497 $page = new CRM_Mailing_Page_View();
498 $value = $page->run($mailing->id
, NULL, FALSE, TRUE);
501 case 'approvalStatus':
502 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id
);
506 $value = $mailing->approval_note
;
510 $value = CRM_Utils_System
::url('civicrm/mailing/approve',
511 "reset=1&mid={$mailing->id}",
512 TRUE, NULL, FALSE, TRUE
517 $value = CRM_Contact_BAO_Contact
::displayName($mailing->created_id
);
521 $value = CRM_Contact_BAO_Contact
::getPrimaryEmail($mailing->created_id
);
525 $value = "{mailing.$token}";
530 $value = self
::tokenEscapeSmarty($value);
536 * Replace all action tokens in $str
539 * The string with tokens to be replaced.
540 * @param array $addresses
541 * Assoc. array of VERP event addresses.
543 * Assoc. array of action URLs.
545 * Replace tokens with HTML or plain text.
546 * @param array $knownTokens
547 * A list of tokens that are known to exist in the email body.
549 * @param bool $escapeSmarty
552 * The processed string
554 public static function &replaceActionTokens(
560 $escapeSmarty = FALSE
563 // here we intersect with the list of pre-configured valid tokens
564 // so that we remove anything we do not recognize
565 // I hope to move this step out of here soon and
566 // then we will just iterate on a list of tokens that are passed to us
567 if (!$knownTokens ||
empty($knownTokens[$key])) {
571 $str = preg_replace_callback(
572 self
::tokenRegex($key),
573 function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) {
574 return CRM_Utils_Token
::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
586 * @param bool $escapeSmarty
588 * @return mixed|string
590 public static function getActionTokenReplacement(
595 $escapeSmarty = FALSE
597 // If the token is an email action, use it. Otherwise, find the
600 if (!in_array($token, self
::$_tokens['action'])) {
601 $value = "{action.$token}";
604 $value = $addresses[$token] ??
NULL;
606 if ($value == NULL) {
607 $value = $urls[$token] ??
NULL;
610 if ($value && $html) {
612 if ((substr($token, -3) != 'Url') && ($token != 'forward')) {
613 $value = "mailto:$value";
616 elseif ($value && !$html) {
617 $value = str_replace('&', '&', $value);
622 $value = self
::tokenEscapeSmarty($value);
628 * Replace all the contact-level tokens in $str with information from
632 * The string with tokens to be replaced.
633 * @param array $contact
634 * Associative array of contact properties.
636 * Replace tokens with HTML or plain text.
637 * @param array $knownTokens
638 * A list of tokens that are known to exist in the email body.
639 * @param bool $returnBlankToken
640 * Return unevaluated token if value is null.
642 * @param bool $escapeSmarty
645 * The processed string
647 public static function &replaceContactTokens(
652 $returnBlankToken = FALSE,
653 $escapeSmarty = FALSE
655 // Refresh contact tokens in case they have changed. There is heavy caching
656 // in exportable fields so there is no benefit in doing this conditionally.
657 self
::$_tokens['contact'] = array_merge(
658 array_keys(CRM_Contact_BAO_Contact
::exportableFields('All')),
659 ['checksum', 'contact_id']
663 // here we intersect with the list of pre-configured valid tokens
664 // so that we remove anything we do not recognize
665 // I hope to move this step out of here soon and
666 // then we will just iterate on a list of tokens that are passed to us
667 if (!$knownTokens ||
empty($knownTokens[$key])) {
671 $str = preg_replace_callback(
672 self
::tokenRegex($key),
673 function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) {
674 return CRM_Utils_Token
::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
679 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
687 * @param bool $returnBlankToken
688 * @param bool $escapeSmarty
690 * @return bool|mixed|null|string
692 public static function getContactTokenReplacement(
696 $returnBlankToken = FALSE,
697 $escapeSmarty = FALSE
699 if (self
::$_tokens['contact'] == NULL) {
700 /* This should come from UF */
702 self
::$_tokens['contact']
704 array_keys(CRM_Contact_BAO_Contact
::exportableFields('All')),
705 ['checksum', 'contact_id']
709 // Construct value from $token and $contact
714 // Support legacy tokens
715 $token = CRM_Utils_Array
::value($token, self
::legacyContactTokens(), $token);
717 // check if the token we were passed is valid
718 // we have to do this because this function is
719 // called only when we find a token in the string
721 if (!in_array($token, self
::$_tokens['contact'])) {
724 elseif ($token == 'checksum') {
725 $hash = $contact['hash'] ??
NULL;
726 $contactID = CRM_Utils_Array
::retrieveValueRecursive($contact, 'contact_id');
727 $cs = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID,
735 $value = CRM_Utils_Array
::retrieveValueRecursive($contact, $token);
737 // FIXME: for some pseudoconstants we get array ( 0 => id, 1 => label )
738 if (is_array($value)) {
741 // Convert pseudoconstants using metadata
742 elseif ($value && is_numeric($value)) {
743 $allFields = CRM_Contact_BAO_Contact
::exportableFields('All');
744 if (!empty($allFields[$token]['pseudoconstant'])) {
745 $value = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $token, $value);
748 elseif ($value && CRM_Utils_String
::endsWith($token, '_date')) {
749 $value = CRM_Utils_Date
::customFormat($value);
754 $value = str_replace('&', '&', $value);
757 // if null then return actual token
758 if ($returnBlankToken && !$value) {
763 $value = "{contact.$token}";
767 && !($returnBlankToken && $noReplace)
769 // $returnBlankToken means the caller wants to do further attempts at
770 // processing unreplaced tokens -- so don't escape them yet in this case.
771 $value = self
::tokenEscapeSmarty($value);
778 * Replace all the hook tokens in $str with information from
782 * The string with tokens to be replaced.
783 * @param array $contact
784 * Associative array of contact properties (including hook token values).
787 * Replace tokens with HTML or plain text.
789 * @param bool $escapeSmarty
792 * The processed string
794 public static function &replaceHookTokens(
799 $escapeSmarty = FALSE
801 foreach ($categories as $key) {
802 $str = preg_replace_callback(
803 self
::tokenRegex($key),
804 function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
805 return CRM_Utils_Token
::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
814 * Parse html through Smarty resolving any smarty functions.
815 * @param string $tokenHtml
816 * @param array $entity
817 * @param string $entityType
819 * html parsed through smarty
821 public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
822 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
823 $smarty = CRM_Core_Smarty
::singleton();
824 // also add the tokens to the template
825 $smarty->assign_by_ref($entityType, $entity);
826 $tokenHtml = $smarty->fetch("string:$tokenHtml");
836 * @param bool $escapeSmarty
838 * @return mixed|string
840 public static function getHookTokenReplacement(
845 $escapeSmarty = FALSE
847 $value = $contact["{$category}.{$token}"] ??
NULL;
849 if ($value && !$html) {
850 $value = str_replace('&', '&', $value);
854 $value = self
::tokenEscapeSmarty($value);
861 * unescapeTokens removes any characters that caused the replacement routines to skip token replacement
862 * for example {{token}} or \{token} will result in {token} in the final email
864 * this routine will remove the extra backslashes and braces
866 * @param $str ref to the string that will be scanned and modified
868 public static function unescapeTokens(&$str) {
869 $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
873 * Replace unsubscribe tokens.
876 * The string with tokens to be replaced.
877 * @param object $domain
879 * @param array $groups
880 * The groups (if any) being unsubscribed.
882 * Replace tokens with html or plain text.
883 * @param int $contact_id
885 * @param string $hash The security hash of the unsub event
888 * The processed string
890 public static function &replaceUnsubscribeTokens(
898 if (self
::token_match('unsubscribe', 'group', $str)) {
899 if (!empty($groups)) {
900 $config = CRM_Core_Config
::singleton();
901 $base = CRM_Utils_System
::baseURL();
903 // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented
904 $dao = new CRM_Contact_DAO_Group();
906 while ($dao->fetch()) {
907 if (substr($dao->visibility
, 0, 6) == 'Public') {
908 $visibleGroups[] = $dao->id
;
911 $value = implode(', ', $groups);
912 self
::token_replace('unsubscribe', 'group', $value, $str);
919 * Replace resubscribe tokens.
922 * The string with tokens to be replaced.
923 * @param object $domain
925 * @param array $groups
926 * The groups (if any) being resubscribed.
928 * Replace tokens with html or plain text.
929 * @param int $contact_id
931 * @param string $hash The security hash of the resub event
934 * The processed string
936 public static function &replaceResubscribeTokens(
937 $str, &$domain, &$groups, $html,
940 if (self
::token_match('resubscribe', 'group', $str)) {
941 if (!empty($groups)) {
942 $value = implode(', ', $groups);
943 self
::token_replace('resubscribe', 'group', $value, $str);
950 * Replace subscription-confirmation-request tokens
953 * The string with tokens to be replaced.
954 * @param string $group
955 * The name of the group being subscribed.
958 * Replace tokens with html or plain text.
961 * The processed string
963 public static function &replaceSubscribeTokens($str, $group, $url, $html) {
964 if (self
::token_match('subscribe', 'group', $str)) {
965 self
::token_replace('subscribe', 'group', $group, $str);
967 if (self
::token_match('subscribe', 'url', $str)) {
968 self
::token_replace('subscribe', 'url', $url, $str);
974 * Replace subscription-invitation tokens
977 * The string with tokens to be replaced.
980 * The processed string
982 public static function &replaceSubscribeInviteTokens($str) {
983 if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
984 $url = CRM_Utils_System
::url('civicrm/mailing/subscribe',
986 TRUE, NULL, FALSE, TRUE
988 $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str);
991 if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) {
992 foreach ($matches as $key => $value) {
993 $gid = substr($value, 21, -1);
994 $url = CRM_Utils_System
::url('civicrm/mailing/subscribe',
995 "reset=1&gid={$gid}",
996 TRUE, NULL, FALSE, TRUE
998 $str = preg_replace('/' . preg_quote($value) . '/', $url, $str);
1002 if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
1003 foreach ($matches as $key => $value) {
1004 $gid = substr($value, 18, -1);
1005 $config = CRM_Core_Config
::singleton();
1006 $domain = CRM_Core_BAO_MailSettings
::defaultDomain();
1007 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
1008 // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
1009 $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
1016 * Replace welcome/confirmation tokens
1018 * @param string $str
1019 * The string with tokens to be replaced.
1020 * @param string $group
1021 * The name of the group being subscribed.
1023 * Replace tokens with html or plain text.
1026 * The processed string
1028 public static function &replaceWelcomeTokens($str, $group, $html) {
1029 if (self
::token_match('welcome', 'group', $str)) {
1030 self
::token_replace('welcome', 'group', $group, $str);
1036 * Find unprocessed tokens (call this last)
1038 * @param string $str
1039 * The string to search.
1042 * Array of tokens that weren't replaced
1044 public static function &unmatchedTokens(&$str) {
1045 //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
1046 preg_match_all('/\{(\w+\.\w+)\}/', $str, $match);
1051 * Find and replace tokens for each component.
1053 * @param string $str
1054 * The string to search.
1055 * @param array $contact
1056 * Associative array of contact properties.
1057 * @param array $components
1058 * A list of tokens that are known to exist in the email body.
1060 * @param bool $escapeSmarty
1061 * @param bool $returnEmptyToken
1064 * The processed string
1066 public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
1067 if (!is_array($components) ||
empty($contact)) {
1071 foreach ($components as $name => $tokens) {
1072 if (!is_array($tokens) ||
empty($tokens)) {
1076 foreach ($tokens as $token) {
1077 if (self
::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) {
1078 self
::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty);
1080 elseif (!$returnEmptyToken) {
1081 //replacing empty token
1082 self
::token_replace($name, $token, "", $str, $escapeSmarty);
1090 * Get array of string tokens.
1092 * @param string $string
1093 * The input string to parse for tokens.
1096 * array of tokens mentioned in field
1098 public static function getTokens($string) {
1101 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
1108 foreach ($matches[1] as $token) {
1109 list($type, $name) = preg_split('/\./', $token, 2);
1110 if ($name && $type) {
1111 if (!isset($tokens[$type])) {
1112 $tokens[$type] = [];
1114 $tokens[$type][] = $name;
1122 * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
1123 * and getTokens appears to be historical rather than intentional and should be reviewed
1126 * fields to pass in as return properties when populating token
1128 public static function getReturnProperties(&$string) {
1129 $returnProperties = [];
1131 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
1137 foreach ($matches[1] as $token) {
1138 list($type, $name) = preg_split('/\./', $token, 2);
1140 $returnProperties["{$name}"] = 1;
1145 return $returnProperties;
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
1161 * @param array $tokens
1162 * The list of tokens we've extracted from the content.
1163 * @param null $className
1165 * The mailing list jobID - this is a legacy param.
1169 public static function getTokenDetails(
1171 $returnProperties = NULL,
1173 $skipDeceased = TRUE,
1174 $extraParams = NULL,
1181 foreach ($contactIDs as $contactID) {
1183 CRM_Core_Form
::CB_PREFIX
. $contactID,
1192 if ($skipDeceased) {
1193 $params[] = ['is_deceased', '=', 0, 0, 0];
1198 $params[] = ['on_hold', '=', 0, 0, 0];
1202 $params = array_merge($params, $extraParams);
1205 // if return properties are not passed then get all return properties
1206 if (empty($returnProperties)) {
1207 $fields = array_merge(array_keys(CRM_Contact_BAO_Contact
::exportableFields()),
1208 ['display_name', 'checksum', 'contact_id']
1210 foreach ($fields as $val) {
1211 // The unavailable fields are not available as tokens, do not have a one-2-one relationship
1212 // with contacts and are expensive to resolve.
1213 // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
1214 // and upstream calls to this should populate return properties.
1215 $unavailableFields = ['group', 'tag'];
1216 if (!in_array($val, $unavailableFields)) {
1217 $returnProperties[$val] = 1;
1223 foreach ($returnProperties as $name => $dontCare) {
1224 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
1230 $details = CRM_Contact_BAO_Query
::apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs), TRUE, FALSE, TRUE, CRM_Contact_BAO_Query
::MODE_CONTACTS
, NULL, TRUE);
1231 $contactDetails = &$details[0];
1233 foreach ($contactIDs as $contactID) {
1234 if (array_key_exists($contactID, $contactDetails)) {
1235 if (!empty($contactDetails[$contactID]['preferred_communication_method'])
1237 $communicationPreferences = [];
1238 foreach ($contactDetails[$contactID]['preferred_communication_method'] as $val) {
1240 $communicationPreferences[$val] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
1243 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $communicationPreferences);
1246 foreach ($custom as $cfID) {
1247 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
1248 $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField
::displayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID);
1252 // special case for greeting replacement
1258 if (!empty($contactDetails[$contactID][$val])) {
1259 $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
1265 // $contactDetails = &$details[0] = is an array of [ contactID => contactDetails ]
1266 // also call a hook and get token details
1267 CRM_Utils_Hook
::tokenValues($contactDetails,
1277 * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact
1278 * specific tokens to be rendered
1280 * @param array $contactIDs
1281 * This should always be array(0) or its not anonymous - left to keep signature same.
1283 * @param string $returnProperties
1284 * @param bool $skipOnHold
1285 * @param bool $skipDeceased
1286 * @param string $extraParams
1287 * @param array $tokens
1288 * @param string $className
1289 * Sent as context to the hook.
1290 * @param string $jobID
1292 * contactDetails with hooks swapped out
1294 public static function getAnonymousTokenDetails($contactIDs = [0],
1295 $returnProperties = NULL,
1297 $skipDeceased = TRUE,
1298 $extraParams = NULL,
1302 $details = [0 => []];
1303 // also call a hook and get token details
1304 CRM_Utils_Hook
::tokenValues($details[0],
1314 * Get Membership Token Details.
1315 * @param array $membershipIDs
1316 * Array of membership IDS.
1318 public static function getMembershipTokenDetails($membershipIDs) {
1319 $memberships = civicrm_api3('membership', 'get', [
1320 'options' => ['limit' => 0],
1321 'membership_id' => ['IN' => (array) $membershipIDs],
1323 return $memberships['values'];
1327 * Replace existing greeting tokens in message/subject.
1329 * This function operates by reference, modifying the first parameter. Other
1330 * methods for token replacement in this class return the modified string.
1331 * This leads to inconsistency in how these methods must be applied.
1333 * @TODO Remove that inconsistency in usage.
1335 * ::replaceContactTokens() may need to be called after this method, to
1336 * replace tokens supplied from this method.
1338 * @param string $tokenString
1339 * @param array $contactDetails
1340 * @param int $contactId
1341 * @param string $className
1342 * @param bool $escapeSmarty
1344 public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
1346 if (!$contactDetails && !$contactId) {
1350 // check if there are any tokens
1351 $greetingTokens = self
::getTokens($tokenString);
1353 if (!empty($greetingTokens)) {
1354 // first use the existing contact object for token replacement
1355 if (!empty($contactDetails)) {
1356 $tokenString = CRM_Utils_Token
::replaceContactTokens($tokenString, $contactDetails, TRUE, $greetingTokens, TRUE, $escapeSmarty);
1359 self
::removeNullContactTokens($tokenString, $contactDetails, $greetingTokens);
1360 // check if there are any unevaluated tokens
1361 $greetingTokens = self
::getTokens($tokenString);
1363 // $greetingTokens not empty, means there are few tokens which are not
1364 // evaluated, like custom data etc
1365 // so retrieve it from database
1366 if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) {
1367 $greetingsReturnProperties = array_flip(CRM_Utils_Array
::value('contact', $greetingTokens));
1368 $greetingsReturnProperties = array_fill_keys(array_keys($greetingsReturnProperties), 1);
1369 $contactParams = ['contact_id' => $contactId];
1371 $greetingDetails = self
::getTokenDetails($contactParams,
1372 $greetingsReturnProperties,
1378 // again replace tokens
1379 $tokenString = CRM_Utils_Token
::replaceContactTokens($tokenString,
1388 // check if there are still any unevaluated tokens
1389 $remainingTokens = self
::getTokens($tokenString);
1391 // $greetingTokens not empty, there are customized or hook tokens to replace
1392 if (!empty($remainingTokens)) {
1393 // Fill the return properties array
1394 $greetingTokens = $remainingTokens;
1395 reset($greetingTokens);
1396 $greetingsReturnProperties = [];
1397 foreach ($greetingTokens as $value) {
1398 $props = array_flip($value);
1399 $props = array_fill_keys(array_keys($props), 1);
1400 $greetingsReturnProperties = $greetingsReturnProperties +
$props;
1402 $contactParams = ['contact_id' => $contactId];
1403 $greetingDetails = self
::getTokenDetails($contactParams,
1404 $greetingsReturnProperties,
1409 // Prepare variables for calling replaceHookTokens
1410 $categories = array_keys($greetingTokens);
1411 list($contact) = $greetingDetails;
1412 // Replace tokens defined in Hooks.
1413 $tokenString = CRM_Utils_Token
::replaceHookTokens($tokenString, $contact[$contactId], $categories);
1419 * At this point, $contactDetails has loaded the contact from the DAO. Any
1420 * (non-custom) missing fields are null. By removing them, we can avoid
1421 * expensive calls to CRM_Contact_BAO_Query.
1423 * @param string $tokenString
1424 * @param array $contactDetails
1425 * @param array $greetingTokens
1427 private static function removeNullContactTokens(&$tokenString, $contactDetails, &$greetingTokens) {
1429 // Only applies to contact tokens
1430 if (!array_key_exists('contact', $greetingTokens)) {
1434 $greetingTokensOriginal = $greetingTokens;
1435 $contactFieldList = CRM_Contact_DAO_Contact
::fields();
1436 // Sometimes contactDetails are in a multidemensional array, sometimes a
1437 // single-dimension array.
1438 if (array_key_exists(0, $contactDetails) && is_array($contactDetails[0])) {
1439 $contactDetails = current($contactDetails[0]);
1441 $nullFields = array_keys(array_diff_key($contactFieldList, $contactDetails));
1443 // Handle legacy tokens
1444 foreach (self
::legacyContactTokens() as $oldToken => $newToken) {
1445 if (CRM_Utils_Array
::key($newToken, $nullFields)) {
1446 $nullFields[] = $oldToken;
1450 // Remove null contact fields from $greetingTokens
1451 $greetingTokens['contact'] = array_diff($greetingTokens['contact'], $nullFields);
1453 // Also remove them from $tokenString
1454 $removedTokens = array_diff($greetingTokensOriginal['contact'], $greetingTokens['contact']);
1455 // Handle legacy tokens again, sigh
1456 if (!empty($removedTokens)) {
1457 foreach ($removedTokens as $token) {
1458 if (CRM_Utils_Array
::value($token, self
::legacyContactTokens()) !== NULL) {
1459 $removedTokens[] = CRM_Utils_Array
::value($token, self
::legacyContactTokens());
1462 foreach ($removedTokens as $token) {
1463 $tokenString = str_replace("{contact.$token}", '', $tokenString);
1473 public static function flattenTokens(&$tokens) {
1474 $flattenTokens = [];
1481 if (!isset($tokens[$prop])) {
1484 foreach ($tokens[$prop] as $type => $names) {
1485 if (!isset($flattenTokens[$type])) {
1486 $flattenTokens[$type] = [];
1488 foreach ($names as $name) {
1489 $flattenTokens[$type][$name] = 1;
1494 return $flattenTokens;
1498 * Replace all user tokens in $str
1500 * @param string $str
1501 * The string with tokens to be replaced.
1503 * @param null $knownTokens
1504 * @param bool $escapeSmarty
1507 * The processed string
1509 public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
1511 if (!$knownTokens ||
1512 !isset($knownTokens[$key])
1517 $str = preg_replace_callback(
1518 self
::tokenRegex($key),
1519 function ($matches) use ($escapeSmarty) {
1520 return CRM_Utils_Token
::getUserTokenReplacement($matches[1], $escapeSmarty);
1529 * @param bool $escapeSmarty
1533 public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) {
1536 list($objectName, $objectValue) = explode('-', $token, 2);
1538 switch ($objectName) {
1540 $value = CRM_Core_Permission
::permissionEmails($objectValue);
1544 $value = CRM_Core_Permission
::roleEmails($objectValue);
1548 if ($escapeSmarty) {
1549 $value = self
::tokenEscapeSmarty($value);
1555 protected static function _buildContributionTokens() {
1556 $key = 'contribution';
1557 if (self
::$_tokens[$key] == NULL) {
1558 self
::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution
::exportableFields('All'),
1559 ['campaign', 'financial_type'],
1560 self
::getCustomFieldTokens('Contribution')
1566 * Store membership tokens on the static _tokens array.
1568 protected static function _buildMembershipTokens() {
1569 $key = 'membership';
1570 if (!isset(self
::$_tokens[$key]) || self
::$_tokens[$key] == NULL) {
1571 $membershipTokens = [];
1572 $tokens = CRM_Core_SelectValues
::membershipTokens();
1573 foreach ($tokens as $token => $dontCare) {
1574 $membershipTokens[] = substr($token, (strpos($token, '.') +
1), -1);
1576 self
::$_tokens[$key] = $membershipTokens;
1581 * Replace tokens for an entity.
1582 * @param string $entity
1583 * @param array $entityArray
1584 * (e.g. in format from api).
1585 * @param string $str
1586 * String to replace in.
1587 * @param array $knownTokens
1588 * Array of tokens present.
1589 * @param bool $escapeSmarty
1591 * string with replacements made
1593 public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1594 if (!$knownTokens ||
empty($knownTokens[$entity])) {
1598 $fn = 'get' . ucfirst($entity) . 'TokenReplacement';
1599 $fn = is_callable(['CRM_Utils_Token', $fn]) ?
$fn : 'getApiTokenReplacement';
1600 // since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
1601 foreach ($knownTokens[$entity] as $token) {
1602 $replacement = self
::$fn($entity, $token, $entityArray);
1603 if ($escapeSmarty) {
1604 $replacement = self
::tokenEscapeSmarty($replacement);
1606 $str = str_replace('{' . $entity . '.' . $token . '}', $replacement, $str);
1608 return preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1612 * @param int $caseId
1613 * @param string $str
1614 * @param array $knownTokens
1615 * @param bool $escapeSmarty
1617 * @throws \CiviCRM_API3_Exception
1619 public static function replaceCaseTokens($caseId, $str, $knownTokens = [], $escapeSmarty = FALSE) {
1620 if (!$knownTokens ||
empty($knownTokens['case'])) {
1623 $case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
1624 return self
::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
1628 * Generic function for formatting token replacement for an api field
1630 * @param string $entity
1631 * @param string $token
1632 * @param array $entityArray
1634 * @throws \CiviCRM_API3_Exception
1636 public static function getApiTokenReplacement($entity, $token, $entityArray) {
1637 if (!isset($entityArray[$token])) {
1640 $field = civicrm_api3($entity, 'getfield', ['action' => 'get', 'name' => $token, 'get_options' => 'get']);
1641 $field = $field['values'];
1642 $fieldType = $field['type'] ??
NULL;
1644 if ($fieldType == CRM_Utils_Type
::T_BOOLEAN
&& empty($field['options'])) {
1645 $field['options'] = [ts('No'), ts('Yes')];
1647 // Match pseudoconstants
1648 if (!empty($field['options'])) {
1650 foreach ((array) $entityArray[$token] as $val) {
1651 $ret[] = $field['options'][$val];
1653 return implode(', ', $ret);
1655 // Format date fields
1656 elseif ($entityArray[$token] && $fieldType == CRM_Utils_Type
::T_DATE
) {
1657 return CRM_Utils_Date
::customFormat($entityArray[$token]);
1659 return implode(', ', (array) $entityArray[$token]);
1663 * Replace Contribution tokens in html.
1665 * @param string $str
1666 * @param array $contribution
1667 * @param bool|string $html
1668 * @param string $knownTokens
1669 * @param bool|string $escapeSmarty
1673 public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1674 $key = 'contribution';
1675 if (!$knownTokens ||
empty($knownTokens[$key])) {
1679 self
::_buildContributionTokens();
1681 // here we intersect with the list of pre-configured valid tokens
1682 // so that we remove anything we do not recognize
1683 // I hope to move this step out of here soon and
1684 // then we will just iterate on a list of tokens that are passed to us
1686 $str = preg_replace_callback(
1687 self
::tokenRegex($key),
1688 function ($matches) use (&$contribution, $html, $escapeSmarty) {
1689 return CRM_Utils_Token
::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
1694 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1699 * We have a situation where we are rendering more than one token in each field because we are combining
1700 * tokens from more than one contribution when pdf thank you letters are grouped (CRM-14367)
1702 * The replaceContributionToken doesn't handle receive_date correctly in this scenario because of the formatting
1703 * it applies (other tokens are OK including date fields)
1705 * So we sort this out & then call the main function. Note that we are not escaping smarty on this fields like the main function
1706 * does - but the fields is already being formatted through a date function
1708 * @param string $separator
1709 * @param string $str
1710 * @param array $contribution
1711 * @param bool|string $html
1712 * @param string $knownTokens
1713 * @param bool|string $escapeSmarty
1717 public static function replaceMultipleContributionTokens($separator, $str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1718 if (empty($knownTokens['contribution'])) {
1722 if (in_array('receive_date', $knownTokens['contribution'])) {
1723 $formattedDates = [];
1724 $dates = explode($separator, $contribution['receive_date']);
1725 foreach ($dates as $date) {
1726 $formattedDates[] = CRM_Utils_Date
::customFormat($date, NULL, ['j', 'm', 'Y']);
1728 $str = str_replace("{contribution.receive_date}", implode($separator, $formattedDates), $str);
1729 unset($knownTokens['contribution']['receive_date']);
1731 return self
::replaceContributionTokens($str, $contribution, $html, $knownTokens, $escapeSmarty);
1735 * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
1736 * - this is used by the pdfLetter task from membership search
1737 * @param string $entity
1738 * should always be "membership"
1739 * @param string $token
1741 * @param array $membership
1742 * An api result array for a single membership.
1743 * @return string token replacement
1745 public static function getMembershipTokenReplacement($entity, $token, $membership) {
1746 self
::_buildMembershipTokens();
1749 $value = $membership['membership_name'];
1753 $statuses = CRM_Member_BAO_Membership
::buildOptions('status_id');
1754 $value = $statuses[$membership['status_id']];
1759 $value = civicrm_api3('membership_type', 'getvalue', [
1760 'id' => $membership['membership_type_id'],
1761 'return' => 'minimum_fee',
1763 $value = CRM_Utils_Money
::format($value, NULL, NULL, TRUE);
1765 catch (CiviCRM_API3_Exception
$e) {
1766 // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
1767 // api handles NULL (4.4)
1773 if (in_array($token, self
::$_tokens[$entity])) {
1774 $value = $membership[$token];
1775 if (CRM_Utils_String
::endsWith($token, '_date')) {
1776 $value = CRM_Utils_Date
::customFormat($value);
1781 $value = "{$entity}.{$token}";
1791 * @param $contribution
1793 * @param bool $escapeSmarty
1795 * @return mixed|string
1797 public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) {
1798 self
::_buildContributionTokens();
1801 case 'total_amount':
1804 case 'non_deductible_amount':
1805 $value = CRM_Utils_Money
::format(CRM_Utils_Array
::retrieveValueRecursive($contribution, $token));
1808 case 'receive_date':
1809 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1810 $value = CRM_Utils_Date
::customFormat($value, NULL, ['j', 'm', 'Y']);
1814 if (!in_array($token, self
::$_tokens['contribution'])) {
1815 $value = "{contribution.$token}";
1818 $value = CRM_Utils_Array
::retrieveValueRecursive($contribution, $token);
1823 if ($escapeSmarty) {
1824 $value = self
::tokenEscapeSmarty($value);
1831 * [legacy_token => new_token]
1833 public static function legacyContactTokens() {
1835 'individual_prefix' => 'prefix_id',
1836 'individual_suffix' => 'suffix_id',
1837 'gender' => 'gender_id',
1838 'communication_style' => 'communication_style_id',
1843 * Get all custom field tokens of $entity
1845 * @param string $entity
1846 * @param bool $usedForTokenWidget
1849 * return custom field tokens in array('custom_N' => 'label') format
1851 public static function getCustomFieldTokens($entity, $usedForTokenWidget = FALSE) {
1853 $tokenName = $usedForTokenWidget ?
"{contribution.custom_%d}" : "custom_%d";
1854 foreach (CRM_Core_BAO_CustomField
::getFields($entity) as $id => $info) {
1855 $customTokens[sprintf($tokenName, $id)] = $info['label'];
1858 return $customTokens;
1862 * Formats a token list for the select2 widget
1867 public static function formatTokensForDisplay($tokens) {
1868 $sorted = $output = [];
1870 // Sort in ascending order by ignoring word case
1871 natcasesort($tokens);
1873 // Attempt to place tokens into optgroups
1874 // @todo These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice.
1875 foreach ($tokens as $k => $v) {
1876 // Check to see if this token is already in a group e.g. for custom fields
1877 $split = explode(' :: ', $v);
1878 if (!empty($split[1])) {
1879 $sorted[$split[1]][] = ['id' => $k, 'text' => $split[0]];
1883 $split = explode('.', trim($k, '{}'));
1884 if (isset($split[1])) {
1885 $entity = array_key_exists($split[1], CRM_Core_DAO_Address
::export()) ?
'Address' : ucwords(str_replace('_', ' ', $split[0]));
1888 $entity = 'Contact';
1890 $sorted[ts($entity)][] = ['id' => $k, 'text' => $v];
1895 foreach ($sorted as $k => $v) {
1896 $output[] = ['text' => $k, 'children' => $v];