|
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_Mail |
|
17 * @subpackage Storage |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Mbox.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 |
|
24 /** |
|
25 * @see Zend_Loader |
|
26 * May be used in constructor, but commented out for now |
|
27 */ |
|
28 // require_once 'Zend/Loader.php'; |
|
29 |
|
30 /** |
|
31 * @see Zend_Mail_Storage_Abstract |
|
32 */ |
|
33 require_once 'Zend/Mail/Storage/Abstract.php'; |
|
34 |
|
35 /** |
|
36 * @see Zend_Mail_Message_File |
|
37 */ |
|
38 require_once 'Zend/Mail/Message/File.php'; |
|
39 |
|
40 |
|
41 /** |
|
42 * @category Zend |
|
43 * @package Zend_Mail |
|
44 * @subpackage Storage |
|
45 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
46 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
47 */ |
|
48 class Zend_Mail_Storage_Mbox extends Zend_Mail_Storage_Abstract |
|
49 { |
|
50 /** |
|
51 * file handle to mbox file |
|
52 * @var null|resource |
|
53 */ |
|
54 protected $_fh; |
|
55 |
|
56 /** |
|
57 * filename of mbox file for __wakeup |
|
58 * @var string |
|
59 */ |
|
60 protected $_filename; |
|
61 |
|
62 /** |
|
63 * modification date of mbox file for __wakeup |
|
64 * @var int |
|
65 */ |
|
66 protected $_filemtime; |
|
67 |
|
68 /** |
|
69 * start and end position of messages as array('start' => start, 'seperator' => headersep, 'end' => end) |
|
70 * @var array |
|
71 */ |
|
72 protected $_positions; |
|
73 |
|
74 /** |
|
75 * used message class, change it in an extened class to extend the returned message class |
|
76 * @var string |
|
77 */ |
|
78 protected $_messageClass = 'Zend_Mail_Message_File'; |
|
79 |
|
80 /** |
|
81 * Count messages all messages in current box |
|
82 * |
|
83 * @return int number of messages |
|
84 * @throws Zend_Mail_Storage_Exception |
|
85 */ |
|
86 public function countMessages() |
|
87 { |
|
88 return count($this->_positions); |
|
89 } |
|
90 |
|
91 |
|
92 /** |
|
93 * Get a list of messages with number and size |
|
94 * |
|
95 * @param int|null $id number of message or null for all messages |
|
96 * @return int|array size of given message of list with all messages as array(num => size) |
|
97 */ |
|
98 public function getSize($id = 0) |
|
99 { |
|
100 if ($id) { |
|
101 $pos = $this->_positions[$id - 1]; |
|
102 return $pos['end'] - $pos['start']; |
|
103 } |
|
104 |
|
105 $result = array(); |
|
106 foreach ($this->_positions as $num => $pos) { |
|
107 $result[$num + 1] = $pos['end'] - $pos['start']; |
|
108 } |
|
109 |
|
110 return $result; |
|
111 } |
|
112 |
|
113 |
|
114 /** |
|
115 * Get positions for mail message or throw exeption if id is invalid |
|
116 * |
|
117 * @param int $id number of message |
|
118 * @return array positions as in _positions |
|
119 * @throws Zend_Mail_Storage_Exception |
|
120 */ |
|
121 protected function _getPos($id) |
|
122 { |
|
123 if (!isset($this->_positions[$id - 1])) { |
|
124 /** |
|
125 * @see Zend_Mail_Storage_Exception |
|
126 */ |
|
127 require_once 'Zend/Mail/Storage/Exception.php'; |
|
128 throw new Zend_Mail_Storage_Exception('id does not exist'); |
|
129 } |
|
130 |
|
131 return $this->_positions[$id - 1]; |
|
132 } |
|
133 |
|
134 |
|
135 /** |
|
136 * Fetch a message |
|
137 * |
|
138 * @param int $id number of message |
|
139 * @return Zend_Mail_Message_File |
|
140 * @throws Zend_Mail_Storage_Exception |
|
141 */ |
|
142 public function getMessage($id) |
|
143 { |
|
144 // TODO that's ugly, would be better to let the message class decide |
|
145 if (strtolower($this->_messageClass) == 'zend_mail_message_file' || is_subclass_of($this->_messageClass, 'zend_mail_message_file')) { |
|
146 // TODO top/body lines |
|
147 $messagePos = $this->_getPos($id); |
|
148 return new $this->_messageClass(array('file' => $this->_fh, 'startPos' => $messagePos['start'], |
|
149 'endPos' => $messagePos['end'])); |
|
150 } |
|
151 |
|
152 $bodyLines = 0; // TODO: need a way to change that |
|
153 |
|
154 $message = $this->getRawHeader($id); |
|
155 // file pointer is after headers now |
|
156 if ($bodyLines) { |
|
157 $message .= "\n"; |
|
158 while ($bodyLines-- && ftell($this->_fh) < $this->_positions[$id - 1]['end']) { |
|
159 $message .= fgets($this->_fh); |
|
160 } |
|
161 } |
|
162 |
|
163 return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message)); |
|
164 } |
|
165 |
|
166 /* |
|
167 * Get raw header of message or part |
|
168 * |
|
169 * @param int $id number of message |
|
170 * @param null|array|string $part path to part or null for messsage header |
|
171 * @param int $topLines include this many lines with header (after an empty line) |
|
172 * @return string raw header |
|
173 * @throws Zend_Mail_Protocol_Exception |
|
174 * @throws Zend_Mail_Storage_Exception |
|
175 */ |
|
176 public function getRawHeader($id, $part = null, $topLines = 0) |
|
177 { |
|
178 if ($part !== null) { |
|
179 // TODO: implement |
|
180 /** |
|
181 * @see Zend_Mail_Storage_Exception |
|
182 */ |
|
183 require_once 'Zend/Mail/Storage/Exception.php'; |
|
184 throw new Zend_Mail_Storage_Exception('not implemented'); |
|
185 } |
|
186 $messagePos = $this->_getPos($id); |
|
187 // TODO: toplines |
|
188 return stream_get_contents($this->_fh, $messagePos['separator'] - $messagePos['start'], $messagePos['start']); |
|
189 } |
|
190 |
|
191 /* |
|
192 * Get raw content of message or part |
|
193 * |
|
194 * @param int $id number of message |
|
195 * @param null|array|string $part path to part or null for messsage content |
|
196 * @return string raw content |
|
197 * @throws Zend_Mail_Protocol_Exception |
|
198 * @throws Zend_Mail_Storage_Exception |
|
199 */ |
|
200 public function getRawContent($id, $part = null) |
|
201 { |
|
202 if ($part !== null) { |
|
203 // TODO: implement |
|
204 /** |
|
205 * @see Zend_Mail_Storage_Exception |
|
206 */ |
|
207 require_once 'Zend/Mail/Storage/Exception.php'; |
|
208 throw new Zend_Mail_Storage_Exception('not implemented'); |
|
209 } |
|
210 $messagePos = $this->_getPos($id); |
|
211 return stream_get_contents($this->_fh, $messagePos['end'] - $messagePos['separator'], $messagePos['separator']); |
|
212 } |
|
213 |
|
214 /** |
|
215 * Create instance with parameters |
|
216 * Supported parameters are: |
|
217 * - filename filename of mbox file |
|
218 * |
|
219 * @param $params array mail reader specific parameters |
|
220 * @throws Zend_Mail_Storage_Exception |
|
221 */ |
|
222 public function __construct($params) |
|
223 { |
|
224 if (is_array($params)) { |
|
225 $params = (object)$params; |
|
226 } |
|
227 |
|
228 if (!isset($params->filename) /* || Zend_Loader::isReadable($params['filename']) */) { |
|
229 /** |
|
230 * @see Zend_Mail_Storage_Exception |
|
231 */ |
|
232 require_once 'Zend/Mail/Storage/Exception.php'; |
|
233 throw new Zend_Mail_Storage_Exception('no valid filename given in params'); |
|
234 } |
|
235 |
|
236 $this->_openMboxFile($params->filename); |
|
237 $this->_has['top'] = true; |
|
238 $this->_has['uniqueid'] = false; |
|
239 } |
|
240 |
|
241 /** |
|
242 * check if given file is a mbox file |
|
243 * |
|
244 * if $file is a resource its file pointer is moved after the first line |
|
245 * |
|
246 * @param resource|string $file stream resource of name of file |
|
247 * @param bool $fileIsString file is string or resource |
|
248 * @return bool file is mbox file |
|
249 */ |
|
250 protected function _isMboxFile($file, $fileIsString = true) |
|
251 { |
|
252 if ($fileIsString) { |
|
253 $file = @fopen($file, 'r'); |
|
254 if (!$file) { |
|
255 return false; |
|
256 } |
|
257 } else { |
|
258 fseek($file, 0); |
|
259 } |
|
260 |
|
261 $result = false; |
|
262 |
|
263 $line = fgets($file); |
|
264 if (strpos($line, 'From ') === 0) { |
|
265 $result = true; |
|
266 } |
|
267 |
|
268 if ($fileIsString) { |
|
269 @fclose($file); |
|
270 } |
|
271 |
|
272 return $result; |
|
273 } |
|
274 |
|
275 /** |
|
276 * open given file as current mbox file |
|
277 * |
|
278 * @param string $filename filename of mbox file |
|
279 * @return null |
|
280 * @throws Zend_Mail_Storage_Exception |
|
281 */ |
|
282 protected function _openMboxFile($filename) |
|
283 { |
|
284 if ($this->_fh) { |
|
285 $this->close(); |
|
286 } |
|
287 |
|
288 $this->_fh = @fopen($filename, 'r'); |
|
289 if (!$this->_fh) { |
|
290 /** |
|
291 * @see Zend_Mail_Storage_Exception |
|
292 */ |
|
293 require_once 'Zend/Mail/Storage/Exception.php'; |
|
294 throw new Zend_Mail_Storage_Exception('cannot open mbox file'); |
|
295 } |
|
296 $this->_filename = $filename; |
|
297 $this->_filemtime = filemtime($this->_filename); |
|
298 |
|
299 if (!$this->_isMboxFile($this->_fh, false)) { |
|
300 @fclose($this->_fh); |
|
301 /** |
|
302 * @see Zend_Mail_Storage_Exception |
|
303 */ |
|
304 require_once 'Zend/Mail/Storage/Exception.php'; |
|
305 throw new Zend_Mail_Storage_Exception('file is not a valid mbox format'); |
|
306 } |
|
307 |
|
308 $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0); |
|
309 while (($line = fgets($this->_fh)) !== false) { |
|
310 if (strpos($line, 'From ') === 0) { |
|
311 $messagePos['end'] = ftell($this->_fh) - strlen($line) - 2; // + newline |
|
312 if (!$messagePos['separator']) { |
|
313 $messagePos['separator'] = $messagePos['end']; |
|
314 } |
|
315 $this->_positions[] = $messagePos; |
|
316 $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0); |
|
317 } |
|
318 if (!$messagePos['separator'] && !trim($line)) { |
|
319 $messagePos['separator'] = ftell($this->_fh); |
|
320 } |
|
321 } |
|
322 |
|
323 $messagePos['end'] = ftell($this->_fh); |
|
324 if (!$messagePos['separator']) { |
|
325 $messagePos['separator'] = $messagePos['end']; |
|
326 } |
|
327 $this->_positions[] = $messagePos; |
|
328 } |
|
329 |
|
330 /** |
|
331 * Close resource for mail lib. If you need to control, when the resource |
|
332 * is closed. Otherwise the destructor would call this. |
|
333 * |
|
334 * @return void |
|
335 */ |
|
336 public function close() |
|
337 { |
|
338 @fclose($this->_fh); |
|
339 $this->_positions = array(); |
|
340 } |
|
341 |
|
342 |
|
343 /** |
|
344 * Waste some CPU cycles doing nothing. |
|
345 * |
|
346 * @return void |
|
347 */ |
|
348 public function noop() |
|
349 { |
|
350 return true; |
|
351 } |
|
352 |
|
353 |
|
354 /** |
|
355 * stub for not supported message deletion |
|
356 * |
|
357 * @return null |
|
358 * @throws Zend_Mail_Storage_Exception |
|
359 */ |
|
360 public function removeMessage($id) |
|
361 { |
|
362 /** |
|
363 * @see Zend_Mail_Storage_Exception |
|
364 */ |
|
365 require_once 'Zend/Mail/Storage/Exception.php'; |
|
366 throw new Zend_Mail_Storage_Exception('mbox is read-only'); |
|
367 } |
|
368 |
|
369 /** |
|
370 * get unique id for one or all messages |
|
371 * |
|
372 * Mbox does not support unique ids (yet) - it's always the same as the message number. |
|
373 * That shouldn't be a problem, because we can't change mbox files. Therefor the message |
|
374 * number is save enough. |
|
375 * |
|
376 * @param int|null $id message number |
|
377 * @return array|string message number for given message or all messages as array |
|
378 * @throws Zend_Mail_Storage_Exception |
|
379 */ |
|
380 public function getUniqueId($id = null) |
|
381 { |
|
382 if ($id) { |
|
383 // check if id exists |
|
384 $this->_getPos($id); |
|
385 return $id; |
|
386 } |
|
387 |
|
388 $range = range(1, $this->countMessages()); |
|
389 return array_combine($range, $range); |
|
390 } |
|
391 |
|
392 /** |
|
393 * get a message number from a unique id |
|
394 * |
|
395 * I.e. if you have a webmailer that supports deleting messages you should use unique ids |
|
396 * as parameter and use this method to translate it to message number right before calling removeMessage() |
|
397 * |
|
398 * @param string $id unique id |
|
399 * @return int message number |
|
400 * @throws Zend_Mail_Storage_Exception |
|
401 */ |
|
402 public function getNumberByUniqueId($id) |
|
403 { |
|
404 // check if id exists |
|
405 $this->_getPos($id); |
|
406 return $id; |
|
407 } |
|
408 |
|
409 /** |
|
410 * magic method for serialize() |
|
411 * |
|
412 * with this method you can cache the mbox class |
|
413 * |
|
414 * @return array name of variables |
|
415 */ |
|
416 public function __sleep() |
|
417 { |
|
418 return array('_filename', '_positions', '_filemtime'); |
|
419 } |
|
420 |
|
421 /** |
|
422 * magic method for unserialize() |
|
423 * |
|
424 * with this method you can cache the mbox class |
|
425 * for cache validation the mtime of the mbox file is used |
|
426 * |
|
427 * @return null |
|
428 * @throws Zend_Mail_Storage_Exception |
|
429 */ |
|
430 public function __wakeup() |
|
431 { |
|
432 if ($this->_filemtime != @filemtime($this->_filename)) { |
|
433 $this->close(); |
|
434 $this->_openMboxFile($this->_filename); |
|
435 } else { |
|
436 $this->_fh = @fopen($this->_filename, 'r'); |
|
437 if (!$this->_fh) { |
|
438 /** |
|
439 * @see Zend_Mail_Storage_Exception |
|
440 */ |
|
441 require_once 'Zend/Mail/Storage/Exception.php'; |
|
442 throw new Zend_Mail_Storage_Exception('cannot open mbox file'); |
|
443 } |
|
444 } |
|
445 } |
|
446 |
|
447 } |