SimpleMimeEntity.php 23 KB

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