2 /********************************************************************************
3 * banana/protocoleinterface.inc.php : interface for box access
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
11 require_once dirname(__FILE__
) . '/protocoleinterface.inc.php';
12 require_once dirname(__FILE__
) . '/message.inc.php';
14 class BananaMBox
implements BananaProtocoleInterface
17 private $filesize = null
;
18 private $current_id = null
;
19 private $at_beginning = false
;
20 private $file_cache = null
;
22 private $_lasterrno = 0;
23 private $_lasterror = null
;
25 private $count = null
;
26 private $new_messages = null
;
27 private $messages = null
;
29 /** Build a protocole handler plugged on the given box
31 public function __construct()
33 $filename = $this->getFileName();
34 if (is_null($filename)) {
37 $this->file
= @fopen
($filename, 'r');
42 $this->filesize
= filesize($filename);
44 $this->current_id
= 0;
45 $this->at_beginning
= true
;
50 public function __destruct()
57 /** Indicate if the Protocole handler has been succesfully built
59 public function isValid()
62 //!Banana::$group || $this->file;
65 /** Indicate last error n°
67 public function lastErrNo()
69 return $this->_lasterrno
;;
72 /** Indicate last error text
74 public function lastError()
76 return $this->_lasterror
;
79 /** Return the description of the current box
81 public function getDescription()
86 /** Return the list of the boxes
87 * @param mode Kind of boxes to list
88 * @param since date of last check (for new boxes and new messages)
89 * @param withstats Indicated whether msgnum and unread must be set in the result
90 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
92 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
94 return array(Banana
::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
98 * @param id Id of the emssage (can be either an Message-id or a message index)
99 * @return A BananaMessage or null if the given id can't be retreived
101 public function &getMessage($id)
104 if (is_null($this->file
)) {
107 if (!is_numeric($id)) {
108 if (!Banana
::$spool) {
111 $id = Banana
::$spool->ids
[$id];
113 $messages = $this->readMessages(array($id));
114 if (!empty($messages)) {
115 $message = new BananaMessage($messages[$id]['message']);
120 /** Return the sources of the given message
122 public function getMessageSource($id)
125 if (is_null($this->file
)) {
128 if (!is_numeric($id)) {
129 if (!Banana
::$spool) {
132 $id = Banana
::$spool->ids
[$id];
134 $message = $this->readMessages(array($id));
135 return implode("\n", $message[$id]['message']);
138 /** Compute the number of messages of the box
140 private function getCount()
142 $this->count
= count(Banana
::$spool->overview
);
143 $max = @max
(array_keys(Banana
::$spool->overview
));
144 if ($max && Banana
::$spool->overview
[$max]->storage
['next'] == $this->filesize
) {
145 $this->new_messages
= 0;
147 $this->new_messages
= $this->countMessages($this->count
);
148 $this->count +
= $this->new_messages
;
152 /** Return the indexes of the messages presents in the Box
153 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
155 public function getIndexes()
157 if (is_null($this->file
)) {
158 return array(0, 0, 0);
160 if (is_null($this->count
)) {
163 return array($this->count
, 0, $this->count
- 1);
166 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
167 * @return Array(id => array(headername => headervalue))
169 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
171 $msg_headers = array_map('strtolower', $msg_headers);
172 $messages =& $this->readMessages(range($firstid, $lastid), true
);
173 $msg_headers = array_map('strtolower', $msg_headers);
175 if (is_null($this->file
)) {
178 foreach ($msg_headers as $header) {
179 foreach ($messages as $id=>&$message) {
180 if (!isset($headers[$id])) {
181 $headers[$id] = array('beginning' => $message['beginning'], 'end' => $message['end']);
183 if ($header == 'date') {
184 $headers[$id][$header] = @strtotime
($message['message'][$header]);
186 $headers[$id][$header] = @$message['message'][$header];
190 unset($this->messages
);
195 /** Add storage data in spool overview
197 public function updateSpool(array &$messages)
199 foreach ($messages as $id=>&$data) {
200 if (isset(Banana
::$spool->overview
[$id])) {
201 Banana
::$spool->overview
[$id]->storage
['offset'] = $data['beginning'];
202 Banana
::$spool->overview
[$id]->storage
['next'] = $data['end'];
207 /** Return the indexes of the new messages since the give date
208 * @return Array(MSGNUM of new messages)
210 public function getNewIndexes($since)
212 if (is_null($this->file
)) {
215 if (is_null($this->new_messages
)) {
218 return range($this->count
- $this->new_messages
, $this->count
- 1);
221 /** Return wether or not the protocole can be used to add new messages
223 public function canSend()
228 /** Return false because we can't cancel a mail
230 public function canCancel()
235 /** Return the list of requested headers
236 * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header
237 * and 'reply' for the reply header, eg:
238 * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To')
239 * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To')
241 public function requestedHeaders()
243 return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To');
247 * @return true if it was successfull
249 public function send(BananaMessage
&$message)
251 $headers = $message->getHeaders();
252 $to = $headers['To'];
253 $subject = $headers['Subject'];
254 unset($headers['To']);
255 unset($headers['Subject']);
257 foreach ($headers as $key=>$value) {
258 if (!empty($value)) {
259 $hdrs .= "$key: $value\r\n";
262 $body = $message->get(false
);
263 return mail($to, $subject, $body, $hdrs);
267 * @return true if it was successfull
269 public function cancel(BananaMessage
&$message)
274 /** Return the protocole name
276 public function name()
281 /** Return the spool filename
283 public function filename()
285 @list
($mail, $domain) = explode('@', Banana
::$group);
287 if (isset($domain)) {
288 $file = $domain . '_';
290 return $file . $mail;
294 # Filesystem functions
297 protected function getFileName()
299 if (is_null(Banana
::$group)) {
302 @list
($mail, $domain) = explode('@', Banana
::$group);
303 return Banana
::$mbox_path . '/' . $mail;
310 /** Go to the given message
312 private function goTo($id)
314 if ($this->current_id
== $id && $this->at_beginning
) {
318 fseek($this->file
, 0);
319 $this->current_id
= 0;
320 $this->at_beginning
= true
;
322 } elseif (isset(Banana
::$spool->overview
[$id]) ||
isset($this->messages
[$id])) {
323 if (isset(Banana
::$spool->overview
[$id])) {
324 $pos = Banana
::$spool->overview
[$id]->storage
['offset'];
326 $pos = $this->messages
[$id]['beginning'];
328 if (fseek($this->file
, $pos) == 0) {
329 $this->current_id
= $id;
330 $this->at_beginning
= true
;
333 $this->current_id
= null
;
334 $this->_lasterrno
= 2;
335 $this->_lasterror
= _b_('Can\'t find message ') . $id;
339 $max = @max
(array_keys(Banana
::$spool->overview
));
343 if ($id <= $max && $max != 0) {
344 $this->current_id
= null
;
345 $this->_lasterrno
= 3;
346 $this->_lasterror
= _b_('Invalid message index ') . $id;
349 if (!$this->goTo($max)) {
352 if (feof($this->file
)) {
353 $this->current_id
= null
;
354 $this->_lasterrno
= 4;
355 $this->_lasterror
= _b_('Requested index does not exists or file has been truncated');
358 while ($this->readCurrentMessage(true
) && $this->current_id
< $id);
359 if ($this->current_id
== $id) {
362 $this->current_id
= null
;
363 $this->_lasterrno
= 5;
364 $this->_lasterror
= _b_('Requested index does not exists or file has been truncated');
369 private function countMessages($from = 0)
371 $this->messages
=& $this->readMessages(array($from), true
, true
);
372 return count($this->messages
);
375 /** Read the current message (identified by current_id)
376 * @param needFrom_ BOOLEAN is true if the first line *must* be a From_ line
377 * @param alignNext BOOLEAN is true if the buffer must be aligned at the beginning of the next From_ line
378 * @return message sources (without storage data)
380 private function &readCurrentMessage($stripBody = false
, $needFrom_ = true
, $alignNext = true
)
382 $file_cache =& $this->file_cache
;
383 if ($file_cache && $file_cache != ftell($this->file
)) {
389 while(!feof($this->file
)) {
390 // Process file cache
391 if ($file_cache) { // this is a From_ line
393 $this->at_beginning
= false
;
399 $line = rtrim(fgets($this->file
), "\r\n");
401 // Process From_ line
402 if ($needFrom_ ||
!$msg ||
$canFrom_) {
403 if (substr($line, 0, 5) == 'From ') { // this is a From_ line
409 $this->current_id++
; // we are finally in the next message
410 if ($alignNext) { // align the file pointer at the beginning of the new message
411 $this->at_beginning
= true
;
412 $file_cache = ftell($this->file
);
416 } elseif ($needFrom_) {
421 // Process non-From_ lines
422 if (substr($line, 0, 6) == '>From ') { // remove inline From_ quotation
423 $line = substr($line, 1);
425 if (!$stripBody ||
!$inBody) {
426 $msg[] = $line; // add the line to the message source
428 $canFrom_ = empty($line); // check if next line can be a From_ line
429 if ($canFrom_ && !$inBody && $stripBody) {
432 $this->at_beginning
= false
;
434 if (!feof($this->file
) && !$canFrom_) {
440 /** Read message with the given ids
441 * @param ids ARRAY of ids to look for
442 * @param strip BOOLEAN if true, only headers are retrieved
443 * @param from BOOLEAN if true, process all messages from max(ids) to the end of the mbox
444 * @return Array(Array('message' => message sources (or parsed message headers if $strip is true),
445 * 'beginning' => offset of message beginning,
446 * 'end' => offset of message end))
448 private function &readMessages(array $ids, $strip = false
, $from = false
)
450 if ($this->messages
) {
451 return $this->messages
;
455 while ((count($ids) ||
$from) && !feof($this->file
)) {
457 $id = array_shift($ids);
461 if ($id != $this->current_id ||
!$this->at_beginning
) {
462 if (!$this->goTo($id)) {
470 $beginning = ftell($this->file
);
471 $message =& $this->readCurrentMessage($strip, false
);
473 $message =& BananaMimePart
::parseHeaders($message);
475 $end = ftell($this->file
);
476 $messages[$id] = array('message' => $message, 'beginning' => $beginning, 'end' => $end);
482 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: