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 | |
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 | { |
19fc7e1d |
16 | private $debug = false; |
6a684b9b |
17 | private $bt = array(); |
18 | |
7027794f |
19 | private $_lasterrno = 0; |
20 | private $_lasterror = null; |
19fc7e1d |
21 | |
0e25d15d |
22 | public function __construct() |
7027794f |
23 | { |
19fc7e1d |
24 | $this->debug = Banana::$debug_mbox; |
7027794f |
25 | } |
19fc7e1d |
26 | |
7027794f |
27 | public function isValid() |
28 | { |
c28d3016 |
29 | return true; |
30 | //!Banana::$group || $this->file; |
7027794f |
31 | } |
32 | |
598a1c53 |
33 | /** Indicate last error n° |
7027794f |
34 | */ |
35 | public function lastErrNo() |
36 | { |
37 | return $this->_lasterrno;; |
38 | } |
39 | |
40 | /** Indicate last error text |
41 | */ |
42 | public function lastError() |
43 | { |
44 | return $this->_lasterror; |
45 | } |
46 | |
47 | /** Return the description of the current box |
48 | */ |
49 | public function getDescription() |
50 | { |
51 | return null; |
52 | } |
53 | |
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) |
59 | */ |
60 | public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false) |
61 | { |
0e25d15d |
62 | return array(Banana::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0)); |
7027794f |
63 | } |
64 | |
19fc7e1d |
65 | private function &getRawMessage($id) |
7027794f |
66 | { |
7d3f4749 |
67 | $message = null; |
7a5823f9 |
68 | if (!is_numeric($id)) { |
19fc7e1d |
69 | if (!Banana::$spool) { |
7d3f4749 |
70 | return $message; |
7027794f |
71 | } |
72 | $id = Banana::$spool->ids[$id]; |
73 | } |
19fc7e1d |
74 | $options = array ('-m ' . $id); |
6c6b7002 |
75 | $this->getMBoxPosition($options, $id); |
19fc7e1d |
76 | return $this->callHelper('-b', $options); |
77 | } |
78 | |
79 | /** Return a message |
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 |
82 | */ |
83 | public function &getMessage($id) |
84 | { |
85 | $messages =& $this->getRawMessage($id); |
86 | if ($messages) { |
87 | $messages = new BananaMessage($messages); |
88 | } |
89 | return $messages; |
7027794f |
90 | } |
91 | |
7a5823f9 |
92 | /** Return the sources of the given message |
93 | */ |
94 | public function getMessageSource($id) |
19fc7e1d |
95 | { |
96 | $message =& $this->getRawMessage($id); |
97 | if ($message) { |
98 | $message = implode("\n", $message); |
c28d3016 |
99 | } |
19fc7e1d |
100 | return $message; |
7a5823f9 |
101 | } |
102 | |
103 | /** Compute the number of messages of the box |
104 | */ |
7027794f |
105 | private function getCount() |
106 | { |
19fc7e1d |
107 | $options = array(); |
dc5f77ad |
108 | if (@filesize($this->getFileName()) == Banana::$spool->storage['size']) { |
109 | return max(Banana::$spool->ids); |
110 | } |
6c6b7002 |
111 | $this->getMBoxPosition($options); |
19fc7e1d |
112 | $val =& $this->callHelper('-c', $options); |
113 | if (!$val) { |
114 | return 0; |
115 | } |
116 | return intval(trim($val[0])); |
7027794f |
117 | } |
118 | |
119 | /** Return the indexes of the messages presents in the Box |
120 | * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message) |
121 | */ |
122 | public function getIndexes() |
123 | { |
19fc7e1d |
124 | $count = $this->getCount(); |
125 | return array($count, 0, $count - 1); |
7027794f |
126 | } |
127 | |
128 | /** Return the message headers (in BananaMessage) for messages from firstid to lastid |
129 | * @return Array(id => array(headername => headervalue)) |
130 | */ |
131 | public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array()) |
132 | { |
19fc7e1d |
133 | $headers = null; |
134 | $options = array(); |
135 | $options[] = "-m $firstid:$lastid"; |
6c6b7002 |
136 | $this->getMboxPosition($options, $firstid); |
19fc7e1d |
137 | $lines =& $this->callHelper('-d', $options, $msg_headers); |
138 | if (!$lines) { |
c28d3016 |
139 | return $headers; |
140 | } |
19fc7e1d |
141 | $headers = array(); |
142 | while ($lines) { |
143 | $id = array_shift($lines); |
144 | if ($id === '') { |
145 | continue; |
146 | } |
147 | $offset = array_shift($lines); |
148 | if ($offset === '') { |
149 | continue; |
150 | } |
151 | $id = intval($id); |
152 | $headers[$id] = array('beginning' => intval($offset)); |
153 | while (true) { |
154 | $hname = array_shift($lines); |
155 | if ($hname === '') { |
156 | break; |
157 | } |
158 | $hval = array_shift($lines); |
19fc7e1d |
159 | if ($hname == 'date') { |
160 | $headers[$id][$hname] = @strtotime($hval); |
7027794f |
161 | } else { |
19fc7e1d |
162 | $headers[$id][$hname] = $hval; |
7027794f |
163 | } |
164 | } |
165 | } |
19fc7e1d |
166 | array_walk_recursive($headers, array('BananaMimePart', 'decodeHeader')); |
7027794f |
167 | return $headers; |
168 | } |
169 | |
170 | /** Add storage data in spool overview |
171 | */ |
172 | public function updateSpool(array &$messages) |
173 | { |
174 | foreach ($messages as $id=>&$data) { |
175 | if (isset(Banana::$spool->overview[$id])) { |
176 | Banana::$spool->overview[$id]->storage['offset'] = $data['beginning']; |
7027794f |
177 | } |
178 | } |
dc5f77ad |
179 | Banana::$spool->storage['size'] = @filesize($this->getFileName()); |
7027794f |
180 | } |
181 | |
182 | /** Return the indexes of the new messages since the give date |
183 | * @return Array(MSGNUM of new messages) |
184 | */ |
185 | public function getNewIndexes($since) |
186 | { |
703b83c3 |
187 | $this->open(); |
c28d3016 |
188 | if (is_null($this->file)) { |
189 | return array(); |
190 | } |
7027794f |
191 | if (is_null($this->new_messages)) { |
192 | $this->getCount(); |
193 | } |
194 | return range($this->count - $this->new_messages, $this->count - 1); |
195 | } |
196 | |
197 | /** Return wether or not the protocole can be used to add new messages |
198 | */ |
199 | public function canSend() |
200 | { |
201 | return true; |
202 | } |
203 | |
204 | /** Return false because we can't cancel a mail |
205 | */ |
206 | public function canCancel() |
207 | { |
208 | return false; |
209 | } |
210 | |
211 | /** Return the list of requested headers |
212 | * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header |
213 | * and 'reply' for the reply header, eg: |
214 | * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To') |
215 | * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To') |
216 | */ |
217 | public function requestedHeaders() |
218 | { |
219 | return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To'); |
220 | } |
221 | |
222 | /** Send a message |
223 | * @return true if it was successfull |
224 | */ |
225 | public function send(BananaMessage &$message) |
226 | { |
e1debf92 |
227 | $headers = $message->getHeaders(); |
228 | $to = $headers['To']; |
229 | $subject = $headers['Subject']; |
230 | unset($headers['To']); |
231 | unset($headers['Subject']); |
232 | $hdrs = ''; |
233 | foreach ($headers as $key=>$value) { |
234 | if (!empty($value)) { |
235 | $hdrs .= "$key: $value\r\n"; |
236 | } |
237 | } |
238 | $body = $message->get(false); |
239 | return mail($to, $subject, $body, $hdrs); |
7027794f |
240 | } |
241 | |
242 | /** Cancel a message |
243 | * @return true if it was successfull |
244 | */ |
245 | public function cancel(BananaMessage &$message) |
246 | { |
247 | return false; |
248 | } |
249 | |
250 | /** Return the protocole name |
251 | */ |
252 | public function name() |
253 | { |
254 | return 'MBOX'; |
255 | } |
256 | |
e9360b11 |
257 | /** Return the spool filename |
258 | */ |
259 | public function filename() |
260 | { |
261 | @list($mail, $domain) = explode('@', Banana::$group); |
262 | $file = ""; |
263 | if (isset($domain)) { |
264 | $file = $domain . '_'; |
265 | } |
266 | return $file . $mail; |
267 | } |
268 | |
6a684b9b |
269 | /** Return the execution backtrace |
270 | */ |
271 | public function backtrace() |
272 | { |
273 | if ($this->debug) { |
274 | return $this->bt; |
275 | } else { |
276 | return null; |
277 | } |
278 | } |
279 | |
7027794f |
280 | ####### |
281 | # Filesystem functions |
282 | ####### |
283 | |
c28d3016 |
284 | protected function getFileName() |
7027794f |
285 | { |
c28d3016 |
286 | if (is_null(Banana::$group)) { |
7027794f |
287 | return null; |
288 | } |
c28d3016 |
289 | @list($mail, $domain) = explode('@', Banana::$group); |
e9360b11 |
290 | return Banana::$mbox_path . '/' . $mail; |
7027794f |
291 | } |
292 | |
293 | ####### |
294 | # MBox parser |
295 | ####### |
6c6b7002 |
296 | |
297 | /** Add the '-p' optioin for callHelper |
298 | */ |
dc5f77ad |
299 | private function getMBoxPosition(array &$options, $id = null) |
6c6b7002 |
300 | { |
301 | if (Banana::$spool->overview) { |
dc5f77ad |
302 | if (!is_null($id) && isset(Banana::$spool->overview[$id])) { |
6c6b7002 |
303 | $key = $id; |
304 | } else { |
dc5f77ad |
305 | $key = max(Banana::$spool->ids); |
6c6b7002 |
306 | if (!is_null($id) && $key >= $id) { |
307 | return; |
308 | } |
309 | } |
310 | if (isset(Banana::$spool->overview[$key]->storage['offset'])) { |
311 | $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset']; |
312 | } |
313 | } |
314 | } |
7027794f |
315 | |
19fc7e1d |
316 | private function &callHelper($action, array $options = array(), array $headers = array()) |
7027794f |
317 | { |
19fc7e1d |
318 | $action .= ' -f ' . $this->getFileName(); |
319 | $cmd = Banana::$mbox_helper . " $action " . implode(' ', $options) . ' ' . implode(' ', $headers); |
320 | if ($this->debug) { |
19fc7e1d |
321 | $start = microtime(true); |
7027794f |
322 | } |
19fc7e1d |
323 | exec($cmd, $out, $return); |
324 | if ($this->debug) { |
6a684b9b |
325 | $this->bt[] = array('action' => $cmd, 'time' => (microtime(true) - $start), |
6c6b7002 |
326 | 'code' => $return, 'response' => count($out), 'error' => $return ? "Helper failed" : null); |
7027794f |
327 | } |
19fc7e1d |
328 | if ($return != 0) { |
329 | $this->_lasterrorno = 1; |
330 | $this->_lasterrorcode = "Helper failed"; |
331 | $out = null; |
7027794f |
332 | } |
19fc7e1d |
333 | return $out; |
7027794f |
334 | } |
335 | } |
336 | |
598a1c53 |
337 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
7027794f |
338 | ?> |