SimpleHeaderSet.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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, $charset = null)
  36. {
  37. $this->_factory = $factory;
  38. if (isset($charset)) {
  39. $this->setCharset($charset);
  40. }
  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,
  62. $this->_factory->createMailboxHeader($name, $addresses));
  63. }
  64. /**
  65. * Add a new Date header using $timestamp (UNIX time).
  66. *
  67. * @param string $name
  68. * @param int $timestamp
  69. */
  70. public function addDateHeader($name, $timestamp = null)
  71. {
  72. $this->_storeHeader($name,
  73. $this->_factory->createDateHeader($name, $timestamp));
  74. }
  75. /**
  76. * Add a new basic text header with $name and $value.
  77. *
  78. * @param string $name
  79. * @param string $value
  80. */
  81. public function addTextHeader($name, $value = null)
  82. {
  83. $this->_storeHeader($name,
  84. $this->_factory->createTextHeader($name, $value));
  85. }
  86. /**
  87. * Add a new ParameterizedHeader with $name, $value and $params.
  88. *
  89. * @param string $name
  90. * @param string $value
  91. * @param array $params
  92. */
  93. public function addParameterizedHeader($name, $value = null, $params = array())
  94. {
  95. $this->_storeHeader($name,
  96. $this->_factory->createParameterizedHeader($name, $value,
  97. $params));
  98. }
  99. /**
  100. * Add a new ID header for Message-ID or Content-ID.
  101. *
  102. * @param string $name
  103. * @param string|array $ids
  104. */
  105. public function addIdHeader($name, $ids = null)
  106. {
  107. $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
  108. }
  109. /**
  110. * Add a new Path header with an address (path) in it.
  111. *
  112. * @param string $name
  113. * @param string $path
  114. */
  115. public function addPathHeader($name, $path = null)
  116. {
  117. $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
  118. }
  119. /**
  120. * Returns true if at least one header with the given $name exists.
  121. *
  122. * If multiple headers match, the actual one may be specified by $index.
  123. *
  124. * @param string $name
  125. * @param int $index
  126. *
  127. * @return boolean
  128. */
  129. public function has($name, $index = 0)
  130. {
  131. $lowerName = strtolower($name);
  132. return array_key_exists($lowerName, $this->_headers)
  133. && 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 Swift_Mime_Header $header
  145. * @param int $index
  146. */
  147. public function set(Swift_Mime_Header $header, $index = 0)
  148. {
  149. $this->_storeHeader($header->getFieldName(), $header, $index);
  150. }
  151. /**
  152. * Get the header with the given $name.
  153. *
  154. * If multiple headers match, the actual one may be specified by $index.
  155. * Returns NULL if none present.
  156. *
  157. * @param string $name
  158. * @param int $index
  159. *
  160. * @return Swift_Mime_Header
  161. */
  162. public function get($name, $index = 0)
  163. {
  164. if ($this->has($name, $index)) {
  165. $lowerName = strtolower($name);
  166. return $this->_headers[$lowerName][$index];
  167. }
  168. }
  169. /**
  170. * Get all headers with the given $name.
  171. *
  172. * @param string $name
  173. *
  174. * @return array
  175. */
  176. public function getAll($name = null)
  177. {
  178. if (!isset($name)) {
  179. $headers = array();
  180. foreach ($this->_headers as $collection) {
  181. $headers = array_merge($headers, $collection);
  182. }
  183. return $headers;
  184. }
  185. $lowerName = strtolower($name);
  186. if (!array_key_exists($lowerName, $this->_headers)) {
  187. return array();
  188. }
  189. return $this->_headers[$lowerName];
  190. }
  191. /**
  192. * Remove the header with the given $name if it's set.
  193. *
  194. * If multiple headers match, the actual one may be specified by $index.
  195. *
  196. * @param string $name
  197. * @param int $index
  198. */
  199. public function remove($name, $index = 0)
  200. {
  201. $lowerName = strtolower($name);
  202. unset($this->_headers[$lowerName][$index]);
  203. }
  204. /**
  205. * Remove all headers with the given $name.
  206. *
  207. * @param string $name
  208. */
  209. public function removeAll($name)
  210. {
  211. $lowerName = strtolower($name);
  212. unset($this->_headers[$lowerName]);
  213. }
  214. /**
  215. * Create a new instance of this HeaderSet.
  216. *
  217. * @return Swift_Mime_HeaderSet
  218. */
  219. public function newInstance()
  220. {
  221. return new self($this->_factory);
  222. }
  223. /**
  224. * Define a list of Header names as an array in the correct order.
  225. *
  226. * These Headers will be output in the given order where present.
  227. *
  228. * @param array $sequence
  229. */
  230. public function defineOrdering(array $sequence)
  231. {
  232. $this->_order = array_flip(array_map('strtolower', $sequence));
  233. }
  234. /**
  235. * Set a list of header names which must always be displayed when set.
  236. *
  237. * Usually headers without a field value won't be output unless set here.
  238. *
  239. * @param array $names
  240. */
  241. public function setAlwaysDisplayed(array $names)
  242. {
  243. $this->_required = array_flip(array_map('strtolower', $names));
  244. }
  245. /**
  246. * Notify this observer that the entity's charset has changed.
  247. *
  248. * @param string $charset
  249. */
  250. public function charsetChanged($charset)
  251. {
  252. $this->setCharset($charset);
  253. }
  254. /**
  255. * Returns a string with a representation of all headers.
  256. *
  257. * @return string
  258. */
  259. public function toString()
  260. {
  261. $string = '';
  262. $headers = $this->_headers;
  263. if ($this->_canSort()) {
  264. uksort($headers, array($this, '_sortHeaders'));
  265. }
  266. foreach ($headers as $collection) {
  267. foreach ($collection as $header) {
  268. if ($this->_isDisplayed($header) || $header->getFieldBody() != '') {
  269. $string .= $header->toString();
  270. }
  271. }
  272. }
  273. return $string;
  274. }
  275. /**
  276. * Returns a string representation of this object.
  277. *
  278. * @return string
  279. *
  280. * @see toString()
  281. */
  282. public function __toString()
  283. {
  284. return $this->toString();
  285. }
  286. // -- Private methods
  287. /** Save a Header to the internal collection */
  288. private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
  289. {
  290. if (!isset($this->_headers[strtolower($name)])) {
  291. $this->_headers[strtolower($name)] = array();
  292. }
  293. if (!isset($offset)) {
  294. $this->_headers[strtolower($name)][] = $header;
  295. } else {
  296. $this->_headers[strtolower($name)][$offset] = $header;
  297. }
  298. }
  299. /** Test if the headers can be sorted */
  300. private function _canSort()
  301. {
  302. return count($this->_order) > 0;
  303. }
  304. /** uksort() algorithm for Header ordering */
  305. private function _sortHeaders($a, $b)
  306. {
  307. $lowerA = strtolower($a);
  308. $lowerB = strtolower($b);
  309. $aPos = array_key_exists($lowerA, $this->_order)
  310. ? $this->_order[$lowerA]
  311. : -1;
  312. $bPos = array_key_exists($lowerB, $this->_order)
  313. ? $this->_order[$lowerB]
  314. : -1;
  315. if ($aPos == -1) {
  316. return 1;
  317. } elseif ($bPos == -1) {
  318. return -1;
  319. }
  320. return ($aPos < $bPos) ? -1 : 1;
  321. }
  322. /** Test if the given Header is always displayed */
  323. private function _isDisplayed(Swift_Mime_Header $header)
  324. {
  325. return array_key_exists(strtolower($header->getFieldName()), $this->_required);
  326. }
  327. /** Notify all Headers of the new charset */
  328. private function _notifyHeadersOfCharset($charset)
  329. {
  330. foreach ($this->_headers as $headerGroup) {
  331. foreach ($headerGroup as $header) {
  332. $header->setCharset($charset);
  333. }
  334. }
  335. }
  336. }