2
0

Part.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mime
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Mime
  22. */
  23. require_once 'Zend/Mime.php';
  24. /**
  25. * Class representing a MIME part.
  26. *
  27. * @category Zend
  28. * @package Zend_Mime
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Mime_Part {
  33. public $type = Zend_Mime::TYPE_OCTETSTREAM;
  34. public $encoding = Zend_Mime::ENCODING_8BIT;
  35. public $id;
  36. public $disposition;
  37. public $filename;
  38. public $description;
  39. public $charset;
  40. public $boundary;
  41. public $location;
  42. public $language;
  43. protected $_content;
  44. protected $_isStream = false;
  45. /**
  46. * create a new Mime Part.
  47. * The (unencoded) content of the Part as passed
  48. * as a string or stream
  49. *
  50. * @param mixed $content String or Stream containing the content
  51. */
  52. public function __construct($content)
  53. {
  54. $this->_content = $content;
  55. if (is_resource($content)) {
  56. $this->_isStream = true;
  57. }
  58. }
  59. /**
  60. * @todo setters/getters
  61. * @todo error checking for setting $type
  62. * @todo error checking for setting $encoding
  63. */
  64. /**
  65. * check if this part can be read as a stream.
  66. * if true, getEncodedStream can be called, otherwise
  67. * only getContent can be used to fetch the encoded
  68. * content of the part
  69. *
  70. * @return bool
  71. */
  72. public function isStream()
  73. {
  74. return $this->_isStream;
  75. }
  76. /**
  77. * if this was created with a stream, return a filtered stream for
  78. * reading the content. very useful for large file attachments.
  79. *
  80. * @return stream
  81. * @throws Zend_Mime_Exception if not a stream or unable to append filter
  82. */
  83. public function getEncodedStream()
  84. {
  85. if (!$this->_isStream) {
  86. require_once 'Zend/Mime/Exception.php';
  87. throw new Zend_Mime_Exception('Attempt to get a stream from a string part');
  88. }
  89. //stream_filter_remove(); // ??? is that right?
  90. switch ($this->encoding) {
  91. case Zend_Mime::ENCODING_QUOTEDPRINTABLE:
  92. $filter = stream_filter_append(
  93. $this->_content,
  94. 'convert.quoted-printable-encode',
  95. STREAM_FILTER_READ,
  96. array(
  97. 'line-length' => 76,
  98. 'line-break-chars' => Zend_Mime::LINEEND
  99. )
  100. );
  101. if (!is_resource($filter)) {
  102. require_once 'Zend/Mime/Exception.php';
  103. throw new Zend_Mime_Exception('Failed to append quoted-printable filter');
  104. }
  105. break;
  106. case Zend_Mime::ENCODING_BASE64:
  107. $filter = stream_filter_append(
  108. $this->_content,
  109. 'convert.base64-encode',
  110. STREAM_FILTER_READ,
  111. array(
  112. 'line-length' => 76,
  113. 'line-break-chars' => Zend_Mime::LINEEND
  114. )
  115. );
  116. if (!is_resource($filter)) {
  117. require_once 'Zend/Mime/Exception.php';
  118. throw new Zend_Mime_Exception('Failed to append base64 filter');
  119. }
  120. break;
  121. default:
  122. }
  123. return $this->_content;
  124. }
  125. /**
  126. * Get the Content of the current Mime Part in the given encoding.
  127. *
  128. * @return String
  129. */
  130. public function getContent($EOL = Zend_Mime::LINEEND)
  131. {
  132. if ($this->_isStream) {
  133. return stream_get_contents($this->getEncodedStream());
  134. } else {
  135. return Zend_Mime::encode($this->_content, $this->encoding, $EOL);
  136. }
  137. }
  138. /**
  139. * Create and return the array of headers for this MIME part
  140. *
  141. * @access public
  142. * @return array
  143. */
  144. public function getHeadersArray($EOL = Zend_Mime::LINEEND)
  145. {
  146. $headers = array();
  147. $contentType = $this->type;
  148. if ($this->charset) {
  149. $contentType .= '; charset=' . $this->charset;
  150. }
  151. if ($this->boundary) {
  152. $contentType .= ';' . $EOL
  153. . " boundary=\"" . $this->boundary . '"';
  154. }
  155. $headers[] = array('Content-Type', $contentType);
  156. if ($this->encoding) {
  157. $headers[] = array('Content-Transfer-Encoding', $this->encoding);
  158. }
  159. if ($this->id) {
  160. $headers[] = array('Content-ID', '<' . $this->id . '>');
  161. }
  162. if ($this->disposition) {
  163. $disposition = $this->disposition;
  164. if ($this->filename) {
  165. $disposition .= '; filename="' . $this->filename . '"';
  166. }
  167. $headers[] = array('Content-Disposition', $disposition);
  168. }
  169. if ($this->description) {
  170. $headers[] = array('Content-Description', $this->description);
  171. }
  172. if ($this->location) {
  173. $headers[] = array('Content-Location', $this->location);
  174. }
  175. if ($this->language){
  176. $headers[] = array('Content-Language', $this->language);
  177. }
  178. return $headers;
  179. }
  180. /**
  181. * Return the headers for this part as a string
  182. *
  183. * @return String
  184. */
  185. public function getHeaders($EOL = Zend_Mime::LINEEND)
  186. {
  187. $res = '';
  188. foreach ($this->getHeadersArray($EOL) as $header) {
  189. $res .= $header[0] . ': ' . $header[1] . $EOL;
  190. }
  191. return $res;
  192. }
  193. }