Fix mbox and feed to the new spool
[banana.git] / banana / spool.inc.php
CommitLineData
810ac1df 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
7027794f 10require_once dirname(__FILE__) . '/banana.inc.php';
3ca86dfe 11
0580f927 12define('BANANA_SPOOL_VERSION', '0.5.12');
01681efd 13
810ac1df 14/** Class spoolhead
15 * class used in thread overviews
16 */
d4c19591 17class BananaSpoolHead
e785d91c 18{
0580f927 19 public $id;
20 public $msgid;
21
e785d91c 22 /** date (timestamp) */
7027794f 23 public $date;
e785d91c 24 /** subject */
7027794f 25 public $subject;
e785d91c 26 /** author */
7027794f 27 public $from;
66e81236 28 public $name;
dce7d862 29 public $color;
e785d91c 30 /** reference of parent */
7027794f 31 public $parent = null;
e785d91c 32 /** array of children */
7027794f 33 public $children = Array();
e785d91c 34 /** true if post is read */
7027794f 35 public $isread;
e785d91c 36 /** number of posts deeper in this branch of tree */
7027794f 37 public $desc;
e785d91c 38 /** same as desc, but counts only unread posts */
7027794f 39 public $descunread;
40
41 /** storage data */
42 public $storage = array();
810ac1df 43
e785d91c 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 */
0580f927 52 public function __construct($id, array &$message)
e785d91c 53 {
0580f927 54 $this->id = $id;
954b9478 55 $this->msgid = @$message['message-id'];
7027794f 56 $this->date = $message['date'];
954b9478 57 $this->subject = @$message['subject'];
7027794f 58 $this->from = $message['from'];
dce7d862 59 $this->color = sprintf('#%06x', abs(crc32($this->from) % 0xffffff));
7027794f 60 $this->desc = 1;
61 $this->isread = true;
62 $this->descunread = 0;
66e81236 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 }
e785d91c 77 }
810ac1df 78}
79
3ca86dfe 80
d4c19591 81class BananaSpool
e785d91c 82{
7027794f 83 private $version;
e9360b11 84 private $mode;
7027794f 85
e785d91c 86 /** group name */
7027794f 87 public $group;
88 /** spool */
0580f927 89 public $overview = array();
e785d91c 90 /** array msgid => msgnum */
0580f927 91 public $ids = array();
cced14b6 92 /** thread starts */
0580f927 93 public $roots = array();
66e81236 94 /** thread trees (one tree per root node) */
0580f927 95 public $trees = array();
66e81236 96
dc5f77ad 97 /** protocole specific data */
98 public $storage = array();
7027794f 99
d265608a 100 private $unreadnb = 0;
810ac1df 101
e785d91c 102 /** constructor
e785d91c 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 */
7027794f 107 protected function __construct($group)
e785d91c 108 {
7027794f 109 $this->version = BANANA_SPOOL_VERSION;
e9360b11 110 $this->mode = Banana::SPOOL_ALL;
7027794f 111 $this->group = $group;
112 }
fb6428c8 113
e02edbfe 114 public static function &getSpool($group, $since = 0, $clean = false)
7027794f 115 {
116 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
b3d11736 117 $spool =& Banana::$spool;
82c17a91 118 } else {
e02edbfe 119 $spool =& BananaSpool::readFromFile($group);
64f89fae 120 }
7027794f 121 if (is_null($spool)) {
122 $spool = new BananaSpool($group);
82c17a91 123 }
7027794f 124 Banana::$spool =& $spool;
125 $spool->build();
b3d11736 126 if ($clean) {
127 $spool->markAllAsRead();
128 }
7027794f 129 $spool->updateUnread($since);
190f587f 130 //var_dump($spool->trees);
7027794f 131 return $spool;
132 }
e785d91c 133
7027794f 134 private static function spoolFilename($group)
135 {
edff14de 136 $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/';
7027794f 137 if (!is_dir($file)) {
138 mkdir($file);
dd7d1c59 139 }
e9360b11 140 return $file . Banana::$protocole->filename();
dd7d1c59 141 }
142
e02edbfe 143 private static function &readFromFile($group)
dd7d1c59 144 {
e02edbfe 145 $spool = null;
7027794f 146 $file = BananaSpool::spoolFilename($group);
147 if (!file_exists($file)) {
e02edbfe 148 return $spool;
dd7d1c59 149 }
7027794f 150 $spool = unserialize(file_get_contents($file));
e9360b11 151 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
e02edbfe 152 $spool = null;
153 return $spool;
7027794f 154 }
d265608a 155 $spool->markAllAsRead();
7027794f 156 return $spool;
157 }
158
0580f927 159 private function compare(&$a, &$b)
7027794f 160 {
0580f927 161 return ($b->date - $a->date);
dd7d1c59 162 }
163
7027794f 164 private function saveToFile()
dd7d1c59 165 {
7027794f 166 $file = BananaSpool::spoolFilename($this->group);
dd7d1c59 167
168 $this->roots = Array();
0580f927 169 foreach($this->overview as &$msg) {
dd7d1c59 170 if (is_null($msg->parent)) {
0580f927 171 $this->roots[] =& $msg;
dd7d1c59 172 }
173 }
345c3a85 174 usort($this->roots, array($this, 'compare'));
2c606d23 175
e9360b11 176 if ($this->mode == Banana::SPOOL_ALL) {
177 file_put_contents($file, serialize($this));
64f89fae 178 }
dd7d1c59 179 }
180
7027794f 181 private function build()
182 {
183 $threshold = 0;
184
185 // Compute the range of indexes
186 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
187 if ($last < $first) {
6bd49b4b 188 $threshold = $first + $msgnum - $last;
7027794f 189 $threshold = (int)(log($threshold)/log(2));
190 $threshold = (2 ^ ($threshold + 1)) - 1;
191 }
e9360b11 192 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
193 $first = $last - Banana::$spool_max;
7027794f 194 if ($first < 0) {
195 $first += $threshold;
196 }
197 }
198 $clean = $this->clean($first, $last, $msgnum);
199 $update = $this->update($first, $last, $msgnum);
64f89fae 200
7027794f 201 if ($clean || $update) {
202 $this->saveToFile();
203 }
204 }
64f89fae 205
7027794f 206 private function clean(&$first, &$last, $msgnum)
9090c673 207 {
7027794f 208 $do_save = false;
0580f927 209 if (!empty($this->overview)) {
7027794f 210 $mids = array_keys($this->overview);
211 foreach ($mids as $id) {
212 if (($first <= $last && ($id < $first || $id > $last))
e9360b11 213 || ($first > $last && $id < $first && $id > $last)) {
7027794f 214 $this->delid($id, false);
215 $do_save = true;
216 }
217 }
218 if (!empty($this->overview)) {
219 $first = max(array_keys($this->overview))+1;
220 }
221 }
222 return $do_save;
9090c673
PHM
223 }
224
7027794f 225 private function update(&$first, &$last, $msgnum)
dd7d1c59 226 {
64f89fae 227 if ($first > $last || !$msgnum) {
7027794f 228 return false;
dd7d1c59 229 }
230
7027794f 231 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
232 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
dd7d1c59 233
0580f927 234 // Build all the new Spool Heads
7027794f 235 foreach ($messages as $id=>&$message) {
0580f927 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 }
7027794f 241 }
242
0580f927 243 // Build tree
66e81236 244 $updateTrees = array();
0580f927 245 $null = null;
7027794f 246 foreach ($messages as $id=>&$message) {
0580f927 247 $msg =& $this->overview[$id];
248 $parents =& $this->getReferences($message);
249 while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) {
954b9478 250 @$msg->parent =& array_pop($parents);
3316e34e 251 }
0580f927 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;
190f587f 258 $prev =& $parent;
0580f927 259 if ($parent !== $parent->parent) {
260 $parent =& $parent->parent;
7027794f 261 } else {
0580f927 262 $parent =& $null;
66e81236 263 }
e785d91c 264 }
0580f927 265 $updateTrees[$prev->id] = true;
810ac1df 266 }
810ac1df 267 }
66e81236 268 foreach ($updateTrees as $root=>$t) {
06a59a10 269 $this->trees[$root] =& $this->buildTree($root);
66e81236 270 }
7027794f 271 Banana::$protocole->updateSpool($messages);
272 return true;
dd7d1c59 273 }
e785d91c 274
bffb37b4 275 public function updateUnread($since)
75ff2f64 276 {
7027794f 277 if (empty($since)) {
278 return;
279 }
280
281 $newpostsids = Banana::$protocole->getNewIndexes($since);
b3d11736 282
7027794f 283 if (empty($newpostsids)) {
284 return;
285 }
286
7027794f 287 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
288 foreach ($newpostsids as $mid) {
0580f927 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;
e785d91c 295 }
810ac1df 296 }
7027794f 297 }
0580f927 298 $this->unreadnb += count($newpostsids);
7027794f 299 }
dd7d1c59 300
7027794f 301 public function setMode($mode)
302 {
e9360b11 303 $this->mode = $mode;
7027794f 304 switch ($mode) {
305 case Banana::SPOOL_UNREAD:
64f89fae 306 $num = max(array_keys($this->overview));
307 if ($this->overview[$num]->isread) {
308 break;
309 }
0580f927 310 foreach ($this->roots as &$root) {
311 if ($root->descunread == 0) {
312 $this->killdesc($root->id);
e785d91c 313 }
810ac1df 314 }
7027794f 315 break;
810ac1df 316 }
cced14b6 317 }
318
0580f927 319 /** Fetch list of references
320 */
321 public function &getReferences(array &$refs)
322 {
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];
329 }
330 }
331 } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) {
332 $references[] =& $this->ids[$refs['in-reply-to']];
333 }
334 return $references;
335 }
336
e9360b11 337 /** Mark the given id as read
338 * @param id MSGNUM of post
339 */
340 public function markAsRead($id)
341 {
342 if (!$this->overview[$id]->isread) {
343 $this->overview[$id]->isread = true;
d265608a 344 $this->unreadnb--;
e9360b11 345 while (isset($id)) {
346 $this->overview[$id]->descunread--;
347 $id = $this->overview[$id]->parent;
348 }
349 }
350 }
351
352 /** Mark all unread messages as read
353 */
354 public function markAllAsRead(array &$array = null)
355 {
d265608a 356 if (!$this->unreadnb) {
357 return;
358 }
0580f927 359 if (is_null($array) && !empty($this->roots)) {
e9360b11 360 $array =& $this->roots;
c4f176d8 361 } elseif (is_null($array)) {
c28d3016 362 return;
363 }
0580f927 364 foreach ($array as &$msg) {
365 if (!$msg->isread) {
366 $this->markAsRead($msg->id);
d265608a 367 if (!$this->unreadnb) {
368 return;
369 }
e9360b11 370 }
0580f927 371 if ($msg->descunread) {
372 $this->markAllAsRead($msg->children);
e9360b11 373 }
374 }
375 }
376
e785d91c 377 /** kill post and childrens
378 * @param $_id MSGNUM of post
379 */
7027794f 380 private function killdesc($_id)
e785d91c 381 {
0580f927 382 $overview =& $this->overview[$_id];
383 $children =& $overview->children;
384 if (sizeof($children)) {
385 foreach ($children as &$c) {
386 $this->killdesc($c->id);
e785d91c 387 }
388 }
389 unset($this->overview[$_id]);
0580f927 390 foreach ($this->roots as $k=>&$root) {
391 if ($root === $overview) {
392 unset($this->roots[$k]);
393 break;
394 }
e785d91c 395 }
0580f927 396 unset($this->ids[$overview->msgid]);
397 unset($this->trees[$_id]);
398 $overview = null;
810ac1df 399 }
e785d91c 400
401 /** delete a post from overview
402 * @param $_id MSGNUM of post
403 */
7027794f 404 public function delid($_id, $write = true)
e785d91c 405 {
0580f927 406 if (!isset($this->overview[$_id])) {
407 return;
408 }
409
410 $overview =& $this->overview[$_id];
411 // Be sure it is not counted as unread
412 if (!$overview->isread) {
413 $this->markAsRead($_id);
414 }
415
416 $parent =& $overview->parent;
417
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]);
423 break;
e785d91c 424 }
425 }
0580f927 426 if (sizeof($overview->children)) {
427 $parent->children = array_merge($parent->children, $overview->children);
428 foreach ($overview->children as &$child) {
429 $child->parent =& $parent;
430 }
e785d91c 431 }
0580f927 432 while (!is_null($parent)) {
433 $parent->desc--;
434 $parent =& $parent->parent;
cfb7fe5d 435 }
0580f927 436 }
64f89fae 437
0580f927 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]);
445 break;
7027794f 446 }
e785d91c 447 }
0580f927 448 $overview = null;
449
450 if ($write) {
451 $this->saveToFile();
452 }
35ca8036 453 }
810ac1df 454
0580f927 455 public function formatDate(BananaSpoolHead &$head)
7027794f 456 {
0580f927 457 $stamp = $head->date;
7027794f 458 $today = intval(time() / (24*3600));
459 $dday = intval($stamp / (24*3600));
460
461 if ($today == $dday) {
462 $format = "%H:%M";
463 } elseif ($today == 1 + $dday) {
464 $format = _b_('hier')." %H:%M";
465 } elseif ($today < 7 + $dday) {
466 $format = '%a %H:%M';
d924433f 467 } elseif ($today < 90 + $dday) {
7027794f 468 $format = '%a %e %b';
d924433f 469 } else {
470 $format = '%a %e %b %Y';
7027794f 471 }
22b95309 472 return strftime($format, $stamp);
7027794f 473 }
474
0580f927 475 public function formatSubject(BananaSpoolHead &$head)
66e81236 476 {
0580f927 477 $subject = $popup = $head->subject;
66e81236 478 $popup = $subject;
479 if (function_exists('hook_formatDisplayHeader')) {
480 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
481 } else {
482 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
483 $link = null;
484 }
485 if (empty($subject)) {
486 $subject = _b_('(pas de sujet)');
487 }
0580f927 488 if ($head->id != Banana::$artid) {
489 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
66e81236 490 'text' => $subject, 'popup' => $popup));
491 }
492 return $subject . $link;
493 }
494
0580f927 495 public function formatFrom(BananaSpoolHead &$head)
66e81236 496 {
0580f927 497 return BananaMessage::formatFrom($head->from);
66e81236 498 }
499
500 public function start()
501 {
502 if (Banana::$first) {
503 return Banana::$first;
504 } else {
505 $first = array_search(Banana::$artid, $this->roots);
506 return max(0, $first - Banana::$spool_tbefore);
507 }
508 }
509
510 public function context()
511 {
512 return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext;
513 }
514
65d96b1f 515
0580f927 516 private function &_buildTree(BananaSpoolHead &$head) {
1a85c7a6 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' => '&nbsp;', '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));
530 }
531 $style = 'background-color:' . $head->color . '; text-decoration: none';
0580f927 532 $text = '<span style="' . $style . '" title="' . banana_entities($head->name . ', ' . $this->formatDate($head))
06a59a10 533 . '"><input type="radio" name="banana_tree" '
534 . (Banana::$msgshow_javascript ? 'onchange="window.location=\'' .
190f587f 535 banana_entities(Banana::$page->makeURL(array('group' => $this->group, 'artid' => $head->id))) . '\'"'
66e81236 536 : ' disabled="disabled"')
06a59a10 537 . ' /></span>';
dce7d862 538 $array = array($text);
0580f927 539 foreach ($head->children as $key=>&$msg) {
540 $tree =& $this->_buildTree($msg);
dce7d862 541 $last = $key == count($head->children) - 1;
542 foreach ($tree as $kt=>&$line) {
06a59a10 543 if ($kt === 0 && $key === 0 && !$last) {
1a85c7a6 544 $array[0] .= ($msg->isread ? $r_ht : $u_ht) . $line;
06a59a10 545 } else if($kt === 0 && $key === 0) {
1a85c7a6 546 $array[0] .= ($msg->isread ? $r_h : $u_h) . $line;
06a59a10 547 } else if ($kt === 0 && $last) {
1a85c7a6 548 $array[] = $t_e . ($msg->isread ? $r_vt : $u_vt) . $line;
06a59a10 549 } else if ($kt === 0) {
1a85c7a6 550 $array[] = $t_e . ($msg->isread ? $r_f : $u_f) . $line;
dce7d862 551 } else if ($last) {
1a85c7a6 552 $array[] = $t_e . $t_e . $line;
dce7d862 553 } else {
1a85c7a6 554 $array[] = $t_e . ($msg->isread ? $r_l : $u_l) . $line;
dce7d862 555 }
556 }
66e81236 557 unset($tree);
dce7d862 558 }
06a59a10 559 return $array;
dce7d862 560 }
561
562 /** build the spool tree associated with the given message
563 */
06a59a10 564 public function &buildTree($id, $force = false) {
190f587f 565 $root =& $this->root($id);
566 $id = $root->id;
06a59a10 567 if (!$force && isset($this->trees[$id])) {
568 return $this->trees[$id];
66e81236 569 } else {
190f587f 570 $tree =& $this->_buildTree($root);
06a59a10 571 $tree = '<div class="tree"><div style="height:18px">'
572 . implode("</div>\n<div style=\"height:18px\">", $tree)
573 . '</div></div>';
574 return $tree;
66e81236 575 }
dce7d862 576 }
577
e785d91c 578 /** computes linear post index
579 * @param $_id INTEGER MSGNUM of post
580 * @return INTEGER linear index of post
581 */
7027794f 582 public function getNdX($_id)
75ff2f64 583 {
cced14b6 584 $ndx = 1;
585 $id_cur = $_id;
4ced5065 586 while (true) {
cced14b6 587 $id_parent = $this->overview[$id_cur]->parent;
4ced5065 588 if (is_null($id_parent)) break;
06a59a10 589 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
64f89fae 590
cced14b6 591 for ($i = 0; $i < $pos ; $i++) {
e785d91c 592 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
593 }
951030b7 594 $ndx++; //noeud père
cced14b6 595
596 $id_cur = $id_parent;
810ac1df 597 }
cced14b6 598
599 foreach ($this->roots as $i) {
600 if ($i==$id_cur) {
e785d91c 601 break;
602 }
cced14b6 603 $ndx += $this->overview[$i]->desc;
e785d91c 604 }
605 return $ndx;
810ac1df 606 }
d8e2470c 607
608 /** Return root message of the given thread
609 * @param id INTEGER id of a message
610 */
190f587f 611 public function &root($id)
7027794f 612 {
190f587f 613 $parent =& $this->overview[$id];
614 while (!is_null($parent->parent)) {
615 $parent =& $parent->parent;
d8e2470c 616 }
190f587f 617 return $parent;
d8e2470c 618 }
619
bffb37b4 620 /** Return the last post id with the given subject
621 * @param subject
622 */
623 public function getPostId($subject)
624 {
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)) {
0a5b736c 632 $test = banana_html_entity_decode($val[0]);
bffb37b4 633 } else {
0a5b736c 634 $test = banana_html_entity_decode($val);
bffb37b4 635 }
636 }
637 $test = trim($test);
bffb37b4 638 if ($test == $subject) {
639 return $id;
640 }
641 $id--;
642 }
643 return -1;
644 }
645
d8e2470c 646 /** Returns previous thread root index
647 * @param id INTEGER message number
648 */
7027794f 649 public function prevThread($id)
d8e2470c 650 {
190f587f 651 $root =& $this->root($id);
d8e2470c 652 $last = null;
190f587f 653 foreach ($this->roots as &$i) {
654 if ($i === $root) {
d8e2470c 655 return $last;
656 }
190f587f 657 $last = $i->id;
d8e2470c 658 }
659 return $last;
660 }
661
662 /** Returns next thread root index
663 * @param id INTEGER message number
664 */
7027794f 665 public function nextThread($id)
d8e2470c 666 {
190f587f 667 $root =& $this->root($id);
d8e2470c 668 $ok = false;
190f587f 669 foreach ($this->roots as &$i) {
d8e2470c 670 if ($ok) {
190f587f 671 return $i->id;
d8e2470c 672 }
190f587f 673 if ($i === $root) {
d8e2470c 674 $ok = true;
675 }
676 }
677 return null;
678 }
679
680 /** Return prev post in the thread
681 * @param id INTEGER message number
682 */
7027794f 683 public function prevPost($id)
d8e2470c 684 {
190f587f 685 $parent =& $this->overview[$id]->parent;
d8e2470c 686 if (is_null($parent)) {
687 return null;
688 }
190f587f 689 $last = $parent->id;
690 foreach ($parent->children as &$child) {
691 if ($child->id == $id) {
d8e2470c 692 return $last;
693 }
190f587f 694 $last = $child->id;
d8e2470c 695 }
696 return null;
697 }
698
699 /** Return next post in the thread
700 * @param id INTEGER message number
701 */
7027794f 702 public function nextPost($id)
d8e2470c 703 {
190f587f 704 $cur =& $this->overview[$id];
705 if (count($cur->children) != 0) {
706 return $cur->children[0]->id;
d8e2470c 707 }
64f89fae 708
190f587f 709 $parent =& $cur;
d8e2470c 710 while (true) {
190f587f 711 $parent =& $cur->parent;
d8e2470c 712 if (is_null($parent)) {
713 return null;
714 }
715 $ok = false;
190f587f 716 foreach ($parent->children as &$child) {
d8e2470c 717 if ($ok) {
190f587f 718 return $child->id;
d8e2470c 719 }
190f587f 720 if ($child === $cur) {
d8e2470c 721 $ok = true;
722 }
723 }
190f587f 724 $cur =& $parent;
d8e2470c 725 }
726 return null;
727 }
d634c13c 728
729 /** Look for an unread message in the thread rooted by the message
730 * @param id INTEGER message number
731 */
190f587f 732 private function _nextUnread(BananaSpoolHead &$cur)
d634c13c 733 {
190f587f 734 if (!$cur->isread) {
735 return $cur->id;
d634c13c 736 }
190f587f 737 foreach ($cur->children as &$child) {
bdad7c9d 738 $unread = $this->_nextUnread($child);
739 if (!is_null($unread)) {
740 return $unread;
64f89fae 741 }
d634c13c 742 }
743 return null;
744 }
745
746 /** Find next unread message
747 * @param id INTEGER message number
748 */
7027794f 749 public function nextUnread($id = null)
d634c13c 750 {
d265608a 751 if (!$this->unreadnb) {
752 return null;
753 }
754
b7d59a47 755 if (!is_null($id)) {
756 // Look in message children
190f587f 757 foreach ($this->overview[$id]->children as &$child) {
b7d59a47 758 $next = $this->_nextUnread($child);
759 if (!is_null($next)) {
760 return $next;
761 }
d634c13c 762 }
763 }
764
765 // Look in current thread
190f587f 766 if (is_null($id)) {
767 $cur = null;
768 } else {
769 $cur =& $this->overview[$id];
770 }
b7d59a47 771 do {
190f587f 772 if (is_null($cur)) {
773 $parent =& $cur;
774 $ok = true;
775 } else {
776 $parent =& $cur->parent;
777 $ok = false;
778 }
1e016f3a 779 if (!is_null($parent)) {
190f587f 780 $array =& $parent->children;
d634c13c 781 } else {
190f587f 782 $array =& $this->roots;
d634c13c 783 }
190f587f 784 foreach ($array as &$child) {
d634c13c 785 if ($ok) {
786 $next = $this->_nextUnread($child);
787 if (!is_null($next)) {
788 return $next;
789 }
790 }
190f587f 791 if ($child === $cur) {
d634c13c 792 $ok = true;
793 }
794 }
190f587f 795 $cur =& $parent;
b7d59a47 796 } while(!is_null($cur));
d634c13c 797 return null;
64f89fae 798 }
810ac1df 799}
800
598a1c53 801// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
810ac1df 802?>