3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
11 namespace Civi\FlexMailer\Listener
;
13 use CRM_Flexmailer_ExtensionUtil
as E
;
14 use Civi\FlexMailer\Event\CheckSendableEvent
;
17 * Class RequiredTokens
18 * @package Civi\FlexMailer\Listener
20 * The RequiredTokens listener checks draft mailings for traditional
21 * CiviMail tokens like `{action.unsubscribeUrl}`, which are often required
22 * to comply with anti-spam regulations.
24 class RequiredTokens
extends BaseListener
{
28 * Ex: array('domain.address' => ts('The organizational postal address'))
30 private $requiredTokens;
35 * List of template-types for which we are capable of enforcing token
38 private $templateTypes;
41 * RequiredTokens constructor.
43 * @param array $templateTypes
44 * Ex: array('traditional').
45 * @param array $requiredTokens
46 * Ex: array('domain.address' => ts('The organizational postal address'))
48 public function __construct($templateTypes, $requiredTokens) {
49 $this->templateTypes
= $templateTypes;
50 $this->requiredTokens
= $requiredTokens;
54 * Check for required fields.
56 * @param \Civi\FlexMailer\Event\CheckSendableEvent $e
58 public function onCheckSendable(CheckSendableEvent
$e) {
59 if (!$this->isActive()) {
62 if (\Civi
::settings()->get('disable_mandatory_tokens_check')) {
65 if (!in_array($e->getMailing()->template_type
, $this->getTemplateTypes())) {
69 foreach (array('body_html', 'body_text') as $field) {
70 $str = $e->getFullBody($field);
74 foreach ($this->findMissingTokens($str) as $token => $desc) {
75 $e->setError("{$field}:{$token}", E
::ts('This message is missing a required token - {%1}: %2',
76 array(1 => $token, 2 => $desc)
82 public function findMissingTokens($str) {
84 foreach ($this->getRequiredTokens() as $token => $value) {
85 if (!is_array($value)) {
86 if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
87 $missing[$token] = $value;
93 foreach ($value as $t => $d) {
95 if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) {
100 $missing[$token] = $desc;
109 * Ex: array('domain.address' => ts('The organizational postal address'))
111 public function getRequiredTokens() {
112 return $this->requiredTokens
;
116 * @param array $requiredTokens
117 * Ex: array('domain.address' => ts('The organizational postal address'))
118 * @return RequiredTokens
120 public function setRequiredTokens($requiredTokens) {
121 $this->requiredTokens
= $requiredTokens;
127 * Ex: array('traditional').
129 public function getTemplateTypes() {
130 return $this->templateTypes
;
134 * Set the list of template-types for which we check tokens.
136 * @param array $templateTypes
137 * Ex: array('traditional').
138 * @return RequiredTokens
140 public function setTemplateTypes($templateTypes) {
141 $this->templateTypes
= $templateTypes;
146 * Add to the list of template-types for which we check tokens.
148 * @param array $templateTypes
149 * Ex: array('traditional').
150 * @return RequiredTokens
152 public function addTemplateTypes($templateTypes) {
153 $this->templateTypes
= array_unique(array_merge($this->templateTypes
, $templateTypes));