Merge pull request #17349 from eileenmcnaughton/validate
[civicrm-core.git] / CRM / Utils / Mail / FilteredPearMailer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * The filtered-mailer is a utility to wrap an existing PEAR Mail class
14 * and apply extra filters. It is primarily intended for resolving
15 * quirks in the standard implementations.
16 *
17 * This wrapper acts a bit like a chameleon, passing-through properties
18 * from the underlying object. Consequently, internal properties are
19 * prefixed with `_` to avoid conflict.
20 *
21 * @package CRM
22 * @copyright CiviCRM LLC https://civicrm.org/licensing
23 */
24 class CRM_Utils_Mail_FilteredPearMailer extends Mail {
25
26 /**
27 * @var string
28 * Ex: 'smtp' or 'sendmail'
29 */
30 protected $_driver;
31
32 /**
33 * @var array
34 */
35 protected $_params;
36
37 /**
38 * @var Mail
39 */
40 protected $_delegate;
41
42 /**
43 * @var callable[]
44 */
45 protected $_filters = [];
46
47 /**
48 * CRM_Utils_Mail_FilteredPearMailer constructor.
49 * @param string $driver
50 * @param array $params
51 * @param Mail $mailer
52 */
53 public function __construct($driver, $params, $mailer) {
54 $this->_driver = $driver;
55 $this->_params = $params;
56 $this->_delegate = $mailer;
57 }
58
59 public function send($recipients, $headers, $body) {
60 $filterArgs = [$this, &$recipients, &$headers, &$body];
61 foreach ($this->_filters as $filter) {
62 $result = call_user_func_array($filter, $filterArgs);
63 if ($result !== NULL) {
64 return $result;
65 }
66 }
67
68 return $this->_delegate->send($recipients, $headers, $body);
69 }
70
71 /**
72 * @param string $id
73 * Unique ID for this filter. Filters are sorted by ID.
74 * Suggestion: '{nnnn}_{name}', where '{nnnn}' is a number.
75 * Filters are sorted and executed in order.
76 * @param callable $func
77 * function(FilteredPearMailer $mailer, mixed $recipients, array $headers, string $body).
78 * The return value should generally be null/void. However, if you wish to
79 * short-circuit execution of the filters, then return a concrete value.
80 * @return static
81 */
82 public function addFilter($id, $func) {
83 $this->_filters[$id] = $func;
84 ksort($this->_filters);
85 return $this;
86 }
87
88 /**
89 * @return string
90 * Ex: 'smtp', 'sendmail', 'mail'.
91 */
92 public function getDriver() {
93 return $this->_driver;
94 }
95
96 public function &__get($name) {
97 return $this->_delegate->{$name};
98 }
99
100 public function __set($name, $value) {
101 return $this->_delegate->{$name} = $value;
102 }
103
104 public function __isset($name) {
105 return isset($this->_delegate->{$name});
106 }
107
108 public function __unset($name) {
109 unset($this->_delegate->{$name});
110 }
111
112 public function disconnect() {
113 if (is_callable([$this->_delegate, 'disconnect'])) {
114 return $this->_delegate->disconnect();
115 }
116 return TRUE;
117 }
118
119 }