Unfound mbox is now handled as if it was empty
[banana.git] / banana / spool.inc.php
1 <?php
2 /********************************************************************************
3 * include/spool.inc.php : spool subroutines
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
12 define('BANANA_SPOOL_VERSION', '0.3');
13
14 /** Class spoolhead
15 * class used in thread overviews
16 */
17 class BananaSpoolHead
18 {
19 /** date (timestamp) */
20 public $date;
21 /** subject */
22 public $subject;
23 /** author */
24 public $from;
25 /** reference of parent */
26 public $parent = null;
27 /** paren is direct */
28 public $parent_direct;
29 /** array of children */
30 public $children = Array();
31 /** true if post is read */
32 public $isread;
33 /** number of posts deeper in this branch of tree */
34 public $desc;
35 /** same as desc, but counts only unread posts */
36 public $descunread;
37
38 /** storage data */
39 public $storage = array();
40
41 /** constructor
42 * @param $_date INTEGER timestamp of post
43 * @param $_subject STRING subject of post
44 * @param $_from STRING author of post
45 * @param $_desc INTEGER desc value (1 for a new post)
46 * @param $_read BOOLEAN true if read
47 * @param $_descunread INTEGER descunread value (0 for a new post)
48 */
49 public function __construct(array &$message)
50 {
51 $this->date = $message['date'];
52 $this->subject = $message['subject'];
53 $this->from = $message['from'];
54 $this->desc = 1;
55 $this->isread = true;
56 $this->descunread = 0;
57 }
58 }
59
60
61 class BananaSpool
62 {
63 private $version;
64 private $mode;
65
66 /** group name */
67 public $group;
68 /** spool */
69 public $overview;
70 /** array msgid => msgnum */
71 public $ids;
72 /** thread starts */
73 public $roots;
74
75
76 /** constructor
77 * @param $_group STRING group name
78 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
79 * @param $_since INTEGER time stamp (used for read/unread)
80 */
81 protected function __construct($group)
82 {
83 $this->version = BANANA_SPOOL_VERSION;
84 $this->mode = Banana::SPOOL_ALL;
85 $this->group = $group;
86 }
87
88 public static function getSpool($group, $since = 0, $clean = false)
89 {
90 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
91 $spool =& Banana::$spool;
92 } else {
93 $spool = BananaSpool::readFromFile($group);
94 }
95 if (is_null($spool)) {
96 $spool = new BananaSpool($group);
97 }
98 Banana::$spool =& $spool;
99 $spool->build();
100 if ($clean) {
101 $spool->markAllAsRead();
102 }
103 $spool->updateUnread($since);
104 return $spool;
105 }
106
107 private static function spoolFilename($group)
108 {
109 $file = dirname(dirname(__FILE__));
110 $file .= '/spool/' . Banana::$protocole->name() . '/';
111 if (!is_dir($file)) {
112 mkdir($file);
113 }
114 return $file . Banana::$protocole->filename();
115 }
116
117 private static function readFromFile($group)
118 {
119 $file = BananaSpool::spoolFilename($group);
120 if (!file_exists($file)) {
121 return null;
122 }
123 $spool = unserialize(file_get_contents($file));
124 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
125 return null;
126 }
127 return $spool;
128 }
129
130 private function compare($a, $b)
131 {
132 return ($b->date >= $a->date);
133 }
134
135 private function saveToFile()
136 {
137 $file = BananaSpool::spoolFilename($this->group);
138 uasort($this->overview, array($this, 'compare'));
139
140 $this->roots = Array();
141 foreach($this->overview as $id=>$msg) {
142 if (is_null($msg->parent)) {
143 $this->roots[] = $id;
144 }
145 }
146
147 if ($this->mode == Banana::SPOOL_ALL) {
148 file_put_contents($file, serialize($this));
149 }
150 }
151
152 private function build()
153 {
154 $threshold = 0;
155
156 // Compute the range of indexes
157 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
158 if ($last < $first) {
159 $threshold = $firt + $msgnum - $last;
160 $threshold = (int)(log($threshold)/log(2));
161 $threshold = (2 ^ ($threshold + 1)) - 1;
162 }
163 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
164 $first = $last - Banana::$spool_max;
165 if ($first < 0) {
166 $first += $threshold;
167 }
168 }
169 $clean = $this->clean($first, $last, $msgnum);
170 $update = $this->update($first, $last, $msgnum);
171
172 if ($clean || $update) {
173 $this->saveToFile();
174 }
175 }
176
177 private function clean(&$first, &$last, $msgnum)
178 {
179 $do_save = false;
180 if (is_array($this->overview)) {
181 $mids = array_keys($this->overview);
182 foreach ($mids as $id) {
183 if (($first <= $last && ($id < $first || $id > $last))
184 || ($first > $last && $id < $first && $id > $last)) {
185 $this->delid($id, false);
186 $do_save = true;
187 }
188 }
189 if (!empty($this->overview)) {
190 $first = max(array_keys($this->overview))+1;
191 }
192 }
193 return $do_save;
194 }
195
196 private function update(&$first, &$last, $msgnum)
197 {
198 if ($first > $last || !$msgnum) {
199 return false;
200 }
201
202 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
203 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
204
205 if (!is_array($this->ids)) {
206 $this->ids = array();
207 }
208 foreach ($messages as $id=>&$message) {
209 $this->ids[$message['message-id']] = $id;
210 }
211
212 foreach ($messages as $id=>&$message) {
213 if (!isset($this->overview[$id])) {
214 $this->overview[$id] = new BananaSpoolHead($message);
215 }
216 $msg =& $this->overview[$id];
217 $msgrefs = BananaMessage::formatReferences($message);
218 $parents = preg_grep('/^\d+$/', $msgrefs);
219 $msg->parent = array_pop($parents);
220 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
221
222 if (!is_null($p = $msg->parent)) {
223 if (empty($this->overview[$p])) {
224 $this->overview[$p] = new BananaSpoolHead($messages[$p]);
225 }
226 $this->overview[$p]->children[] = $id;
227
228 while (!is_null($p)) {
229 $this->overview[$p]->desc += $msg->desc;
230 if ($p != $this->overview[$p]->parent) {
231 $p = $this->overview[$p]->parent;
232 } else {
233 $p = null;
234 }
235 }
236 }
237 }
238 Banana::$protocole->updateSpool($messages);
239 return true;
240 }
241
242 private function updateUnread($since)
243 {
244 if (empty($since)) {
245 return;
246 }
247
248 $newpostsids = Banana::$protocole->getNewIndexes($since);
249
250 if (empty($newpostsids)) {
251 return;
252 }
253
254 if (!is_array($this->ids)) {
255 $this->ids = array();
256 }
257 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
258 foreach ($newpostsids as $mid) {
259 $id = $this->ids[$mid];
260 if ($this->overview[$id]->isread) {
261 $this->overview[$id]->isread = false;
262 while (isset($id)) {
263 $this->overview[$id]->descunread ++;
264 $id = $this->overview[$id]->parent;
265 }
266 }
267 }
268 }
269
270 public function setMode($mode)
271 {
272 $this->mode = $mode;
273 switch ($mode) {
274 case Banana::SPOOL_UNREAD:
275 foreach ($this->roots as $k=>$i) {
276 if ($this->overview[$i]->descunread == 0) {
277 $this->killdesc($i);
278 unset($this->roots[$k]);
279 }
280 }
281 break;
282 }
283 }
284
285 /** Mark the given id as read
286 * @param id MSGNUM of post
287 */
288 public function markAsRead($id)
289 {
290 if (!$this->overview[$id]->isread) {
291 $this->overview[$id]->isread = true;
292 while (isset($id)) {
293 $this->overview[$id]->descunread--;
294 $id = $this->overview[$id]->parent;
295 }
296 }
297 }
298
299 /** Mark all unread messages as read
300 */
301 public function markAllAsRead(array &$array = null)
302 {
303 if (is_null($array)) {
304 $array =& $this->roots;
305 }
306 if (!is_array($this->roots)) {
307 return;
308 }
309 foreach ($array as $id) {
310 if (!$this->overview[$id]->isread) {
311 $this->markAsRead($id);
312 }
313 if ($this->overview[$id]->descunread) {
314 $this->markAllAsRead($this->overview[$id]->children);
315 }
316 }
317 }
318
319 /** kill post and childrens
320 * @param $_id MSGNUM of post
321 */
322 private function killdesc($_id)
323 {
324 if (sizeof($this->overview[$_id]->children)) {
325 foreach ($this->overview[$_id]->children as $c) {
326 $this->killdesc($c);
327 }
328 }
329 unset($this->overview[$_id]);
330 if (($msgid = array_search($_id, $this->ids)) !== false) {
331 unset($this->ids[$msgid]);
332 }
333 }
334
335 /** delete a post from overview
336 * @param $_id MSGNUM of post
337 */
338 public function delid($_id, $write = true)
339 {
340 if (isset($this->overview[$_id])) {
341 if (sizeof($this->overview[$_id]->parent)) {
342 $this->overview[$this->overview[$_id]->parent]->children =
343 array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id));
344 if (sizeof($this->overview[$_id]->children)) {
345 $this->overview[$this->overview[$_id]->parent]->children =
346 array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children);
347 foreach ($this->overview[$_id]->children as $c) {
348 $this->overview[$c]->parent = $this->overview[$_id]->parent;
349 $this->overview[$c]->parent_direct = false;
350 }
351 }
352 $p = $this->overview[$_id]->parent;
353 while ($p) {
354 $this->overview[$p]->desc--;
355 $p = $this->overview[$p]->parent;
356 }
357 } elseif (sizeof($this->overview[$_id]->children)) {
358 foreach ($this->overview[$_id]->children as $c) {
359 $this->overview[$c]->parent = null;
360 }
361 }
362 unset($this->overview[$_id]);
363 $msgid = array_search($_id, $this->ids);
364 if ($msgid) {
365 unset($this->ids[$msgid]);
366 }
367
368 if ($write) {
369 $this->markAllAsRead();
370 $this->saveToFile();
371 }
372 }
373 }
374
375 private function formatDate($stamp)
376 {
377 $today = intval(time() / (24*3600));
378 $dday = intval($stamp / (24*3600));
379
380 if ($today == $dday) {
381 $format = "%H:%M";
382 } elseif ($today == 1 + $dday) {
383 $format = _b_('hier')." %H:%M";
384 } elseif ($today < 7 + $dday) {
385 $format = '%a %H:%M';
386 } else {
387 $format = '%a %e %b';
388 }
389 return utf8_encode(strftime($format, $stamp));
390 }
391
392 /** displays children tree of a post
393 * @param $_id INTEGER MSGNUM of post
394 * @param $_index INTEGER linear number of post in the tree
395 * @param $_first INTEGER linear number of first post displayed
396 * @param $_last INTEGER linear number of last post displayed
397 * @param $_ref STRING MSGNUM of current post
398 * @param $_pfx_node STRING prefix used for current node
399 * @param $_pfx_end STRING prefix used for children of current node
400 * @param $_head BOOLEAN true if first post in thread
401 *
402 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
403 */
404 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
405 {
406 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
407 if (!isset($spfx_f)) {
408 $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
409 $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
410 $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
411 $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
412 $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
413 $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
414 $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
415 $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
416 $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => '&nbsp;', 'height' => 21, 'width' => 12));
417 $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
418 }
419
420 $overview =& $this->overview[$_id];
421 if ($_index + $overview->desc < $_first || $_index > $_last) {
422 return '';
423 }
424
425 $res = '';
426 if ($_index >= $_first) {
427 $hc = empty($overview->children);
428
429 $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
430 $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n";
431 $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">'
432 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n);
433 $subject = $overview->subject;
434 if (function_exists('hook_formatDisplayHeader')) {
435 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
436 } else {
437 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
438 $link = null;
439 }
440 if (empty($subject)) {
441 $subject = _b_('(pas de sujet)');
442 }
443 if ($_index != $_ref) {
444 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
445 'text' => $subject, 'popup' => $subject));
446 }
447 $res .= '&nbsp;' . $subject . $link;
448 $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>";
449
450 if ($hc) {
451 return $res;
452 }
453 }
454
455 $_index ++;
456 $children = $overview->children;
457 while ($child = array_shift($children)) {
458 $overview =& $this->overview[$child];
459 if ($_index > $_last) {
460 return $res;
461 }
462 if ($_index + $overview->desc >= $_first) {
463 if (sizeof($children)) {
464 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
465 $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd),
466 $_pfx_end . $spfx_I, false);
467 } else {
468 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
469 $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd),
470 $_pfx_end . $spfx_e, false);
471 }
472 }
473 $_index += $overview->desc;
474 }
475
476 return $res;
477 }
478
479 /** Displays overview
480 * @param $_first INTEGER MSGNUM of first post
481 * @param $_last INTEGER MSGNUM of last post
482 * @param $_ref STRING MSGNUM of current/selectionned post
483 */
484 public function toHtml($first = 0, $overview = false)
485 {
486 $res = '';
487
488 if (!$overview) {
489 $_first = $first;
490 $_last = $first + Banana::$spool_tmax - 1;
491 $_ref = null;
492 } else {
493 $_ref = $this->getNdx($first);
494 $_last = $_ref + Banana::$spool_tafter;
495 $_first = $_ref - Banana::$spool_tbefore;
496 if ($_first < 0) {
497 $_last -= $_first;
498 }
499 }
500 $index = 1;
501 foreach ($this->roots as $id) {
502 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
503 $index += $this->overview[$id]->desc ;
504 if ($index > $_last) {
505 break;
506 }
507 }
508 return $res;
509 }
510
511 /** computes linear post index
512 * @param $_id INTEGER MSGNUM of post
513 * @return INTEGER linear index of post
514 */
515 public function getNdX($_id)
516 {
517 $ndx = 1;
518 $id_cur = $_id;
519 while (true) {
520 $id_parent = $this->overview[$id_cur]->parent;
521 if (is_null($id_parent)) break;
522 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
523
524 for ($i = 0; $i < $pos ; $i++) {
525 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
526 }
527 $ndx++; //noeud père
528
529 $id_cur = $id_parent;
530 }
531
532 foreach ($this->roots as $i) {
533 if ($i==$id_cur) {
534 break;
535 }
536 $ndx += $this->overview[$i]->desc;
537 }
538 return $ndx;
539 }
540
541 /** Return root message of the given thread
542 * @param id INTEGER id of a message
543 */
544 public function root($id)
545 {
546 $id_cur = $id;
547 while (true) {
548 $id_parent = $this->overview[$id_cur]->parent;
549 if (is_null($id_parent)) break;
550 $id_cur = $id_parent;
551 }
552 return $id_cur;
553 }
554
555 /** Returns previous thread root index
556 * @param id INTEGER message number
557 */
558 public function prevThread($id)
559 {
560 $root = $this->root($id);
561 $last = null;
562 foreach ($this->roots as $i) {
563 if ($i == $root) {
564 return $last;
565 }
566 $last = $i;
567 }
568 return $last;
569 }
570
571 /** Returns next thread root index
572 * @param id INTEGER message number
573 */
574 public function nextThread($id)
575 {
576 $root = $this->root($id);
577 $ok = false;
578 foreach ($this->roots as $i) {
579 if ($ok) {
580 return $i;
581 }
582 if ($i == $root) {
583 $ok = true;
584 }
585 }
586 return null;
587 }
588
589 /** Return prev post in the thread
590 * @param id INTEGER message number
591 */
592 public function prevPost($id)
593 {
594 $parent = $this->overview[$id]->parent;
595 if (is_null($parent)) {
596 return null;
597 }
598 $last = $parent;
599 foreach ($this->overview[$parent]->children as $child) {
600 if ($child == $id) {
601 return $last;
602 }
603 $last = $child;
604 }
605 return null;
606 }
607
608 /** Return next post in the thread
609 * @param id INTEGER message number
610 */
611 public function nextPost($id)
612 {
613 if (count($this->overview[$id]->children) != 0) {
614 return $this->overview[$id]->children[0];
615 }
616
617 $cur = $id;
618 while (true) {
619 $parent = $this->overview[$cur]->parent;
620 if (is_null($parent)) {
621 return null;
622 }
623 $ok = false;
624 foreach ($this->overview[$parent]->children as $child) {
625 if ($ok) {
626 return $child;
627 }
628 if ($child == $cur) {
629 $ok = true;
630 }
631 }
632 $cur = $parent;
633 }
634 return null;
635 }
636
637 /** Look for an unread message in the thread rooted by the message
638 * @param id INTEGER message number
639 */
640 private function _nextUnread($id)
641 {
642 if (!$this->overview[$id]->isread) {
643 return $id;
644 }
645 foreach ($this->overview[$id]->children as $child) {
646 $unread = $this->_nextUnread($child);
647 if (!is_null($unread)) {
648 return $unread;
649 }
650 }
651 return null;
652 }
653
654 /** Find next unread message
655 * @param id INTEGER message number
656 */
657 public function nextUnread($id = null)
658 {
659 if (!is_null($id)) {
660 // Look in message children
661 foreach ($this->overview[$id]->children as $child) {
662 $next = $this->_nextUnread($child);
663 if (!is_null($next)) {
664 return $next;
665 }
666 }
667 }
668
669 // Look in current thread
670 $cur = $id;
671 do {
672 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
673 $ok = is_null($cur) ? true : false;
674 if (!is_null($parent)) {
675 $array = &$this->overview[$parent]->children;
676 } else {
677 $array = &$this->roots;
678 }
679 foreach ($array as $child) {
680 if ($ok) {
681 $next = $this->_nextUnread($child);
682 if (!is_null($next)) {
683 return $next;
684 }
685 }
686 if ($child == $cur) {
687 $ok = true;
688 }
689 }
690 $cur = $parent;
691 } while(!is_null($cur));
692 return null;
693 }
694 }
695
696 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
697 ?>