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