PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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