You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
8.2 KiB

3 years ago
  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\Address;
  12. use Symfony\Component\Mime\Exception\LogicException;
  13. /**
  14. * A collection of headers.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. final class Headers
  19. {
  20. private const UNIQUE_HEADERS = [
  21. 'date', 'from', 'sender', 'reply-to', 'to', 'cc', 'bcc',
  22. 'message-id', 'in-reply-to', 'references', 'subject',
  23. ];
  24. private const HEADER_CLASS_MAP = [
  25. 'date' => DateHeader::class,
  26. 'from' => MailboxListHeader::class,
  27. 'sender' => MailboxHeader::class,
  28. 'reply-to' => MailboxListHeader::class,
  29. 'to' => MailboxListHeader::class,
  30. 'cc' => MailboxListHeader::class,
  31. 'bcc' => MailboxListHeader::class,
  32. 'message-id' => IdentificationHeader::class,
  33. 'in-reply-to' => IdentificationHeader::class,
  34. 'references' => IdentificationHeader::class,
  35. 'return-path' => PathHeader::class,
  36. ];
  37. /**
  38. * @var HeaderInterface[][]
  39. */
  40. private $headers = [];
  41. private $lineLength = 76;
  42. public function __construct(HeaderInterface ...$headers)
  43. {
  44. foreach ($headers as $header) {
  45. $this->add($header);
  46. }
  47. }
  48. public function __clone()
  49. {
  50. foreach ($this->headers as $name => $collection) {
  51. foreach ($collection as $i => $header) {
  52. $this->headers[$name][$i] = clone $header;
  53. }
  54. }
  55. }
  56. public function setMaxLineLength(int $lineLength)
  57. {
  58. $this->lineLength = $lineLength;
  59. foreach ($this->all() as $header) {
  60. $header->setMaxLineLength($lineLength);
  61. }
  62. }
  63. public function getMaxLineLength(): int
  64. {
  65. return $this->lineLength;
  66. }
  67. /**
  68. * @param array<Address|string> $addresses
  69. *
  70. * @return $this
  71. */
  72. public function addMailboxListHeader(string $name, array $addresses): self
  73. {
  74. return $this->add(new MailboxListHeader($name, Address::createArray($addresses)));
  75. }
  76. /**
  77. * @param Address|string $address
  78. *
  79. * @return $this
  80. */
  81. public function addMailboxHeader(string $name, $address): self
  82. {
  83. return $this->add(new MailboxHeader($name, Address::create($address)));
  84. }
  85. /**
  86. * @param string|array $ids
  87. *
  88. * @return $this
  89. */
  90. public function addIdHeader(string $name, $ids): self
  91. {
  92. return $this->add(new IdentificationHeader($name, $ids));
  93. }
  94. /**
  95. * @param Address|string $path
  96. *
  97. * @return $this
  98. */
  99. public function addPathHeader(string $name, $path): self
  100. {
  101. return $this->add(new PathHeader($name, $path instanceof Address ? $path : new Address($path)));
  102. }
  103. /**
  104. * @return $this
  105. */
  106. public function addDateHeader(string $name, \DateTimeInterface $dateTime): self
  107. {
  108. return $this->add(new DateHeader($name, $dateTime));
  109. }
  110. /**
  111. * @return $this
  112. */
  113. public function addTextHeader(string $name, string $value): self
  114. {
  115. return $this->add(new UnstructuredHeader($name, $value));
  116. }
  117. /**
  118. * @return $this
  119. */
  120. public function addParameterizedHeader(string $name, string $value, array $params = []): self
  121. {
  122. return $this->add(new ParameterizedHeader($name, $value, $params));
  123. }
  124. /**
  125. * @return $this
  126. */
  127. public function addHeader(string $name, $argument, array $more = []): self
  128. {
  129. $parts = explode('\\', self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class);
  130. $method = 'add'.ucfirst(array_pop($parts));
  131. if ('addUnstructuredHeader' === $method) {
  132. $method = 'addTextHeader';
  133. } elseif ('addIdentificationHeader' === $method) {
  134. $method = 'addIdHeader';
  135. }
  136. return $this->$method($name, $argument, $more);
  137. }
  138. public function has(string $name): bool
  139. {
  140. return isset($this->headers[strtolower($name)]);
  141. }
  142. /**
  143. * @return $this
  144. */
  145. public function add(HeaderInterface $header): self
  146. {
  147. self::checkHeaderClass($header);
  148. $header->setMaxLineLength($this->lineLength);
  149. $name = strtolower($header->getName());
  150. if (\in_array($name, self::UNIQUE_HEADERS, true) && isset($this->headers[$name]) && \count($this->headers[$name]) > 0) {
  151. throw new LogicException(sprintf('Impossible to set header "%s" as it\'s already defined and must be unique.', $header->getName()));
  152. }
  153. $this->headers[$name][] = $header;
  154. return $this;
  155. }
  156. public function get(string $name): ?HeaderInterface
  157. {
  158. $name = strtolower($name);
  159. if (!isset($this->headers[$name])) {
  160. return null;
  161. }
  162. $values = array_values($this->headers[$name]);
  163. return array_shift($values);
  164. }
  165. public function all(string $name = null): iterable
  166. {
  167. if (null === $name) {
  168. foreach ($this->headers as $name => $collection) {
  169. foreach ($collection as $header) {
  170. yield $name => $header;
  171. }
  172. }
  173. } elseif (isset($this->headers[strtolower($name)])) {
  174. foreach ($this->headers[strtolower($name)] as $header) {
  175. yield $header;
  176. }
  177. }
  178. }
  179. public function getNames(): array
  180. {
  181. return array_keys($this->headers);
  182. }
  183. public function remove(string $name): void
  184. {
  185. unset($this->headers[strtolower($name)]);
  186. }
  187. public static function isUniqueHeader(string $name): bool
  188. {
  189. return \in_array(strtolower($name), self::UNIQUE_HEADERS, true);
  190. }
  191. /**
  192. * @throws LogicException if the header name and class are not compatible
  193. */
  194. public static function checkHeaderClass(HeaderInterface $header): void
  195. {
  196. $name = strtolower($header->getName());
  197. if (($c = self::HEADER_CLASS_MAP[$name] ?? null) && !$header instanceof $c) {
  198. throw new LogicException(sprintf('The "%s" header must be an instance of "%s" (got "%s").', $header->getName(), $c, get_debug_type($header)));
  199. }
  200. }
  201. public function toString(): string
  202. {
  203. $string = '';
  204. foreach ($this->toArray() as $str) {
  205. $string .= $str."\r\n";
  206. }
  207. return $string;
  208. }
  209. public function toArray(): array
  210. {
  211. $arr = [];
  212. foreach ($this->all() as $header) {
  213. if ('' !== $header->getBodyAsString()) {
  214. $arr[] = $header->toString();
  215. }
  216. }
  217. return $arr;
  218. }
  219. public function getHeaderBody($name)
  220. {
  221. return $this->has($name) ? $this->get($name)->getBody() : null;
  222. }
  223. /**
  224. * @internal
  225. */
  226. public function setHeaderBody(string $type, string $name, $body): void
  227. {
  228. if ($this->has($name)) {
  229. $this->get($name)->setBody($body);
  230. } else {
  231. $this->{'add'.$type.'Header'}($name, $body);
  232. }
  233. }
  234. public function getHeaderParameter(string $name, string $parameter): ?string
  235. {
  236. if (!$this->has($name)) {
  237. return null;
  238. }
  239. $header = $this->get($name);
  240. if (!$header instanceof ParameterizedHeader) {
  241. throw new LogicException(sprintf('Unable to get parameter "%s" on header "%s" as the header is not of class "%s".', $parameter, $name, ParameterizedHeader::class));
  242. }
  243. return $header->getParameter($parameter);
  244. }
  245. /**
  246. * @internal
  247. */
  248. public function setHeaderParameter(string $name, string $parameter, $value): void
  249. {
  250. if (!$this->has($name)) {
  251. throw new LogicException(sprintf('Unable to set parameter "%s" on header "%s" as the header is not defined.', $parameter, $name));
  252. }
  253. $header = $this->get($name);
  254. if (!$header instanceof ParameterizedHeader) {
  255. throw new LogicException(sprintf('Unable to set parameter "%s" on header "%s" as the header is not of class "%s".', $parameter, $name, ParameterizedHeader::class));
  256. }
  257. $header->setParameter($parameter, $value);
  258. }
  259. }