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
16 private $debug = false
;
17 private $bt = array();
19 private $_lasterrno = 0;
20 private $_lasterror = null
;
22 public function __construct()
24 $this->debug
= Banana
::$debug_mbox;
27 public function isValid()
30 //!Banana::$group || $this->file;
33 /** Indicate last error n°
35 public function lastErrNo()
37 return $this->_lasterrno
;;
40 /** Indicate last error text
42 public function lastError()
44 return $this->_lasterror
;
47 /** Return the description of the current box
49 public function getDescription()
54 /** Return the list of the boxes
55 * @param mode Kind of boxes to list
56 * @param since date of last check (for new boxes and new messages)
57 * @param withstats Indicated whether msgnum and unread must be set in the result
58 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
60 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
62 return array(Banana
::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
65 private function &getRawMessage($id)
68 if (!is_numeric($id)) {
69 if (!Banana
::$spool) {
72 $id = Banana
::$spool->ids
[$id]->id
;
74 $options = array ('-m ' . $id);
75 $this->getMBoxPosition($options, $id);
76 return $this->callHelper('-b', $options);
80 * @param id Id of the emssage (can be either an Message-id or a message index)
81 * @return A BananaMessage or null if the given id can't be retreived
83 public function &getMessage($id)
85 $messages =& $this->getRawMessage($id);
87 $messages = new BananaMessage($messages);
94 /** Return the sources of the given message
96 public function getMessageSource($id)
98 $message =& $this->getRawMessage($id);
100 $message = implode("\n", $message);
105 /** Compute the number of messages of the box
107 private function getCount()
110 if (@filesize
($this->getFileName()) == @Banana
::$spool->storage
['size']) {
111 if (!empty(Banana
::$spool->overview
)) {
112 return max(array_keys(Banana
::$spool->overview
)) +
1;
116 $this->getMBoxPosition($options);
117 $val =& $this->callHelper('-c', $options);
121 return intval(trim($val[0]));
124 /** Return the indexes of the messages presents in the Box
125 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
127 public function getIndexes()
129 $count = $this->getCount();
130 return array($count, 0, $count - 1);
133 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
134 * @return Array(id => array(headername => headervalue))
136 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
140 $options[] = "-m $firstid:$lastid";
141 $this->getMboxPosition($options, $firstid);
142 $lines =& $this->callHelper('-d', $options, $msg_headers);
150 foreach ($lines as $key=>&$line) {
157 } elseif ($get_pos) {
158 $headers[$id] = array('beginning' => intval($line));
160 } elseif (empty($line) && empty($hname)) {
162 } elseif (empty($hname)) {
164 } elseif ($hname == 'date') {
165 $headers[$id][$hname] = @strtotime
($line);
168 BananaMimePart
::decodeHeader($line, $hname);
169 $headers[$id][$hname] = $line;
177 /** Add storage data in spool overview
179 public function updateSpool(array &$messages)
181 foreach ($messages as $id=>&$data) {
182 if (isset(Banana
::$spool->overview
[$id])) {
183 Banana
::$spool->overview
[$id]->storage
['offset'] = $data['beginning'];
186 Banana
::$spool->storage
['size'] = @filesize
($this->getFileName());
189 /** Return the indexes of the new messages since the give date
190 * @return Array(MSGNUM of new messages)
192 public function getNewIndexes($since)
195 if (is_null($this->file
)) {
198 if (is_null($this->new_messages
)) {
201 return range($this->count
- $this->new_messages
, $this->count
- 1);
204 /** Return wether or not the protocole can be used to add new messages
206 public function canSend()
211 /** Return false because we can't cancel a mail
213 public function canCancel()
218 /** Return the list of requested headers
219 * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header
220 * and 'reply' for the reply header, eg:
221 * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To')
222 * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To')
224 public function requestedHeaders()
226 return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To');
230 * @return true if it was successfull
232 public function send(BananaMessage
$message)
234 $headers = $message->getHeaders();
235 $to = $headers['To'];
236 $subject = $headers['Subject'];
237 unset($headers['To']);
238 unset($headers['Subject']);
240 foreach ($headers as $key=>$value) {
241 if (!empty($value)) {
242 $hdrs .= "$key: $value\r\n";
245 $body = $message->get(false
);
246 return mail($to, $subject, $body, $hdrs);
250 * @return true if it was successfull
252 public function cancel(BananaMessage
$message)
257 /** Return the protocole name
259 public function name()
264 /** Return the spool filename
266 public function filename()
268 @list
($mail, $domain) = explode('@', Banana
::$group);
270 if (isset($domain)) {
271 $file = $domain . '_';
273 return $file . $mail;
276 /** Return the execution backtrace
278 public function backtrace()
288 # Filesystem functions
291 protected function getFileName()
293 if (is_null(Banana
::$group)) {
296 @list
($mail, $domain) = explode('@', Banana
::$group);
297 return Banana
::$mbox_path . '/' . $mail;
304 /** Add the '-p' optioin for callHelper
306 private function getMBoxPosition(array &$options, $id = null
)
308 if (Banana
::$spool && Banana
::$spool->overview
) {
309 if (!is_null($id) && isset(Banana
::$spool->overview
[$id])) {
312 $key = max(array_keys(Banana
::$spool->overview
));
313 if (!is_null($id) && $key >= $id) {
317 if (isset(Banana
::$spool->overview
[$key]->storage
['offset'])) {
318 $options[] = '-p ' . $key . ':' . Banana
::$spool->overview
[$key]->storage
['offset'];
323 private function &callHelper($action, array $options = array(), array $headers = array())
325 $action .= ' -f ' . $this->getFileName();
326 $cmd = Banana
::$mbox_helper . " $action " . implode(' ', $options) . ' ' . implode(' ', $headers);
328 $start = microtime(true
);
330 exec($cmd, $out, $return);
332 $this->bt
[] = array('action' => $cmd, 'time' => (microtime(true
) - $start),
333 'code' => $return, 'response' => count($out), 'error' => $return ?
"Helper failed" : null
);
336 $this->_lasterrorno
= 1;
337 $this->_lasterrorcode
= "Helper failed";
344 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: