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