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