PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/ParameterizedHeader.php

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