Dashboard sipadu mbip
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.

Headers.php 7.5KB

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