11fd2255abed07be67d324ac791713bede46aa30
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);
130 //var_dump($spool->trees);
134 private static function spoolFilename($group)
136 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
137 if (!is_dir($file)) {
140 return $file . Banana
::$protocole->filename();
143 private static function &readFromFile($group)
146 $file = BananaSpool
::spoolFilename($group);
147 if (!file_exists($file)) {
150 $spool = unserialize(file_get_contents($file));
151 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
155 $spool->markAllAsRead();
159 private function compare(&$a, &$b)
161 return ($b->date
- $a->date
);
164 private function saveToFile()
166 $file = BananaSpool
::spoolFilename($this->group
);
168 $this->roots
= Array();
169 foreach($this->overview
as &$msg) {
170 if (is_null($msg->parent
)) {
171 $this->roots
[] =& $msg;
174 usort($this->roots
, array($this, 'compare'));
176 if ($this->mode
== Banana
::SPOOL_ALL
) {
177 file_put_contents($file, serialize($this));
181 private function build()
185 // Compute the range of indexes
186 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
187 if ($last < $first) {
188 $threshold = $first +
$msgnum - $last;
189 $threshold = (int)(log($threshold)/log(2));
190 $threshold = (2 ^
($threshold +
1)) - 1;
192 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
193 $first = $last - Banana
::$spool_max;
195 $first +
= $threshold;
198 $clean = $this->clean($first, $last, $msgnum);
199 $update = $this->update($first, $last, $msgnum);
201 if ($clean ||
$update) {
206 private function clean(&$first, &$last, $msgnum)
209 if (!empty($this->overview
)) {
210 $mids = array_keys($this->overview
);
211 foreach ($mids as $id) {
212 if (($first <= $last && ($id < $first ||
$id > $last))
213 ||
($first > $last && $id < $first && $id > $last)) {
214 $this->delid($id, false
);
218 if (!empty($this->overview
)) {
219 $first = max(array_keys($this->overview
))+
1;
225 private function update(&$first, &$last, $msgnum)
227 if ($first > $last ||
!$msgnum) {
231 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
232 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
234 // Build all the new Spool Heads
235 foreach ($messages as $id=>&$message) {
236 if (!isset($this->overview
[$id])) {
237 $this->overview
[$id] = new BananaSpoolHead($id, $message);
238 $head =& $this->overview
[$id];
239 $this->ids
[$head->msgid
] =& $head;
244 $updateTrees = array();
246 foreach ($messages as $id=>&$message) {
247 $msg =& $this->overview
[$id];
248 $parents =& $this->getReferences($message);
249 while (!empty($parents) && ($msg->parent
=== $msg ||
is_null($msg->parent
))) {
250 $msg->parent
=& array_pop($parents);
253 if (!is_null($msg->parent
)) {
254 $parent =& $msg->parent
;
255 $parent->children
[] =& $msg;
256 while (!is_null($parent)) {
257 $parent->desc +
= $msg->desc
;
259 if ($parent !== $parent->parent
) {
260 $parent =& $parent->parent
;
265 $updateTrees[$prev->id
] = true
;
268 foreach ($updateTrees as $root=>$t) {
269 $this->trees
[$root] =& $this->buildTree($root);
271 Banana
::$protocole->updateSpool($messages);
275 public function updateUnread($since)
281 $newpostsids = Banana
::$protocole->getNewIndexes($since);
283 if (empty($newpostsids)) {
287 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
288 foreach ($newpostsids as $mid) {
289 $overview =& $this->ids
[$mid];
290 if ($overview->isread
) {
291 $overview->isread
= false
;
292 while (!is_null($overview)) {
293 $overview->descunread++
;
294 $overview =& $overview->parent
;
298 $this->unreadnb +
= count($newpostsids);
301 public function setMode($mode)
305 case Banana
::SPOOL_UNREAD
:
306 $num = max(array_keys($this->overview
));
307 if ($this->overview
[$num]->isread
) {
310 foreach ($this->roots
as &$root) {
311 if ($root->descunread
== 0) {
312 $this->killdesc($root->id
);
319 /** Fetch list of references
321 public function &getReferences(array &$refs)
323 $references = array();
324 if (isset($refs['references'])) {
325 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
326 foreach ($text as $id=>&$value) {
327 if (isset($this->ids
[$value])) {
328 $references[] =& $this->ids
[$value];
331 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
332 $references[] =& $this->ids
[$refs['in-reply-to']];
337 /** Mark the given id as read
338 * @param id MSGNUM of post
340 public function markAsRead($id)
342 if (!$this->overview
[$id]->isread
) {
343 $this->overview
[$id]->isread
= true
;
346 $this->overview
[$id]->descunread
--;
347 $id = $this->overview
[$id]->parent
;
352 /** Mark all unread messages as read
354 public function markAllAsRead(array &$array = null
)
356 if (!$this->unreadnb
) {
359 if (is_null($array) && !empty($this->roots
)) {
360 $array =& $this->roots
;
361 } elseif (is_null($array)) {
364 foreach ($array as &$msg) {
366 $this->markAsRead($msg->id
);
367 if (!$this->unreadnb
) {
371 if ($msg->descunread
) {
372 $this->markAllAsRead($msg->children
);
377 /** kill post and childrens
378 * @param $_id MSGNUM of post
380 private function killdesc($_id)
382 $overview =& $this->overview
[$_id];
383 $children =& $overview->children
;
384 if (sizeof($children)) {
385 foreach ($children as &$c) {
386 $this->killdesc($c->id
);
389 unset($this->overview
[$_id]);
390 foreach ($this->roots
as $k=>&$root) {
391 if ($root === $overview) {
392 unset($this->roots
[$k]);
396 unset($this->ids
[$overview->msgid
]);
397 unset($this->trees
[$_id]);
401 /** delete a post from overview
402 * @param $_id MSGNUM of post
404 public function delid($_id, $write = true
)
406 if (!isset($this->overview
[$_id])) {
410 $overview =& $this->overview
[$_id];
411 // Be sure it is not counted as unread
412 if (!$overview->isread
) {
413 $this->markAsRead($_id);
416 $parent =& $overview->parent
;
418 // Remove from the message tree
419 if (!is_null($parent)) {
420 foreach ($parent->children
as $key=>&$child) {
421 if ($child === $overview) {
422 unset($parent->children
[$key]);
426 if (sizeof($overview->children
)) {
427 $parent->children
= array_merge($parent->children
, $overview->children
);
428 foreach ($overview->children
as &$child) {
429 $child->parent
=& $parent;
432 while (!is_null($parent)) {
434 $parent =& $parent->parent
;
438 // Remove all refenrences and assign null to the object
439 unset($this->ids
[$overview->msgid
]);
440 unset($this->overview
[$_id]);
441 unset($this->trees
[$_id]);
442 foreach ($this->roots
as $k=>&$root) {
443 if ($root === $overview) {
444 unset($this->roots
[$k]);
455 public function formatDate(BananaSpoolHead
&$head)
457 $stamp = $head->date
;
458 $today = intval(time() / (24*3600));
459 $dday = intval($stamp / (24*3600));
461 if ($today == $dday) {
463 } elseif ($today == 1 +
$dday) {
464 $format = _b_('hier')." %H:%M";
465 } elseif ($today < 7 +
$dday) {
466 $format = '%a %H:%M';
467 } elseif ($today < 90 +
$dday) {
468 $format = '%a %e %b';
470 $format = '%a %e %b %Y';
472 return strftime($format, $stamp);
475 public function formatSubject(BananaSpoolHead
&$head)
477 $subject = $popup = $head->subject
;
479 if (function_exists('hook_formatDisplayHeader')) {
480 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
482 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
485 if (empty($subject)) {
486 $subject = _b_('(pas de sujet)');
488 if ($head->id
!= Banana
::$artid) {
489 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
490 'text' => $subject, 'popup' => $popup));
492 return $subject . $link;
495 public function formatFrom(BananaSpoolHead
&$head)
497 return BananaMessage
::formatFrom($head->from
);
500 public function start()
502 if (Banana
::$first) {
503 return Banana
::$first;
505 $first = array_search(Banana
::$artid, $this->roots
);
506 return max(0, $first - Banana
::$spool_tbefore);
510 public function context()
512 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
516 private function &_buildTree(BananaSpoolHead
&$head) {
517 static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
518 if (!isset($spfx_f)) {
519 $t_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 18, 'width' => 14));
520 $u_h = Banana
::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18, 'width' => 14));
521 $u_ht = Banana
::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14));
522 $u_vt = Banana
::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14));
523 $u_l = Banana
::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14));
524 $u_f = Banana
::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14));
525 $r_h = Banana
::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18, 'width' => 14));
526 $r_ht = Banana
::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14));
527 $r_vt = Banana
::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14));
528 $r_l = Banana
::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14));
529 $r_f = Banana
::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14));
531 $style = 'background-color:' . $head->color
. '; text-decoration: none';
532 $text = '<span style="' . $style . '" title="' . banana_entities($head->name
. ', ' . $this->formatDate($head))
533 . '"><input type="radio" name="banana_tree" '
534 . (Banana
::$msgshow_javascript ?
'onchange="window.location=\'' .
535 banana_entities(Banana
::$page->makeURL(array('group' => $this->group
, 'artid' => $head->id
))) . '\'"'
536 : ' disabled="disabled"')
538 $array = array($text);
539 foreach ($head->children
as $key=>&$msg) {
540 $tree =& $this->_buildTree($msg);
541 $last = $key == count($head->children
) - 1;
542 foreach ($tree as $kt=>&$line) {
543 if ($kt === 0 && $key === 0 && !$last) {
544 $array[0] .= ($msg->isread ?
$r_ht : $u_ht) . $line;
545 } else if($kt === 0 && $key === 0) {
546 $array[0] .= ($msg->isread ?
$r_h : $u_h) . $line;
547 } else if ($kt === 0 && $last) {
548 $array[] = $t_e . ($msg->isread ?
$r_vt : $u_vt) . $line;
549 } else if ($kt === 0) {
550 $array[] = $t_e . ($msg->isread ?
$r_f : $u_f) . $line;
552 $array[] = $t_e . $t_e . $line;
554 $array[] = $t_e . ($msg->isread ?
$r_l : $u_l) . $line;
562 /** build the spool tree associated with the given message
564 public function &buildTree($id, $force = false
) {
565 $root =& $this->root($id);
567 if (!$force && isset($this->trees
[$id])) {
568 return $this->trees
[$id];
570 $tree =& $this->_buildTree($root);
571 $tree = '<div class="tree"><div style="height:18px">'
572 . implode("</div>\n<div style=\"height:18px\">", $tree)
578 /** computes linear post index
579 * @param $_id INTEGER MSGNUM of post
580 * @return INTEGER linear index of post
582 public function getNdX($_id)
587 $id_parent = $this->overview
[$id_cur]->parent
;
588 if (is_null($id_parent)) break;
589 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
591 for ($i = 0; $i < $pos ; $i++
) {
592 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
594 $ndx++
; //noeud père
596 $id_cur = $id_parent;
599 foreach ($this->roots
as $i) {
603 $ndx +
= $this->overview
[$i]->desc
;
608 /** Return root message of the given thread
609 * @param id INTEGER id of a message
611 public function &root($id)
613 $parent =& $this->overview
[$id];
614 while (!is_null($parent->parent
)) {
615 $parent =& $parent->parent
;
620 /** Return the last post id with the given subject
623 public function getPostId($subject)
625 $subject = trim($subject);
626 $id = max(array_keys($this->overview
));
627 while (isset($this->overview
[$id])) {
628 $test = $this->overview
[$id]->subject
;
629 if (function_exists('hook_formatDisplayHeader')) {
630 $val = hook_formatDisplayHeader('subject', $test, true
);
631 if (is_array($val)) {
632 $test = banana_html_entity_decode($val[0]);
634 $test = banana_html_entity_decode($val);
638 if ($test == $subject) {
646 /** Returns previous thread root index
647 * @param id INTEGER message number
649 public function prevThread($id)
651 $root =& $this->root($id);
653 foreach ($this->roots
as &$i) {
662 /** Returns next thread root index
663 * @param id INTEGER message number
665 public function nextThread($id)
667 $root =& $this->root($id);
669 foreach ($this->roots
as &$i) {
680 /** Return prev post in the thread
681 * @param id INTEGER message number
683 public function prevPost($id)
685 $parent =& $this->overview
[$id]->parent
;
686 if (is_null($parent)) {
690 foreach ($parent->children
as &$child) {
691 if ($child->id
== $id) {
699 /** Return next post in the thread
700 * @param id INTEGER message number
702 public function nextPost($id)
704 $cur =& $this->overview
[$id];
705 if (count($cur->children
) != 0) {
706 return $cur->children
[0]->id
;
711 $parent =& $cur->parent
;
712 if (is_null($parent)) {
716 foreach ($parent->children
as &$child) {
720 if ($child === $cur) {
729 /** Look for an unread message in the thread rooted by the message
730 * @param id INTEGER message number
732 private function _nextUnread(BananaSpoolHead
&$cur)
737 foreach ($cur->children
as &$child) {
738 $unread = $this->_nextUnread($child);
739 if (!is_null($unread)) {
746 /** Find next unread message
747 * @param id INTEGER message number
749 public function nextUnread($id = null
)
751 if (!$this->unreadnb
) {
756 // Look in message children
757 foreach ($this->overview
[$id]->children
as &$child) {
758 $next = $this->_nextUnread($child);
759 if (!is_null($next)) {
765 // Look in current thread
769 $cur =& $this->overview
[$id];
776 $parent =& $cur->parent
;
779 if (!is_null($parent)) {
780 $array =& $parent->children
;
782 $array =& $this->roots
;
784 foreach ($array as &$child) {
786 $next = $this->_nextUnread($child);
787 if (!is_null($next)) {
791 if ($child === $cur) {
796 } while(!is_null($cur));
801 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: