[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/ -> MailboxHeader.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   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  use Egulias\EmailValidator\EmailValidator;
  12  use Egulias\EmailValidator\Validation\RFCValidation;
  13  
  14  /**
  15   * A Mailbox Address MIME Header for something like From or Sender.
  16   *
  17   * @author Chris Corbyn
  18   */
  19  class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
  20  {
  21      /**
  22       * The mailboxes used in this Header.
  23       *
  24       * @var string[]
  25       */
  26      private $mailboxes = [];
  27  
  28      /**
  29       * The strict EmailValidator.
  30       *
  31       * @var EmailValidator
  32       */
  33      private $emailValidator;
  34  
  35      private $addressEncoder;
  36  
  37      /**
  38       * Creates a new MailboxHeader with $name.
  39       *
  40       * @param string $name of Header
  41       */
  42      public function __construct($name, Swift_Mime_HeaderEncoder $encoder, EmailValidator $emailValidator, Swift_AddressEncoder $addressEncoder = null)
  43      {
  44          $this->setFieldName($name);
  45          $this->setEncoder($encoder);
  46          $this->emailValidator = $emailValidator;
  47          $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder();
  48      }
  49  
  50      /**
  51       * Get the type of Header that this instance represents.
  52       *
  53       * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  54       * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  55       *
  56       * @return int
  57       */
  58      public function getFieldType()
  59      {
  60          return self::TYPE_MAILBOX;
  61      }
  62  
  63      /**
  64       * Set the model for the field body.
  65       *
  66       * This method takes a string, or an array of addresses.
  67       *
  68       * @param mixed $model
  69       *
  70       * @throws Swift_RfcComplianceException
  71       */
  72      public function setFieldBodyModel($model)
  73      {
  74          $this->setNameAddresses($model);
  75      }
  76  
  77      /**
  78       * Get the model for the field body.
  79       *
  80       * This method returns an associative array like {@link getNameAddresses()}
  81       *
  82       * @throws Swift_RfcComplianceException
  83       *
  84       * @return array
  85       */
  86      public function getFieldBodyModel()
  87      {
  88          return $this->getNameAddresses();
  89      }
  90  
  91      /**
  92       * Set a list of mailboxes to be shown in this Header.
  93       *
  94       * The mailboxes can be a simple array of addresses, or an array of
  95       * key=>value pairs where (email => personalName).
  96       * Example:
  97       * <code>
  98       * <?php
  99       * //Sets two mailboxes in the Header, one with a personal name
 100       * $header->setNameAddresses(array(
 101       *  'chris@swiftmailer.org' => 'Chris Corbyn',
 102       *  'mark@swiftmailer.org' //No associated personal name
 103       *  ));
 104       * ?>
 105       * </code>
 106       *
 107       * @see __construct()
 108       * @see setAddresses()
 109       * @see setValue()
 110       *
 111       * @param string|string[] $mailboxes
 112       *
 113       * @throws Swift_RfcComplianceException
 114       */
 115      public function setNameAddresses($mailboxes)
 116      {
 117          $this->mailboxes = $this->normalizeMailboxes((array) $mailboxes);
 118          $this->setCachedValue(null); //Clear any cached value
 119      }
 120  
 121      /**
 122       * Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
 123       *
 124       * Example:
 125       * <code>
 126       * <?php
 127       * $header = new Swift_Mime_Headers_MailboxHeader('From',
 128       *  array('chris@swiftmailer.org' => 'Chris Corbyn',
 129       *  'mark@swiftmailer.org' => 'Mark Corbyn')
 130       *  );
 131       * print_r($header->getNameAddressStrings());
 132       * // array (
 133       * // 0 => Chris Corbyn <chris@swiftmailer.org>,
 134       * // 1 => Mark Corbyn <mark@swiftmailer.org>
 135       * // )
 136       * ?>
 137       * </code>
 138       *
 139       * @see getNameAddresses()
 140       * @see toString()
 141       *
 142       * @throws Swift_RfcComplianceException
 143       *
 144       * @return string[]
 145       */
 146      public function getNameAddressStrings()
 147      {
 148          return $this->createNameAddressStrings($this->getNameAddresses());
 149      }
 150  
 151      /**
 152       * Get all mailboxes in this Header as key=>value pairs.
 153       *
 154       * The key is the address and the value is the name (or null if none set).
 155       * Example:
 156       * <code>
 157       * <?php
 158       * $header = new Swift_Mime_Headers_MailboxHeader('From',
 159       *  array('chris@swiftmailer.org' => 'Chris Corbyn',
 160       *  'mark@swiftmailer.org' => 'Mark Corbyn')
 161       *  );
 162       * print_r($header->getNameAddresses());
 163       * // array (
 164       * // chris@swiftmailer.org => Chris Corbyn,
 165       * // mark@swiftmailer.org => Mark Corbyn
 166       * // )
 167       * ?>
 168       * </code>
 169       *
 170       * @see getAddresses()
 171       * @see getNameAddressStrings()
 172       *
 173       * @return string[]
 174       */
 175      public function getNameAddresses()
 176      {
 177          return $this->mailboxes;
 178      }
 179  
 180      /**
 181       * Makes this Header represent a list of plain email addresses with no names.
 182       *
 183       * Example:
 184       * <code>
 185       * <?php
 186       * //Sets three email addresses as the Header data
 187       * $header->setAddresses(
 188       *  array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
 189       *  );
 190       * ?>
 191       * </code>
 192       *
 193       * @see setNameAddresses()
 194       * @see setValue()
 195       *
 196       * @param string[] $addresses
 197       *
 198       * @throws Swift_RfcComplianceException
 199       */
 200      public function setAddresses($addresses)
 201      {
 202          $this->setNameAddresses(array_values((array) $addresses));
 203      }
 204  
 205      /**
 206       * Get all email addresses in this Header.
 207       *
 208       * @see getNameAddresses()
 209       *
 210       * @return string[]
 211       */
 212      public function getAddresses()
 213      {
 214          return array_keys($this->mailboxes);
 215      }
 216  
 217      /**
 218       * Remove one or more addresses from this Header.
 219       *
 220       * @param string|string[] $addresses
 221       */
 222      public function removeAddresses($addresses)
 223      {
 224          $this->setCachedValue(null);
 225          foreach ((array) $addresses as $address) {
 226              unset($this->mailboxes[$address]);
 227          }
 228      }
 229  
 230      /**
 231       * Get the string value of the body in this Header.
 232       *
 233       * This is not necessarily RFC 2822 compliant since folding white space will
 234       * not be added at this stage (see {@link toString()} for that).
 235       *
 236       * @see toString()
 237       *
 238       * @throws Swift_RfcComplianceException
 239       *
 240       * @return string
 241       */
 242      public function getFieldBody()
 243      {
 244          // Compute the string value of the header only if needed
 245          if (null === $this->getCachedValue()) {
 246              $this->setCachedValue($this->createMailboxListString($this->mailboxes));
 247          }
 248  
 249          return $this->getCachedValue();
 250      }
 251  
 252      /**
 253       * Normalizes a user-input list of mailboxes into consistent key=>value pairs.
 254       *
 255       * @param string[] $mailboxes
 256       *
 257       * @return string[]
 258       */
 259      protected function normalizeMailboxes(array $mailboxes)
 260      {
 261          $actualMailboxes = [];
 262  
 263          foreach ($mailboxes as $key => $value) {
 264              if (is_string($key)) {
 265                  //key is email addr
 266                  $address = $key;
 267                  $name = $value;
 268              } else {
 269                  $address = $value;
 270                  $name = null;
 271              }
 272              $this->assertValidAddress($address);
 273              $actualMailboxes[$address] = $name;
 274          }
 275  
 276          return $actualMailboxes;
 277      }
 278  
 279      /**
 280       * Produces a compliant, formatted display-name based on the string given.
 281       *
 282       * @param string $displayName as displayed
 283       * @param bool   $shorten     the first line to make remove for header name
 284       *
 285       * @return string
 286       */
 287      protected function createDisplayNameString($displayName, $shorten = false)
 288      {
 289          return $this->createPhrase($this, $displayName, $this->getCharset(), $this->getEncoder(), $shorten);
 290      }
 291  
 292      /**
 293       * Creates a string form of all the mailboxes in the passed array.
 294       *
 295       * @param string[] $mailboxes
 296       *
 297       * @throws Swift_RfcComplianceException
 298       *
 299       * @return string
 300       */
 301      protected function createMailboxListString(array $mailboxes)
 302      {
 303          return implode(', ', $this->createNameAddressStrings($mailboxes));
 304      }
 305  
 306      /**
 307       * Redefine the encoding requirements for mailboxes.
 308       *
 309       * All "specials" must be encoded as the full header value will not be quoted
 310       *
 311       * @see RFC 2822 3.2.1
 312       *
 313       * @param string $token
 314       *
 315       * @return bool
 316       */
 317      protected function tokenNeedsEncoding($token)
 318      {
 319          return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token);
 320      }
 321  
 322      /**
 323       * Return an array of strings conforming the the name-addr spec of RFC 2822.
 324       *
 325       * @param string[] $mailboxes
 326       *
 327       * @return string[]
 328       */
 329      private function createNameAddressStrings(array $mailboxes)
 330      {
 331          $strings = [];
 332  
 333          foreach ($mailboxes as $email => $name) {
 334              $mailboxStr = $this->addressEncoder->encodeString($email);
 335              if (null !== $name) {
 336                  $nameStr = $this->createDisplayNameString($name, empty($strings));
 337                  $mailboxStr = $nameStr.' <'.$mailboxStr.'>';
 338              }
 339              $strings[] = $mailboxStr;
 340          }
 341  
 342          return $strings;
 343      }
 344  
 345      /**
 346       * Throws an Exception if the address passed does not comply with RFC 2822.
 347       *
 348       * @param string $address
 349       *
 350       * @throws Swift_RfcComplianceException If invalid.
 351       */
 352      private function assertValidAddress($address)
 353      {
 354          if (!$this->emailValidator->isValid($address, new RFCValidation())) {
 355              throw new Swift_RfcComplianceException(
 356                  'Address in mailbox given ['.$address.'] does not comply with RFC 2822, 3.6.2.'
 357              );
 358          }
 359      }
 360  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1