PageRenderTime 16ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/zend/Zend/Mime/Part.php

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