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 | /** |
---|
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-2010 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 | * Create and return the array of headers for this MIME part |
---|
152 | * |
---|
153 | * @access public |
---|
154 | * @return array |
---|
155 | */ |
---|
156 | public function getHeadersArray($EOL = Zend_Mime::LINEEND) |
---|
157 | { |
---|
158 | $headers = array(); |
---|
159 | |
---|
160 | $contentType = $this->type; |
---|
161 | if ($this->charset) { |
---|
162 | $contentType .= '; charset=' . $this->charset; |
---|
163 | } |
---|
164 | |
---|
165 | if ($this->boundary) { |
---|
166 | $contentType .= ';' . $EOL |
---|
167 | . " boundary=\"" . $this->boundary . '"'; |
---|
168 | } |
---|
169 | |
---|
170 | $headers[] = array('Content-Type', $contentType); |
---|
171 | |
---|
172 | if ($this->encoding) { |
---|
173 | $headers[] = array('Content-Transfer-Encoding', $this->encoding); |
---|
174 | } |
---|
175 | |
---|
176 | if ($this->id) { |
---|
177 | $headers[] = array('Content-ID', '<' . $this->id . '>'); |
---|
178 | } |
---|
179 | |
---|
180 | if ($this->disposition) { |
---|
181 | $disposition = $this->disposition; |
---|
182 | if ($this->filename) { |
---|
183 | $disposition .= '; filename="' . $this->filename . '"'; |
---|
184 | } |
---|
185 | $headers[] = array('Content-Disposition', $disposition); |
---|
186 | } |
---|
187 | |
---|
188 | if ($this->description) { |
---|
189 | $headers[] = array('Content-Description', $this->description); |
---|
190 | } |
---|
191 | |
---|
192 | if ($this->location) { |
---|
193 | $headers[] = array('Content-Location', $this->location); |
---|
194 | } |
---|
195 | |
---|
196 | if ($this->language){ |
---|
197 | $headers[] = array('Content-Language', $this->language); |
---|
198 | } |
---|
199 | |
---|
200 | return $headers; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Return the headers for this part as a string |
---|
205 | * |
---|
206 | * @return String |
---|
207 | */ |
---|
208 | public function getHeaders($EOL = Zend_Mime::LINEEND) |
---|
209 | { |
---|
210 | $res = ''; |
---|
211 | foreach ($this->getHeadersArray($EOL) as $header) { |
---|
212 | $res .= $header[0] . ': ' . $header[1] . $EOL; |
---|
213 | } |
---|
214 | |
---|
215 | return $res; |
---|
216 | } |
---|
217 | } |
---|