PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/email/vendor/swift/classes/Swift/Mime/SimpleHeaderSet.php

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