SimpleMimeEntity.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 MIME entity, in a multipart message.
  11. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_SimpleMimeEntity implements Swift_Mime_MimeEntity
  16. {
  17. /** A collection of Headers for this mime entity */
  18. private $_headers;
  19. /** The body as a string, or a stream */
  20. private $_body;
  21. /** The encoder that encodes the body into a streamable format */
  22. private $_encoder;
  23. /** The grammar to use for id validation */
  24. private $_grammar;
  25. /** A mime bounary, if any is used */
  26. private $_boundary;
  27. /** Mime types to be used based on the nesting level */
  28. private $_compositeRanges = array(
  29. 'multipart/mixed' => array(self::LEVEL_TOP, self::LEVEL_MIXED),
  30. 'multipart/alternative' => array(self::LEVEL_MIXED, self::LEVEL_ALTERNATIVE),
  31. 'multipart/related' => array(self::LEVEL_ALTERNATIVE, self::LEVEL_RELATED)
  32. );
  33. /** A set of filter rules to define what level an entity should be nested at */
  34. private $_compoundLevelFilters = array();
  35. /** The nesting level of this entity */
  36. private $_nestingLevel = self::LEVEL_ALTERNATIVE;
  37. /** A KeyCache instance used during encoding and streaming */
  38. private $_cache;
  39. /** Direct descendants of this entity */
  40. private $_immediateChildren = array();
  41. /** All descendants of this entity */
  42. private $_children = array();
  43. /** The maximum line length of the body of this entity */
  44. private $_maxLineLength = 78;
  45. /** The order in which alternative mime types should appear */
  46. private $_alternativePartOrder = array(
  47. 'text/plain' => 1,
  48. 'text/html' => 2,
  49. 'multipart/related' => 3
  50. );
  51. /** The CID of this entity */
  52. private $_id;
  53. /** The key used for accessing the cache */
  54. private $_cacheKey;
  55. protected $_userContentType;
  56. /**
  57. * Create a new SimpleMimeEntity with $headers, $encoder and $cache.
  58. * @param Swift_Mime_HeaderSet $headers
  59. * @param Swift_Mime_ContentEncoder $encoder
  60. * @param Swift_KeyCache $cache
  61. * @param Swift_Mime_Grammar $grammar
  62. */
  63. public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar)
  64. {
  65. $this->_cacheKey = uniqid();
  66. $this->_cache = $cache;
  67. $this->_headers = $headers;
  68. $this->_grammar = $grammar;
  69. $this->setEncoder($encoder);
  70. $this->_headers->defineOrdering(
  71. array('Content-Type', 'Content-Transfer-Encoding')
  72. );
  73. // This array specifies that, when the entire MIME document contains
  74. // $compoundLevel, then for each child within $level, if its Content-Type
  75. // is $contentType then it should be treated as if it's level is
  76. // $neededLevel instead. I tried to write that unambiguously! :-\
  77. // Data Structure:
  78. // array (
  79. // $compoundLevel => array(
  80. // $level => array(
  81. // $contentType => $neededLevel
  82. // )
  83. // )
  84. // )
  85. $this->_compoundLevelFilters = array(
  86. (self::LEVEL_ALTERNATIVE + self::LEVEL_RELATED) => array(
  87. self::LEVEL_ALTERNATIVE => array(
  88. 'text/plain' => self::LEVEL_ALTERNATIVE,
  89. 'text/html' => self::LEVEL_RELATED
  90. )
  91. )
  92. );
  93. $this->_id = $this->getRandomId();
  94. }
  95. /**
  96. * Generate a new Content-ID or Message-ID for this MIME entity.
  97. * @return string
  98. */
  99. public function generateId()
  100. {
  101. $this->setId($this->getRandomId());
  102. return $this->_id;
  103. }
  104. /**
  105. * Get the {@link Swift_Mime_HeaderSet} for this entity.
  106. * @return Swift_Mime_HeaderSet
  107. */
  108. public function getHeaders()
  109. {
  110. return $this->_headers;
  111. }
  112. /**
  113. * Get the nesting level of this entity.
  114. * @return int
  115. * @see LEVEL_TOP, LEVEL_MIXED, LEVEL_RELATED, LEVEL_ALTERNATIVE
  116. */
  117. public function getNestingLevel()
  118. {
  119. return $this->_nestingLevel;
  120. }
  121. /**
  122. * Get the Content-type of this entity.
  123. * @return string
  124. */
  125. public function getContentType()
  126. {
  127. return $this->_getHeaderFieldModel('Content-Type');
  128. }
  129. /**
  130. * Set the Content-type of this entity.
  131. * @param string $type
  132. * @return Swift_Mime_SimpleMimeEntity
  133. */
  134. public function setContentType($type)
  135. {
  136. $this->_setContentTypeInHeaders($type);
  137. // Keep track of the value so that if the content-type changes automatically
  138. // due to added child entities, it can be restored if they are later removed
  139. $this->_userContentType = $type;
  140. return $this;
  141. }
  142. /**
  143. * Get the CID of this entity.
  144. * The CID will only be present in headers if a Content-ID header is present.
  145. * @return string
  146. */
  147. public function getId()
  148. {
  149. return $this->_headers->has($this->_getIdField())
  150. ? current((array) $this->_getHeaderFieldModel($this->_getIdField()))
  151. : $this->_id;
  152. }
  153. /**
  154. * Set the CID of this entity.
  155. * @param string $id
  156. * @return Swift_Mime_SimpleMimeEntity
  157. */
  158. public function setId($id)
  159. {
  160. if (!$this->_setHeaderFieldModel($this->_getIdField(), $id)) {
  161. $this->_headers->addIdHeader($this->_getIdField(), $id);
  162. }
  163. $this->_id = $id;
  164. return $this;
  165. }
  166. /**
  167. * Get the description of this entity.
  168. * This value comes from the Content-Description header if set.
  169. * @return string
  170. */
  171. public function getDescription()
  172. {
  173. return $this->_getHeaderFieldModel('Content-Description');
  174. }
  175. /**
  176. * Set the description of this entity.
  177. * This method sets a value in the Content-ID header.
  178. * @param string $description
  179. * @return Swift_Mime_SimpleMimeEntity
  180. */
  181. public function setDescription($description)
  182. {
  183. if (!$this->_setHeaderFieldModel('Content-Description', $description)) {
  184. $this->_headers->addTextHeader('Content-Description', $description);
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Get the maximum line length of the body of this entity.
  190. * @return int
  191. */
  192. public function getMaxLineLength()
  193. {
  194. return $this->_maxLineLength;
  195. }
  196. /**
  197. * Set the maximum line length of lines in this body.
  198. * Though not enforced by the library, lines should not exceed 1000 chars.
  199. * @param int $length
  200. * @return Swift_Mime_SimpleMimeEntity
  201. */
  202. public function setMaxLineLength($length)
  203. {
  204. $this->_maxLineLength = $length;
  205. return $this;
  206. }
  207. /**
  208. * Get all children added to this entity.
  209. * @return array of Swift_Mime_Entity
  210. */
  211. public function getChildren()
  212. {
  213. return $this->_children;
  214. }
  215. /**
  216. * Set all children of this entity.
  217. * @param array $children Swiift_Mime_Entity instances
  218. * @param int $compoundLevel For internal use only
  219. * @return Swift_Mime_SimpleMimeEntity
  220. */
  221. public function setChildren(array $children, $compoundLevel = null)
  222. {
  223. //TODO: Try to refactor this logic
  224. $compoundLevel = isset($compoundLevel)
  225. ? $compoundLevel
  226. : $this->_getCompoundLevel($children)
  227. ;
  228. $immediateChildren = array();
  229. $grandchildren = array();
  230. $newContentType = $this->_userContentType;
  231. foreach ($children as $child) {
  232. $level = $this->_getNeededChildLevel($child, $compoundLevel);
  233. if (empty($immediateChildren)) { //first iteration
  234. $immediateChildren = array($child);
  235. } else {
  236. $nextLevel = $this->_getNeededChildLevel($immediateChildren[0], $compoundLevel);
  237. if ($nextLevel == $level) {
  238. $immediateChildren[] = $child;
  239. } elseif ($level < $nextLevel) {
  240. //Re-assign immediateChildren to grandchilden
  241. $grandchildren = array_merge($grandchildren, $immediateChildren);
  242. //Set new children
  243. $immediateChildren = array($child);
  244. } else {
  245. $grandchildren[] = $child;
  246. }
  247. }
  248. }
  249. if (!empty($immediateChildren)) {
  250. $lowestLevel = $this->_getNeededChildLevel($immediateChildren[0], $compoundLevel);
  251. //Determine which composite media type is needed to accomodate the
  252. // immediate children
  253. foreach ($this->_compositeRanges as $mediaType => $range) {
  254. if ($lowestLevel > $range[0]
  255. && $lowestLevel <= $range[1])
  256. {
  257. $newContentType = $mediaType;
  258. break;
  259. }
  260. }
  261. //Put any grandchildren in a subpart
  262. if (!empty($grandchildren)) {
  263. $subentity = $this->_createChild();
  264. $subentity->_setNestingLevel($lowestLevel);
  265. $subentity->setChildren($grandchildren, $compoundLevel);
  266. array_unshift($immediateChildren, $subentity);
  267. }
  268. }
  269. $this->_immediateChildren = $immediateChildren;
  270. $this->_children = $children;
  271. $this->_setContentTypeInHeaders($newContentType);
  272. $this->_fixHeaders();
  273. $this->_sortChildren();
  274. return $this;
  275. }
  276. /**
  277. * Get the body of this entity as a string.
  278. * @return string
  279. */
  280. public function getBody()
  281. {
  282. return ($this->_body instanceof Swift_OutputByteStream)
  283. ? $this->_readStream($this->_body)
  284. : $this->_body;
  285. }
  286. /**
  287. * Set the body of this entity, either as a string, or as an instance of
  288. * {@link Swift_OutputByteStream}.
  289. * @param mixed $body
  290. * @param string $contentType optional
  291. * @return Swift_Mime_SimpleMimeEntity
  292. */
  293. public function setBody($body, $contentType = null)
  294. {
  295. if ($body !== $this->_body) {
  296. $this->_clearCache();
  297. }
  298. $this->_body = $body;
  299. if (isset($contentType)) {
  300. $this->setContentType($contentType);
  301. }
  302. return $this;
  303. }
  304. /**
  305. * Get the encoder used for the body of this entity.
  306. * @return Swift_Mime_ContentEncoder
  307. */
  308. public function getEncoder()
  309. {
  310. return $this->_encoder;
  311. }
  312. /**
  313. * Set the encoder used for the body of this entity.
  314. * @param Swift_Mime_ContentEncoder $encoder
  315. * @return Swift_Mime_SimpleMimeEntity
  316. */
  317. public function setEncoder(Swift_Mime_ContentEncoder $encoder)
  318. {
  319. if ($encoder !== $this->_encoder) {
  320. $this->_clearCache();
  321. }
  322. $this->_encoder = $encoder;
  323. $this->_setEncoding($encoder->getName());
  324. $this->_notifyEncoderChanged($encoder);
  325. return $this;
  326. }
  327. /**
  328. * Get the boundary used to separate children in this entity.
  329. * @return string
  330. */
  331. public function getBoundary()
  332. {
  333. if (!isset($this->_boundary)) {
  334. $this->_boundary = '_=_swift_v4_' . time() . uniqid() . '_=_';
  335. }
  336. return $this->_boundary;
  337. }
  338. /**
  339. * Set the boundary used to separate children in this entity.
  340. * @param string $boundary
  341. * @throws Swift_RfcComplianceException
  342. * @return Swift_Mime_SimpleMimeEntity
  343. */
  344. public function setBoundary($boundary)
  345. {
  346. $this->_assertValidBoundary($boundary);
  347. $this->_boundary = $boundary;
  348. return $this;
  349. }
  350. /**
  351. * Receive notification that the charset of this entity, or a parent entity
  352. * has changed.
  353. * @param string $charset
  354. */
  355. public function charsetChanged($charset)
  356. {
  357. $this->_notifyCharsetChanged($charset);
  358. }
  359. /**
  360. * Receive notification that the encoder of this entity or a parent entity
  361. * has changed.
  362. * @param Swift_Mime_ContentEncoder $encoder
  363. */
  364. public function encoderChanged(Swift_Mime_ContentEncoder $encoder)
  365. {
  366. $this->_notifyEncoderChanged($encoder);
  367. }
  368. /**
  369. * Get this entire entity as a string.
  370. * @return string
  371. */
  372. public function toString()
  373. {
  374. $string = $this->_headers->toString();
  375. if (isset($this->_body) && empty($this->_immediateChildren)) {
  376. if ($this->_cache->hasKey($this->_cacheKey, 'body')) {
  377. $body = $this->_cache->getString($this->_cacheKey, 'body');
  378. } else {
  379. $body = "\r\n" . $this->_encoder->encodeString($this->getBody(), 0,
  380. $this->getMaxLineLength()
  381. );
  382. $this->_cache->setString($this->_cacheKey, 'body', $body,
  383. Swift_KeyCache::MODE_WRITE
  384. );
  385. }
  386. $string .= $body;
  387. }
  388. if (!empty($this->_immediateChildren)) {
  389. foreach ($this->_immediateChildren as $child) {
  390. $string .= "\r\n\r\n--" . $this->getBoundary() . "\r\n";
  391. $string .= $child->toString();
  392. }
  393. $string .= "\r\n\r\n--" . $this->getBoundary() . "--\r\n";
  394. }
  395. return $string;
  396. }
  397. /**
  398. * Returns a string representation of this object.
  399. *
  400. * @return string
  401. *
  402. * @see toString()
  403. */
  404. public function __toString()
  405. {
  406. return $this->toString();
  407. }
  408. /**
  409. * Write this entire entity to a {@link Swift_InputByteStream}.
  410. * @param Swift_InputByteStream
  411. */
  412. public function toByteStream(Swift_InputByteStream $is)
  413. {
  414. $is->write($this->_headers->toString());
  415. $is->commit();
  416. if (empty($this->_immediateChildren)) {
  417. if (isset($this->_body)) {
  418. if ($this->_cache->hasKey($this->_cacheKey, 'body')) {
  419. $this->_cache->exportToByteStream($this->_cacheKey, 'body', $is);
  420. } else {
  421. $cacheIs = $this->_cache->getInputByteStream($this->_cacheKey, 'body');
  422. if ($cacheIs) {
  423. $is->bind($cacheIs);
  424. }
  425. $is->write("\r\n");
  426. if ($this->_body instanceof Swift_OutputByteStream) {
  427. $this->_body->setReadPointer(0);
  428. $this->_encoder->encodeByteStream($this->_body, $is, 0,
  429. $this->getMaxLineLength()
  430. );
  431. } else {
  432. $is->write($this->_encoder->encodeString(
  433. $this->getBody(), 0, $this->getMaxLineLength()
  434. ));
  435. }
  436. if ($cacheIs) {
  437. $is->unbind($cacheIs);
  438. }
  439. }
  440. }
  441. }
  442. if (!empty($this->_immediateChildren)) {
  443. foreach ($this->_immediateChildren as $child) {
  444. $is->write("\r\n\r\n--" . $this->getBoundary() . "\r\n");
  445. $child->toByteStream($is);
  446. }
  447. $is->write("\r\n\r\n--" . $this->getBoundary() . "--\r\n");
  448. }
  449. }
  450. // -- Protected methods
  451. /**
  452. * Get the name of the header that provides the ID of this entity */
  453. protected function _getIdField()
  454. {
  455. return 'Content-ID';
  456. }
  457. /**
  458. * Get the model data (usually an array or a string) for $field.
  459. */
  460. protected function _getHeaderFieldModel($field)
  461. {
  462. if ($this->_headers->has($field)) {
  463. return $this->_headers->get($field)->getFieldBodyModel();
  464. }
  465. }
  466. /**
  467. * Set the model data for $field.
  468. */
  469. protected function _setHeaderFieldModel($field, $model)
  470. {
  471. if ($this->_headers->has($field)) {
  472. $this->_headers->get($field)->setFieldBodyModel($model);
  473. return true;
  474. } else {
  475. return false;
  476. }
  477. }
  478. /**
  479. * Get the parameter value of $parameter on $field header.
  480. */
  481. protected function _getHeaderParameter($field, $parameter)
  482. {
  483. if ($this->_headers->has($field)) {
  484. return $this->_headers->get($field)->getParameter($parameter);
  485. }
  486. }
  487. /**
  488. * Set the parameter value of $parameter on $field header.
  489. */
  490. protected function _setHeaderParameter($field, $parameter, $value)
  491. {
  492. if ($this->_headers->has($field)) {
  493. $this->_headers->get($field)->setParameter($parameter, $value);
  494. return true;
  495. } else {
  496. return false;
  497. }
  498. }
  499. /**
  500. * Re-evaluate what content type and encoding should be used on this entity.
  501. */
  502. protected function _fixHeaders()
  503. {
  504. if (count($this->_immediateChildren)) {
  505. $this->_setHeaderParameter('Content-Type', 'boundary',
  506. $this->getBoundary()
  507. );
  508. $this->_headers->remove('Content-Transfer-Encoding');
  509. } else {
  510. $this->_setHeaderParameter('Content-Type', 'boundary', null);
  511. $this->_setEncoding($this->_encoder->getName());
  512. }
  513. }
  514. /**
  515. * Get the KeyCache used in this entity.
  516. */
  517. protected function _getCache()
  518. {
  519. return $this->_cache;
  520. }
  521. /**
  522. * Get the grammar used for validation.
  523. * @return Swift_Mime_Grammar
  524. */
  525. protected function _getGrammar()
  526. {
  527. return $this->_grammar;
  528. }
  529. /**
  530. * Empty the KeyCache for this entity.
  531. */
  532. protected function _clearCache()
  533. {
  534. $this->_cache->clearKey($this->_cacheKey, 'body');
  535. }
  536. /**
  537. * Returns a random Content-ID or Message-ID.
  538. * @return string
  539. */
  540. protected function getRandomId()
  541. {
  542. $idLeft = time() . '.' . uniqid();
  543. $idRight = !empty($_SERVER['SERVER_NAME'])
  544. ? $_SERVER['SERVER_NAME']
  545. : 'swift.generated';
  546. $id = $idLeft . '@' . $idRight;
  547. try {
  548. $this->_assertValidId($id);
  549. } catch (Swift_RfcComplianceException $e) {
  550. $id = $idLeft . '@swift.generated';
  551. }
  552. return $id;
  553. }
  554. // -- Private methods
  555. private function _readStream(Swift_OutputByteStream $os)
  556. {
  557. $string = '';
  558. while (false !== $bytes = $os->read(8192)) {
  559. $string .= $bytes;
  560. }
  561. return $string;
  562. }
  563. private function _setEncoding($encoding)
  564. {
  565. if (!$this->_setHeaderFieldModel('Content-Transfer-Encoding', $encoding)) {
  566. $this->_headers->addTextHeader('Content-Transfer-Encoding', $encoding);
  567. }
  568. }
  569. private function _assertValidBoundary($boundary)
  570. {
  571. if (!preg_match(
  572. '/^[a-z0-9\'\(\)\+_\-,\.\/:=\?\ ]{0,69}[a-z0-9\'\(\)\+_\-,\.\/:=\?]$/Di',
  573. $boundary))
  574. {
  575. throw new Swift_RfcComplianceException('Mime boundary set is not RFC 2046 compliant.');
  576. }
  577. }
  578. private function _setContentTypeInHeaders($type)
  579. {
  580. if (!$this->_setHeaderFieldModel('Content-Type', $type)) {
  581. $this->_headers->addParameterizedHeader('Content-Type', $type);
  582. }
  583. }
  584. private function _setNestingLevel($level)
  585. {
  586. $this->_nestingLevel = $level;
  587. }
  588. private function _getCompoundLevel($children)
  589. {
  590. $level = 0;
  591. foreach ($children as $child) {
  592. $level |= $child->getNestingLevel();
  593. }
  594. return $level;
  595. }
  596. private function _getNeededChildLevel($child, $compoundLevel)
  597. {
  598. $filter = array();
  599. foreach ($this->_compoundLevelFilters as $bitmask => $rules) {
  600. if (($compoundLevel & $bitmask) === $bitmask) {
  601. $filter = $rules + $filter;
  602. }
  603. }
  604. $realLevel = $child->getNestingLevel();
  605. $lowercaseType = strtolower($child->getContentType());
  606. if (isset($filter[$realLevel])
  607. && isset($filter[$realLevel][$lowercaseType]))
  608. {
  609. return $filter[$realLevel][$lowercaseType];
  610. } else {
  611. return $realLevel;
  612. }
  613. }
  614. private function _createChild()
  615. {
  616. return new self($this->_headers->newInstance(),
  617. $this->_encoder, $this->_cache, $this->_grammar);
  618. }
  619. private function _notifyEncoderChanged(Swift_Mime_ContentEncoder $encoder)
  620. {
  621. foreach ($this->_immediateChildren as $child) {
  622. $child->encoderChanged($encoder);
  623. }
  624. }
  625. private function _notifyCharsetChanged($charset)
  626. {
  627. $this->_encoder->charsetChanged($charset);
  628. $this->_headers->charsetChanged($charset);
  629. foreach ($this->_immediateChildren as $child) {
  630. $child->charsetChanged($charset);
  631. }
  632. }
  633. private function _sortChildren()
  634. {
  635. $shouldSort = false;
  636. foreach ($this->_immediateChildren as $child) {
  637. //NOTE: This include alternative parts moved into a related part
  638. if ($child->getNestingLevel() == self::LEVEL_ALTERNATIVE) {
  639. $shouldSort = true;
  640. break;
  641. }
  642. }
  643. //Sort in order of preference, if there is one
  644. if ($shouldSort) {
  645. usort($this->_immediateChildren, array($this, '_childSortAlgorithm'));
  646. }
  647. }
  648. private function _childSortAlgorithm($a, $b)
  649. {
  650. $typePrefs = array();
  651. $types = array(
  652. strtolower($a->getContentType()),
  653. strtolower($b->getContentType())
  654. );
  655. foreach ($types as $type) {
  656. $typePrefs[] = (array_key_exists($type, $this->_alternativePartOrder))
  657. ? $this->_alternativePartOrder[$type]
  658. : (max($this->_alternativePartOrder) + 1);
  659. }
  660. return ($typePrefs[0] >= $typePrefs[1]) ? 1 : -1;
  661. }
  662. // -- Destructor
  663. /**
  664. * Empties it's own contents from the cache.
  665. */
  666. public function __destruct()
  667. {
  668. $this->_cache->clearAll($this->_cacheKey);
  669. }
  670. /**
  671. * Throws an Exception if the id passed does not comply with RFC 2822.
  672. * @param string $id
  673. * @throws Swift_RfcComplianceException
  674. */
  675. private function _assertValidId($id)
  676. {
  677. if (!preg_match(
  678. '/^' . $this->_grammar->getDefinition('id-left') . '@' .
  679. $this->_grammar->getDefinition('id-right') . '$/D',
  680. $id
  681. ))
  682. {
  683. throw new Swift_RfcComplianceException(
  684. 'Invalid ID given <' . $id . '>'
  685. );
  686. }
  687. }
  688. }