ParameterizedHeader.php 6.7KB

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