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.
18
* @copyright Copyright (c) 2005-2008 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 12519 2008-11-10 18:41:24Z alexander $
26
* May be used in constructor, but commented out for now
28
// require_once 'Zend/Loader.php';
31
* @see Zend_Mail_Storage_Abstract
33
require_once 'Zend/Mail/Storage/Abstract.php';
36
* @see Zend_Mail_Message_File
38
require_once 'Zend/Mail/Message/File.php';
45
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
46
* @license http://framework.zend.com/license/new-bsd New BSD License
48
class Zend_Mail_Storage_Mbox extends Zend_Mail_Storage_Abstract
51
* file handle to mbox file
57
* filename of mbox file for __wakeup
63
* modification date of mbox file for __wakeup
66
protected $_filemtime;
69
* start and end position of messages as array('start' => start, 'seperator' => headersep, 'end' => end)
72
protected $_positions;
75
* used message class, change it in an extened class to extend the returned message class
78
protected $_messageClass = 'Zend_Mail_Message_File';
81
* Count messages all messages in current box
83
* @return int number of messages
84
* @throws Zend_Mail_Storage_Exception
86
public function countMessages()
88
return count($this->_positions);
93
* Get a list of messages with number and size
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)
98
public function getSize($id = 0)
101
$pos = $this->_positions[$id - 1];
102
return $pos['end'] - $pos['start'];
106
foreach ($this->_positions as $num => $pos) {
107
$result[$num + 1] = $pos['end'] - $pos['start'];
115
* Get positions for mail message or throw exeption if id is invalid
117
* @param int $id number of message
118
* @return array positions as in _positions
119
* @throws Zend_Mail_Storage_Exception
121
protected function _getPos($id)
123
if (!isset($this->_positions[$id - 1])) {
125
* @see Zend_Mail_Storage_Exception
127
require_once 'Zend/Mail/Storage/Exception.php';
128
throw new Zend_Mail_Storage_Exception('id does not exist');
131
return $this->_positions[$id - 1];
138
* @param int $id number of message
139
* @return Zend_Mail_Message_File
140
* @throws Zend_Mail_Storage_Exception
142
public function getMessage($id)
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']));
152
$bodyLines = 0; // TODO: need a way to change that
154
$message = $this->getRawHeader($id);
155
// file pointer is after headers now
158
while ($bodyLines-- && ftell($this->_fh) < $this->_positions[$id - 1]['end']) {
159
$message .= fgets($this->_fh);
163
return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message));
167
* Get raw header of message or part
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
176
public function getRawHeader($id, $part = null, $topLines = 0)
178
if ($part !== null) {
181
* @see Zend_Mail_Storage_Exception
183
require_once 'Zend/Mail/Storage/Exception.php';
184
throw new Zend_Mail_Storage_Exception('not implemented');
186
$messagePos = $this->_getPos($id);
188
return stream_get_contents($this->_fh, $messagePos['separator'] - $messagePos['start'], $messagePos['start']);
192
* Get raw content of message or part
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
200
public function getRawContent($id, $part = null)
202
if ($part !== null) {
205
* @see Zend_Mail_Storage_Exception
207
require_once 'Zend/Mail/Storage/Exception.php';
208
throw new Zend_Mail_Storage_Exception('not implemented');
210
$messagePos = $this->_getPos($id);
211
return stream_get_contents($this->_fh, $messagePos['end'] - $messagePos['separator'], $messagePos['separator']);
215
* Create instance with parameters
216
* Supported parameters are:
217
* - filename filename of mbox file
219
* @param $params array mail reader specific parameters
220
* @throws Zend_Mail_Storage_Exception
222
public function __construct($params)
224
if (is_array($params)) {
225
$params = (object)$params;
228
if (!isset($params->filename) /* || Zend_Loader::isReadable($params['filename']) */) {
230
* @see Zend_Mail_Storage_Exception
232
require_once 'Zend/Mail/Storage/Exception.php';
233
throw new Zend_Mail_Storage_Exception('no valid filename given in params');
236
$this->_openMboxFile($params->filename);
237
$this->_has['top'] = true;
238
$this->_has['uniqueid'] = false;
242
* check if given file is a mbox file
244
* if $file is a resource its file pointer is moved after the first line
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
250
protected function _isMboxFile($file, $fileIsString = true)
253
$file = @fopen($file, 'r');
263
$line = fgets($file);
264
if (strpos($line, 'From ') === 0) {
276
* open given file as current mbox file
278
* @param string $filename filename of mbox file
280
* @throws Zend_Mail_Storage_Exception
282
protected function _openMboxFile($filename)
288
$this->_fh = @fopen($filename, 'r');
291
* @see Zend_Mail_Storage_Exception
293
require_once 'Zend/Mail/Storage/Exception.php';
294
throw new Zend_Mail_Storage_Exception('cannot open mbox file');
296
$this->_filename = $filename;
297
$this->_filemtime = filemtime($this->_filename);
299
if (!$this->_isMboxFile($this->_fh, false)) {
302
* @see Zend_Mail_Storage_Exception
304
require_once 'Zend/Mail/Storage/Exception.php';
305
throw new Zend_Mail_Storage_Exception('file is not a valid mbox format');
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'];
315
$this->_positions[] = $messagePos;
316
$messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0);
318
if (!$messagePos['separator'] && !trim($line)) {
319
$messagePos['separator'] = ftell($this->_fh);
323
$messagePos['end'] = ftell($this->_fh);
324
if (!$messagePos['separator']) {
325
$messagePos['separator'] = $messagePos['end'];
327
$this->_positions[] = $messagePos;
331
* Close resource for mail lib. If you need to control, when the resource
332
* is closed. Otherwise the destructor would call this.
336
public function close()
339
$this->_positions = array();
344
* Waste some CPU cycles doing nothing.
348
public function noop()
355
* stub for not supported message deletion
358
* @throws Zend_Mail_Storage_Exception
360
public function removeMessage($id)
363
* @see Zend_Mail_Storage_Exception
365
require_once 'Zend/Mail/Storage/Exception.php';
366
throw new Zend_Mail_Storage_Exception('mbox is read-only');
370
* get unique id for one or all messages
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.
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
380
public function getUniqueId($id = null)
383
// check if id exists
388
$range = range(1, $this->countMessages());
389
return array_combine($range, $range);
393
* get a message number from a unique id
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()
398
* @param string $id unique id
399
* @return int message number
400
* @throws Zend_Mail_Storage_Exception
402
public function getNumberByUniqueId($id)
404
// check if id exists
410
* magic method for serialize()
412
* with this method you can cache the mbox class
414
* @return array name of variables
416
public function __sleep()
418
return array('_filename', '_positions', '_filemtime');
422
* magic method for unserialize()
424
* with this method you can cache the mbox class
425
* for cache validation the mtime of the mbox file is used
428
* @throws Zend_Mail_Storage_Exception
430
public function __wakeup()
432
if ($this->_filemtime != @filemtime($this->_filename)) {
434
$this->_openMboxFile($this->_filename);
436
$this->_fh = @fopen($this->_filename, 'r');
439
* @see Zend_Mail_Storage_Exception
441
require_once 'Zend/Mail/Storage/Exception.php';
442
throw new Zend_Mail_Storage_Exception('cannot open mbox file');