ab02e8a9 |
1 | <?php |
2 | /******************************************************************************** |
3 | * banana/protocoleinterface.inc.php : interface for box access |
4 | * ------------------------ |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
11 | require_once dirname(__FILE__) . '/protocoleinterface.inc.php'; |
12 | require_once dirname(__FILE__) . '/message.inc.php'; |
13 | |
14 | class BananaMBox implements BananaProtocoleInterface |
15 | { |
ab02e8a9 |
16 | private $file = null; |
17 | private $filesize = null; |
18 | private $current_id = null; |
19 | private $at_beginning = false; |
20 | private $file_cache = null; |
21 | |
22 | private $_lasterrno = 0; |
23 | private $_lasterror = null; |
24 | |
25 | private $count = null; |
26 | private $new_messages = null; |
27 | private $messages = null; |
28 | |
29 | /** Build a protocole handler plugged on the given box |
30 | */ |
916e4a56 |
31 | public function __construct() |
ab02e8a9 |
32 | { |
916e4a56 |
33 | $filename = $this->getFileName(Banana::$group); |
ab02e8a9 |
34 | if (is_null($filename)) { |
35 | return; |
36 | } |
37 | $this->filesize = filesize($filename); |
38 | $this->file = @fopen($filename, 'r'); |
39 | if (!$this->file) { |
40 | $this->_lasterrno = 1; |
41 | $this->_lasterror = _b_('Can\'t open file'); |
42 | $this->file = null; |
43 | } |
44 | $this->current_id = 0; |
45 | $this->at_beginning = true; |
46 | } |
47 | |
48 | /** Close the file |
49 | */ |
50 | public function __destruct() |
51 | { |
52 | if ($this->file) { |
53 | fclose($this->file); |
54 | } |
55 | } |
56 | |
57 | /** Indicate if the Protocole handler has been succesfully built |
58 | */ |
59 | public function isValid() |
60 | { |
916e4a56 |
61 | return !Banana::$group || $this->file; |
ab02e8a9 |
62 | } |
63 | |
d8d416c4 |
64 | /** Indicate last error n° |
ab02e8a9 |
65 | */ |
66 | public function lastErrNo() |
67 | { |
68 | return $this->_lasterrno;; |
69 | } |
70 | |
71 | /** Indicate last error text |
72 | */ |
73 | public function lastError() |
74 | { |
75 | return $this->_lasterror; |
76 | } |
77 | |
78 | /** Return the description of the current box |
79 | */ |
80 | public function getDescription() |
81 | { |
82 | return null; |
83 | } |
84 | |
85 | /** Return the list of the boxes |
86 | * @param mode Kind of boxes to list |
87 | * @param since date of last check (for new boxes and new messages) |
88 | * @param withstats Indicated whether msgnum and unread must be set in the result |
89 | * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages) |
90 | */ |
91 | public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false) |
92 | { |
916e4a56 |
93 | return array(Banana::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0)); |
ab02e8a9 |
94 | } |
95 | |
96 | /** Return a message |
97 | * @param id Id of the emssage (can be either an Message-id or a message index) |
ab02e8a9 |
98 | * @return A BananaMessage or null if the given id can't be retreived |
99 | */ |
9bc195d6 |
100 | public function &getMessage($id) |
ab02e8a9 |
101 | { |
b188e462 |
102 | $message = null; |
9bc195d6 |
103 | if (!is_numeric($id)) { |
104 | if (!Banana::$spool) { |
b188e462 |
105 | return $message; |
ab02e8a9 |
106 | } |
107 | $id = Banana::$spool->ids[$id]; |
108 | } |
109 | $message = $this->readMessages(array($id)); |
110 | if (empty($message)) { |
b188e462 |
111 | $message = null; |
112 | return $message; |
ab02e8a9 |
113 | } |
bf791d69 |
114 | $message = new BananaMessage($message[$id]['message']); |
115 | return $message; |
ab02e8a9 |
116 | } |
117 | |
9bc195d6 |
118 | /** Return the sources of the given message |
119 | */ |
120 | public function getMessageSource($id) |
121 | { |
b188e462 |
122 | $message = null; |
9bc195d6 |
123 | if (!is_numeric($id)) { |
124 | if (!Banana::$spool) { |
b188e462 |
125 | return $message; |
9bc195d6 |
126 | } |
127 | $id = Banana::$spool->ids[$id]; |
b188e462 |
128 | } |
9bc195d6 |
129 | $message = $this->readMessages(array($id)); |
b188e462 |
130 | return implode("\n", $message[$id]['message']); |
9bc195d6 |
131 | } |
132 | |
133 | /** Compute the number of messages of the box |
134 | */ |
ab02e8a9 |
135 | private function getCount() |
136 | { |
137 | $this->count = count(Banana::$spool->overview); |
138 | $max = @max(array_keys(Banana::$spool->overview)); |
139 | if ($max && Banana::$spool->overview[$max]->storage['next'] == $this->filesize) { |
140 | $this->new_messages = 0; |
141 | } else { |
142 | $this->new_messages = $this->countMessages($this->count); |
143 | $this->count += $this->new_messages; |
144 | } |
145 | } |
146 | |
147 | /** Return the indexes of the messages presents in the Box |
148 | * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message) |
149 | */ |
150 | public function getIndexes() |
151 | { |
152 | if (is_null($this->count)) { |
153 | $this->getCount(); |
154 | } |
155 | return array($this->count, 0, $this->count - 1); |
156 | } |
157 | |
158 | /** Return the message headers (in BananaMessage) for messages from firstid to lastid |
159 | * @return Array(id => array(headername => headervalue)) |
160 | */ |
161 | public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array()) |
162 | { |
163 | $msg_headers = array_map('strtolower', $msg_headers); |
164 | $messages =& $this->readMessages(range($firstid, $lastid), true); |
165 | $msg_headers = array_map('strtolower', $msg_headers); |
166 | $headers = array(); |
167 | foreach ($msg_headers as $header) { |
168 | foreach ($messages as $id=>&$message) { |
169 | if (!isset($headers[$id])) { |
170 | $headers[$id] = array('beginning' => $message['beginning'], 'end' => $message['end']); |
171 | } |
172 | if ($header == 'date') { |
9bc195d6 |
173 | $headers[$id][$header] = @strtotime($message['message'][$header]); |
ab02e8a9 |
174 | } else { |
9bc195d6 |
175 | $headers[$id][$header] = @$message['message'][$header]; |
ab02e8a9 |
176 | } |
177 | } |
178 | } |
179 | unset($this->messages); |
180 | unset($messages); |
181 | return $headers; |
182 | } |
183 | |
184 | /** Add storage data in spool overview |
185 | */ |
186 | public function updateSpool(array &$messages) |
187 | { |
188 | foreach ($messages as $id=>&$data) { |
189 | if (isset(Banana::$spool->overview[$id])) { |
190 | Banana::$spool->overview[$id]->storage['offset'] = $data['beginning']; |
191 | Banana::$spool->overview[$id]->storage['next'] = $data['end']; |
192 | } |
193 | } |
194 | } |
195 | |
196 | /** Return the indexes of the new messages since the give date |
197 | * @return Array(MSGNUM of new messages) |
198 | */ |
199 | public function getNewIndexes($since) |
200 | { |
201 | if (is_null($this->new_messages)) { |
202 | $this->getCount(); |
203 | } |
204 | return range($this->count - $this->new_messages, $this->count - 1); |
205 | } |
206 | |
207 | /** Return wether or not the protocole can be used to add new messages |
208 | */ |
209 | public function canSend() |
210 | { |
211 | return true; |
212 | } |
213 | |
214 | /** Return false because we can't cancel a mail |
215 | */ |
216 | public function canCancel() |
217 | { |
218 | return false; |
219 | } |
220 | |
221 | /** Return the list of requested headers |
222 | * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header |
223 | * and 'reply' for the reply header, eg: |
224 | * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To') |
225 | * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To') |
226 | */ |
227 | public function requestedHeaders() |
228 | { |
229 | return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To'); |
230 | } |
231 | |
232 | /** Send a message |
233 | * @return true if it was successfull |
234 | */ |
235 | public function send(BananaMessage &$message) |
236 | { |
237 | return true; |
238 | } |
239 | |
240 | /** Cancel a message |
241 | * @return true if it was successfull |
242 | */ |
243 | public function cancel(BananaMessage &$message) |
244 | { |
245 | return false; |
246 | } |
247 | |
248 | /** Return the protocole name |
249 | */ |
250 | public function name() |
251 | { |
252 | return 'MBOX'; |
253 | } |
254 | |
7972645b |
255 | /** Return the spool filename |
256 | */ |
257 | public function filename() |
258 | { |
259 | @list($mail, $domain) = explode('@', Banana::$group); |
260 | $file = ""; |
261 | if (isset($domain)) { |
262 | $file = $domain . '_'; |
263 | } |
264 | return $file . $mail; |
265 | } |
266 | |
ab02e8a9 |
267 | ####### |
268 | # Filesystem functions |
269 | ####### |
270 | |
271 | protected function getFileName($box) |
272 | { |
273 | if (is_null($box)) { |
274 | return null; |
275 | } |
276 | @list($mail, $domain) = explode('@', $box); |
7972645b |
277 | return Banana::$mbox_path . '/' . $mail; |
ab02e8a9 |
278 | } |
279 | |
280 | ####### |
281 | # MBox parser |
282 | ####### |
283 | |
284 | /** Go to the given message |
285 | */ |
286 | private function goTo($id) |
287 | { |
288 | if ($this->current_id == $id && $this->at_beginning) { |
289 | return true; |
290 | } |
291 | if ($id == 0) { |
292 | fseek($this->file, 0); |
293 | $this->current_id = 0; |
294 | $this->at_beginning = true; |
295 | return true; |
296 | } elseif (isset(Banana::$spool->overview[$id]) || isset($this->messages[$id])) { |
297 | if (isset(Banana::$spool->overview[$id])) { |
298 | $pos = Banana::$spool->overview[$id]->storage['offset']; |
299 | } else { |
300 | $pos = $this->messages[$id]['beginning']; |
301 | } |
302 | if (fseek($this->file, $pos) == 0) { |
303 | $this->current_id = $id; |
304 | $this->at_beginning = true; |
305 | return true; |
306 | } else { |
307 | $this->current_id = null; |
308 | $this->_lasterrno = 2; |
309 | $this->_lasterror = _b_('Can\'t find message ') . $id; |
310 | return false; |
311 | } |
312 | } else { |
313 | $max = @max(array_keys(Banana::$spool->overview)); |
314 | if (is_null($max)) { |
315 | $max = 0; |
316 | } |
317 | if ($id <= $max && $max != 0) { |
318 | $this->current_id = null; |
319 | $this->_lasterrno = 3; |
320 | $this->_lasterror = _b_('Invalid message index ') . $id; |
321 | return false; |
322 | } |
323 | if (!$this->goTo($max)) { |
324 | return false; |
325 | } |
326 | if (feof($this->file)) { |
327 | $this->current_id = null; |
328 | $this->_lasterrno = 4; |
329 | $this->_lasterror = _b_('Requested index does not exists or file has been truncated'); |
330 | return false; |
331 | } |
332 | while ($this->readCurrentMessage(true) && $this->current_id < $id); |
333 | if ($this->current_id == $id) { |
334 | return true; |
335 | } |
336 | $this->current_id = null; |
337 | $this->_lasterrno = 5; |
338 | $this->_lasterror = _b_('Requested index does not exists or file has been truncated'); |
339 | return false; |
340 | } |
341 | } |
342 | |
343 | private function countMessages($from = 0) |
344 | { |
345 | $this->messages =& $this->readMessages(array($from), true, true); |
346 | return count($this->messages); |
347 | } |
348 | |
349 | /** Read the current message (identified by current_id) |
350 | * @param needFrom_ BOOLEAN is true if the first line *must* be a From_ line |
351 | * @param alignNext BOOLEAN is true if the buffer must be aligned at the beginning of the next From_ line |
352 | * @return message sources (without storage data) |
353 | */ |
354 | private function &readCurrentMessage($stripBody = false, $needFrom_ = true, $alignNext = true) |
355 | { |
356 | $file_cache =& $this->file_cache; |
357 | if ($file_cache && $file_cache != ftell($this->file)) { |
358 | $file_cache = null; |
359 | } |
360 | $msg = array(); |
361 | $canFrom_ = false; |
362 | $inBody = false; |
363 | while(!feof($this->file)) { |
364 | // Process file cache |
365 | if ($file_cache) { // this is a From_ line |
366 | $needFrom_ = false; |
367 | $this->at_beginning = false; |
368 | $file_cache = null; |
369 | continue; |
370 | } |
371 | |
372 | // Read a line |
373 | $line = rtrim(fgets($this->file), "\r\n"); |
374 | |
375 | // Process From_ line |
376 | if ($needFrom_ || !$msg || $canFrom_) { |
377 | if (substr($line, 0, 5) == 'From ') { // this is a From_ line |
378 | if ($needFrom_) { |
379 | $needFrom = false; |
380 | } elseif (!$msg) { |
381 | continue; |
382 | } else { |
383 | $this->current_id++; // we are finally in the next message |
384 | if ($alignNext) { // align the file pointer at the beginning of the new message |
385 | $this->at_beginning = true; |
386 | $file_cache = ftell($this->file); |
387 | } |
388 | break; |
389 | } |
390 | } elseif ($needFrom_) { |
391 | return $msg; |
392 | } |
393 | } |
394 | |
395 | // Process non-From_ lines |
396 | if (substr($line, 0, 6) == '>From ') { // remove inline From_ quotation |
397 | $line = substr($line, 1); |
398 | } |
399 | if (!$stripBody || !$inBody) { |
400 | $msg[] = $line; // add the line to the message source |
401 | } |
402 | $canFrom_ = empty($line); // check if next line can be a From_ line |
403 | if ($canFrom_ && !$inBody && $stripBody) { |
404 | $inBody = true; |
405 | } |
406 | $this->at_beginning = false; |
407 | } |
408 | if (!feof($this->file) && !$canFrom_) { |
409 | $msg = array(); |
410 | } |
411 | return $msg; |
412 | } |
413 | |
414 | /** Read message with the given ids |
415 | * @param ids ARRAY of ids to look for |
416 | * @param strip BOOLEAN if true, only headers are retrieved |
417 | * @param from BOOLEAN if true, process all messages from max(ids) to the end of the mbox |
418 | * @return Array(Array('message' => message sources (or parsed message headers if $strip is true), |
419 | * 'beginning' => offset of message beginning, |
420 | * 'end' => offset of message end)) |
421 | */ |
422 | private function &readMessages(array $ids, $strip = false, $from = false) |
423 | { |
9bc195d6 |
424 | if ($this->messages) { |
ab02e8a9 |
425 | return $this->messages; |
426 | } |
427 | sort($ids); |
428 | $messages = array(); |
429 | while ((count($ids) || $from) && !feof($this->file)) { |
430 | if (count($ids)) { |
431 | $id = array_shift($ids); |
432 | } else { |
433 | $id++; |
434 | } |
435 | if ($id != $this->current_id || !$this->at_beginning) { |
436 | if (!$this->goTo($id)) { |
437 | continue; |
438 | } |
439 | } |
440 | $beginning = ftell($this->file); |
441 | $message =& $this->readCurrentMessage($strip, false); |
442 | if ($strip) { |
443 | $message =& BananaMimePart::parseHeaders($message); |
444 | } |
445 | $end = ftell($this->file); |
446 | $messages[$id] = array('message' => $message, 'beginning' => $beginning, 'end' => $end); |
447 | } |
448 | return $messages; |
449 | } |
450 | } |
451 | |
d8d416c4 |
452 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
ab02e8a9 |
453 | ?> |