SimpleHeaderSet.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. * @package Swift
  13. * @subpackage Mime
  14. *
  15. * @author Chris Corbyn
  16. */
  17. class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
  18. {
  19. /** HeaderFactory */
  20. private $_factory;
  21. /** Collection of set Headers */
  22. private $_headers = array();
  23. /** Field ordering details */
  24. private $_order = array();
  25. /** List of fields which are required to be displayed */
  26. private $_required = array();
  27. /** The charset used by Headers */
  28. private $_charset;
  29. /**
  30. * Create a new SimpleHeaderSet with the given $factory.
  31. *
  32. * @param Swift_Mime_HeaderFactory $factory
  33. * @param string $charset
  34. */
  35. public function __construct(Swift_Mime_HeaderFactory $factory,
  36. $charset = null)
  37. {
  38. $this->_factory = $factory;
  39. if (isset($charset))
  40. {
  41. $this->setCharset($charset);
  42. }
  43. }
  44. /**
  45. * Set the charset used by these headers.
  46. *
  47. * @param string $charset
  48. */
  49. public function setCharset($charset)
  50. {
  51. $this->_charset = $charset;
  52. $this->_factory->charsetChanged($charset);
  53. $this->_notifyHeadersOfCharset($charset);
  54. }
  55. /**
  56. * Add a new Mailbox Header with a list of $addresses.
  57. *
  58. * @param string $name
  59. * @param array|string $addresses
  60. */
  61. public function addMailboxHeader($name, $addresses = null)
  62. {
  63. $this->_storeHeader($name,
  64. $this->_factory->createMailboxHeader($name, $addresses));
  65. }
  66. /**
  67. * Add a new Date header using $timestamp (UNIX time).
  68. *
  69. * @param string $name
  70. * @param int $timestamp
  71. */
  72. public function addDateHeader($name, $timestamp = null)
  73. {
  74. $this->_storeHeader($name,
  75. $this->_factory->createDateHeader($name, $timestamp));
  76. }
  77. /**
  78. * Add a new basic text header with $name and $value.
  79. *
  80. * @param string $name
  81. * @param string $value
  82. */
  83. public function addTextHeader($name, $value = null)
  84. {
  85. $this->_storeHeader($name,
  86. $this->_factory->createTextHeader($name, $value));
  87. }
  88. /**
  89. * Add a new ParameterizedHeader with $name, $value and $params.
  90. *
  91. * @param string $name
  92. * @param string $value
  93. * @param array $params
  94. */
  95. public function addParameterizedHeader($name, $value = null,
  96. $params = array())
  97. {
  98. $this->_storeHeader($name,
  99. $this->_factory->createParameterizedHeader($name, $value,
  100. $params));
  101. }
  102. /**
  103. * Add a new ID header for Message-ID or Content-ID.
  104. *
  105. * @param string $name
  106. * @param string|array $ids
  107. */
  108. public function addIdHeader($name, $ids = null)
  109. {
  110. $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
  111. }
  112. /**
  113. * Add a new Path header with an address (path) in it.
  114. *
  115. * @param string $name
  116. * @param string $path
  117. */
  118. public function addPathHeader($name, $path = null)
  119. {
  120. $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
  121. }
  122. /**
  123. * Returns true if at least one header with the given $name exists.
  124. *
  125. * If multiple headers match, the actual one may be specified by $index.
  126. *
  127. * @param string $name
  128. * @param int $index
  129. *
  130. * @return boolean
  131. */
  132. public function has($name, $index = 0)
  133. {
  134. $lowerName = strtolower($name);
  135. return array_key_exists($lowerName, $this->_headers)
  136. && array_key_exists($index, $this->_headers[$lowerName]);
  137. }
  138. /**
  139. * Set a header in the HeaderSet.
  140. *
  141. * The header may be a previously fetched header via {@link get()} or it may
  142. * be one that has been created separately.
  143. *
  144. * If $index is specified, the header will be inserted into the set at this
  145. * offset.
  146. *
  147. * @param Swift_Mime_Header $header
  148. * @param int $index
  149. */
  150. public function set(Swift_Mime_Header $header, $index = 0)
  151. {
  152. $this->_storeHeader($header->getFieldName(), $header, $index);
  153. }
  154. /**
  155. * Get the header with the given $name.
  156. *
  157. * If multiple headers match, the actual one may be specified by $index.
  158. * Returns NULL if none present.
  159. *
  160. * @param string $name
  161. * @param int $index
  162. *
  163. * @return Swift_Mime_Header
  164. */
  165. public function get($name, $index = 0)
  166. {
  167. if ($this->has($name, $index))
  168. {
  169. $lowerName = strtolower($name);
  170. return $this->_headers[$lowerName][$index];
  171. }
  172. }
  173. /**
  174. * Get all headers with the given $name.
  175. *
  176. * @param string $name
  177. *
  178. * @return array
  179. */
  180. public function getAll($name = null)
  181. {
  182. if (!isset($name))
  183. {
  184. $headers = array();
  185. foreach ($this->_headers as $collection)
  186. {
  187. $headers = array_merge($headers, $collection);
  188. }
  189. return $headers;
  190. }
  191. $lowerName = strtolower($name);
  192. if (!array_key_exists($lowerName, $this->_headers))
  193. {
  194. return array();
  195. }
  196. return $this->_headers[$lowerName];
  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. {
  272. uksort($headers, array($this, '_sortHeaders'));
  273. }
  274. foreach ($headers as $collection)
  275. {
  276. foreach ($collection as $header)
  277. {
  278. if ($this->_isDisplayed($header) || $header->getFieldBody() != '')
  279. {
  280. $string .= $header->toString();
  281. }
  282. }
  283. }
  284. return $string;
  285. }
  286. /**
  287. * Returns a string representation of this object.
  288. *
  289. * @return string
  290. *
  291. * @see toString()
  292. */
  293. public function __toString()
  294. {
  295. return $this->toString();
  296. }
  297. // -- Private methods
  298. /** Save a Header to the internal collection */
  299. private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
  300. {
  301. if (!isset($this->_headers[strtolower($name)]))
  302. {
  303. $this->_headers[strtolower($name)] = array();
  304. }
  305. if (!isset($offset))
  306. {
  307. $this->_headers[strtolower($name)][] = $header;
  308. }
  309. else
  310. {
  311. $this->_headers[strtolower($name)][$offset] = $header;
  312. }
  313. }
  314. /** Test if the headers can be sorted */
  315. private function _canSort()
  316. {
  317. return count($this->_order) > 0;
  318. }
  319. /** uksort() algorithm for Header ordering */
  320. private function _sortHeaders($a, $b)
  321. {
  322. $lowerA = strtolower($a);
  323. $lowerB = strtolower($b);
  324. $aPos = array_key_exists($lowerA, $this->_order)
  325. ? $this->_order[$lowerA]
  326. : -1;
  327. $bPos = array_key_exists($lowerB, $this->_order)
  328. ? $this->_order[$lowerB]
  329. : -1;
  330. if ($aPos == -1)
  331. {
  332. return 1;
  333. }
  334. elseif ($bPos == -1)
  335. {
  336. return -1;
  337. }
  338. return ($aPos < $bPos) ? -1 : 1;
  339. }
  340. /** Test if the given Header is always displayed */
  341. private function _isDisplayed(Swift_Mime_Header $header)
  342. {
  343. return array_key_exists(strtolower($header->getFieldName()), $this->_required);
  344. }
  345. /** Notify all Headers of the new charset */
  346. private function _notifyHeadersOfCharset($charset)
  347. {
  348. foreach ($this->_headers as $headerGroup)
  349. {
  350. foreach ($headerGroup as $header)
  351. {
  352. $header->setCharset($charset);
  353. }
  354. }
  355. }
  356. }