commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / ezc / Mail / src / transports / mbox / mbox_set.php
1 <?php
2 /**
3 * File containing the ezcMailMboxSet class
4 *
5 * @package Mail
6 * @version 1.7beta1
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
9 */
10
11 /**
12 * ezcMailMboxSet is an internal class that fetches a series of mail
13 * from an mbox file.
14 *
15 * The mbox set is constructed from a file pointer and iterates over all the
16 * messages in an mbox file.
17 *
18 * @package Mail
19 * @version 1.7beta1
20 */
21 class ezcMailMboxSet implements ezcMailParserSet
22 {
23 /**
24 * Holds the filepointer to the mbox
25 *
26 * @var resource(filepointer)
27 */
28 private $fh;
29
30 /**
31 * This variable is true if there is more data in the mail that is being fetched.
32 *
33 * It is false if there is no mail being fetched currently or if all the data of the current mail
34 * has been fetched.
35 *
36 * @var bool
37 */
38 private $hasMoreMailData = false;
39
40 /**
41 * Records whether we initialized the mbox or not
42 *
43 * @var bool
44 */
45 private $initialized = false;
46
47 /**
48 * Holds the current message positions.
49 *
50 * @var array(int=>int)
51 */
52 private $messagePositions = array();
53
54 /**
55 * Holds the current message position in array $messagePositions.
56 *
57 * @var int
58 */
59 private $currentMesssagePosition = 0;
60
61 /**
62 * Constructs a new mbox parser set.
63 *
64 * @throws ezcBaseFileIoException
65 * if $fh is not a filepointer resource.
66 * @param resource(filepointer) $fh
67 * @param array(int=>int) $messages
68 */
69 public function __construct( $fh, array $messages )
70 {
71 if ( !is_resource( $fh ) || get_resource_type( $fh ) != 'stream' )
72 {
73 throw new ezcBaseFileIoException( 'filepointer', ezcBaseFileException::READ, "The passed filepointer is not a stream resource." );
74 }
75 $this->fh = $fh;
76 $this->initialized = false;
77 $this->hasMoreMailData = true;
78 $this->messagePositions = $messages;
79 $this->currentMessagePosition = 0;
80 }
81
82 /**
83 * Returns true if all the data has been fetched from this set.
84 *
85 * @return bool
86 */
87 public function isFinished()
88 {
89 return feof( $this->fh ) ? true : false;
90 }
91
92 /**
93 * Returns one line of data from the current mail in the set
94 * including the ending linebreak.
95 *
96 * Null is returned if there is no current mail in the set or
97 * the end of the mail is reached.
98 *
99 * @return string
100 */
101 public function getNextLine()
102 {
103 if ( $this->currentMessagePosition === 0 )
104 {
105 $this->nextMail();
106 }
107 if ( $this->hasMoreMailData )
108 {
109 $data = fgets( $this->fh );
110 if ( feof( $this->fh ) || substr( $data, 0, 5 ) === "From " )
111 {
112 $this->hasMoreMailData = false;
113
114 return null;
115 }
116 return $data;
117 }
118 return null;
119 }
120
121 /**
122 * Returns whether the set contains mails.
123 *
124 * @return bool
125 */
126 public function hasData()
127 {
128 return ( $this->hasMoreMailData === true && count( $this->messagePositions ) > 0 );
129 }
130
131 /**
132 * Moves the set to the next mail and returns true upon success.
133 *
134 * False is returned if there are no more mail in the set.
135 *
136 * @return bool
137 */
138 public function nextMail()
139 {
140 // seek to next message if available
141 if ( $this->currentMessagePosition > count( $this->messagePositions ) - 1 )
142 {
143 $this->hasMoreMailData = false;
144 return false;
145 }
146 fseek( $this->fh, $this->messagePositions[$this->currentMessagePosition] );
147 $this->currentMessagePosition++;
148 $this->hasMoreMailData = true;
149
150 return true;
151 }
152
153 /**
154 * Returns message numbers for current set.
155 *
156 * @return array(int=>int)
157 */
158 public function getMessageNumbers()
159 {
160 return array_keys( $this->messagePositions );
161 }
162 }
163 ?>