PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/horde-3.3.13/lib/Horde/MIME/Part.php

#
PHP | 1462 lines | 671 code | 156 blank | 635 comment | 129 complexity | a6b7e1f822445754531c4ea823a47d92 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. require_once 'Horde/String.php';
  3. require_once dirname(__FILE__) . '/../MIME.php';
  4. /**
  5. * The character(s) used internally for EOLs.
  6. */
  7. define('MIME_PART_EOL', "\n");
  8. /**
  9. * The character string designated by RFCs 822/2045 to designate EOLs in MIME
  10. * messages.
  11. */
  12. define('MIME_PART_RFC_EOL', "\r\n");
  13. /* Default MIME parameters. */
  14. /**
  15. * The default MIME character set.
  16. */
  17. define('MIME_DEFAULT_CHARSET', 'us-ascii');
  18. /**
  19. * The default MIME description.
  20. */
  21. define('MIME_DEFAULT_DESCRIPTION', _("unnamed"));
  22. /**
  23. * The default MIME disposition.
  24. */
  25. define('MIME_DEFAULT_DISPOSITION', 'inline');
  26. /**
  27. * The default MIME encoding.
  28. */
  29. define('MIME_DEFAULT_ENCODING', '7bit');
  30. /**
  31. * The MIME_Part:: class provides a wrapper around MIME parts and methods
  32. * for dealing with them.
  33. *
  34. * $Horde: framework/MIME/MIME/Part.php,v 1.177.4.30 2010/07/07 23:27:51 slusarz Exp $
  35. *
  36. * Copyright 1999-2009 The Horde Project (http://www.horde.org/)
  37. *
  38. * See the enclosed file COPYING for license information (LGPL). If you
  39. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  40. *
  41. * @author Chuck Hagenbuch <chuck@horde.org>
  42. * @author Michael Slusarz <slusarz@horde.org>
  43. * @package Horde_MIME
  44. */
  45. class MIME_Part {
  46. /**
  47. * The type (ex.: text) of this part.
  48. * Per RFC 2045, the default is 'application'.
  49. *
  50. * @var string
  51. */
  52. var $_type = 'application';
  53. /**
  54. * The subtype (ex.: plain) of this part.
  55. * Per RFC 2045, the default is 'octet-stream'.
  56. *
  57. * @var string
  58. */
  59. var $_subtype = 'octet-stream';
  60. /**
  61. * The body of the part.
  62. *
  63. * @var string
  64. */
  65. var $_contents = '';
  66. /**
  67. * The desired transfer encoding of this part.
  68. *
  69. * @var string
  70. */
  71. var $_transferEncoding = MIME_DEFAULT_ENCODING;
  72. /**
  73. * The current transfer encoding of the contents of this part.
  74. *
  75. * @var string
  76. */
  77. var $_currentEncoding = null;
  78. /**
  79. * Should the message be encoded via 7-bit?
  80. *
  81. * @var boolean
  82. */
  83. var $_encode7bit = true;
  84. /**
  85. * The description of this part.
  86. *
  87. * @var string
  88. */
  89. var $_description = '';
  90. /**
  91. * The disposition of this part (inline or attachment).
  92. *
  93. * @var string
  94. */
  95. var $_disposition = MIME_DEFAULT_DISPOSITION;
  96. /**
  97. * The disposition parameters of this part.
  98. *
  99. * @var array
  100. */
  101. var $_dispositionParameters = array();
  102. /**
  103. * The content type parameters of this part.
  104. *
  105. * @var array
  106. */
  107. var $_contentTypeParameters = array();
  108. /**
  109. * The subparts of this part.
  110. *
  111. * @var array
  112. */
  113. var $_parts = array();
  114. /**
  115. * Information/Statistics on the subpart.
  116. *
  117. * @var array
  118. */
  119. var $_information = array();
  120. /**
  121. * The list of CIDs for this part.
  122. *
  123. * @var array
  124. */
  125. var $_cids = array();
  126. /**
  127. * The MIME ID of this part.
  128. *
  129. * @var string
  130. */
  131. var $_mimeid = null;
  132. /**
  133. * The sequence to use as EOL for this part.
  134. * The default is currently to output the EOL sequence internally as
  135. * just "\n" instead of the canonical "\r\n" required in RFC 822 & 2045.
  136. * To be RFC complaint, the full <CR><LF> EOL combination should be used
  137. * when sending a message.
  138. * It is not crucial here since the PHP/PEAR mailing functions will handle
  139. * the EOL details.
  140. *
  141. * @var string
  142. */
  143. var $_eol = MIME_PART_EOL;
  144. /**
  145. * Internal class flags.
  146. *
  147. * @var array
  148. */
  149. var $_flags = array();
  150. /**
  151. * Part -> ID mapping cache.
  152. *
  153. * @var array
  154. */
  155. var $_idmap = array();
  156. /**
  157. * Unique MIME_Part boundary string.
  158. *
  159. * @var string
  160. */
  161. var $_boundary = null;
  162. /**
  163. * Default value for this Part's size.
  164. *
  165. * @var integer
  166. */
  167. var $_bytes = 0;
  168. /**
  169. * The content-ID for this part.
  170. *
  171. * @var string
  172. */
  173. var $_contentid = null;
  174. /**
  175. * MIME_Part constructor.
  176. *
  177. * @param string $mimetype The content type of the part.
  178. * @param string $contents The body of the part.
  179. * @param string $charset The character set of the part.
  180. * @param string $disposition The content disposition of the part.
  181. * @param string $encoding The content encoding of the contents.
  182. */
  183. function MIME_Part($mimetype = null, $contents = null,
  184. $charset = MIME_DEFAULT_CHARSET, $disposition = null,
  185. $encoding = null)
  186. {
  187. /* Create the unique MIME_Part boundary string. */
  188. $this->_generateBoundary();
  189. /* The character set should always be set, even if we are dealing
  190. * with Content-Types other than text/*. */
  191. $this->setCharset($charset);
  192. if (!is_null($mimetype)) {
  193. $this->setType($mimetype);
  194. }
  195. if (!is_null($contents)) {
  196. $this->setContents($contents, $encoding);
  197. }
  198. if (!is_null($disposition)) {
  199. $this->setDisposition($disposition);
  200. }
  201. }
  202. /**
  203. * Set the content-disposition of this part.
  204. *
  205. * @param string $disposition The content-disposition to set (inline or
  206. * attachment).
  207. */
  208. function setDisposition($disposition)
  209. {
  210. $disposition = String::lower($disposition);
  211. if (($disposition == 'inline') || ($disposition == 'attachment')) {
  212. $this->_disposition = $disposition;
  213. }
  214. }
  215. /**
  216. * Get the content-disposition of this part.
  217. *
  218. * @return string The part's content-disposition.
  219. */
  220. function getDisposition()
  221. {
  222. return $this->_disposition;
  223. }
  224. /**
  225. * Set the name of this part.
  226. * TODO: MIME encode here instead of in header() - add a charset
  227. * parameter.
  228. *
  229. * @param string $name The name to set.
  230. */
  231. function setName($name)
  232. {
  233. $this->setContentTypeParameter('name', $name);
  234. }
  235. /**
  236. * Get the name of this part.
  237. *
  238. * @param boolean $decode MIME decode description?
  239. * @param boolean $default If the name parameter doesn't exist, should we
  240. * use the default name from the description
  241. * parameter?
  242. *
  243. * @return string The name of the part.
  244. */
  245. function getName($decode = false, $default = false)
  246. {
  247. $name = $this->getContentTypeParameter('name');
  248. if ($default && empty($name)) {
  249. $name = preg_replace('|\W|', '_', $this->getDescription(false, true));
  250. }
  251. if ($decode) {
  252. return trim(MIME::decode($name));
  253. } else {
  254. return $name;
  255. }
  256. }
  257. /**
  258. * Set the body contents of this part.
  259. *
  260. * @param string $contents The part body.
  261. * @param string $encoding The current encoding of the contents.
  262. */
  263. function setContents($contents, $encoding = null)
  264. {
  265. $this->_contents = $contents;
  266. $this->_flags['contentsSet'] = true;
  267. $this->_currentEncoding = (is_null($encoding)) ? $this->getCurrentEncoding() : MIME::encoding($encoding, MIME_STRING);
  268. }
  269. /**
  270. * Add to the body contents of this part.
  271. *
  272. * @param string $contents The contents to append to the current part
  273. * body.
  274. * @param string $encoding The current encoding of the contents. If not
  275. * specified, will try to auto determine the
  276. * encoding.
  277. */
  278. function appendContents($contents, $encoding = null)
  279. {
  280. $this->setContents($this->_contents . $contents, $encoding);
  281. }
  282. /**
  283. * Clears the body contents of this part.
  284. */
  285. function clearContents()
  286. {
  287. $this->_contents = '';
  288. $this->_flags['contentsSet'] = false;
  289. $this->_currentEncoding = null;
  290. }
  291. /**
  292. * Return the body of the part.
  293. *
  294. * @return string The raw body of the part.
  295. */
  296. function getContents()
  297. {
  298. return $this->_contents;
  299. }
  300. /**
  301. * Returns the contents in strict RFC 822 & 2045 output - namely, all
  302. * newlines end with the canonical <CR><LF> sequence.
  303. *
  304. * @return string The entire MIME part.
  305. */
  306. function getCanonicalContents()
  307. {
  308. return $this->replaceEOL($this->_contents, MIME_PART_RFC_EOL);
  309. }
  310. /**
  311. * Transfer encode the contents (to the transfer encoding identified via
  312. * getTransferEncoding()) and set as the part's new contents.
  313. */
  314. function transferEncodeContents()
  315. {
  316. $contents = $this->transferEncode();
  317. $this->_currentEncoding = $this->_flags['lastTransferEncode'];
  318. $this->setContents($contents, $this->_currentEncoding);
  319. $this->setTransferEncoding($this->_currentEncoding);
  320. }
  321. /**
  322. * Transfer decode the contents and set them as the new contents.
  323. */
  324. function transferDecodeContents()
  325. {
  326. $contents = $this->transferDecode();
  327. $this->_currentEncoding = $this->_flags['lastTransferDecode'];
  328. $this->setTransferEncoding($this->_currentEncoding);
  329. /* Don't set contents if they are empty, because this will do stuff
  330. like reset the internal bytes field, even though we shouldn't do
  331. that (the user has their reasons to set the bytes field to a
  332. non-zero value without putting the contents into this part. */
  333. if (strlen($contents)) {
  334. $this->setContents($contents, $this->_currentEncoding);
  335. }
  336. }
  337. /**
  338. * Set the mimetype of this part.
  339. *
  340. * @param string $mimetype The mimetype to set (ex.: text/plain).
  341. */
  342. function setType($mimetype)
  343. {
  344. /* RFC 2045: Any entity with unrecognized encoding must be treated
  345. as if it has a Content-Type of "application/octet-stream"
  346. regardless of what the Content-Type field actually says. */
  347. if ($this->_transferEncoding == 'x-unknown') {
  348. return;
  349. }
  350. /* Set the 'setType' flag. */
  351. $this->_flags['setType'] = true;
  352. list($this->_type, $this->_subtype) = explode('/', String::lower($mimetype));
  353. if (($type = MIME::type($this->_type, MIME_STRING))) {
  354. $this->_type = $type;
  355. /* Set the boundary string for 'multipart/*' parts. */
  356. if ($type == 'multipart') {
  357. if (!$this->getContentTypeParameter('boundary')) {
  358. $this->setContentTypeParameter('boundary', $this->_generateBoundary());
  359. }
  360. } else {
  361. $this->clearContentTypeParameter('boundary');
  362. }
  363. } else {
  364. $this->_type = 'x-unknown';
  365. $this->clearContentTypeParameter('boundary');
  366. }
  367. }
  368. /**
  369. * Get the full MIME Content-Type of this part.
  370. *
  371. * @param boolean $charset Append character set information to the end of
  372. * the content type if this is a text/* part.
  373. *
  374. * @return string The mimetype of this part
  375. * (ex.: text/plain; charset=us-ascii).
  376. */
  377. function getType($charset = false)
  378. {
  379. if (!isset($this->_type) || !isset($this->_subtype)) {
  380. return false;
  381. }
  382. $ptype = $this->getPrimaryType();
  383. $type = $ptype . '/' . $this->getSubType();
  384. if ($charset && ($ptype == 'text')) {
  385. $type .= '; charset=' . $this->getCharset();
  386. }
  387. return $type;
  388. }
  389. /**
  390. * If the subtype of a MIME part is unrecognized by an application, the
  391. * default type should be used instead (See RFC 2046). This method
  392. * returns the default subtype for a particular primary MIME Type.
  393. *
  394. * @return string The default mimetype of this part (ex.: text/plain).
  395. */
  396. function getDefaultType()
  397. {
  398. switch ($this->getPrimaryType()) {
  399. case 'text':
  400. /* RFC 2046 (4.1.4): text parts default to text/plain. */
  401. return 'text/plain';
  402. case 'multipart':
  403. /* RFC 2046 (5.1.3): multipart parts default to multipart/mixed. */
  404. return 'multipart/mixed';
  405. default:
  406. /* RFC 2046 (4.2, 4.3, 4.4, 4.5.3, 5.2.4): all others default to
  407. application/octet-stream. */
  408. return 'application/octet-stream';
  409. }
  410. }
  411. /**
  412. * Get the primary type of this part.
  413. *
  414. * @return string The primary MIME type of this part.
  415. */
  416. function getPrimaryType()
  417. {
  418. return $this->_type;
  419. }
  420. /**
  421. * Get the subtype of this part.
  422. *
  423. * @return string The MIME subtype of this part.
  424. */
  425. function getSubType()
  426. {
  427. return $this->_subtype;
  428. }
  429. /**
  430. * Set the character set of this part.
  431. *
  432. * @param string $charset The character set of this part.
  433. */
  434. function setCharset($charset)
  435. {
  436. $this->setContentTypeParameter('charset', $charset);
  437. }
  438. /**
  439. * Get the character set to use for of this part. Returns a charset for
  440. * all types (not just 'text/*') since we use this charset to determine
  441. * how to encode text in MIME headers.
  442. *
  443. * @return string The character set of this part. Returns null if there
  444. * is no character set.
  445. */
  446. function getCharset()
  447. {
  448. $charset = $this->getContentTypeParameter('charset');
  449. return (empty($charset)) ? null : $charset;
  450. }
  451. /**
  452. * Set the description of this part.
  453. *
  454. * @param string $description The description of this part.
  455. */
  456. function setDescription($description)
  457. {
  458. $this->_description = MIME::encode($description, $this->getCharset());
  459. }
  460. /**
  461. * Get the description of this part.
  462. *
  463. * @param boolean $decode MIME decode description?
  464. * @param boolean $default If the name parameter doesn't exist, should we
  465. * use the default name from the description
  466. * parameter?
  467. *
  468. * @return string The description of this part.
  469. */
  470. function getDescription($decode = false, $default = false)
  471. {
  472. $desc = $this->_description;
  473. if ($default && empty($desc)) {
  474. $desc = $this->getName();
  475. if (empty($desc)) {
  476. $desc = MIME_DEFAULT_DESCRIPTION;
  477. }
  478. }
  479. if ($decode) {
  480. return MIME::decode($desc);
  481. } else {
  482. return $desc;
  483. }
  484. }
  485. /**
  486. * Set the transfer encoding to use for this part.
  487. *
  488. * @param string $encoding The transfer encoding to use.
  489. */
  490. function setTransferEncoding($encoding)
  491. {
  492. if (($mime_encoding = MIME::encoding($encoding, MIME_STRING))) {
  493. $this->_transferEncoding = $mime_encoding;
  494. } else {
  495. /* RFC 2045: Any entity with unrecognized encoding must be treated
  496. as if it has a Content-Type of "application/octet-stream"
  497. regardless of what the Content-Type field actually says. */
  498. $this->setType('application/octet-stream');
  499. $this->_transferEncoding = 'x-unknown';
  500. }
  501. }
  502. /**
  503. * Add a MIME subpart.
  504. *
  505. * @param MIME_Part $mime_part Add a MIME_Part subpart to the current
  506. * MIME_Part.
  507. * @param string $index The index of the added MIME_Part.
  508. */
  509. function addPart($mime_part, $index = null)
  510. {
  511. /* Add the part to the parts list. */
  512. if (is_null($index)) {
  513. end($this->_parts);
  514. $id = key($this->_parts) + 1;
  515. $ptr = &$this->_parts;
  516. } else {
  517. $ptr = &$this->_partFind($index, $this->_parts, true);
  518. if (($pos = strrpos($index, '.'))) {
  519. $id = substr($index, $pos + 1);
  520. } else {
  521. $id = $index;
  522. }
  523. }
  524. /* Set the MIME ID if it has not already been set. */
  525. if ($mime_part->getMIMEId() === null) {
  526. $mime_part->setMIMEId($id);
  527. }
  528. /* Store the part now. */
  529. $ptr[$id] = $mime_part;
  530. /* Clear the ID -> Part mapping cache. */
  531. $this->_idmap = array();
  532. }
  533. /**
  534. * Get a list of all MIME subparts.
  535. *
  536. * @return array An array of the MIME_Part subparts.
  537. */
  538. function getParts()
  539. {
  540. return $this->_parts;
  541. }
  542. /**
  543. * Retrieve a specific MIME part.
  544. *
  545. * @param string $id The MIME_Part ID string.
  546. *
  547. * @return MIME_Part The MIME_Part requested, or false if the part
  548. * doesn't exist.
  549. */
  550. function getPart($id)
  551. {
  552. $mimeid = $this->getMIMEId();
  553. /* This will convert '#.0' to simply '#', which is how the part is
  554. * internally stored. */
  555. $search_id = $id;
  556. if (($str = strrchr($id, '.')) &&
  557. ($str == '.0')) {
  558. $search_id = substr($search_id, 0, -2);
  559. }
  560. /* Return this part if:
  561. 1) There is only one part (e.g. the MIME ID is 0, or the
  562. MIME ID is 1 and there are no subparts.
  563. 2) $id matches this parts MIME ID. */
  564. if (($search_id == 0) ||
  565. (($search_id == 1) && !count($this->_parts)) ||
  566. (!empty($mimeid) && ($search_id == $mimeid))) {
  567. $part = $this;
  568. } else {
  569. $part = $this->_partFind($id, $this->_parts);
  570. }
  571. if ($part &&
  572. ($search_id != $id) &&
  573. ($part->getType() == 'message/rfc822')) {
  574. $ret_part = Util::cloneObject($part);
  575. $ret_part->_parts = array();
  576. return $ret_part;
  577. }
  578. return $part;
  579. }
  580. /**
  581. * Remove a MIME_Part subpart.
  582. *
  583. * @param string $id The MIME Part to delete.
  584. */
  585. function removePart($id)
  586. {
  587. if (($ptr = &$this->_partFind($id, $this->_parts))) {
  588. unset($ptr);
  589. $this->_idmap = array();
  590. }
  591. }
  592. /**
  593. * Alter a current MIME subpart.
  594. *
  595. * @param string $id The MIME Part ID to alter.
  596. * @param MIME_Part $mime_part The MIME Part to store.
  597. */
  598. function alterPart($id, $mime_part)
  599. {
  600. if (($ptr = &$this->_partFind($id, $this->_parts))) {
  601. $ptr = $mime_part;
  602. $this->_idmap = array();
  603. }
  604. }
  605. /**
  606. * Function used to find a specific MIME Part by ID.
  607. *
  608. * @access private
  609. *
  610. * @param string $id The MIME_Part ID string.
  611. * @param array &$parts A list of MIME_Part objects.
  612. * @param boolean $retarray Return a pointer to the array that stores
  613. * (would store) the part rather than the part
  614. * itself?
  615. */
  616. function &_partFind($id, &$parts, $retarray = false)
  617. {
  618. /* Pointers don't persist through sessions; therefore, we must make
  619. * sure that the IdMap is destroyed at the end of each request.
  620. * How can we do this? We check to see if $_idmap contains an array
  621. * of MIME_Parts or an array of arrays. */
  622. $check = reset($this->_idmap);
  623. if (empty($check) || !is_a($check, 'MIME_Part')) {
  624. $this->_idmap = array();
  625. $this->_generateIdMap($this->_parts);
  626. }
  627. if ($retarray) {
  628. if ($pos = strrpos($id, '.')) {
  629. $id = substr($id, 0, $pos);
  630. } else {
  631. return $parts;
  632. }
  633. }
  634. if (isset($this->_idmap[$id])) {
  635. return $this->_idmap[$id];
  636. } else {
  637. $part = false;
  638. return $part;
  639. }
  640. }
  641. /**
  642. * Generates a mapping of MIME_Parts with their MIME IDs.
  643. *
  644. * @access private
  645. *
  646. * @param array &$parts An array of MIME_Parts to map.
  647. */
  648. function _generateIdMap(&$parts)
  649. {
  650. if (!empty($parts)) {
  651. foreach (array_keys($parts) as $key) {
  652. $ptr = &$parts[$key];
  653. $this->_idmap[$ptr->getMIMEId()] = &$ptr;
  654. $this->_generateIdMap($ptr->_parts);
  655. }
  656. }
  657. }
  658. /**
  659. * Add information about the MIME_Part.
  660. *
  661. * @param string $label The information label.
  662. * @param mixed $data The information to store.
  663. */
  664. function setInformation($label, $data)
  665. {
  666. $this->_information[$label] = $data;
  667. }
  668. /**
  669. * Retrieve information about the MIME_Part.
  670. *
  671. * @param string $label The information label.
  672. *
  673. * @return mixed The information requested.
  674. * Returns false if $label is not set.
  675. */
  676. function getInformation($label)
  677. {
  678. return (isset($this->_information[$label])) ? $this->_information[$label] : false;
  679. }
  680. /**
  681. * Add a disposition parameter to this part.
  682. *
  683. * @param string $label The disposition parameter label.
  684. * @param string $data The disposition parameter data.
  685. */
  686. function setDispositionParameter($label, $data)
  687. {
  688. $this->_dispositionParameters[$label] = $data;
  689. }
  690. /**
  691. * Get a disposition parameter from this part.
  692. *
  693. * @param string $label The disposition parameter label.
  694. *
  695. * @return string The data requested.
  696. * Returns false if $label is not set.
  697. */
  698. function getDispositionParameter($label)
  699. {
  700. return (isset($this->_dispositionParameters[$label])) ? $this->_dispositionParameters[$label] : false;
  701. }
  702. /**
  703. * Get all parameters from the Content-Disposition header.
  704. *
  705. * @return array An array of all the parameters
  706. * Returns the empty array if no parameters set.
  707. */
  708. function getAllDispositionParameters()
  709. {
  710. return $this->_dispositionParameters;
  711. }
  712. /**
  713. * Add a content type parameter to this part.
  714. *
  715. * @param string $label The disposition parameter label.
  716. * @param string $data The disposition parameter data.
  717. */
  718. function setContentTypeParameter($label, $data)
  719. {
  720. $this->_contentTypeParameters[$label] = $data;
  721. }
  722. /**
  723. * Clears a content type parameter from this part.
  724. *
  725. * @param string $label The disposition parameter label.
  726. * @param string $data The disposition parameter data.
  727. */
  728. function clearContentTypeParameter($label)
  729. {
  730. unset($this->_contentTypeParameters[$label]);
  731. }
  732. /**
  733. * Get a content type parameter from this part.
  734. *
  735. * @param string $label The content type parameter label.
  736. *
  737. * @return string The data requested.
  738. * Returns false if $label is not set.
  739. */
  740. function getContentTypeParameter($label)
  741. {
  742. return (isset($this->_contentTypeParameters[$label])) ? $this->_contentTypeParameters[$label] : false;
  743. }
  744. /**
  745. * Get all parameters from the Content-Type header.
  746. *
  747. * @return array An array of all the parameters
  748. * Returns the empty array if no parameters set.
  749. */
  750. function getAllContentTypeParameters()
  751. {
  752. return $this->_contentTypeParameters;
  753. }
  754. /**
  755. * Sets a new string to use for EOLs.
  756. *
  757. * @param string $eol The string to use for EOLs.
  758. */
  759. function setEOL($eol)
  760. {
  761. $this->_eol = $eol;
  762. }
  763. /**
  764. * Get the string to use for EOLs.
  765. *
  766. * @return string The string to use for EOLs.
  767. */
  768. function getEOL()
  769. {
  770. return $this->_eol;
  771. }
  772. /**
  773. * Add the appropriate MIME headers for this part to an existing array.
  774. *
  775. * @param array $headers An array of any other headers for the part.
  776. *
  777. * @return array The headers, with the MIME headers added.
  778. */
  779. function header($headers = array())
  780. {
  781. $eol = $this->getEOL();
  782. $ptype = $this->getPrimaryType();
  783. /* Get the character set for this part. */
  784. $charset = $this->getCharset();
  785. /* Get the Content-Type - this is ALWAYS required. */
  786. $ctype = $this->getType(true);
  787. foreach ($this->getAllContentTypeParameters() as $key => $value) {
  788. /* Skip the charset key since that would have already been
  789. * added to $ctype by getType(). */
  790. if ($key == 'charset') {
  791. continue;
  792. }
  793. $encode_2231 = MIME::encodeRFC2231($key, $value, $charset);
  794. /* Try to work around non RFC 2231-compliant MUAs by sending both
  795. * a RFC 2047-like parameter name and then the correct RFC 2231
  796. * parameter. See:
  797. * http://lists.horde.org/archives/dev/Week-of-Mon-20040426/014240.html */
  798. if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) &&
  799. ((strpos($encode_2231, '*=') !== false) ||
  800. (strpos($encode_2231, '*0=') !== false))) {
  801. $ctype .= '; ' . $key . '="' . MIME::encode($value, $charset) . '"';
  802. }
  803. $ctype .= '; ' . $encode_2231;
  804. }
  805. $headers['Content-Type'] = MIME::wrapHeaders('Content-Type', $ctype, $eol);
  806. /* Get the description, if any. */
  807. if (($descrip = $this->getDescription())) {
  808. $headers['Content-Description'] = MIME::wrapHeaders('Content-Description', MIME::encode($descrip, $charset), $eol);
  809. }
  810. /* message/* parts require no additional header information. */
  811. if ($ptype == 'message') {
  812. return $headers;
  813. }
  814. /* Don't show Content-Disposition for multipart messages unless
  815. there is a name parameter. */
  816. $name = $this->getName();
  817. if (($ptype != 'multipart') || !empty($name)) {
  818. $disp = $this->getDisposition();
  819. /* Add any disposition parameter information, if available. */
  820. if (!empty($name)) {
  821. $encode_2231 = MIME::encodeRFC2231('filename', $name, $charset);
  822. /* Same broken RFC 2231 workaround as above. */
  823. if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) &&
  824. ((strpos($encode_2231, '*=') !== false) ||
  825. (strpos($encode_2231, '*0=') !== false))) {
  826. $disp .= '; filename="' . MIME::encode($name, $charset) . '"';
  827. }
  828. $disp .= '; ' . $encode_2231;
  829. }
  830. $headers['Content-Disposition'] = MIME::wrapHeaders('Content-Disposition', $disp, $eol);
  831. }
  832. /* Add transfer encoding information. */
  833. $headers['Content-Transfer-Encoding'] = $this->getTransferEncoding();
  834. /* Add content ID information. */
  835. if (!is_null($this->_contentid)) {
  836. $headers['Content-ID'] = '<' . $this->_contentid . '>';
  837. }
  838. return $headers;
  839. }
  840. /**
  841. * Return the entire part in MIME format. Includes headers on request.
  842. *
  843. * @param boolean $headers Include the MIME headers?
  844. *
  845. * @return string The MIME string.
  846. */
  847. function toString($headers = true)
  848. {
  849. $eol = $this->getEOL();
  850. $ptype = $this->getPrimaryType();
  851. if ($headers) {
  852. $text = '';
  853. foreach ($this->header() as $key => $val) {
  854. $text .= $key . ': ' . $val . $eol;
  855. }
  856. $text .= $eol;
  857. }
  858. /* Any information about a message/* is embedded in the message
  859. contents themself. Simply output the contents of the part
  860. directly and return. */
  861. if ($ptype == 'message') {
  862. if (isset($text)) {
  863. return $text . $this->_contents;
  864. } else {
  865. return $this->_contents;
  866. }
  867. }
  868. if (isset($text)) {
  869. $text .= $this->transferEncode();
  870. } else {
  871. $text = $this->transferEncode();
  872. }
  873. /* Deal with multipart messages. */
  874. if ($ptype == 'multipart') {
  875. $boundary = trim($this->getContentTypeParameter('boundary'), '"');
  876. if (!strlen($this->_contents)) {
  877. $text .= 'This message is in MIME format.' . $eol;
  878. }
  879. reset($this->_parts);
  880. while (list(,$part) = each($this->_parts)) {
  881. $text .= $eol . '--' . $boundary . $eol;
  882. $oldEOL = $part->getEOL();
  883. $part->setEOL($eol);
  884. $text .= $part->toString(true);
  885. $part->setEOL($oldEOL);
  886. }
  887. $text .= $eol . '--' . $boundary . '--' . $eol;
  888. }
  889. return $text;
  890. }
  891. /**
  892. * Returns the encoded part in strict RFC 822 & 2045 output - namely, all
  893. * newlines end with the canonical <CR><LF> sequence.
  894. *
  895. * @param boolean $headers Include the MIME headers?
  896. *
  897. * @return string The entire MIME part.
  898. */
  899. function toCanonicalString($headers = true)
  900. {
  901. $string = $this->toString($headers);
  902. return $this->replaceEOL($string, MIME_PART_RFC_EOL);
  903. }
  904. /**
  905. * Should we make sure the message is encoded via 7-bit (e.g. to adhere
  906. * to mail delivery standards such as RFC 2821)?
  907. *
  908. * @param boolean $use7bit Use 7-bit encoding?
  909. */
  910. function strict7bit($use7bit)
  911. {
  912. $this->_encode7bit = $use7bit;
  913. }
  914. /**
  915. * Get the transfer encoding for the part based on the user requested
  916. * transfer encoding and the current contents of the part.
  917. *
  918. * @return string The transfer-encoding of this part.
  919. */
  920. function getTransferEncoding()
  921. {
  922. $encoding = $this->_transferEncoding;
  923. $ptype = $this->getPrimaryType();
  924. $text = str_replace($this->getEOL(), ' ', $this->_contents);
  925. /* If there are no contents, return whatever the current value of
  926. $_transferEncoding is. */
  927. if (empty($text)) {
  928. return $encoding;
  929. }
  930. switch ($ptype) {
  931. case 'message':
  932. /* RFC 2046 [5.2.1] - message/rfc822 messages only allow 7bit,
  933. 8bit, and binary encodings. If the current encoding is either
  934. base64 or q-p, switch it to 8bit instead.
  935. RFC 2046 [5.2.2, 5.2.3, 5.2.4] - All other message/* messages
  936. only allow 7bit encodings. */
  937. $encoding = ($this->getSubType() == 'rfc822') ? '8bit' : '7bit';
  938. break;
  939. case 'text':
  940. $eol = $this->getEOL();
  941. if (MIME::is8bit($text)) {
  942. $encoding = ($this->_encode7bit) ? 'quoted-printable' : '8bit';
  943. } elseif (preg_match("/(?:" . $eol . "|^)[^" . $eol . "]{999,}(?:" . $eol . "|$)/", $this->_contents)) {
  944. /* If the text is longer than 998 characters between
  945. * linebreaks, use quoted-printable encoding to ensure the
  946. * text will not be chopped (i.e. by sendmail if being sent
  947. * as mail text). */
  948. $encoding = 'quoted-printable';
  949. }
  950. break;
  951. default:
  952. if (MIME::is8bit($text)) {
  953. $encoding = ($this->_encode7bit) ? 'base64' : '8bit';
  954. }
  955. break;
  956. }
  957. /* Need to do one last check for binary data if encoding is 7bit or
  958. * 8bit. If the message contains a NULL character at all, the message
  959. * MUST be in binary format. RFC 2046 [2.7, 2.8, 2.9]. Q-P and base64
  960. * can handle binary data fine so no need to switch those encodings. */
  961. if ((($encoding == '8bit') || ($encoding == '7bit')) &&
  962. preg_match('/\x00/', $text)) {
  963. $encoding = ($this->_encode7bit) ? 'base64' : 'binary';
  964. }
  965. return $encoding;
  966. }
  967. /**
  968. * Retrieves the current encoding of the contents in the object.
  969. *
  970. * @return string The current encoding.
  971. */
  972. function getCurrentEncoding()
  973. {
  974. return (is_null($this->_currentEncoding)) ? $this->_transferEncoding : $this->_currentEncoding;
  975. }
  976. /**
  977. * Encodes the contents with the part's transfer encoding.
  978. *
  979. * @return string The encoded text.
  980. */
  981. function transferEncode()
  982. {
  983. $encoding = $this->getTransferEncoding();
  984. $eol = $this->getEOL();
  985. /* Set the 'lastTransferEncode' flag so that transferEncodeContents()
  986. can save a call to getTransferEncoding(). */
  987. $this->_flags['lastTransferEncode'] = $encoding;
  988. /* If contents are empty, or contents are already encoded to the
  989. correct encoding, return now. */
  990. if (!strlen($this->_contents) ||
  991. ($encoding == $this->_currentEncoding)) {
  992. return $this->_contents;
  993. }
  994. switch ($encoding) {
  995. /* Base64 Encoding: See RFC 2045, section 6.8 */
  996. case 'base64':
  997. /* Keeping these two lines separate seems to use much less
  998. memory than combining them (as of PHP 4.3). */
  999. $encoded_contents = base64_encode($this->_contents);
  1000. return chunk_split($encoded_contents, 76, $eol);
  1001. /* Quoted-Printable Encoding: See RFC 2045, section 6.7 */
  1002. case 'quoted-printable':
  1003. $output = MIME::quotedPrintableEncode($this->_contents, $eol);
  1004. if (($eollength = String::length($eol)) &&
  1005. (substr($output, $eollength * -1) == $eol)) {
  1006. return substr($output, 0, $eollength * -1);
  1007. } else {
  1008. return $output;
  1009. }
  1010. default:
  1011. return $this->replaceEOL($this->_contents);
  1012. }
  1013. }
  1014. /**
  1015. * Decodes the contents of the part to either a 7bit or 8bit encoding.
  1016. *
  1017. * @return string The decoded text.
  1018. * Returns the empty string if there is no text to decode.
  1019. */
  1020. function transferDecode()
  1021. {
  1022. $encoding = $this->getCurrentEncoding();
  1023. /* If the contents are empty, return now. */
  1024. if (!strlen($this->_contents)) {
  1025. $this->_flags['lastTransferDecode'] = $encoding;
  1026. return $this->_contents;
  1027. }
  1028. switch ($encoding) {
  1029. case 'base64':
  1030. $message = base64_decode($this->_contents);
  1031. $this->_flags['lastTransferDecode'] = '8bit';
  1032. break;
  1033. case 'quoted-printable':
  1034. $message = preg_replace("/=\r?\n/", '', $this->_contents);
  1035. $message = $this->replaceEOL($message);
  1036. $message = quoted_printable_decode($message);
  1037. $this->_flags['lastTransferDecode'] = (MIME::is8bit($message)) ? '8bit' : '7bit';
  1038. break;
  1039. /* Support for uuencoded encoding - although not required by RFCs,
  1040. some mailers may still encode this way. */
  1041. case 'uuencode':
  1042. case 'x-uuencode':
  1043. case 'x-uue':
  1044. if (function_exists('convert_uudecode')) {
  1045. $message = convert_uuencode($this->_contents);
  1046. } else {
  1047. // TODO: Remove once PHP5 is required
  1048. require_once 'Mail/mimeDecode.php';
  1049. $files = &Mail_mimeDecode::uudecode($this->_contents);
  1050. $message = $files[0]['filedata'];
  1051. }
  1052. $this->_flags['lastTransferDecode'] = '8bit';
  1053. break;
  1054. default:
  1055. if (isset($this->_flags['lastTransferDecode']) &&
  1056. ($this->_flags['lastTransferDecode'] != $encoding)) {
  1057. $message = $this->replaceEOL($this->_contents);
  1058. } else {
  1059. $message = $this->_contents;
  1060. }
  1061. $this->_flags['lastTransferDecode'] = $encoding;
  1062. break;
  1063. }
  1064. return $message;
  1065. }
  1066. /**
  1067. * Split the contents of the current Part into its respective subparts,
  1068. * if it is multipart MIME encoding. Unlike the imap_*() functions, this
  1069. * will preserve all MIME header information.
  1070. *
  1071. * The boundary content-type parameter must be set for this function to
  1072. * work correctly.
  1073. *
  1074. * @return boolean True if the contents were successfully split.
  1075. * False if any error occurred.
  1076. */
  1077. function splitContents()
  1078. {
  1079. if (!($boundary = $this->getContentTypeParameter('boundary'))) {
  1080. return false;
  1081. }
  1082. if (!strlen($this->_contents)) {
  1083. return false;
  1084. }
  1085. $eol = $this->getEOL();
  1086. $retvalue = false;
  1087. $boundary = '--' . $boundary;
  1088. if (substr($this->_contents, 0, strlen($boundary)) == $boundary) {
  1089. $pos1 = 0;
  1090. } else {
  1091. $pos1 = strpos($this->_contents, $eol . $boundary);
  1092. }
  1093. if ($pos1 === false) {
  1094. return false;
  1095. }
  1096. $pos1 = strpos($this->_contents, $eol, $pos1 + 1);
  1097. if ($pos1 === false) {
  1098. return false;
  1099. }
  1100. $pos1 += strlen($eol);
  1101. reset($this->_parts);
  1102. $part_ptr = key($this->_parts);
  1103. while ($pos2 = strpos($this->_contents, $eol . $boundary, $pos1)) {
  1104. $this->_parts[$part_ptr]->setContents(substr($this->_contents, $pos1, $pos2 - $pos1));
  1105. $this->_parts[$part_ptr]->splitContents();
  1106. next($this->_parts);
  1107. $part_ptr = key($this->_parts);
  1108. if (is_null($part_ptr)) {
  1109. return false;
  1110. }
  1111. $pos1 = strpos($this->_contents, $eol, $pos2 + 1);
  1112. if ($pos1 === false) {
  1113. return true;
  1114. }
  1115. $pos1 += strlen($eol);
  1116. }
  1117. return true;
  1118. }
  1119. /**
  1120. * Replace newlines in this part's contents with those specified by either
  1121. * the given newline sequence or the part's current EOL setting.
  1122. *
  1123. * @param string $text The text to replace.
  1124. * @param string $eol The EOL sequence to use. If not present, uses the
  1125. * part's current EOL setting.
  1126. *
  1127. * @return string The text with the newlines replaced by the desired
  1128. * newline sequence.
  1129. */
  1130. function replaceEOL($text, $eol = null)
  1131. {
  1132. if (is_null($eol)) {
  1133. $eol = $this->getEOL();
  1134. }
  1135. return preg_replace("/\r?\n/", $eol, $text);
  1136. }
  1137. /**
  1138. * Return the unique MIME_Part boundary string generated for this object.
  1139. * This may not be the boundary string used when building the message
  1140. * since a user defined 'boundary' Content-Type parameter will override
  1141. * this value.
  1142. *
  1143. * @return string The unique boundary string.
  1144. */
  1145. function getUniqueID()
  1146. {
  1147. return $this->_boundary;
  1148. }
  1149. /**
  1150. * Determine the size of a MIME_Part and its child members.
  1151. *
  1152. * @return integer Size of the MIME_Part, in bytes.
  1153. */
  1154. function getBytes()
  1155. {
  1156. $bytes = 0;
  1157. if (empty($this->_flags['contentsSet']) && $this->_bytes) {
  1158. $bytes = $this->_bytes;
  1159. } elseif ($this->getPrimaryType() == 'multipart') {
  1160. reset($this->_parts);
  1161. while (list(,$part) = each($this->_parts)) {
  1162. /* Skip multipart entries (since this may result in double
  1163. counting). */
  1164. if ($part->getPrimaryType() != 'multipart') {
  1165. $bytes += $part->getBytes();
  1166. }
  1167. }
  1168. } else {
  1169. if ($this->getPrimaryType() == 'text') {
  1170. $bytes = String::length($this->_contents, $this->getCharset());
  1171. } else {
  1172. $bytes = strlen($this->_contents);
  1173. }
  1174. }
  1175. return $bytes;
  1176. }
  1177. /**
  1178. * Explicitly set the size (in bytes) of this part. This value will only
  1179. * be returned (via getBytes()) if there are no contents currently set.
  1180. * This function is useful for setting the size of the part when the
  1181. * contents of the part are not fully loaded (i.e. creating a MIME_Part
  1182. * object from IMAP header information without loading the data of the
  1183. * part).
  1184. *
  1185. * @param integer $bytes The size of this part in bytes.
  1186. */
  1187. function setBytes($bytes)
  1188. {
  1189. $this->_bytes = $bytes;
  1190. }
  1191. /**
  1192. * Output the size of this MIME_Part in KB.
  1193. *
  1194. * @return string Size of the MIME_Part, in string format.
  1195. */
  1196. function getSize()
  1197. {
  1198. $bytes = $this->getBytes();
  1199. if (empty($bytes)) {
  1200. return $bytes;
  1201. }
  1202. return NLS::numberFormat($bytes / 1024, 2);
  1203. }
  1204. /**
  1205. * Add to the list of CIDs for this part.
  1206. *
  1207. * @param array $cids A list of MIME IDs of the part.
  1208. * Key - MIME ID
  1209. * Value - CID for the part
  1210. */
  1211. function addCID($cids = array())
  1212. {
  1213. $this->_cids += $cids;
  1214. }
  1215. /**
  1216. * Returns the list of CIDs for this part.
  1217. *
  1218. * @return array The list of CIDs for this part.
  1219. */
  1220. function getCIDList()
  1221. {
  1222. asort($this->_cids, SORT_STRING);
  1223. return $this->_cids;
  1224. }
  1225. /**
  1226. * Sets the Content-ID header for this part.
  1227. *
  1228. * @param string $cid Use this CID (if not already set). Else, generate a
  1229. * random CID.
  1230. */
  1231. function setContentID($cid = null)
  1232. {
  1233. if (is_null($this->_contentid)) {
  1234. $this->_contentid = (is_null($cid)) ? (base_convert(uniqid(mt_rand()), 10, 36) . '@' . $_SERVER['SERVER_NAME']) : $cid;
  1235. }
  1236. return $this->_contentid;
  1237. }
  1238. /**
  1239. * Returns the Content-ID for this part.
  1240. *
  1241. * @return string The Content-ID for this part.
  1242. */
  1243. function getContentID()
  1244. {
  1245. return $this->_contentid;
  1246. }
  1247. /**
  1248. * Alter the MIME ID of this part.
  1249. *
  1250. * @param string $mimeid The MIME ID.
  1251. */
  1252. function setMIMEId($mimeid)
  1253. {
  1254. $this->_mimeid = $mimeid;
  1255. }
  1256. /**
  1257. * Returns the MIME ID of this part.
  1258. *
  1259. * @return string The MIME ID.
  1260. */
  1261. function getMIMEId()
  1262. {
  1263. return $this->_mimeid;
  1264. }
  1265. /**
  1266. * Returns the relative MIME ID of this part.
  1267. * e.g., if the base part has MIME ID of 2, and you want the first
  1268. * subpart of the base part, the relative MIME ID is 2.1.
  1269. *
  1270. * @param string $id The relative part ID.
  1271. *
  1272. * @return string The relative MIME ID.
  1273. */
  1274. function getRelativeMIMEId($id)
  1275. {
  1276. $rel = $this->getMIMEId();
  1277. return (empty($rel)) ? $id : $rel . '.' . $id;
  1278. }
  1279. /**
  1280. * Returns a mapping of all MIME IDs to their content-types.
  1281. *
  1282. * @return array KEY: MIME ID, VALUE: Content type
  1283. */
  1284. function contentTypeMap()
  1285. {
  1286. $map = array($this->getMIMEId() => $this->getType());
  1287. foreach ($this->_parts as $val) {
  1288. $map += $val->contentTypeMap();
  1289. }
  1290. return $map;
  1291. }
  1292. /**
  1293. * Generate the unique boundary string (if not already done).
  1294. *
  1295. * @access private
  1296. *
  1297. * @return string The boundary string.
  1298. */
  1299. function _generateBoundary()
  1300. {
  1301. if (is_null($this->_boundary)) {
  1302. $this->_boundary = '=_' . base_convert(uniqid(mt_rand()), 10, 36);
  1303. }
  1304. return $this->_boundary;
  1305. }
  1306. }