SimpleMimeEntity.php 21KB

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