[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Plugins/ -> RedirectingPlugin.php (source)

   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  
  11  /**
  12   * Redirects all email to a single recipient.
  13   *
  14   * @author     Fabien Potencier
  15   */
  16  class Swift_Plugins_RedirectingPlugin implements Swift_Events_SendListener
  17  {
  18      /**
  19       * The recipient who will receive all messages.
  20       *
  21       * @var mixed
  22       */
  23      private $recipient;
  24  
  25      /**
  26       * List of regular expression for recipient whitelisting.
  27       *
  28       * @var array
  29       */
  30      private $whitelist = [];
  31  
  32      /**
  33       * Create a new RedirectingPlugin.
  34       *
  35       * @param mixed $recipient
  36       */
  37      public function __construct($recipient, array $whitelist = [])
  38      {
  39          $this->recipient = $recipient;
  40          $this->whitelist = $whitelist;
  41      }
  42  
  43      /**
  44       * Set the recipient of all messages.
  45       *
  46       * @param mixed $recipient
  47       */
  48      public function setRecipient($recipient)
  49      {
  50          $this->recipient = $recipient;
  51      }
  52  
  53      /**
  54       * Get the recipient of all messages.
  55       *
  56       * @return mixed
  57       */
  58      public function getRecipient()
  59      {
  60          return $this->recipient;
  61      }
  62  
  63      /**
  64       * Set a list of regular expressions to whitelist certain recipients.
  65       */
  66      public function setWhitelist(array $whitelist)
  67      {
  68          $this->whitelist = $whitelist;
  69      }
  70  
  71      /**
  72       * Get the whitelist.
  73       *
  74       * @return array
  75       */
  76      public function getWhitelist()
  77      {
  78          return $this->whitelist;
  79      }
  80  
  81      /**
  82       * Invoked immediately before the Message is sent.
  83       */
  84      public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  85      {
  86          $message = $evt->getMessage();
  87          $headers = $message->getHeaders();
  88  
  89          // conditionally save current recipients
  90  
  91          if ($headers->has('to')) {
  92              $headers->addMailboxHeader('X-Swift-To', $message->getTo());
  93          }
  94  
  95          if ($headers->has('cc')) {
  96              $headers->addMailboxHeader('X-Swift-Cc', $message->getCc());
  97          }
  98  
  99          if ($headers->has('bcc')) {
 100              $headers->addMailboxHeader('X-Swift-Bcc', $message->getBcc());
 101          }
 102  
 103          // Filter remaining headers against whitelist
 104          $this->filterHeaderSet($headers, 'To');
 105          $this->filterHeaderSet($headers, 'Cc');
 106          $this->filterHeaderSet($headers, 'Bcc');
 107  
 108          // Add each hard coded recipient
 109          $to = $message->getTo();
 110          if (null === $to) {
 111              $to = [];
 112          }
 113  
 114          foreach ((array) $this->recipient as $recipient) {
 115              if (!array_key_exists($recipient, $to)) {
 116                  $message->addTo($recipient);
 117              }
 118          }
 119      }
 120  
 121      /**
 122       * Filter header set against a whitelist of regular expressions.
 123       *
 124       * @param string $type
 125       */
 126      private function filterHeaderSet(Swift_Mime_SimpleHeaderSet $headerSet, $type)
 127      {
 128          foreach ($headerSet->getAll($type) as $headers) {
 129              $headers->setNameAddresses($this->filterNameAddresses($headers->getNameAddresses()));
 130          }
 131      }
 132  
 133      /**
 134       * Filtered list of addresses => name pairs.
 135       *
 136       * @return array
 137       */
 138      private function filterNameAddresses(array $recipients)
 139      {
 140          $filtered = [];
 141  
 142          foreach ($recipients as $address => $name) {
 143              if ($this->isWhitelisted($address)) {
 144                  $filtered[$address] = $name;
 145              }
 146          }
 147  
 148          return $filtered;
 149      }
 150  
 151      /**
 152       * Matches address against whitelist of regular expressions.
 153       *
 154       * @return bool
 155       */
 156      protected function isWhitelisted($recipient)
 157      {
 158          if (in_array($recipient, (array) $this->recipient)) {
 159              return true;
 160          }
 161  
 162          foreach ($this->whitelist as $pattern) {
 163              if (preg_match($pattern, $recipient)) {
 164                  return true;
 165              }
 166          }
 167  
 168          return false;
 169      }
 170  
 171      /**
 172       * Invoked immediately after the Message is sent.
 173       */
 174      public function sendPerformed(Swift_Events_SendEvent $evt)
 175      {
 176          $this->restoreMessage($evt->getMessage());
 177      }
 178  
 179      private function restoreMessage(Swift_Mime_SimpleMessage $message)
 180      {
 181          // restore original headers
 182          $headers = $message->getHeaders();
 183  
 184          if ($headers->has('X-Swift-To')) {
 185              $message->setTo($headers->get('X-Swift-To')->getNameAddresses());
 186              $headers->removeAll('X-Swift-To');
 187          } else {
 188              $message->setTo(null);
 189          }
 190  
 191          if ($headers->has('X-Swift-Cc')) {
 192              $message->setCc($headers->get('X-Swift-Cc')->getNameAddresses());
 193              $headers->removeAll('X-Swift-Cc');
 194          }
 195  
 196          if ($headers->has('X-Swift-Bcc')) {
 197              $message->setBcc($headers->get('X-Swift-Bcc')->getNameAddresses());
 198              $headers->removeAll('X-Swift-Bcc');
 199          }
 200      }
 201  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1