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