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