PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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