/include/swift/lib/classes/Swift/Mime/Headers/MailboxHeader.php

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