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