ParameterizedHeader.php 7.3KB

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