3 * File containing the ezcMailMboxSet 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 * ezcMailMboxSet is an internal class that fetches a series of mail
15 * The mbox set is constructed from a file pointer and iterates over all the
16 * messages in an mbox file.
21 class ezcMailMboxSet
implements ezcMailParserSet
24 * Holds the filepointer to the mbox
26 * @var resource(filepointer)
31 * This variable is true if there is more data in the mail that is being fetched.
33 * It is false if there is no mail being fetched currently or if all the data of the current mail
38 private $hasMoreMailData = false;
41 * Records whether we initialized the mbox or not
45 private $initialized = false;
48 * Holds the current message positions.
50 * @var array(int=>int)
52 private $messagePositions = array();
55 * Holds the current message position in array $messagePositions.
59 private $currentMesssagePosition = 0;
62 * Constructs a new mbox parser set.
64 * @throws ezcBaseFileIoException
65 * if $fh is not a filepointer resource.
66 * @param resource(filepointer) $fh
67 * @param array(int=>int) $messages
69 public function __construct( $fh, array $messages )
71 if ( !is_resource( $fh ) ||
get_resource_type( $fh ) != 'stream' )
73 throw new ezcBaseFileIoException( 'filepointer', ezcBaseFileException
::READ
, "The passed filepointer is not a stream resource." );
76 $this->initialized
= false;
77 $this->hasMoreMailData
= true;
78 $this->messagePositions
= $messages;
79 $this->currentMessagePosition
= 0;
83 * Returns true if all the data has been fetched from this set.
87 public function isFinished()
89 return feof( $this->fh
) ?
true : false;
93 * Returns one line of data from the current mail in the set
94 * including the ending linebreak.
96 * Null is returned if there is no current mail in the set or
97 * the end of the mail is reached.
101 public function getNextLine()
103 if ( $this->currentMessagePosition
=== 0 )
107 if ( $this->hasMoreMailData
)
109 $data = fgets( $this->fh
);
110 if ( feof( $this->fh
) ||
substr( $data, 0, 5 ) === "From " )
112 $this->hasMoreMailData
= false;
122 * Returns whether the set contains mails.
126 public function hasData()
128 return ( $this->hasMoreMailData
=== true && count( $this->messagePositions
) > 0 );
132 * Moves the set to the next mail and returns true upon success.
134 * False is returned if there are no more mail in the set.
138 public function nextMail()
140 // seek to next message if available
141 if ( $this->currentMessagePosition
> count( $this->messagePositions
) - 1 )
143 $this->hasMoreMailData
= false;
146 fseek( $this->fh
, $this->messagePositions
[$this->currentMessagePosition
] );
147 $this->currentMessagePosition++
;
148 $this->hasMoreMailData
= true;
154 * Returns message numbers for current set.
156 * @return array(int=>int)
158 public function getMessageNumbers()
160 return array_keys( $this->messagePositions
);