3d447850a5252b831d71545a69ec3ab2cdccc725
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.13');
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 public static function getPath($file = null
)
135 $path = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/' . Banana
::$protocole->filename();
136 if (!is_dir($path)) {
137 if (file_exists($path)) {
140 mkdir($path, 0777, true
);
142 return $path . '/' . $file;
145 private static function spoolFilename()
147 return BananaSpool
::getPath('spool');
150 private static function &readFromFile($group)
153 $file = BananaSpool
::spoolFilename();
154 if (!file_exists($file)) {
157 $spool = unserialize(file_get_contents($file));
158 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
162 $spool->markAllAsRead();
166 private function compare(&$a, &$b)
168 return ($b->date
- $a->date
);
171 private function saveToFile()
173 $file = BananaSpool
::spoolFilename();
175 $this->roots
= Array();
176 foreach($this->overview
as &$msg) {
177 if (is_null($msg->parent
)) {
178 $this->roots
[] =& $msg;
181 usort($this->roots
, array($this, 'compare'));
183 if ($this->mode
== Banana
::SPOOL_ALL
) {
184 file_put_contents($file, serialize($this));
188 private function build()
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;
199 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
200 $first = $last - Banana
::$spool_max;
202 $first +
= $threshold;
205 $clean = $this->clean($first, $last, $msgnum);
206 $update = $this->update($first, $last, $msgnum);
208 if ($clean ||
$update) {
213 private function clean(&$first, &$last, $msgnum)
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
);
225 if (!empty($this->overview
)) {
226 $first = max(array_keys($this->overview
))+
1;
232 private function update(&$first, &$last, $msgnum)
234 if ($first > $last ||
!$msgnum) {
238 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
239 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
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;
251 $updateTrees = array();
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);
260 if (!is_null($msg->parent
)) {
261 $parent =& $msg->parent
;
262 $parent->children
[] =& $msg;
263 while (!is_null($parent)) {
264 $parent->desc +
= $msg->desc
;
266 if ($parent !== $parent->parent
) {
267 $parent =& $parent->parent
;
272 $updateTrees[$prev->id
] = true
;
275 foreach ($updateTrees as $root=>$t) {
276 $this->trees
[$root] =& $this->buildTree($root);
278 Banana
::$protocole->updateSpool($messages);
282 public function updateUnread($since)
288 $newpostsids = Banana
::$protocole->getNewIndexes($since);
290 if (empty($newpostsids)) {
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
;
305 $this->unreadnb +
= count($newpostsids);
308 public function setMode($mode)
312 case Banana
::SPOOL_UNREAD
:
313 $num = max(array_keys($this->overview
));
314 if ($this->overview
[$num]->isread
) {
317 foreach ($this->roots
as &$root) {
318 if ($root->descunread
== 0) {
319 $this->killdesc($root->id
);
326 /** Fetch list of references
328 public function &getReferences(array &$refs)
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];
338 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
339 $references[] =& $this->ids
[$refs['in-reply-to']];
344 /** Mark the given id as read
345 * @param id MSGNUM of post
347 public function markAsRead($id)
349 if (!$this->overview
[$id]->isread
) {
350 $this->overview
[$id]->isread
= true
;
353 $this->overview
[$id]->descunread
--;
354 $id = $this->overview
[$id]->parent
;
359 /** Mark all unread messages as read
361 public function markAllAsRead(array &$array = null
)
363 if (!$this->unreadnb
) {
366 if (is_null($array) && !empty($this->roots
)) {
367 $array =& $this->roots
;
368 } elseif (is_null($array)) {
371 foreach ($array as &$msg) {
373 $this->markAsRead($msg->id
);
374 if (!$this->unreadnb
) {
378 if ($msg->descunread
) {
379 $this->markAllAsRead($msg->children
);
384 /** kill post and childrens
385 * @param $_id MSGNUM of post
387 private function killdesc($_id)
389 $overview =& $this->overview
[$_id];
390 $children =& $overview->children
;
391 if (sizeof($children)) {
392 foreach ($children as &$c) {
393 $this->killdesc($c->id
);
396 unset($this->overview
[$_id]);
397 foreach ($this->roots
as $k=>&$root) {
398 if ($root === $overview) {
399 unset($this->roots
[$k]);
403 unset($this->ids
[$overview->msgid
]);
404 unset($this->trees
[$_id]);
408 /** delete a post from overview
409 * @param $_id MSGNUM of post
411 public function delid($_id, $write = true
)
413 if (!isset($this->overview
[$_id])) {
417 $overview =& $this->overview
[$_id];
418 // Be sure it is not counted as unread
419 if (!$overview->isread
) {
420 $this->markAsRead($_id);
423 $parent =& $overview->parent
;
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]);
433 if (sizeof($overview->children
)) {
434 $parent->children
= array_merge($parent->children
, $overview->children
);
435 foreach ($overview->children
as &$child) {
436 $child->parent
=& $parent;
439 while (!is_null($parent)) {
441 $parent =& $parent->parent
;
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]);
462 public function formatDate(BananaSpoolHead
&$head)
464 $stamp = $head->date
;
465 $today = intval(time() / (24*3600));
466 $dday = intval($stamp / (24*3600));
468 if ($today == $dday) {
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';
477 $format = '%a %e %b %Y';
479 return strftime($format, $stamp);
482 public function formatSubject(BananaSpoolHead
&$head)
484 $subject = $popup = $head->subject
;
486 if (function_exists('hook_formatDisplayHeader')) {
487 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
489 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
492 if (empty($subject)) {
493 $subject = _b_('(pas de sujet)');
495 if ($head->id
!= Banana
::$artid) {
496 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
497 'text' => $subject, 'popup' => $popup));
499 return $subject . $link;
502 public function formatFrom(BananaSpoolHead
&$head)
504 return BananaMessage
::formatFrom($head->from
);
507 public function start()
509 if (Banana
::$first) {
510 return Banana
::$first;
512 $first = array_search(Banana
::$artid, $this->roots
);
513 return max(0, $first - Banana
::$spool_tbefore);
517 public function context()
519 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
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' => ' ', '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));
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"')
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;
559 $array[] = $t_e . $t_e . $line;
561 $array[] = $t_e . ($msg->isread ?
$r_l : $u_l) . $line;
569 /** build the spool tree associated with the given message
571 public function &buildTree($id, $force = false
) {
572 $root =& $this->root($id);
574 if (!$force && isset($this->trees
[$id])) {
575 return $this->trees
[$id];
577 $tree =& $this->_buildTree($root);
578 $tree = '<div class="tree"><div style="height:18px">'
579 . implode("</div>\n<div style=\"height:18px\">", $tree)
585 /** computes linear post index
586 * @param $_id INTEGER MSGNUM of post
587 * @return INTEGER linear index of post
589 public function getNdX($_id)
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
);
598 for ($i = 0; $i < $pos ; $i++
) {
599 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
601 $ndx++
; //noeud père
603 $id_cur = $id_parent;
606 foreach ($this->roots
as $i) {
610 $ndx +
= $this->overview
[$i]->desc
;
615 /** Return root message of the given thread
616 * @param id INTEGER id of a message
618 public function &root($id)
620 $parent =& $this->overview
[$id];
621 while (!is_null($parent->parent
)) {
622 $parent =& $parent->parent
;
627 /** Return the last post id with the given subject
630 public function getPostId($subject)
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]);
641 $test = banana_html_entity_decode($val);
645 if ($test == $subject) {
653 /** Returns previous thread root index
654 * @param id INTEGER message number
656 public function prevThread($id)
658 $root =& $this->root($id);
660 foreach ($this->roots
as &$i) {
669 /** Returns next thread root index
670 * @param id INTEGER message number
672 public function nextThread($id)
674 $root =& $this->root($id);
676 foreach ($this->roots
as &$i) {
687 /** Return prev post in the thread
688 * @param id INTEGER message number
690 public function prevPost($id)
692 $parent =& $this->overview
[$id]->parent
;
693 if (is_null($parent)) {
697 foreach ($parent->children
as &$child) {
698 if ($child->id
== $id) {
706 /** Return next post in the thread
707 * @param id INTEGER message number
709 public function nextPost($id)
711 $cur =& $this->overview
[$id];
712 if (count($cur->children
) != 0) {
713 return $cur->children
[0]->id
;
718 $parent =& $cur->parent
;
719 if (is_null($parent)) {
723 foreach ($parent->children
as &$child) {
727 if ($child === $cur) {
736 /** Look for an unread message in the thread rooted by the message
737 * @param id INTEGER message number
739 private function _nextUnread(BananaSpoolHead
&$cur)
744 foreach ($cur->children
as &$child) {
745 $unread = $this->_nextUnread($child);
746 if (!is_null($unread)) {
753 /** Find next unread message
754 * @param id INTEGER message number
756 public function nextUnread($id = null
)
758 if (!$this->unreadnb
) {
763 // Look in message children
764 foreach ($this->overview
[$id]->children
as &$child) {
765 $next = $this->_nextUnread($child);
766 if (!is_null($next)) {
772 // Look in current thread
776 $cur =& $this->overview
[$id];
783 $parent =& $cur->parent
;
786 if (!is_null($parent)) {
787 $array =& $parent->children
;
789 $array =& $this->roots
;
791 foreach ($array as &$child) {
793 $next = $this->_nextUnread($child);
794 if (!is_null($next)) {
798 if ($child === $cur) {
803 } while(!is_null($cur));
808 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: