3 * File containing the ezcMailMboxTransport class
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
12 * ezcMailMboxTransport implements mail retrieval from an mbox file.
14 * The mbox set is constructed from a file pointer and iterates over all the
15 * messages in an mbox file.
21 class ezcMailMboxTransport
24 * Holds the filepointer to the mbox
26 * @var resource(filepointer)
31 * Constructs the ezcMailMboxTransport object
33 * Opens the mbox $fileName.
35 * @throws ezcBaseFileNotFoundException
36 * if the mbox file could not be found.
37 * @throws ezcBaseFilePermissionException
38 * if the mbox file could be opened for reading.
39 * @param string $fileName
41 public function __construct( $fileName )
43 if ( !file_exists( $fileName ) )
45 throw new ezcBaseFileNotFoundException( $fileName, 'mbox' );
47 if ( !is_readable( $fileName ) )
49 throw new ezcBaseFilePermissionException( $fileName, ezcBaseFileException
::READ
);
51 $this->fh
= fopen( $fileName, 'rt' );
55 * Finds the position of the first message while skipping a possible header.
57 * Mbox files can contain a header which does not describe an email
58 * message. This method skips over this optional header by checking for a
59 * specific From MAILER-DAEMON header.
63 private function findFirstMessage()
65 $data = fgets( $this->fh
);
66 fseek( $this->fh
, 0 );
67 if ( substr( $data, 0, 18 ) === 'From MAILER-DAEMON' )
69 return $this->findNextMessage();
78 * Reads through the Mbox file and stops at the next message.
80 * Messages in Mbox files are separated with lines starting with "From "
81 * and this function reads to the next "From " marker. It then returns the
82 * current posistion in the file. If EOF is detected during reading the
83 * function returns false instead.
87 private function findNextMessage()
91 $data = fgets( $this->fh
);
92 } while ( !feof( $this->fh
) && substr( $data, 0, 5 ) !== "From " );
94 if ( feof( $this->fh
) )
98 return ftell( $this->fh
);
102 * This function reads through the whole mbox and returns starting positions of the messages.
104 * @return array(int=>int)
106 public function listMessages()
109 fseek( $this->fh
, 0 );
110 // Skip the first mail as this is the mbox header
111 $position = $this->findFirstMessage();
112 if ( $position === false )
116 // Continue reading through the rest of the mbox
119 $position = $this->findNextMessage();
120 if ( $position !== false )
122 $messages[] = $position;
124 } while ( $position !== false );
130 * Returns an ezcMailMboxSet containing all the messages in the mbox.
132 * @return ezcMailMboxSet
134 public function fetchAll()
136 $messages = $this->listMessages();
137 return new ezcMailMboxSet( $this->fh
, $messages );
141 * Returns an ezcMailMboxSet containing only the $number -th message in the mbox.
143 * @throws ezcMailNoSuchMessageException
144 * if the message $number is out of range.
146 * @return ezcMailMboxSet
148 public function fetchByMessageNr( $number )
150 $messages = $this->listMessages();
151 if ( !isset( $messages[$number] ) )
153 throw new ezcMailNoSuchMessageException( $number );
155 return new ezcMailMboxSet( $this->fh
, array( 0 => $messages[$number] ) );
159 * Returns an ezcMailMboxSet with $count messages starting from $offset.
161 * Fetches $count messages starting from the $offset and returns them as a
162 * ezcMailMboxSet. If $count is not specified or if it is 0, it fetches
163 * all messages starting from the $offset.
165 * @throws ezcMailInvalidLimitException
166 * if $count is negative.
167 * @throws ezcMailOffsetOutOfRangeException
168 * if $offset is outside of the existing range of messages.
171 * @return ezcMailMboxSet
173 public function fetchFromOffset( $offset, $count = 0 )
177 throw new ezcMailInvalidLimitException( $offset, $count );
179 $messages = $this->listMessages();
180 if ( !isset( $messages[$offset] ) )
182 throw new ezcMailOffsetOutOfRangeException( $offset, $count );
186 $range = array_slice( $messages, $offset );
190 $range = array_slice( $messages, $offset, $count );
192 return new ezcMailMboxSet( $this->fh
, $range );