PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/application/vendor/Swift/classes/Swift/Mime/SimpleHeaderSet.php

https://bitbucket.org/SinSiXX/tickets
PHP | 394 lines | 193 code | 38 blank | 163 comment | 14 complexity | 42f5d3435984e93abbeb8150883e96ab MD5 | raw file
Possible License(s): BSD-3-Clause
  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. }