[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/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 implements Swift_Mime_ParameterizedHeader
  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 = array();
  38  
  39      /**
  40       * Creates a new ParameterizedHeader with $name.
  41       *
  42       * @param string                   $name
  43       * @param Swift_Mime_HeaderEncoder $encoder
  44       * @param Swift_Encoder            $paramEncoder, optional
  45       * @param Swift_Mime_Grammar       $grammar
  46       */
  47      public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder = null, Swift_Mime_Grammar $grammar)
  48      {
  49          parent::__construct($name, $encoder, $grammar);
  50          $this->_paramEncoder = $paramEncoder;
  51      }
  52  
  53      /**
  54       * Get the type of Header that this instance represents.
  55       *
  56       * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  57       * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  58       *
  59       * @return int
  60       */
  61      public function getFieldType()
  62      {
  63          return self::TYPE_PARAMETERIZED;
  64      }
  65  
  66      /**
  67       * Set the character set used in this Header.
  68       *
  69       * @param string $charset
  70       */
  71      public function setCharset($charset)
  72      {
  73          parent::setCharset($charset);
  74          if (isset($this->_paramEncoder)) {
  75              $this->_paramEncoder->charsetChanged($charset);
  76          }
  77      }
  78  
  79      /**
  80       * Set the value of $parameter.
  81       *
  82       * @param string $parameter
  83       * @param string $value
  84       */
  85      public function setParameter($parameter, $value)
  86      {
  87          $this->setParameters(array_merge($this->getParameters(), array($parameter => $value)));
  88      }
  89  
  90      /**
  91       * Get the value of $parameter.
  92       *
  93       * @param string $parameter
  94       *
  95       * @return string
  96       */
  97      public function getParameter($parameter)
  98      {
  99          $params = $this->getParameters();
 100  
 101          return array_key_exists($parameter, $params)
 102              ? $params[$parameter]
 103              : null;
 104      }
 105  
 106      /**
 107       * Set an associative array of parameter names mapped to values.
 108       *
 109       * @param string[] $parameters
 110       */
 111      public function setParameters(array $parameters)
 112      {
 113          $this->clearCachedValueIf($this->_params != $parameters);
 114          $this->_params = $parameters;
 115      }
 116  
 117      /**
 118       * Returns an associative array of parameter names mapped to values.
 119       *
 120       * @return string[]
 121       */
 122      public function getParameters()
 123      {
 124          return $this->_params;
 125      }
 126  
 127      /**
 128       * Get the value of this header prepared for rendering.
 129       *
 130       * @return string
 131       */
 132      public function getFieldBody() //TODO: Check caching here
 133      {
 134          $body = parent::getFieldBody();
 135          foreach ($this->_params as $name => $value) {
 136              if (!is_null($value)) {
 137                  // Add the parameter
 138                  $body .= '; '.$this->_createParameter($name, $value);
 139              }
 140          }
 141  
 142          return $body;
 143      }
 144  
 145      /**
 146       * Generate a list of all tokens in the final header.
 147       *
 148       * This doesn't need to be overridden in theory, but it is for implementation
 149       * reasons to prevent potential breakage of attributes.
 150       *
 151       * @param string $string The string to tokenize
 152       *
 153       * @return array An array of tokens as strings
 154       */
 155      protected function toTokens($string = null)
 156      {
 157          $tokens = parent::toTokens(parent::getFieldBody());
 158  
 159          // Try creating any parameters
 160          foreach ($this->_params as $name => $value) {
 161              if (!is_null($value)) {
 162                  // Add the semi-colon separator
 163                  $tokens[count($tokens) - 1] .= ';';
 164                  $tokens = array_merge($tokens, $this->generateTokenLines(
 165                      ' '.$this->_createParameter($name, $value)
 166                      ));
 167              }
 168          }
 169  
 170          return $tokens;
 171      }
 172  
 173      /**
 174       * Render a RFC 2047 compliant header parameter from the $name and $value.
 175       *
 176       * @param string $name
 177       * @param string $value
 178       *
 179       * @return string
 180       */
 181      private function _createParameter($name, $value)
 182      {
 183          $origValue = $value;
 184  
 185          $encoded = false;
 186          // Allow room for parameter name, indices, "=" and DQUOTEs
 187          $maxValueLength = $this->getMaxLineLength() - strlen($name.'=*N"";') - 1;
 188          $firstLineOffset = 0;
 189  
 190          // If it's not already a valid parameter value...
 191          if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
 192              // TODO: text, or something else??
 193              // ... and it's not ascii
 194              if (!preg_match('/^'.$this->getGrammar()->getDefinition('text').'*$/D', $value)) {
 195                  $encoded = true;
 196                  // Allow space for the indices, charset and language
 197                  $maxValueLength = $this->getMaxLineLength() - strlen($name.'*N*="";') - 1;
 198                  $firstLineOffset = strlen(
 199                      $this->getCharset()."'".$this->getLanguage()."'"
 200                      );
 201              }
 202          }
 203  
 204          // Encode if we need to
 205          if ($encoded || strlen($value) > $maxValueLength) {
 206              if (isset($this->_paramEncoder)) {
 207                  $value = $this->_paramEncoder->encodeString(
 208                      $origValue, $firstLineOffset, $maxValueLength, $this->getCharset()
 209                      );
 210              } else {
 211                  // We have to go against RFC 2183/2231 in some areas for interoperability
 212                  $value = $this->getTokenAsEncodedWord($origValue);
 213                  $encoded = false;
 214              }
 215          }
 216  
 217          $valueLines = isset($this->_paramEncoder) ? explode("\r\n", $value) : array($value);
 218  
 219          // Need to add indices
 220          if (count($valueLines) > 1) {
 221              $paramLines = array();
 222              foreach ($valueLines as $i => $line) {
 223                  $paramLines[] = $name.'*'.$i.
 224                      $this->_getEndOfParameterValue($line, true, $i == 0);
 225              }
 226  
 227              return implode(";\r\n ", $paramLines);
 228          } else {
 229              return $name.$this->_getEndOfParameterValue(
 230                  $valueLines[0], $encoded, true
 231                  );
 232          }
 233      }
 234  
 235      /**
 236       * Returns the parameter value from the "=" and beyond.
 237       *
 238       * @param string $value     to append
 239       * @param bool   $encoded
 240       * @param bool   $firstLine
 241       *
 242       * @return string
 243       */
 244      private function _getEndOfParameterValue($value, $encoded = false, $firstLine = false)
 245      {
 246          if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
 247              $value = '"'.$value.'"';
 248          }
 249          $prepend = '=';
 250          if ($encoded) {
 251              $prepend = '*=';
 252              if ($firstLine) {
 253                  $prepend = '*='.$this->getCharset()."'".$this->getLanguage().
 254                      "'";
 255              }
 256          }
 257  
 258          return $prepend.$value;
 259      }
 260  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1