TYPO3  7.6
RedirectingPlugin.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2009 Fabien Potencier
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
23  private $_recipient;
24 
30  private $_whitelist = array();
31 
38  public function __construct($recipient, array $whitelist = array())
39  {
40  $this->_recipient = $recipient;
41  $this->_whitelist = $whitelist;
42  }
43 
49  public function setRecipient($recipient)
50  {
51  $this->_recipient = $recipient;
52  }
53 
59  public function getRecipient()
60  {
61  return $this->_recipient;
62  }
63 
69  public function setWhitelist(array $whitelist)
70  {
71  $this->_whitelist = $whitelist;
72  }
73 
79  public function getWhitelist()
80  {
81  return $this->_whitelist;
82  }
83 
90  {
91  $message = $evt->getMessage();
92  $headers = $message->getHeaders();
93 
94  // conditionally save current recipients
95 
96  if ($headers->has('to')) {
97  $headers->addMailboxHeader('X-Swift-To', $message->getTo());
98  }
99 
100  if ($headers->has('cc')) {
101  $headers->addMailboxHeader('X-Swift-Cc', $message->getCc());
102  }
103 
104  if ($headers->has('bcc')) {
105  $headers->addMailboxHeader('X-Swift-Bcc', $message->getBcc());
106  }
107 
108  // Filter remaining headers against whitelist
109  $this->_filterHeaderSet($headers, 'To');
110  $this->_filterHeaderSet($headers, 'Cc');
111  $this->_filterHeaderSet($headers, 'Bcc');
112 
113  // Add each hard coded recipient
114  $to = $message->getTo();
115  if (null === $to) {
116  $to = array();
117  }
118 
119  foreach ((array) $this->_recipient as $recipient) {
120  if (!array_key_exists($recipient, $to)) {
121  $message->addTo($recipient);
122  }
123  }
124  }
125 
132  private function _filterHeaderSet(Swift_Mime_HeaderSet $headerSet, $type)
133  {
134  foreach ($headerSet->getAll($type) as $headers) {
135  $headers->setNameAddresses($this->_filterNameAddresses($headers->getNameAddresses()));
136  }
137  }
138 
146  private function _filterNameAddresses(array $recipients)
147  {
148  $filtered = array();
149 
150  foreach ($recipients as $address => $name) {
151  if ($this->_isWhitelisted($address)) {
152  $filtered[$address] = $name;
153  }
154  }
155 
156  return $filtered;
157  }
158 
166  protected function _isWhitelisted($recipient)
167  {
168  if (in_array($recipient, (array) $this->_recipient)) {
169  return true;
170  }
171 
172  foreach ($this->_whitelist as $pattern) {
173  if (preg_match($pattern, $recipient)) {
174  return true;
175  }
176  }
177 
178  return false;
179  }
180 
186  public function sendPerformed(Swift_Events_SendEvent $evt)
187  {
188  $this->_restoreMessage($evt->getMessage());
189  }
190 
191  private function _restoreMessage(Swift_Mime_Message $message)
192  {
193  // restore original headers
194  $headers = $message->getHeaders();
195 
196  if ($headers->has('X-Swift-To')) {
197  $message->setTo($headers->get('X-Swift-To')->getNameAddresses());
198  $headers->removeAll('X-Swift-To');
199  } else {
200  $message->setTo(null);
201  }
202 
203  if ($headers->has('X-Swift-Cc')) {
204  $message->setCc($headers->get('X-Swift-Cc')->getNameAddresses());
205  $headers->removeAll('X-Swift-Cc');
206  }
207 
208  if ($headers->has('X-Swift-Bcc')) {
209  $message->setBcc($headers->get('X-Swift-Bcc')->getNameAddresses());
210  $headers->removeAll('X-Swift-Bcc');
211  }
212  }
213 }