ace19f7277dde077404d0aba500faba7a184384c
[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 require_once dirname(__FILE__) . '/tree.inc.php';
12
13 define('BANANA_SPOOL_VERSION', '0.5.14');
14
15 /** Class spoolhead
16 * class used in thread overviews
17 */
18 class BananaSpoolHead
19 {
20 public $id;
21 public $msgid;
22
23 /** date (timestamp) */
24 public $date;
25 /** subject */
26 public $subject;
27 /** author */
28 public $from;
29 public $name;
30 public $color;
31 /** reference of parent */
32 public $parent = null;
33 /** array of children */
34 public $children = Array();
35 /** true if post is read */
36 public $isread;
37 /** number of posts deeper in this branch of tree */
38 public $desc;
39 /** same as desc, but counts only unread posts */
40 public $descunread;
41 /** last time the number of children has been updated */
42 public $time = 0;
43
44 /** storage data */
45 public $storage = array();
46
47 /** constructor
48 * @param $_date INTEGER timestamp of post
49 * @param $_subject STRING subject of post
50 * @param $_from STRING author of post
51 * @param $_desc INTEGER desc value (1 for a new post)
52 * @param $_read BOOLEAN true if read
53 * @param $_descunread INTEGER descunread value (0 for a new post)
54 */
55 public function __construct($id, array &$message)
56 {
57 list($name, $from) = BananaMessage::extractMail($message['from']);
58 $this->id = $id;
59 $this->msgid = @$message['message-id'];
60 $this->date = $message['date'];
61 $this->subject = @$message['subject'];
62 $this->from = $message['from'];
63 $this->color = sprintf('#%06x', abs(crc32($from) % 0xffffff));
64 $this->desc = 1;
65 $this->isread = true;
66 $this->descunread = 0;
67 $this->name = $name;
68 if ($name === $from) {
69 $this->name = 'Anonymous';
70 }
71 }
72 }
73
74
75 class BananaSpool
76 {
77 private $version;
78 private $mode;
79
80 /** group name */
81 public $group;
82 /** spool */
83 public $overview = array();
84 /** array msgid => msgnum */
85 public $ids = array();
86 /** thread starts */
87 public $roots = array();
88
89 /** protocole specific data */
90 public $storage = array();
91
92 private $unreadnb = 0;
93
94 /** constructor
95 * @param $_group STRING group name
96 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
97 * @param $_since INTEGER time stamp (used for read/unread)
98 */
99 protected function __construct($group)
100 {
101 $this->version = BANANA_SPOOL_VERSION;
102 $this->mode = Banana::SPOOL_ALL;
103 $this->group = $group;
104 }
105
106 public static function &getSpool($group, $since = 0, $clean = false)
107 {
108 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
109 $spool =& Banana::$spool;
110 } else {
111 $spool =& BananaSpool::readFromFile($group);
112 }
113 if (is_null($spool)) {
114 $spool = new BananaSpool($group);
115 }
116 Banana::$spool =& $spool;
117 $spool->build();
118 if ($clean) {
119 $spool->markAllAsRead();
120 }
121 $spool->updateUnread($since);
122 return $spool;
123 }
124
125 public static function getPath($file = null)
126 {
127 $path = Banana::$spool_root . '/' . Banana::$protocole->name() . '/' . Banana::$protocole->filename();
128 if (!is_dir($path)) {
129 if (file_exists($path)) {
130 @unlink($path);
131 }
132 mkdir($path, 0777, true);
133 }
134 return $path . '/' . $file;
135 }
136
137 private static function spoolFilename()
138 {
139 return BananaSpool::getPath('spool');
140 }
141
142 private static function &readFromFile($group)
143 {
144 $spool = null;
145 $file = BananaSpool::spoolFilename();
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();
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 $time = time();
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;
240 $head->time = $time;
241 }
242 }
243
244 // Build tree
245 $null = null;
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);
251 }
252
253 if (!is_null($msg->parent)) {
254 $parent =& $msg->parent;
255 $parent->children[] =& $msg;
256 while (!is_null($parent)) {
257 $parent->desc += $msg->desc;
258 $parent->time = $time;
259 $prev =& $parent;
260 if ($parent !== $parent->parent) {
261 $parent =& $parent->parent;
262 } else {
263 $parent =& $null;
264 }
265 }
266 }
267 }
268 Banana::$protocole->updateSpool($messages);
269 return true;
270 }
271
272 public function updateUnread($since)
273 {
274 if (empty($since)) {
275 return;
276 }
277
278 $newpostsids = Banana::$protocole->getNewIndexes($since);
279
280 if (empty($newpostsids)) {
281 return;
282 }
283
284 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
285 foreach ($newpostsids as $mid) {
286 $overview =& $this->ids[$mid];
287 if ($overview->isread) {
288 $overview->isread = false;
289 while (!is_null($overview)) {
290 $overview->descunread++;
291 $overview =& $overview->parent;
292 }
293 }
294 }
295 $this->unreadnb += count($newpostsids);
296
297 if (function_exists('hook_listReadMessages')) {
298 $msgs = hook_listReadMessages($this->group);
299 if (!is_array($msgs)) {
300 return;
301 }
302 foreach ($msgs as $msg) {
303 if (!is_numeric($msg)) {
304 if (!isset($this->ids[$msg])) {
305 continue;
306 }
307 $msg = $this->ids[$msg]->id;
308 }
309 $this->markAsRead($msg);
310 }
311 }
312 }
313
314 public function setMode($mode)
315 {
316 $this->mode = $mode;
317 switch ($mode) {
318 case Banana::SPOOL_UNREAD:
319 $num = max(array_keys($this->overview));
320 if ($this->overview[$num]->isread) {
321 break;
322 }
323 foreach ($this->roots as &$root) {
324 if ($root->descunread == 0) {
325 $this->killdesc($root->id);
326 }
327 }
328 break;
329 }
330 }
331
332 /** Fetch list of references
333 */
334 public function &getReferences(array &$refs)
335 {
336 $references = array();
337 if (isset($refs['references'])) {
338 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
339 foreach ($text as $id=>&$value) {
340 if (isset($this->ids[$value])) {
341 $references[] =& $this->ids[$value];
342 }
343 }
344 } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) {
345 $references[] =& $this->ids[$refs['in-reply-to']];
346 }
347 return $references;
348 }
349
350 /** Get the tree associated to a given id
351 */
352 public function &getTree($id)
353 {
354 return BananaTree::build($id)->show();
355 }
356
357 /** Mark the given id as read
358 * @param id MSGNUM of post
359 */
360 public function markAsRead($id)
361 {
362 $overview =& $this->overview[$id];
363 if (!$overview->isread) {
364 $overview->isread = true;
365 $this->unreadnb--;
366 while (!is_null($overview)) {
367 $overview->descunread--;
368 $overview =& $overview->parent;
369 }
370 }
371 }
372
373 /** Mark all unread messages as read
374 */
375 public function markAllAsRead(array &$array = null)
376 {
377 if (!$this->unreadnb) {
378 return;
379 }
380 if (is_null($array) && !empty($this->roots)) {
381 $array =& $this->roots;
382 } elseif (is_null($array)) {
383 return;
384 }
385 foreach ($array as &$msg) {
386 if (!$msg->isread) {
387 $this->markAsRead($msg->id);
388 if (!$this->unreadnb) {
389 return;
390 }
391 }
392 if ($msg->descunread) {
393 $this->markAllAsRead($msg->children);
394 }
395 }
396 }
397
398 /** kill post and childrens
399 * @param $_id MSGNUM of post
400 */
401 private function killdesc($_id)
402 {
403 $overview =& $this->overview[$_id];
404 $children =& $overview->children;
405 if (sizeof($children)) {
406 foreach ($children as &$c) {
407 $this->killdesc($c->id);
408 }
409 }
410 unset($this->overview[$_id]);
411 foreach ($this->roots as $k=>&$root) {
412 if ($root === $overview) {
413 unset($this->roots[$k]);
414 break;
415 }
416 }
417 unset($this->ids[$overview->msgid]);
418 $overview = null;
419 }
420
421 /** delete a post from overview
422 * @param $_id MSGNUM of post
423 */
424 public function delid($_id, $write = true)
425 {
426 if (!isset($this->overview[$_id])) {
427 return;
428 }
429
430 $overview =& $this->overview[$_id];
431 // Be sure it is not counted as unread
432 if (!$overview->isread) {
433 $this->markAsRead($_id);
434 }
435
436 $parent =& $overview->parent;
437
438 // Remove from the message tree
439 if (!is_null($parent)) {
440 $time = time();
441 foreach ($parent->children as $key=>&$child) {
442 if ($child === $overview) {
443 unset($parent->children[$key]);
444 break;
445 }
446 }
447 if (sizeof($overview->children)) {
448 foreach ($overview->children as &$child) {
449 $parent->children[] =& $child;
450 $child->time = $time;
451 $child->parent =& $parent;
452 }
453 }
454 while (!is_null($parent)) {
455 $parent->desc--;
456 $parent->time = $time;
457 $parent =& $parent->parent;
458 }
459 }
460
461 // Remove all refenrences and assign null to the object
462 unset($this->ids[$overview->msgid]);
463 unset($this->overview[$_id]);
464 BananaTree::kill($_id);
465 foreach ($this->roots as $k=>&$root) {
466 if ($root === $overview) {
467 unset($this->roots[$k]);
468 break;
469 }
470 }
471 $overview = null;
472
473 if ($write) {
474 $this->saveToFile();
475 }
476 }
477
478 public function formatDate(BananaSpoolHead $head)
479 {
480 $stamp = $head->date;
481 $today = intval(time() / (24*3600));
482 $dday = intval($stamp / (24*3600));
483
484 if ($today == $dday) {
485 $format = "%H:%M";
486 } elseif ($today == 1 + $dday) {
487 $format = _b_('hier')." %H:%M";
488 } elseif ($today < 7 + $dday) {
489 $format = '%a %H:%M';
490 } elseif ($today < 90 + $dday) {
491 $format = '%a %e %b';
492 } else {
493 $format = '%a %e %b %Y';
494 }
495 return strftime($format, $stamp);
496 }
497
498 public function formatSubject(BananaSpoolHead $head)
499 {
500 $subject = $popup = $head->subject;
501 $popup = $subject;
502 if (function_exists('hook_formatDisplayHeader')) {
503 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
504 } else {
505 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
506 $link = null;
507 }
508 if (empty($subject)) {
509 $subject = _b_('(pas de sujet)');
510 }
511 if (mb_strlen($subject) > 100) {
512 $subject = mb_substr($subject, 0, 99) . '&hellip;';
513 }
514 if ($head->id !== Banana::$artid) {
515 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
516 'text' => $subject, 'popup' => $popup));
517 }
518 return $subject . $link;
519 }
520
521 public function formatFrom(BananaSpoolHead $head)
522 {
523 return BananaMessage::formatFrom($head->from);
524 }
525
526 public function start()
527 {
528 if (Banana::$first) {
529 return Banana::$first;
530 } else {
531 $first = array_search(Banana::$artid, $this->roots);
532 return max(0, $first - Banana::$spool_tbefore);
533 }
534 }
535
536 public function context()
537 {
538 return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext;
539 }
540
541 /** Return root message of the given thread
542 * @param id INTEGER id of a message
543 */
544 public function &root($id)
545 {
546 $parent =& $this->overview[$id];
547 while (!is_null($parent->parent)) {
548 $parent =& $parent->parent;
549 }
550 return $parent;
551 }
552
553 /** Return the last post id with the given subject
554 * @param subject
555 */
556 public function getPostId($subject)
557 {
558 $subject = trim($subject);
559 $id = max(array_keys($this->overview));
560 while (isset($this->overview[$id])) {
561 $test = $this->overview[$id]->subject;
562 if (function_exists('hook_formatDisplayHeader')) {
563 $val = hook_formatDisplayHeader('subject', $test, true);
564 if (is_array($val)) {
565 $test = banana_html_entity_decode($val[0]);
566 } else {
567 $test = banana_html_entity_decode($val);
568 }
569 }
570 $test = trim($test);
571 if ($test == $subject) {
572 return $id;
573 }
574 $id--;
575 }
576 return -1;
577 }
578
579 /** Returns previous thread root index
580 * @param id INTEGER message number
581 */
582 public function prevThread($id)
583 {
584 $root =& $this->root($id);
585 $last = null;
586 foreach ($this->roots as &$i) {
587 if ($i === $root) {
588 return $last;
589 }
590 $last = $i->id;
591 }
592 return $last;
593 }
594
595 /** Returns next thread root index
596 * @param id INTEGER message number
597 */
598 public function nextThread($id)
599 {
600 $root =& $this->root($id);
601 $ok = false;
602 foreach ($this->roots as &$i) {
603 if ($ok) {
604 return $i->id;
605 }
606 if ($i === $root) {
607 $ok = true;
608 }
609 }
610 return null;
611 }
612
613 /** Return prev post in the thread
614 * @param id INTEGER message number
615 */
616 public function prevPost($id)
617 {
618 $parent =& $this->overview[$id]->parent;
619 if (is_null($parent)) {
620 return null;
621 }
622 $last = $parent->id;
623 foreach ($parent->children as &$child) {
624 if ($child->id == $id) {
625 return $last;
626 }
627 $last = $child->id;
628 }
629 return null;
630 }
631
632 /** Return next post in the thread
633 * @param id INTEGER message number
634 */
635 public function nextPost($id)
636 {
637 $cur =& $this->overview[$id];
638 if (count($cur->children) != 0) {
639 return $cur->children[0]->id;
640 }
641
642 $parent =& $cur;
643 while (true) {
644 $parent =& $cur->parent;
645 if (is_null($parent)) {
646 return null;
647 }
648 $ok = false;
649 foreach ($parent->children as &$child) {
650 if ($ok) {
651 return $child->id;
652 }
653 if ($child === $cur) {
654 $ok = true;
655 }
656 }
657 $cur =& $parent;
658 }
659 return null;
660 }
661
662 /** Look for an unread message in the thread rooted by the message
663 * @param id INTEGER message number
664 */
665 private function _nextUnread(BananaSpoolHead $cur)
666 {
667 if (!$cur->isread) {
668 return $cur->id;
669 }
670 foreach ($cur->children as &$child) {
671 $unread = $this->_nextUnread($child);
672 if (!is_null($unread)) {
673 return $unread;
674 }
675 }
676 return null;
677 }
678
679 /** Find next unread message
680 * @param id INTEGER message number
681 */
682 public function nextUnread($id = null)
683 {
684 if (!$this->unreadnb) {
685 return null;
686 }
687
688 if (!is_null($id)) {
689 // Look in message children
690 foreach ($this->overview[$id]->children as &$child) {
691 $next = $this->_nextUnread($child);
692 if (!is_null($next)) {
693 return $next;
694 }
695 }
696 }
697
698 // Look in current thread
699 if (is_null($id)) {
700 $cur = null;
701 } else {
702 $cur =& $this->overview[$id];
703 }
704 do {
705 if (is_null($cur)) {
706 $parent =& $cur;
707 $ok = true;
708 } else {
709 $parent =& $cur->parent;
710 $ok = false;
711 }
712 if (!is_null($parent)) {
713 $array =& $parent->children;
714 } else {
715 $array =& $this->roots;
716 }
717 foreach ($array as &$child) {
718 if ($ok) {
719 $next = $this->_nextUnread($child);
720 if (!is_null($next)) {
721 return $next;
722 }
723 }
724 if ($child === $cur) {
725 $ok = true;
726 }
727 }
728 $cur =& $parent;
729 } while(!is_null($cur));
730 return null;
731 }
732 }
733 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
734 ?>