PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleHeaderSet.php

https://gitlab.com/madwanz64/laravel
PHP | 399 lines | 196 code | 46 blank | 157 comment | 22 complexity | 492b75815e29671cf4c68f902d0a9def 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. * A collection of MIME headers.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Mime_SimpleHeaderSet implements Swift_Mime_CharsetObserver
  15. {
  16. /** HeaderFactory */
  17. private $factory;
  18. /** Collection of set Headers */
  19. private $headers = [];
  20. /** Field ordering details */
  21. private $order = [];
  22. /** List of fields which are required to be displayed */
  23. private $required = [];
  24. /** The charset used by Headers */
  25. private $charset;
  26. /**
  27. * Create a new SimpleHeaderSet with the given $factory.
  28. *
  29. * @param string $charset
  30. */
  31. public function __construct(Swift_Mime_SimpleHeaderFactory $factory, $charset = null)
  32. {
  33. $this->factory = $factory;
  34. if (isset($charset)) {
  35. $this->setCharset($charset);
  36. }
  37. }
  38. public function newInstance()
  39. {
  40. return new self($this->factory);
  41. }
  42. /**
  43. * Set the charset used by these headers.
  44. *
  45. * @param string $charset
  46. */
  47. public function setCharset($charset)
  48. {
  49. $this->charset = $charset;
  50. $this->factory->charsetChanged($charset);
  51. $this->notifyHeadersOfCharset($charset);
  52. }
  53. /**
  54. * Add a new Mailbox Header with a list of $addresses.
  55. *
  56. * @param string $name
  57. * @param array|string $addresses
  58. */
  59. public function addMailboxHeader($name, $addresses = null)
  60. {
  61. $this->storeHeader($name, $this->factory->createMailboxHeader($name, $addresses));
  62. }
  63. /**
  64. * Add a new Date header using $dateTime.
  65. *
  66. * @param string $name
  67. */
  68. public function addDateHeader($name, DateTimeInterface $dateTime = null)
  69. {
  70. $this->storeHeader($name, $this->factory->createDateHeader($name, $dateTime));
  71. }
  72. /**
  73. * Add a new basic text header with $name and $value.
  74. *
  75. * @param string $name
  76. * @param string $value
  77. */
  78. public function addTextHeader($name, $value = null)
  79. {
  80. $this->storeHeader($name, $this->factory->createTextHeader($name, $value));
  81. }
  82. /**
  83. * Add a new ParameterizedHeader with $name, $value and $params.
  84. *
  85. * @param string $name
  86. * @param string $value
  87. * @param array $params
  88. */
  89. public function addParameterizedHeader($name, $value = null, $params = [])
  90. {
  91. $this->storeHeader($name, $this->factory->createParameterizedHeader($name, $value, $params));
  92. }
  93. /**
  94. * Add a new ID header for Message-ID or Content-ID.
  95. *
  96. * @param string $name
  97. * @param string|array $ids
  98. */
  99. public function addIdHeader($name, $ids = null)
  100. {
  101. $this->storeHeader($name, $this->factory->createIdHeader($name, $ids));
  102. }
  103. /**
  104. * Add a new Path header with an address (path) in it.
  105. *
  106. * @param string $name
  107. * @param string $path
  108. */
  109. public function addPathHeader($name, $path = null)
  110. {
  111. $this->storeHeader($name, $this->factory->createPathHeader($name, $path));
  112. }
  113. /**
  114. * Returns true if at least one header with the given $name exists.
  115. *
  116. * If multiple headers match, the actual one may be specified by $index.
  117. *
  118. * @param string $name
  119. * @param int $index
  120. *
  121. * @return bool
  122. */
  123. public function has($name, $index = 0)
  124. {
  125. $lowerName = strtolower($name ?? '');
  126. if (!\array_key_exists($lowerName, $this->headers)) {
  127. return false;
  128. }
  129. if (\func_num_args() < 2) {
  130. // index was not specified, so we only need to check that there is at least one header value set
  131. return (bool) \count($this->headers[$lowerName]);
  132. }
  133. return \array_key_exists($index, $this->headers[$lowerName]);
  134. }
  135. /**
  136. * Set a header in the HeaderSet.
  137. *
  138. * The header may be a previously fetched header via {@link get()} or it may
  139. * be one that has been created separately.
  140. *
  141. * If $index is specified, the header will be inserted into the set at this
  142. * offset.
  143. *
  144. * @param int $index
  145. */
  146. public function set(Swift_Mime_Header $header, $index = 0)
  147. {
  148. $this->storeHeader($header->getFieldName(), $header, $index);
  149. }
  150. /**
  151. * Get the header with the given $name.
  152. *
  153. * If multiple headers match, the actual one may be specified by $index.
  154. * Returns NULL if none present.
  155. *
  156. * @param string $name
  157. * @param int $index
  158. *
  159. * @return Swift_Mime_Header|null
  160. */
  161. public function get($name, $index = 0)
  162. {
  163. $name = strtolower($name ?? '');
  164. if (\func_num_args() < 2) {
  165. if ($this->has($name)) {
  166. $values = array_values($this->headers[$name]);
  167. return array_shift($values);
  168. }
  169. } else {
  170. if ($this->has($name, $index)) {
  171. return $this->headers[$name][$index];
  172. }
  173. }
  174. }
  175. /**
  176. * Get all headers with the given $name.
  177. *
  178. * @param string $name
  179. *
  180. * @return array
  181. */
  182. public function getAll($name = null)
  183. {
  184. if (!isset($name)) {
  185. $headers = [];
  186. foreach ($this->headers as $collection) {
  187. $headers = array_merge($headers, $collection);
  188. }
  189. return $headers;
  190. }
  191. $lowerName = strtolower($name ?? '');
  192. if (!\array_key_exists($lowerName, $this->headers)) {
  193. return [];
  194. }
  195. return $this->headers[$lowerName];
  196. }
  197. /**
  198. * Return the name of all Headers.
  199. *
  200. * @return array
  201. */
  202. public function listAll()
  203. {
  204. $headers = $this->headers;
  205. if ($this->canSort()) {
  206. uksort($headers, [$this, 'sortHeaders']);
  207. }
  208. return array_keys($headers);
  209. }
  210. /**
  211. * Remove the header with the given $name if it's set.
  212. *
  213. * If multiple headers match, the actual one may be specified by $index.
  214. *
  215. * @param string $name
  216. * @param int $index
  217. */
  218. public function remove($name, $index = 0)
  219. {
  220. $lowerName = strtolower($name ?? '');
  221. unset($this->headers[$lowerName][$index]);
  222. }
  223. /**
  224. * Remove all headers with the given $name.
  225. *
  226. * @param string $name
  227. */
  228. public function removeAll($name)
  229. {
  230. $lowerName = strtolower($name ?? '');
  231. unset($this->headers[$lowerName]);
  232. }
  233. /**
  234. * Define a list of Header names as an array in the correct order.
  235. *
  236. * These Headers will be output in the given order where present.
  237. */
  238. public function defineOrdering(array $sequence)
  239. {
  240. $this->order = array_flip(array_map('strtolower', $sequence));
  241. }
  242. /**
  243. * Set a list of header names which must always be displayed when set.
  244. *
  245. * Usually headers without a field value won't be output unless set here.
  246. */
  247. public function setAlwaysDisplayed(array $names)
  248. {
  249. $this->required = array_flip(array_map('strtolower', $names));
  250. }
  251. /**
  252. * Notify this observer that the entity's charset has changed.
  253. *
  254. * @param string $charset
  255. */
  256. public function charsetChanged($charset)
  257. {
  258. $this->setCharset($charset);
  259. }
  260. /**
  261. * Returns a string with a representation of all headers.
  262. *
  263. * @return string
  264. */
  265. public function toString()
  266. {
  267. $string = '';
  268. $headers = $this->headers;
  269. if ($this->canSort()) {
  270. uksort($headers, [$this, 'sortHeaders']);
  271. }
  272. foreach ($headers as $collection) {
  273. foreach ($collection as $header) {
  274. if ($this->isDisplayed($header) || '' != $header->getFieldBody()) {
  275. $string .= $header->toString();
  276. }
  277. }
  278. }
  279. return $string;
  280. }
  281. /**
  282. * Returns a string representation of this object.
  283. *
  284. * @return string
  285. *
  286. * @see toString()
  287. */
  288. public function __toString()
  289. {
  290. return $this->toString();
  291. }
  292. /** Save a Header to the internal collection */
  293. private function storeHeader($name, Swift_Mime_Header $header, $offset = null)
  294. {
  295. if (!isset($this->headers[strtolower($name ?? '')])) {
  296. $this->headers[strtolower($name ?? '')] = [];
  297. }
  298. if (!isset($offset)) {
  299. $this->headers[strtolower($name ?? '')][] = $header;
  300. } else {
  301. $this->headers[strtolower($name ?? '')][$offset] = $header;
  302. }
  303. }
  304. /** Test if the headers can be sorted */
  305. private function canSort()
  306. {
  307. return \count($this->order) > 0;
  308. }
  309. /** uksort() algorithm for Header ordering */
  310. private function sortHeaders($a, $b)
  311. {
  312. $lowerA = strtolower($a ?? '');
  313. $lowerB = strtolower($b ?? '');
  314. $aPos = \array_key_exists($lowerA, $this->order) ? $this->order[$lowerA] : -1;
  315. $bPos = \array_key_exists($lowerB, $this->order) ? $this->order[$lowerB] : -1;
  316. if (-1 === $aPos && -1 === $bPos) {
  317. // just be sure to be determinist here
  318. return $a > $b ? -1 : 1;
  319. }
  320. if (-1 == $aPos) {
  321. return 1;
  322. } elseif (-1 == $bPos) {
  323. return -1;
  324. }
  325. return $aPos < $bPos ? -1 : 1;
  326. }
  327. /** Test if the given Header is always displayed */
  328. private function isDisplayed(Swift_Mime_Header $header)
  329. {
  330. return \array_key_exists(strtolower($header->getFieldName() ?? ''), $this->required);
  331. }
  332. /** Notify all Headers of the new charset */
  333. private function notifyHeadersOfCharset($charset)
  334. {
  335. foreach ($this->headers as $headerGroup) {
  336. foreach ($headerGroup as $header) {
  337. $header->setCharset($charset);
  338. }
  339. }
  340. }
  341. /**
  342. * Make a deep copy of object.
  343. */
  344. public function __clone()
  345. {
  346. $this->factory = clone $this->factory;
  347. foreach ($this->headers as $groupKey => $headerGroup) {
  348. foreach ($headerGroup as $key => $header) {
  349. $this->headers[$groupKey][$key] = clone $header;
  350. }
  351. }
  352. }
  353. }