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