File indexing completed on 2024-12-22 05:36:52
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Mime 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Zend_Mime 0024 */ 0025 // require_once 'Zend/Mime.php'; 0026 0027 /** 0028 * Class representing a MIME part. 0029 * 0030 * @category Zend 0031 * @package Zend_Mime 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_Mime_Part 0036 { 0037 0038 /** 0039 * Type 0040 * 0041 * @var string 0042 */ 0043 public $type = Zend_Mime::TYPE_OCTETSTREAM; 0044 0045 /** 0046 * Encoding 0047 * 0048 * @var string 0049 */ 0050 public $encoding = Zend_Mime::ENCODING_8BIT; 0051 0052 /** 0053 * ID 0054 * 0055 * @var string 0056 */ 0057 public $id; 0058 0059 /** 0060 * Disposition 0061 * 0062 * @var string 0063 */ 0064 public $disposition; 0065 0066 /** 0067 * Filename 0068 * 0069 * @var string 0070 */ 0071 public $filename; 0072 0073 /** 0074 * Description 0075 * 0076 * @var string 0077 */ 0078 public $description; 0079 0080 /** 0081 * Character set 0082 * 0083 * @var string 0084 */ 0085 public $charset; 0086 0087 /** 0088 * Boundary 0089 * 0090 * @var string 0091 */ 0092 public $boundary; 0093 0094 /** 0095 * Location 0096 * 0097 * @var string 0098 */ 0099 public $location; 0100 0101 /** 0102 * Language 0103 * 0104 * @var string 0105 */ 0106 public $language; 0107 0108 /** 0109 * Content 0110 * 0111 * @var mixed 0112 */ 0113 protected $_content; 0114 0115 /** 0116 * @var bool 0117 */ 0118 protected $_isStream = false; 0119 0120 /** 0121 * create a new Mime Part. 0122 * The (unencoded) content of the Part as passed 0123 * as a string or stream 0124 * 0125 * @param mixed $content String or Stream containing the content 0126 */ 0127 public function __construct($content) 0128 { 0129 $this->_content = $content; 0130 if (is_resource($content)) { 0131 $this->_isStream = true; 0132 } 0133 } 0134 0135 /** 0136 * @todo setters/getters 0137 * @todo error checking for setting $type 0138 * @todo error checking for setting $encoding 0139 */ 0140 0141 /** 0142 * check if this part can be read as a stream. 0143 * if true, getEncodedStream can be called, otherwise 0144 * only getContent can be used to fetch the encoded 0145 * content of the part 0146 * 0147 * @return bool 0148 */ 0149 public function isStream() 0150 { 0151 return $this->_isStream; 0152 } 0153 0154 /** 0155 * if this was created with a stream, return a filtered stream for 0156 * reading the content. very useful for large file attachments. 0157 * 0158 * @return mixed Stream 0159 * @throws Zend_Mime_Exception if not a stream or unable to append filter 0160 */ 0161 public function getEncodedStream() 0162 { 0163 if (!$this->_isStream) { 0164 // require_once 'Zend/Mime/Exception.php'; 0165 throw new Zend_Mime_Exception( 0166 'Attempt to get a stream from a string part' 0167 ); 0168 } 0169 0170 //stream_filter_remove(); // ??? is that right? 0171 switch ($this->encoding) { 0172 case Zend_Mime::ENCODING_QUOTEDPRINTABLE: 0173 $filter = stream_filter_append( 0174 $this->_content, 0175 'convert.quoted-printable-encode', 0176 STREAM_FILTER_READ, 0177 array( 0178 'line-length' => 76, 0179 'line-break-chars' => Zend_Mime::LINEEND 0180 ) 0181 ); 0182 if (!is_resource($filter)) { 0183 // require_once 'Zend/Mime/Exception.php'; 0184 throw new Zend_Mime_Exception( 0185 'Failed to append quoted-printable filter' 0186 ); 0187 } 0188 break; 0189 0190 case Zend_Mime::ENCODING_BASE64: 0191 $filter = stream_filter_append( 0192 $this->_content, 0193 'convert.base64-encode', 0194 STREAM_FILTER_READ, 0195 array( 0196 'line-length' => 76, 0197 'line-break-chars' => Zend_Mime::LINEEND 0198 ) 0199 ); 0200 if (!is_resource($filter)) { 0201 // require_once 'Zend/Mime/Exception.php'; 0202 throw new Zend_Mime_Exception( 0203 'Failed to append base64 filter' 0204 ); 0205 } 0206 break; 0207 0208 default: 0209 } 0210 0211 return $this->_content; 0212 } 0213 0214 /** 0215 * Get the Content of the current Mime Part in the given encoding. 0216 * 0217 * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND} 0218 * @throws Zend_Mime_Exception 0219 * @return string 0220 */ 0221 public function getContent($EOL = Zend_Mime::LINEEND) 0222 { 0223 if ($this->_isStream) { 0224 return stream_get_contents($this->getEncodedStream()); 0225 } else { 0226 return Zend_Mime::encode($this->_content, $this->encoding, $EOL); 0227 } 0228 } 0229 0230 /** 0231 * Get the RAW unencoded content from this part 0232 * 0233 * @return string 0234 */ 0235 public function getRawContent() 0236 { 0237 if ($this->_isStream) { 0238 return stream_get_contents($this->_content); 0239 } else { 0240 return $this->_content; 0241 } 0242 } 0243 0244 /** 0245 * Create and return the array of headers for this MIME part 0246 * 0247 * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND} 0248 * @return array 0249 */ 0250 public function getHeadersArray($EOL = Zend_Mime::LINEEND) 0251 { 0252 $headers = array(); 0253 0254 $contentType = $this->type; 0255 if ($this->charset) { 0256 $contentType .= '; charset=' . $this->charset; 0257 } 0258 0259 if ($this->boundary) { 0260 $contentType .= ';' . $EOL 0261 . " boundary=\"" . $this->boundary . '"'; 0262 } 0263 0264 $headers[] = array( 0265 'Content-Type', 0266 $contentType 0267 ); 0268 0269 if ($this->encoding) { 0270 $headers[] = array( 0271 'Content-Transfer-Encoding', 0272 $this->encoding 0273 ); 0274 } 0275 0276 if ($this->id) { 0277 $headers[] = array( 0278 'Content-ID', 0279 '<' . $this->id . '>' 0280 ); 0281 } 0282 0283 if ($this->disposition) { 0284 $disposition = $this->disposition; 0285 if ($this->filename) { 0286 $disposition .= '; filename="' . $this->filename . '"'; 0287 } 0288 $headers[] = array( 0289 'Content-Disposition', 0290 $disposition 0291 ); 0292 } 0293 0294 if ($this->description) { 0295 $headers[] = array( 0296 'Content-Description', 0297 $this->description 0298 ); 0299 } 0300 0301 if ($this->location) { 0302 $headers[] = array( 0303 'Content-Location', 0304 $this->location 0305 ); 0306 } 0307 0308 if ($this->language) { 0309 $headers[] = array( 0310 'Content-Language', 0311 $this->language 0312 ); 0313 } 0314 0315 return $headers; 0316 } 0317 0318 /** 0319 * Return the headers for this part as a string 0320 * 0321 * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND} 0322 * @return string 0323 */ 0324 public function getHeaders($EOL = Zend_Mime::LINEEND) 0325 { 0326 $res = ''; 0327 foreach ($this->getHeadersArray($EOL) as $header) { 0328 $res .= $header[0] . ': ' . $header[1] . $EOL; 0329 } 0330 0331 return $res; 0332 } 0333 }