PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/applications/client/libraries/Swift/classes/Swift/Mime/Headers/MailboxHeader.php

https://bitbucket.org/amitholkar/zenfile-18-05
PHP | 358 lines | 112 code | 26 blank | 220 comment | 6 complexity | 4e53ae75f3b3dcf4b0e058b02a34345e 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 Mailbox Address MIME Header for something like From or Sender.
  11. *
  12. * @package Swift
  13. * @subpackage Mime
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
  17. {
  18. /**
  19. * The mailboxes used in this Header.
  20. *
  21. * @var string[]
  22. */
  23. private $_mailboxes = array();
  24. /**
  25. * Creates a new MailboxHeader with $name.
  26. *
  27. * @param string $name of Header
  28. * @param Swift_Mime_HeaderEncoder $encoder
  29. * @param Swift_Mime_Grammar $grammar
  30. */
  31. public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Mime_Grammar $grammar)
  32. {
  33. $this->setFieldName($name);
  34. $this->setEncoder($encoder);
  35. parent::__construct($grammar);
  36. }
  37. /**
  38. * Get the type of Header that this instance represents.
  39. *
  40. * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  41. * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  42. *
  43. * @return int
  44. */
  45. public function getFieldType()
  46. {
  47. return self::TYPE_MAILBOX;
  48. }
  49. /**
  50. * Set the model for the field body.
  51. *
  52. * This method takes a string, or an array of addresses.
  53. *
  54. * @param mixed $model
  55. *
  56. * @throws Swift_RfcComplianceException
  57. */
  58. public function setFieldBodyModel($model)
  59. {
  60. $this->setNameAddresses($model);
  61. }
  62. /**
  63. * Get the model for the field body.
  64. *
  65. * This method returns an associative array like {@link getNameAddresses()}
  66. *
  67. * @return array
  68. *
  69. * @throws Swift_RfcComplianceException
  70. */
  71. public function getFieldBodyModel()
  72. {
  73. return $this->getNameAddresses();
  74. }
  75. /**
  76. * Set a list of mailboxes to be shown in this Header.
  77. *
  78. * The mailboxes can be a simple array of addresses, or an array of
  79. * key=>value pairs where (email => personalName).
  80. * Example:
  81. * <code>
  82. * <?php
  83. * //Sets two mailboxes in the Header, one with a personal name
  84. * $header->setNameAddresses(array(
  85. * 'chris@swiftmailer.org' => 'Chris Corbyn',
  86. * 'mark@swiftmailer.org' //No associated personal name
  87. * ));
  88. * ?>
  89. * </code>
  90. *
  91. * @see __construct()
  92. * @see setAddresses()
  93. * @see setValue()
  94. *
  95. * @param string|string[] $mailboxes
  96. *
  97. * @throws Swift_RfcComplianceException
  98. */
  99. public function setNameAddresses($mailboxes)
  100. {
  101. $this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
  102. $this->setCachedValue(null); //Clear any cached value
  103. }
  104. /**
  105. * Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
  106. *
  107. * Example:
  108. * <code>
  109. * <?php
  110. * $header = new Swift_Mime_Headers_MailboxHeader('From',
  111. * array('chris@swiftmailer.org' => 'Chris Corbyn',
  112. * 'mark@swiftmailer.org' => 'Mark Corbyn')
  113. * );
  114. * print_r($header->getNameAddressStrings());
  115. * // array (
  116. * // 0 => Chris Corbyn <chris@swiftmailer.org>,
  117. * // 1 => Mark Corbyn <mark@swiftmailer.org>
  118. * // )
  119. * ?>
  120. * </code>
  121. *
  122. * @see getNameAddresses()
  123. * @see toString()
  124. *
  125. * @return string[]
  126. *
  127. * @throws Swift_RfcComplianceException
  128. */
  129. public function getNameAddressStrings()
  130. {
  131. return $this->_createNameAddressStrings($this->getNameAddresses());
  132. }
  133. /**
  134. * Get all mailboxes in this Header as key=>value pairs.
  135. *
  136. * The key is the address and the value is the name (or null if none set).
  137. * Example:
  138. * <code>
  139. * <?php
  140. * $header = new Swift_Mime_Headers_MailboxHeader('From',
  141. * array('chris@swiftmailer.org' => 'Chris Corbyn',
  142. * 'mark@swiftmailer.org' => 'Mark Corbyn')
  143. * );
  144. * print_r($header->getNameAddresses());
  145. * // array (
  146. * // chris@swiftmailer.org => Chris Corbyn,
  147. * // mark@swiftmailer.org => Mark Corbyn
  148. * // )
  149. * ?>
  150. * </code>
  151. *
  152. * @see getAddresses()
  153. * @see getNameAddressStrings()
  154. *
  155. * @return string[]
  156. */
  157. public function getNameAddresses()
  158. {
  159. return $this->_mailboxes;
  160. }
  161. /**
  162. * Makes this Header represent a list of plain email addresses with no names.
  163. *
  164. * Example:
  165. * <code>
  166. * <?php
  167. * //Sets three email addresses as the Header data
  168. * $header->setAddresses(
  169. * array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
  170. * );
  171. * ?>
  172. * </code>
  173. *
  174. * @see setNameAddresses()
  175. * @see setValue()
  176. *
  177. * @param string[] $addresses
  178. *
  179. * @throws Swift_RfcComplianceException
  180. */
  181. public function setAddresses($addresses)
  182. {
  183. $this->setNameAddresses(array_values((array) $addresses));
  184. }
  185. /**
  186. * Get all email addresses in this Header.
  187. *
  188. * @see getNameAddresses()
  189. *
  190. * @return string[]
  191. */
  192. public function getAddresses()
  193. {
  194. return array_keys($this->_mailboxes);
  195. }
  196. /**
  197. * Remove one or more addresses from this Header.
  198. *
  199. * @param string|string[] $addresses
  200. */
  201. public function removeAddresses($addresses)
  202. {
  203. $this->setCachedValue(null);
  204. foreach ((array) $addresses as $address) {
  205. unset($this->_mailboxes[$address]);
  206. }
  207. }
  208. /**
  209. * Get the string value of the body in this Header.
  210. *
  211. * This is not necessarily RFC 2822 compliant since folding white space will
  212. * not be added at this stage (see {@link toString()} for that).
  213. *
  214. * @see toString()
  215. *
  216. * @return string
  217. *
  218. * @throws Swift_RfcComplianceException
  219. */
  220. public function getFieldBody()
  221. {
  222. //Compute the string value of the header only if needed
  223. if (is_null($this->getCachedValue())) {
  224. $this->setCachedValue($this->createMailboxListString($this->_mailboxes));
  225. }
  226. return $this->getCachedValue();
  227. }
  228. // -- Points of extension
  229. /**
  230. * Normalizes a user-input list of mailboxes into consistent key=>value pairs.
  231. *
  232. * @param string[] $mailboxes
  233. *
  234. * @return string[]
  235. */
  236. protected function normalizeMailboxes(array $mailboxes)
  237. {
  238. $actualMailboxes = array();
  239. foreach ($mailboxes as $key => $value) {
  240. if (is_string($key)) { //key is email addr
  241. $address = $key;
  242. $name = $value;
  243. } else {
  244. $address = $value;
  245. $name = null;
  246. }
  247. $this->_assertValidAddress($address);
  248. $actualMailboxes[$address] = $name;
  249. }
  250. return $actualMailboxes;
  251. }
  252. /**
  253. * Produces a compliant, formatted display-name based on the string given.
  254. *
  255. * @param string $displayName as displayed
  256. * @param boolean $shorten the first line to make remove for header name
  257. *
  258. * @return string
  259. */
  260. protected function createDisplayNameString($displayName, $shorten = false)
  261. {
  262. return $this->createPhrase($this, $displayName,
  263. $this->getCharset(), $this->getEncoder(), $shorten
  264. );
  265. }
  266. /**
  267. * Creates a string form of all the mailboxes in the passed array.
  268. *
  269. * @param string[] $mailboxes
  270. *
  271. * @return string
  272. *
  273. * @throws Swift_RfcComplianceException
  274. */
  275. protected function createMailboxListString(array $mailboxes)
  276. {
  277. return implode(', ', $this->_createNameAddressStrings($mailboxes));
  278. }
  279. /**
  280. * Redefine the encoding requirements for mailboxes.
  281. *
  282. * Commas and semicolons are used to separate
  283. * multiple addresses, and should therefore be encoded
  284. *
  285. * @param string $token
  286. *
  287. * @return boolean
  288. */
  289. protected function tokenNeedsEncoding($token)
  290. {
  291. return preg_match('/[,;]/', $token) || parent::tokenNeedsEncoding($token);
  292. }
  293. // -- Private methods
  294. /**
  295. * Return an array of strings conforming the the name-addr spec of RFC 2822.
  296. *
  297. * @param string[] $mailboxes
  298. *
  299. * @return string[]
  300. */
  301. private function _createNameAddressStrings(array $mailboxes)
  302. {
  303. $strings = array();
  304. foreach ($mailboxes as $email => $name) {
  305. $mailboxStr = $email;
  306. if (!is_null($name)) {
  307. $nameStr = $this->createDisplayNameString($name, empty($strings));
  308. $mailboxStr = $nameStr . ' <' . $mailboxStr . '>';
  309. }
  310. $strings[] = $mailboxStr;
  311. }
  312. return $strings;
  313. }
  314. /**
  315. * Throws an Exception if the address passed does not comply with RFC 2822.
  316. *
  317. * @param string $address
  318. *
  319. * @throws Swift_RfcComplianceException If invalid.
  320. */
  321. private function _assertValidAddress($address)
  322. {
  323. if (!preg_match('/^' . $this->getGrammar()->getDefinition('addr-spec') . '$/D',
  324. $address))
  325. {
  326. throw new Swift_RfcComplianceException(
  327. 'Address in mailbox given [' . $address .
  328. '] does not comply with RFC 2822, 3.6.2.'
  329. );
  330. }
  331. }
  332. }