a96f7885719b2b59151b21325e7723a7c886c824
2 /********************************************************************************
3 * include/spool.inc.php : spool subroutines
4 * -----------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
12 define('BANANA_SPOOL_VERSION', '0.5.12');
15 * class used in thread overviews
22 /** date (timestamp) */
30 /** reference of parent */
31 public $parent = null
;
32 /** array of children */
33 public $children = Array();
34 /** true if post is read */
36 /** number of posts deeper in this branch of tree */
38 /** same as desc, but counts only unread posts */
42 public $storage = array();
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)
52 public function __construct($id, array &$message)
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));
62 $this->descunread
= 0;
63 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from
, $regs)) {
64 $this->name
= $regs[2];
66 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from
, $regs)) {
67 $this->name
= preg_replace("/^'(.*)'$/", '\1', $regs[1]);
68 $this->name
= stripslashes($this->name
);
71 $this->name
= preg_replace("/\\\(\(|\))/","\\1", $this->name
);
72 } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from
, $regs)) {
73 $this->name
= $regs[1];
75 $this->name
= 'Anonymous';
89 public $overview = array();
90 /** array msgid => msgnum */
91 public $ids = array();
93 public $roots = array();
94 /** thread trees (one tree per root node) */
95 public $trees = array();
97 /** protocole specific data */
98 public $storage = array();
100 private $unreadnb = 0;
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)
107 protected function __construct($group)
109 $this->version
= BANANA_SPOOL_VERSION
;
110 $this->mode
= Banana
::SPOOL_ALL
;
111 $this->group
= $group;
114 public static function &getSpool($group, $since = 0, $clean = false
)
116 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
117 $spool =& Banana
::$spool;
119 $spool =& BananaSpool
::readFromFile($group);
121 if (is_null($spool)) {
122 $spool = new BananaSpool($group);
124 Banana
::$spool =& $spool;
127 $spool->markAllAsRead();
129 $spool->updateUnread($since);
133 private static function spoolFilename($group)
135 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
136 if (!is_dir($file)) {
139 return $file . Banana
::$protocole->filename();
142 private static function &readFromFile($group)
145 $file = BananaSpool
::spoolFilename($group);
146 if (!file_exists($file)) {
149 $spool = unserialize(file_get_contents($file));
150 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
154 $spool->markAllAsRead();
158 private function compare(&$a, &$b)
160 return ($b->date
- $a->date
);
163 private function saveToFile()
165 $file = BananaSpool
::spoolFilename($this->group
);
167 $this->roots
= Array();
168 foreach($this->overview
as &$msg) {
169 if (is_null($msg->parent
)) {
170 $this->roots
[] =& $msg;
173 usort($this->roots
, array($this, 'compare'));
175 if ($this->mode
== Banana
::SPOOL_ALL
) {
176 file_put_contents($file, serialize($this));
180 private function build()
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;
191 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
192 $first = $last - Banana
::$spool_max;
194 $first +
= $threshold;
197 $clean = $this->clean($first, $last, $msgnum);
198 $update = $this->update($first, $last, $msgnum);
200 if ($clean ||
$update) {
205 private function clean(&$first, &$last, $msgnum)
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
);
217 if (!empty($this->overview
)) {
218 $first = max(array_keys($this->overview
))+
1;
224 private function update(&$first, &$last, $msgnum)
226 if ($first > $last ||
!$msgnum) {
230 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
231 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
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;
243 $updateTrees = array();
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);
252 if (!is_null($msg->parent
)) {
253 $parent =& $msg->parent
;
254 $parent->children
[] =& $msg;
255 while (!is_null($parent)) {
256 $parent->desc +
= $msg->desc
;
258 if ($parent !== $parent->parent
) {
259 $parent =& $parent->parent
;
264 $updateTrees[$prev->id
] = true
;
267 foreach ($updateTrees as $root=>$t) {
268 $this->trees
[$root] =& $this->buildTree($root);
270 Banana
::$protocole->updateSpool($messages);
274 public function updateUnread($since)
280 $newpostsids = Banana
::$protocole->getNewIndexes($since);
282 if (empty($newpostsids)) {
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
;
297 $this->unreadnb +
= count($newpostsids);
300 public function setMode($mode)
304 case Banana
::SPOOL_UNREAD
:
305 $num = max(array_keys($this->overview
));
306 if ($this->overview
[$num]->isread
) {
309 foreach ($this->roots
as &$root) {
310 if ($root->descunread
== 0) {
311 $this->killdesc($root->id
);
318 /** Fetch list of references
320 public function &getReferences(array &$refs)
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];
330 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
331 $references[] =& $this->ids
[$refs['in-reply-to']];
336 /** Mark the given id as read
337 * @param id MSGNUM of post
339 public function markAsRead($id)
341 if (!$this->overview
[$id]->isread
) {
342 $this->overview
[$id]->isread
= true
;
345 $this->overview
[$id]->descunread
--;
346 $id = $this->overview
[$id]->parent
;
351 /** Mark all unread messages as read
353 public function markAllAsRead(array &$array = null
)
355 if (!$this->unreadnb
) {
358 if (is_null($array) && !empty($this->roots
)) {
359 $array =& $this->roots
;
360 } elseif (is_null($array)) {
363 foreach ($array as &$msg) {
365 $this->markAsRead($msg->id
);
366 if (!$this->unreadnb
) {
370 if ($msg->descunread
) {
371 $this->markAllAsRead($msg->children
);
376 /** kill post and childrens
377 * @param $_id MSGNUM of post
379 private function killdesc($_id)
381 $overview =& $this->overview
[$_id];
382 $children =& $overview->children
;
383 if (sizeof($children)) {
384 foreach ($children as &$c) {
385 $this->killdesc($c->id
);
388 unset($this->overview
[$_id]);
389 foreach ($this->roots
as $k=>&$root) {
390 if ($root === $overview) {
391 unset($this->roots
[$k]);
395 unset($this->ids
[$overview->msgid
]);
396 unset($this->trees
[$_id]);
400 /** delete a post from overview
401 * @param $_id MSGNUM of post
403 public function delid($_id, $write = true
)
405 if (!isset($this->overview
[$_id])) {
409 $overview =& $this->overview
[$_id];
410 // Be sure it is not counted as unread
411 if (!$overview->isread
) {
412 $this->markAsRead($_id);
415 $parent =& $overview->parent
;
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]);
425 if (sizeof($overview->children
)) {
426 $parent->children
= array_merge($parent->children
, $overview->children
);
427 foreach ($overview->children
as &$child) {
428 $child->parent
=& $parent;
431 while (!is_null($parent)) {
433 $parent =& $parent->parent
;
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]);
454 public function formatDate(BananaSpoolHead
&$head)
456 $stamp = $head->date
;
457 $today = intval(time() / (24*3600));
458 $dday = intval($stamp / (24*3600));
460 if ($today == $dday) {
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';
469 $format = '%a %e %b %Y';
471 return strftime($format, $stamp);
474 public function formatSubject(BananaSpoolHead
&$head)
476 $subject = $popup = $head->subject
;
478 if (function_exists('hook_formatDisplayHeader')) {
479 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
481 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
484 if (empty($subject)) {
485 $subject = _b_('(pas de sujet)');
487 if ($head->id
!= Banana
::$artid) {
488 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
489 'text' => $subject, 'popup' => $popup));
491 return $subject . $link;
494 public function formatFrom(BananaSpoolHead
&$head)
496 return BananaMessage
::formatFrom($head->from
);
499 public function start()
501 if (Banana
::$first) {
502 return Banana
::$first;
504 $first = array_search(Banana
::$artid, $this->roots
);
505 return max(0, $first - Banana
::$spool_tbefore);
509 public function context()
511 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
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' => ' ', '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));
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"')
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;
551 $array[] = $t_e . $t_e . $line;
553 $array[] = $t_e . ($msg->isread ?
$r_l : $u_l) . $line;
561 /** build the spool tree associated with the given message
563 public function &buildTree($id, $force = false
) {
564 $root =& $this->root($id);
566 if (!$force && isset($this->trees
[$id])) {
567 return $this->trees
[$id];
569 $tree =& $this->_buildTree($root);
570 $tree = '<div class="tree"><div style="height:18px">'
571 . implode("</div>\n<div style=\"height:18px\">", $tree)
577 /** computes linear post index
578 * @param $_id INTEGER MSGNUM of post
579 * @return INTEGER linear index of post
581 public function getNdX($_id)
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
);
590 for ($i = 0; $i < $pos ; $i++
) {
591 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
593 $ndx++
; //noeud père
595 $id_cur = $id_parent;
598 foreach ($this->roots
as $i) {
602 $ndx +
= $this->overview
[$i]->desc
;
607 /** Return root message of the given thread
608 * @param id INTEGER id of a message
610 public function &root($id)
612 $parent =& $this->overview
[$id];
613 while (!is_null($parent->parent
)) {
614 $parent =& $parent->parent
;
619 /** Return the last post id with the given subject
622 public function getPostId($subject)
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]);
633 $test = banana_html_entity_decode($val);
637 if ($test == $subject) {
645 /** Returns previous thread root index
646 * @param id INTEGER message number
648 public function prevThread($id)
650 $root =& $this->root($id);
652 foreach ($this->roots
as &$i) {
661 /** Returns next thread root index
662 * @param id INTEGER message number
664 public function nextThread($id)
666 $root =& $this->root($id);
668 foreach ($this->roots
as &$i) {
679 /** Return prev post in the thread
680 * @param id INTEGER message number
682 public function prevPost($id)
684 $parent =& $this->overview
[$id]->parent
;
685 if (is_null($parent)) {
689 foreach ($parent->children
as &$child) {
690 if ($child->id
== $id) {
698 /** Return next post in the thread
699 * @param id INTEGER message number
701 public function nextPost($id)
703 $cur =& $this->overview
[$id];
704 if (count($cur->children
) != 0) {
705 return $cur->children
[0]->id
;
710 $parent =& $cur->parent
;
711 if (is_null($parent)) {
715 foreach ($parent->children
as &$child) {
719 if ($child === $cur) {
728 /** Look for an unread message in the thread rooted by the message
729 * @param id INTEGER message number
731 private function _nextUnread(BananaSpoolHead
&$cur)
736 foreach ($cur->children
as &$child) {
737 $unread = $this->_nextUnread($child);
738 if (!is_null($unread)) {
745 /** Find next unread message
746 * @param id INTEGER message number
748 public function nextUnread($id = null
)
750 if (!$this->unreadnb
) {
755 // Look in message children
756 foreach ($this->overview
[$id]->children
as &$child) {
757 $next = $this->_nextUnread($child);
758 if (!is_null($next)) {
764 // Look in current thread
768 $cur =& $this->overview
[$id];
775 $parent =& $cur->parent
;
778 if (!is_null($parent)) {
779 $array =& $parent->children
;
781 $array =& $this->roots
;
783 foreach ($array as &$child) {
785 $next = $this->_nextUnread($child);
786 if (!is_null($next)) {
790 if ($child === $cur) {
795 } while(!is_null($cur));
800 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: