ParameterizedHeader.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Header;
  11. use Symfony\Component\Mime\Encoder\Rfc2231Encoder;
  12. /**
  13. * @author Chris Corbyn
  14. */
  15. final class ParameterizedHeader extends UnstructuredHeader
  16. {
  17. /**
  18. * RFC 2231's definition of a token.
  19. *
  20. * @var string
  21. */
  22. public const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
  23. private $encoder;
  24. private $parameters = [];
  25. public function __construct(string $name, string $value, array $parameters = [])
  26. {
  27. parent::__construct($name, $value);
  28. foreach ($parameters as $k => $v) {
  29. $this->setParameter($k, $v);
  30. }
  31. if ('content-type' !== strtolower($name)) {
  32. $this->encoder = new Rfc2231Encoder();
  33. }
  34. }
  35. public function setParameter(string $parameter, ?string $value)
  36. {
  37. $this->setParameters(array_merge($this->getParameters(), [$parameter => $value]));
  38. }
  39. public function getParameter(string $parameter): string
  40. {
  41. return $this->getParameters()[$parameter] ?? '';
  42. }
  43. /**
  44. * @param string[] $parameters
  45. */
  46. public function setParameters(array $parameters)
  47. {
  48. $this->parameters = $parameters;
  49. }
  50. /**
  51. * @return string[]
  52. */
  53. public function getParameters(): array
  54. {
  55. return $this->parameters;
  56. }
  57. public function getBodyAsString(): string
  58. {
  59. $body = parent::getBodyAsString();
  60. foreach ($this->parameters as $name => $value) {
  61. if (null !== $value) {
  62. $body .= '; '.$this->createParameter($name, $value);
  63. }
  64. }
  65. return $body;
  66. }
  67. /**
  68. * Generate a list of all tokens in the final header.
  69. *
  70. * This doesn't need to be overridden in theory, but it is for implementation
  71. * reasons to prevent potential breakage of attributes.
  72. */
  73. protected function toTokens(string $string = null): array
  74. {
  75. $tokens = parent::toTokens(parent::getBodyAsString());
  76. // Try creating any parameters
  77. foreach ($this->parameters as $name => $value) {
  78. if (null !== $value) {
  79. // Add the semi-colon separator
  80. $tokens[\count($tokens) - 1] .= ';';
  81. $tokens = array_merge($tokens, $this->generateTokenLines(' '.$this->createParameter($name, $value)));
  82. }
  83. }
  84. return $tokens;
  85. }
  86. /**
  87. * Render an RFC 2047 compliant header parameter from the $name and $value.
  88. */
  89. private function createParameter(string $name, string $value): string
  90. {
  91. $origValue = $value;
  92. $encoded = false;
  93. // Allow room for parameter name, indices, "=" and DQUOTEs
  94. $maxValueLength = $this->getMaxLineLength() - \strlen($name.'=*N"";') - 1;
  95. $firstLineOffset = 0;
  96. // If it's not already a valid parameter value...
  97. if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
  98. // TODO: text, or something else??
  99. // ... and it's not ascii
  100. if (!preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $value)) {
  101. $encoded = true;
  102. // Allow space for the indices, charset and language
  103. $maxValueLength = $this->getMaxLineLength() - \strlen($name.'*N*="";') - 1;
  104. $firstLineOffset = \strlen($this->getCharset()."'".$this->getLanguage()."'");
  105. }
  106. }
  107. // Encode if we need to
  108. if ($encoded || \strlen($value) > $maxValueLength) {
  109. if (null !== $this->encoder) {
  110. $value = $this->encoder->encodeString($origValue, $this->getCharset(), $firstLineOffset, $maxValueLength);
  111. } else {
  112. // We have to go against RFC 2183/2231 in some areas for interoperability
  113. $value = $this->getTokenAsEncodedWord($origValue);
  114. $encoded = false;
  115. }
  116. }
  117. $valueLines = $this->encoder ? explode("\r\n", $value) : [$value];
  118. // Need to add indices
  119. if (\count($valueLines) > 1) {
  120. $paramLines = [];
  121. foreach ($valueLines as $i => $line) {
  122. $paramLines[] = $name.'*'.$i.$this->getEndOfParameterValue($line, true, 0 === $i);
  123. }
  124. return implode(";\r\n ", $paramLines);
  125. } else {
  126. return $name.$this->getEndOfParameterValue($valueLines[0], $encoded, true);
  127. }
  128. }
  129. /**
  130. * Returns the parameter value from the "=" and beyond.
  131. *
  132. * @param string $value to append
  133. */
  134. private function getEndOfParameterValue(string $value, bool $encoded = false, bool $firstLine = false): string
  135. {
  136. $forceHttpQuoting = 'content-disposition' === strtolower($this->getName()) && 'form-data' === $this->getValue();
  137. if ($forceHttpQuoting || !preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
  138. $value = '"'.$value.'"';
  139. }
  140. $prepend = '=';
  141. if ($encoded) {
  142. $prepend = '*=';
  143. if ($firstLine) {
  144. $prepend = '*='.$this->getCharset()."'".$this->getLanguage()."'";
  145. }
  146. }
  147. return $prepend.$value;
  148. }
  149. }