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