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];
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);
92 /** Return the sources of the given message
94 public function getMessageSource($id)
96 $message =& $this->getRawMessage($id);
98 $message = implode("\n", $message);
103 /** Compute the number of messages of the box
105 private function getCount()
108 $this->getMBoxPosition($options);
109 $val =& $this->callHelper('-c', $options);
113 return intval(trim($val[0]));
116 /** Return the indexes of the messages presents in the Box
117 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
119 public function getIndexes()
121 $count = $this->getCount();
122 return array($count, 0, $count - 1);
125 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
126 * @return Array(id => array(headername => headervalue))
128 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
132 $options[] = "-m $firstid:$lastid";
133 $this->getMboxPosition($options, $firstid);
134 $lines =& $this->callHelper('-d', $options, $msg_headers);
140 $id = array_shift($lines);
144 $offset = array_shift($lines);
145 if ($offset === '') {
149 $headers[$id] = array('beginning' => intval($offset));
151 $hname = array_shift($lines);
155 $hval = array_shift($lines);
159 if ($hname == 'date') {
160 $headers[$id][$hname] = @strtotime
($hval);
162 $headers[$id][$hname] = $hval;
165 if (!isset($headers[$id]['date'])) {
168 print_r($headers[$id]);
171 array_walk_recursive($headers, array('BananaMimePart', 'decodeHeader'));
175 /** Add storage data in spool overview
177 public function updateSpool(array &$messages)
179 foreach ($messages as $id=>&$data) {
180 if (isset(Banana
::$spool->overview
[$id])) {
181 Banana
::$spool->overview
[$id]->storage
['offset'] = $data['beginning'];
186 /** Return the indexes of the new messages since the give date
187 * @return Array(MSGNUM of new messages)
189 public function getNewIndexes($since)
192 if (is_null($this->file
)) {
195 if (is_null($this->new_messages
)) {
198 return range($this->count
- $this->new_messages
, $this->count
- 1);
201 /** Return wether or not the protocole can be used to add new messages
203 public function canSend()
208 /** Return false because we can't cancel a mail
210 public function canCancel()
215 /** Return the list of requested headers
216 * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header
217 * and 'reply' for the reply header, eg:
218 * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To')
219 * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To')
221 public function requestedHeaders()
223 return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To');
227 * @return true if it was successfull
229 public function send(BananaMessage
&$message)
231 $headers = $message->getHeaders();
232 $to = $headers['To'];
233 $subject = $headers['Subject'];
234 unset($headers['To']);
235 unset($headers['Subject']);
237 foreach ($headers as $key=>$value) {
238 if (!empty($value)) {
239 $hdrs .= "$key: $value\r\n";
242 $body = $message->get(false
);
243 return mail($to, $subject, $body, $hdrs);
247 * @return true if it was successfull
249 public function cancel(BananaMessage
&$message)
254 /** Return the protocole name
256 public function name()
261 /** Return the spool filename
263 public function filename()
265 @list
($mail, $domain) = explode('@', Banana
::$group);
267 if (isset($domain)) {
268 $file = $domain . '_';
270 return $file . $mail;
273 /** Return the execution backtrace
275 public function backtrace()
285 # Filesystem functions
288 protected function getFileName()
290 if (is_null(Banana
::$group)) {
293 @list
($mail, $domain) = explode('@', Banana
::$group);
294 return Banana
::$mbox_path . '/' . $mail;
301 /** Add the '-p' optioin for callHelper
303 private function getMBoxPosition(array &$option, $id = null
)
305 if (Banana
::$spool->overview
) {
306 if (!is_null($id) && Banana
::$spool->overview
[$id]) {
309 $key = max(array_keys(Banana
::$spool->overview
));
310 if (!is_null($id) && $key >= $id) {
314 if (isset(Banana
::$spool->overview
[$key]->storage
['offset'])) {
315 $options[] = '-p ' . $key . ':' . Banana
::$spool->overview
[$key]->storage
['offset'];
320 private function &callHelper($action, array $options = array(), array $headers = array())
322 $action .= ' -f ' . $this->getFileName();
323 $cmd = Banana
::$mbox_helper . " $action " . implode(' ', $options) . ' ' . implode(' ', $headers);
325 $start = microtime(true
);
327 exec($cmd, $out, $return);
329 $this->bt
[] = array('action' => $cmd, 'time' => (microtime(true
) - $start),
330 'code' => $return, 'response' => count($out), 'error' => $return ?
"Helper failed" : null
);
333 $this->_lasterrorno
= 1;
334 $this->_lasterrorcode
= "Helper failed";
341 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: