TYPO3  7.6
Headers/ParameterizedHeader.php
Go to the documentation of this file.
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 
17 {
23  const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
24 
30  private $_paramEncoder;
31 
37  private $_params = array();
38 
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 
61  public function getFieldType()
62  {
63  return self::TYPE_PARAMETERIZED;
64  }
65 
71  public function setCharset($charset)
72  {
73  parent::setCharset($charset);
74  if (isset($this->_paramEncoder)) {
75  $this->_paramEncoder->charsetChanged($charset);
76  }
77  }
78 
85  public function setParameter($parameter, $value)
86  {
87  $this->setParameters(array_merge($this->getParameters(), array($parameter => $value)));
88  }
89 
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 
111  public function setParameters(array $parameters)
112  {
113  $this->clearCachedValueIf($this->_params != $parameters);
114  $this->_params = $parameters;
115  }
116 
122  public function getParameters()
123  {
124  return $this->_params;
125  }
126 
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 
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 
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 
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 }