[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/ -> ParameterizedHeader.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  /**
  12   * An abstract base MIME Header.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_Headers_ParameterizedHeader extends Swift_Mime_Headers_UnstructuredHeader
  17  {
  18      /**
  19       * RFC 2231's definition of a token.
  20       *
  21       * @var string
  22       */
  23      const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
  24  
  25      /**
  26       * The Encoder used to encode the parameters.
  27       *
  28       * @var Swift_Encoder
  29       */
  30      private $paramEncoder;
  31  
  32      /**
  33       * The parameters as an associative array.
  34       *
  35       * @var string[]
  36       */
  37      private $params = [];
  38  
  39      /**
  40       * Creates a new ParameterizedHeader with $name.
  41       *
  42       * @param string $name
  43       */
  44      public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder = null)
  45      {
  46          parent::__construct($name, $encoder);
  47          $this->paramEncoder = $paramEncoder;
  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_PARAMETERIZED;
  61      }
  62  
  63      /**
  64       * Set the character set used in this Header.
  65       *
  66       * @param string $charset
  67       */
  68      public function setCharset($charset)
  69      {
  70          parent::setCharset($charset);
  71          if (isset($this->paramEncoder)) {
  72              $this->paramEncoder->charsetChanged($charset);
  73          }
  74      }
  75  
  76      /**
  77       * Set the value of $parameter.
  78       *
  79       * @param string $parameter
  80       * @param string $value
  81       */
  82      public function setParameter($parameter, $value)
  83      {
  84          $this->setParameters(array_merge($this->getParameters(), [$parameter => $value]));
  85      }
  86  
  87      /**
  88       * Get the value of $parameter.
  89       *
  90       * @param string $parameter
  91       *
  92       * @return string
  93       */
  94      public function getParameter($parameter)
  95      {
  96          $params = $this->getParameters();
  97  
  98          return $params[$parameter] ?? null;
  99      }
 100  
 101      /**
 102       * Set an associative array of parameter names mapped to values.
 103       *
 104       * @param string[] $parameters
 105       */
 106      public function setParameters(array $parameters)
 107      {
 108          $this->clearCachedValueIf($this->params != $parameters);
 109          $this->params = $parameters;
 110      }
 111  
 112      /**
 113       * Returns an associative array of parameter names mapped to values.
 114       *
 115       * @return string[]
 116       */
 117      public function getParameters()
 118      {
 119          return $this->params;
 120      }
 121  
 122      /**
 123       * Get the value of this header prepared for rendering.
 124       *
 125       * @return string
 126       */
 127      public function getFieldBody() //TODO: Check caching here
 128      {
 129          $body = parent::getFieldBody();
 130          foreach ($this->params as $name => $value) {
 131              if (null !== $value) {
 132                  // Add the parameter
 133                  $body .= '; '.$this->createParameter($name, $value);
 134              }
 135          }
 136  
 137          return $body;
 138      }
 139  
 140      /**
 141       * Generate a list of all tokens in the final header.
 142       *
 143       * This doesn't need to be overridden in theory, but it is for implementation
 144       * reasons to prevent potential breakage of attributes.
 145       *
 146       * @param string $string The string to tokenize
 147       *
 148       * @return array An array of tokens as strings
 149       */
 150      protected function toTokens($string = null)
 151      {
 152          $tokens = parent::toTokens(parent::getFieldBody());
 153  
 154          // Try creating any parameters
 155          foreach ($this->params as $name => $value) {
 156              if (null !== $value) {
 157                  // Add the semi-colon separator
 158                  $tokens[count($tokens) - 1] .= ';';
 159                  $tokens = array_merge($tokens, $this->generateTokenLines(
 160                      ' '.$this->createParameter($name, $value)
 161                      ));
 162              }
 163          }
 164  
 165          return $tokens;
 166      }
 167  
 168      /**
 169       * Render a RFC 2047 compliant header parameter from the $name and $value.
 170       *
 171       * @param string $name
 172       * @param string $value
 173       *
 174       * @return string
 175       */
 176      private function createParameter($name, $value)
 177      {
 178          $origValue = $value;
 179  
 180          $encoded = false;
 181          // Allow room for parameter name, indices, "=" and DQUOTEs
 182          $maxValueLength = $this->getMaxLineLength() - strlen($name.'=*N"";') - 1;
 183          $firstLineOffset = 0;
 184  
 185          // If it's not already a valid parameter value...
 186          if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
 187              // TODO: text, or something else??
 188              // ... and it's not ascii
 189              if (!preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $value)) {
 190                  $encoded = true;
 191                  // Allow space for the indices, charset and language
 192                  $maxValueLength = $this->getMaxLineLength() - strlen($name.'*N*="";') - 1;
 193                  $firstLineOffset = strlen(
 194                      $this->getCharset()."'".$this->getLanguage()."'"
 195                      );
 196              }
 197          }
 198  
 199          // Encode if we need to
 200          if ($encoded || strlen($value) > $maxValueLength) {
 201              if (isset($this->paramEncoder)) {
 202                  $value = $this->paramEncoder->encodeString(
 203                      $origValue, $firstLineOffset, $maxValueLength, $this->getCharset()
 204                      );
 205              } else {
 206                  // We have to go against RFC 2183/2231 in some areas for interoperability
 207                  $value = $this->getTokenAsEncodedWord($origValue);
 208                  $encoded = false;
 209              }
 210          }
 211  
 212          $valueLines = isset($this->paramEncoder) ? explode("\r\n", $value) : [$value];
 213  
 214          // Need to add indices
 215          if (count($valueLines) > 1) {
 216              $paramLines = [];
 217              foreach ($valueLines as $i => $line) {
 218                  $paramLines[] = $name.'*'.$i.
 219                      $this->getEndOfParameterValue($line, true, 0 == $i);
 220              }
 221  
 222              return implode(";\r\n ", $paramLines);
 223          } else {
 224              return $name.$this->getEndOfParameterValue(
 225                  $valueLines[0], $encoded, true
 226                  );
 227          }
 228      }
 229  
 230      /**
 231       * Returns the parameter value from the "=" and beyond.
 232       *
 233       * @param string $value     to append
 234       * @param bool   $encoded
 235       * @param bool   $firstLine
 236       *
 237       * @return string
 238       */
 239      private function getEndOfParameterValue($value, $encoded = false, $firstLine = false)
 240      {
 241          if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
 242              $value = '"'.$value.'"';
 243          }
 244          $prepend = '=';
 245          if ($encoded) {
 246              $prepend = '*=';
 247              if ($firstLine) {
 248                  $prepend = '*='.$this->getCharset()."'".$this->getLanguage().
 249                      "'";
 250              }
 251          }
 252  
 253          return $prepend.$value;
 254      }
 255  }


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