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