| 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 | |
| 12 | define('BANANA_SPOOL_VERSION', '0.3'); |
| 13 | |
| 14 | /** Class spoolhead |
| 15 | * class used in thread overviews |
| 16 | */ |
| 17 | class BananaSpoolHead |
| 18 | { |
| 19 | /** date (timestamp) */ |
| 20 | public $date; |
| 21 | /** subject */ |
| 22 | public $subject; |
| 23 | /** author */ |
| 24 | public $from; |
| 25 | /** reference of parent */ |
| 26 | public $parent = null; |
| 27 | /** paren is direct */ |
| 28 | public $parent_direct; |
| 29 | /** array of children */ |
| 30 | public $children = Array(); |
| 31 | /** true if post is read */ |
| 32 | public $isread; |
| 33 | /** number of posts deeper in this branch of tree */ |
| 34 | public $desc; |
| 35 | /** same as desc, but counts only unread posts */ |
| 36 | public $descunread; |
| 37 | |
| 38 | /** storage data */ |
| 39 | public $storage = array(); |
| 40 | |
| 41 | /** constructor |
| 42 | * @param $_date INTEGER timestamp of post |
| 43 | * @param $_subject STRING subject of post |
| 44 | * @param $_from STRING author of post |
| 45 | * @param $_desc INTEGER desc value (1 for a new post) |
| 46 | * @param $_read BOOLEAN true if read |
| 47 | * @param $_descunread INTEGER descunread value (0 for a new post) |
| 48 | */ |
| 49 | public function __construct(array &$message) |
| 50 | { |
| 51 | $this->date = $message['date']; |
| 52 | $this->subject = $message['subject']; |
| 53 | $this->from = $message['from']; |
| 54 | $this->desc = 1; |
| 55 | $this->isread = true; |
| 56 | $this->descunread = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | class BananaSpool |
| 62 | { |
| 63 | private $version; |
| 64 | private $mode; |
| 65 | |
| 66 | /** group name */ |
| 67 | public $group; |
| 68 | /** spool */ |
| 69 | public $overview; |
| 70 | /** array msgid => msgnum */ |
| 71 | public $ids; |
| 72 | /** thread starts */ |
| 73 | public $roots; |
| 74 | |
| 75 | |
| 76 | /** constructor |
| 77 | * @param $_group STRING group name |
| 78 | * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts |
| 79 | * @param $_since INTEGER time stamp (used for read/unread) |
| 80 | */ |
| 81 | protected function __construct($group) |
| 82 | { |
| 83 | $this->version = BANANA_SPOOL_VERSION; |
| 84 | $this->mode = Banana::SPOOL_ALL; |
| 85 | $this->group = $group; |
| 86 | } |
| 87 | |
| 88 | public static function getSpool($group, $since = 0, $clean = false) |
| 89 | { |
| 90 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { |
| 91 | $spool =& Banana::$spool; |
| 92 | } else { |
| 93 | $spool = BananaSpool::readFromFile($group); |
| 94 | } |
| 95 | if (is_null($spool)) { |
| 96 | $spool = new BananaSpool($group); |
| 97 | } |
| 98 | Banana::$spool =& $spool; |
| 99 | $spool->build(); |
| 100 | if ($clean) { |
| 101 | $spool->markAllAsRead(); |
| 102 | } |
| 103 | $spool->updateUnread($since); |
| 104 | return $spool; |
| 105 | } |
| 106 | |
| 107 | private static function spoolFilename($group) |
| 108 | { |
| 109 | $file = dirname(dirname(__FILE__)); |
| 110 | $file .= '/spool/' . Banana::$protocole->name() . '/'; |
| 111 | if (!is_dir($file)) { |
| 112 | mkdir($file); |
| 113 | } |
| 114 | return $file . Banana::$protocole->filename(); |
| 115 | } |
| 116 | |
| 117 | private static function readFromFile($group) |
| 118 | { |
| 119 | $file = BananaSpool::spoolFilename($group); |
| 120 | if (!file_exists($file)) { |
| 121 | return null; |
| 122 | } |
| 123 | $spool = unserialize(file_get_contents($file)); |
| 124 | if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { |
| 125 | return null; |
| 126 | } |
| 127 | return $spool; |
| 128 | } |
| 129 | |
| 130 | private function compare($a, $b) |
| 131 | { |
| 132 | return ($b->date >= $a->date); |
| 133 | } |
| 134 | |
| 135 | private function saveToFile() |
| 136 | { |
| 137 | $file = BananaSpool::spoolFilename($this->group); |
| 138 | uasort($this->overview, array($this, 'compare')); |
| 139 | |
| 140 | $this->roots = Array(); |
| 141 | foreach($this->overview as $id=>$msg) { |
| 142 | if (is_null($msg->parent)) { |
| 143 | $this->roots[] = $id; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ($this->mode == Banana::SPOOL_ALL) { |
| 148 | file_put_contents($file, serialize($this)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private function build() |
| 153 | { |
| 154 | $threshold = 0; |
| 155 | |
| 156 | // Compute the range of indexes |
| 157 | list($msgnum, $first, $last) = Banana::$protocole->getIndexes(); |
| 158 | if ($last < $first) { |
| 159 | $threshold = $firt + $msgnum - $last; |
| 160 | $threshold = (int)(log($threshold)/log(2)); |
| 161 | $threshold = (2 ^ ($threshold + 1)) - 1; |
| 162 | } |
| 163 | if (Banana::$spool_max && Banana::$spool_max < $msgnum) { |
| 164 | $first = $last - Banana::$spool_max; |
| 165 | if ($first < 0) { |
| 166 | $first += $threshold; |
| 167 | } |
| 168 | } |
| 169 | $clean = $this->clean($first, $last, $msgnum); |
| 170 | $update = $this->update($first, $last, $msgnum); |
| 171 | |
| 172 | if ($clean || $update) { |
| 173 | $this->saveToFile(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private function clean(&$first, &$last, $msgnum) |
| 178 | { |
| 179 | $do_save = false; |
| 180 | if (is_array($this->overview)) { |
| 181 | $mids = array_keys($this->overview); |
| 182 | foreach ($mids as $id) { |
| 183 | if (($first <= $last && ($id < $first || $id > $last)) |
| 184 | || ($first > $last && $id < $first && $id > $last)) { |
| 185 | $this->delid($id, false); |
| 186 | $do_save = true; |
| 187 | } |
| 188 | } |
| 189 | if (!empty($this->overview)) { |
| 190 | $first = max(array_keys($this->overview))+1; |
| 191 | } |
| 192 | } |
| 193 | return $do_save; |
| 194 | } |
| 195 | |
| 196 | private function update(&$first, &$last, $msgnum) |
| 197 | { |
| 198 | if ($first > $last || !$msgnum) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
| 203 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); |
| 204 | |
| 205 | if (!is_array($this->ids)) { |
| 206 | $this->ids = array(); |
| 207 | } |
| 208 | foreach ($messages as $id=>&$message) { |
| 209 | $this->ids[$message['message-id']] = $id; |
| 210 | } |
| 211 | |
| 212 | if (!is_array($this->overview)) { |
| 213 | $this->overview = array(); |
| 214 | } |
| 215 | foreach ($messages as $id=>&$message) { |
| 216 | if (!isset($this->overview[$id])) { |
| 217 | $this->overview[$id] = new BananaSpoolHead($message); |
| 218 | } |
| 219 | $msg =& $this->overview[$id]; |
| 220 | $msgrefs = BananaMessage::formatReferences($message); |
| 221 | $parents = preg_grep('/^\d+$/', $msgrefs); |
| 222 | $msg->parent = array_pop($parents); |
| 223 | $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); |
| 224 | |
| 225 | if (!is_null($p = $msg->parent)) { |
| 226 | if (empty($this->overview[$p])) { |
| 227 | $this->overview[$p] = new BananaSpoolHead($messages[$p]); |
| 228 | } |
| 229 | $this->overview[$p]->children[] = $id; |
| 230 | |
| 231 | while (!is_null($p)) { |
| 232 | $this->overview[$p]->desc += $msg->desc; |
| 233 | if ($p != $this->overview[$p]->parent) { |
| 234 | $p = $this->overview[$p]->parent; |
| 235 | } else { |
| 236 | $p = null; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | Banana::$protocole->updateSpool($messages); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | private function updateUnread($since) |
| 246 | { |
| 247 | if (empty($since)) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | $newpostsids = Banana::$protocole->getNewIndexes($since); |
| 252 | |
| 253 | if (empty($newpostsids)) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (!is_array($this->ids)) { |
| 258 | $this->ids = array(); |
| 259 | } |
| 260 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
| 261 | foreach ($newpostsids as $mid) { |
| 262 | $id = $this->ids[$mid]; |
| 263 | if ($this->overview[$id]->isread) { |
| 264 | $this->overview[$id]->isread = false; |
| 265 | while (isset($id)) { |
| 266 | $this->overview[$id]->descunread ++; |
| 267 | $id = $this->overview[$id]->parent; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | public function setMode($mode) |
| 274 | { |
| 275 | $this->mode = $mode; |
| 276 | switch ($mode) { |
| 277 | case Banana::SPOOL_UNREAD: |
| 278 | foreach ($this->roots as $k=>$i) { |
| 279 | if ($this->overview[$i]->descunread == 0) { |
| 280 | $this->killdesc($i); |
| 281 | unset($this->roots[$k]); |
| 282 | } |
| 283 | } |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** Mark the given id as read |
| 289 | * @param id MSGNUM of post |
| 290 | */ |
| 291 | public function markAsRead($id) |
| 292 | { |
| 293 | if (!$this->overview[$id]->isread) { |
| 294 | $this->overview[$id]->isread = true; |
| 295 | while (isset($id)) { |
| 296 | $this->overview[$id]->descunread--; |
| 297 | $id = $this->overview[$id]->parent; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** Mark all unread messages as read |
| 303 | */ |
| 304 | public function markAllAsRead(array &$array = null) |
| 305 | { |
| 306 | if (is_null($array)) { |
| 307 | $array =& $this->roots; |
| 308 | } |
| 309 | if (!is_array($this->roots)) { |
| 310 | return; |
| 311 | } |
| 312 | foreach ($array as $id) { |
| 313 | if (!$this->overview[$id]->isread) { |
| 314 | $this->markAsRead($id); |
| 315 | } |
| 316 | if ($this->overview[$id]->descunread) { |
| 317 | $this->markAllAsRead($this->overview[$id]->children); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** kill post and childrens |
| 323 | * @param $_id MSGNUM of post |
| 324 | */ |
| 325 | private function killdesc($_id) |
| 326 | { |
| 327 | if (sizeof($this->overview[$_id]->children)) { |
| 328 | foreach ($this->overview[$_id]->children as $c) { |
| 329 | $this->killdesc($c); |
| 330 | } |
| 331 | } |
| 332 | unset($this->overview[$_id]); |
| 333 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
| 334 | unset($this->ids[$msgid]); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** delete a post from overview |
| 339 | * @param $_id MSGNUM of post |
| 340 | */ |
| 341 | public function delid($_id, $write = true) |
| 342 | { |
| 343 | if (isset($this->overview[$_id])) { |
| 344 | if (sizeof($this->overview[$_id]->parent)) { |
| 345 | $this->overview[$this->overview[$_id]->parent]->children = |
| 346 | array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); |
| 347 | if (sizeof($this->overview[$_id]->children)) { |
| 348 | $this->overview[$this->overview[$_id]->parent]->children = |
| 349 | array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); |
| 350 | foreach ($this->overview[$_id]->children as $c) { |
| 351 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
| 352 | $this->overview[$c]->parent_direct = false; |
| 353 | } |
| 354 | } |
| 355 | $p = $this->overview[$_id]->parent; |
| 356 | while ($p) { |
| 357 | $this->overview[$p]->desc--; |
| 358 | $p = $this->overview[$p]->parent; |
| 359 | } |
| 360 | } elseif (sizeof($this->overview[$_id]->children)) { |
| 361 | foreach ($this->overview[$_id]->children as $c) { |
| 362 | $this->overview[$c]->parent = null; |
| 363 | } |
| 364 | } |
| 365 | unset($this->overview[$_id]); |
| 366 | $msgid = array_search($_id, $this->ids); |
| 367 | if ($msgid !== false) { |
| 368 | unset($this->ids[$msgid]); |
| 369 | } |
| 370 | $msgid = array_search($_id, $this->roots); |
| 371 | if ($msgid !== false) { |
| 372 | unset($this->roots[$msgid]); |
| 373 | } |
| 374 | |
| 375 | if ($write) { |
| 376 | $this->markAllAsRead(); |
| 377 | $this->saveToFile(); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | private function formatDate($stamp) |
| 383 | { |
| 384 | $today = intval(time() / (24*3600)); |
| 385 | $dday = intval($stamp / (24*3600)); |
| 386 | |
| 387 | if ($today == $dday) { |
| 388 | $format = "%H:%M"; |
| 389 | } elseif ($today == 1 + $dday) { |
| 390 | $format = _b_('hier')." %H:%M"; |
| 391 | } elseif ($today < 7 + $dday) { |
| 392 | $format = '%a %H:%M'; |
| 393 | } else { |
| 394 | $format = '%a %e %b'; |
| 395 | } |
| 396 | return utf8_encode(strftime($format, $stamp)); |
| 397 | } |
| 398 | |
| 399 | /** displays children tree of a post |
| 400 | * @param $_id INTEGER MSGNUM of post |
| 401 | * @param $_index INTEGER linear number of post in the tree |
| 402 | * @param $_first INTEGER linear number of first post displayed |
| 403 | * @param $_last INTEGER linear number of last post displayed |
| 404 | * @param $_ref STRING MSGNUM of current post |
| 405 | * @param $_pfx_node STRING prefix used for current node |
| 406 | * @param $_pfx_end STRING prefix used for children of current node |
| 407 | * @param $_head BOOLEAN true if first post in thread |
| 408 | * |
| 409 | * If you want to analyse subject, you can define the function hook_formatDisplayHeader |
| 410 | */ |
| 411 | private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
| 412 | { |
| 413 | static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I; |
| 414 | if (!isset($spfx_f)) { |
| 415 | $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9)); |
| 416 | $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9)); |
| 417 | $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12)); |
| 418 | $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12)); |
| 419 | $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5)); |
| 420 | $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12)); |
| 421 | $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12)); |
| 422 | $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5)); |
| 423 | $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12)); |
| 424 | $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12)); |
| 425 | } |
| 426 | |
| 427 | $overview =& $this->overview[$_id]; |
| 428 | if ($_index + $overview->desc < $_first || $_index > $_last) { |
| 429 | return ''; |
| 430 | } |
| 431 | |
| 432 | $res = ''; |
| 433 | if ($_index >= $_first) { |
| 434 | $hc = empty($overview->children); |
| 435 | |
| 436 | $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n"; |
| 437 | $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n"; |
| 438 | $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">' |
| 439 | . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n); |
| 440 | $subject = $overview->subject; |
| 441 | if (function_exists('hook_formatDisplayHeader')) { |
| 442 | list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); |
| 443 | } else { |
| 444 | $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject))); |
| 445 | $link = null; |
| 446 | } |
| 447 | if (empty($subject)) { |
| 448 | $subject = _b_('(pas de sujet)'); |
| 449 | } |
| 450 | if ($_index != $_ref) { |
| 451 | $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id, |
| 452 | 'text' => $subject, 'popup' => $subject)); |
| 453 | } |
| 454 | $res .= ' ' . $subject . $link; |
| 455 | $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>"; |
| 456 | |
| 457 | if ($hc) { |
| 458 | return $res; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | $_index ++; |
| 463 | $children = $overview->children; |
| 464 | while ($child = array_shift($children)) { |
| 465 | $overview =& $this->overview[$child]; |
| 466 | if ($_index > $_last) { |
| 467 | return $res; |
| 468 | } |
| 469 | if ($_index + $overview->desc >= $_first) { |
| 470 | if (sizeof($children)) { |
| 471 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
| 472 | $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd), |
| 473 | $_pfx_end . $spfx_I, false); |
| 474 | } else { |
| 475 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
| 476 | $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd), |
| 477 | $_pfx_end . $spfx_e, false); |
| 478 | } |
| 479 | } |
| 480 | $_index += $overview->desc; |
| 481 | } |
| 482 | |
| 483 | return $res; |
| 484 | } |
| 485 | |
| 486 | /** Displays overview |
| 487 | * @param $_first INTEGER MSGNUM of first post |
| 488 | * @param $_last INTEGER MSGNUM of last post |
| 489 | * @param $_ref STRING MSGNUM of current/selectionned post |
| 490 | */ |
| 491 | public function toHtml($first = 0, $overview = false) |
| 492 | { |
| 493 | $res = ''; |
| 494 | |
| 495 | if (!$overview) { |
| 496 | $_first = $first; |
| 497 | $_last = $first + Banana::$spool_tmax - 1; |
| 498 | $_ref = null; |
| 499 | } else { |
| 500 | $_ref = $this->getNdx($first); |
| 501 | $_last = $_ref + Banana::$spool_tafter; |
| 502 | $_first = $_ref - Banana::$spool_tbefore; |
| 503 | if ($_first < 0) { |
| 504 | $_last -= $_first; |
| 505 | } |
| 506 | } |
| 507 | $index = 1; |
| 508 | foreach ($this->roots as $id) { |
| 509 | $res .= $this->_to_html($id, $index, $_first, $_last, $_ref); |
| 510 | $index += $this->overview[$id]->desc ; |
| 511 | if ($index > $_last) { |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | return $res; |
| 516 | } |
| 517 | |
| 518 | /** computes linear post index |
| 519 | * @param $_id INTEGER MSGNUM of post |
| 520 | * @return INTEGER linear index of post |
| 521 | */ |
| 522 | public function getNdX($_id) |
| 523 | { |
| 524 | $ndx = 1; |
| 525 | $id_cur = $_id; |
| 526 | while (true) { |
| 527 | $id_parent = $this->overview[$id_cur]->parent; |
| 528 | if (is_null($id_parent)) break; |
| 529 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
| 530 | |
| 531 | for ($i = 0; $i < $pos ; $i++) { |
| 532 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
| 533 | } |
| 534 | $ndx++; //noeud père |
| 535 | |
| 536 | $id_cur = $id_parent; |
| 537 | } |
| 538 | |
| 539 | foreach ($this->roots as $i) { |
| 540 | if ($i==$id_cur) { |
| 541 | break; |
| 542 | } |
| 543 | $ndx += $this->overview[$i]->desc; |
| 544 | } |
| 545 | return $ndx; |
| 546 | } |
| 547 | |
| 548 | /** Return root message of the given thread |
| 549 | * @param id INTEGER id of a message |
| 550 | */ |
| 551 | public function root($id) |
| 552 | { |
| 553 | $id_cur = $id; |
| 554 | while (true) { |
| 555 | $id_parent = $this->overview[$id_cur]->parent; |
| 556 | if (is_null($id_parent)) break; |
| 557 | $id_cur = $id_parent; |
| 558 | } |
| 559 | return $id_cur; |
| 560 | } |
| 561 | |
| 562 | /** Returns previous thread root index |
| 563 | * @param id INTEGER message number |
| 564 | */ |
| 565 | public function prevThread($id) |
| 566 | { |
| 567 | $root = $this->root($id); |
| 568 | $last = null; |
| 569 | foreach ($this->roots as $i) { |
| 570 | if ($i == $root) { |
| 571 | return $last; |
| 572 | } |
| 573 | $last = $i; |
| 574 | } |
| 575 | return $last; |
| 576 | } |
| 577 | |
| 578 | /** Returns next thread root index |
| 579 | * @param id INTEGER message number |
| 580 | */ |
| 581 | public function nextThread($id) |
| 582 | { |
| 583 | $root = $this->root($id); |
| 584 | $ok = false; |
| 585 | foreach ($this->roots as $i) { |
| 586 | if ($ok) { |
| 587 | return $i; |
| 588 | } |
| 589 | if ($i == $root) { |
| 590 | $ok = true; |
| 591 | } |
| 592 | } |
| 593 | return null; |
| 594 | } |
| 595 | |
| 596 | /** Return prev post in the thread |
| 597 | * @param id INTEGER message number |
| 598 | */ |
| 599 | public function prevPost($id) |
| 600 | { |
| 601 | $parent = $this->overview[$id]->parent; |
| 602 | if (is_null($parent)) { |
| 603 | return null; |
| 604 | } |
| 605 | $last = $parent; |
| 606 | foreach ($this->overview[$parent]->children as $child) { |
| 607 | if ($child == $id) { |
| 608 | return $last; |
| 609 | } |
| 610 | $last = $child; |
| 611 | } |
| 612 | return null; |
| 613 | } |
| 614 | |
| 615 | /** Return next post in the thread |
| 616 | * @param id INTEGER message number |
| 617 | */ |
| 618 | public function nextPost($id) |
| 619 | { |
| 620 | if (count($this->overview[$id]->children) != 0) { |
| 621 | return $this->overview[$id]->children[0]; |
| 622 | } |
| 623 | |
| 624 | $cur = $id; |
| 625 | while (true) { |
| 626 | $parent = $this->overview[$cur]->parent; |
| 627 | if (is_null($parent)) { |
| 628 | return null; |
| 629 | } |
| 630 | $ok = false; |
| 631 | foreach ($this->overview[$parent]->children as $child) { |
| 632 | if ($ok) { |
| 633 | return $child; |
| 634 | } |
| 635 | if ($child == $cur) { |
| 636 | $ok = true; |
| 637 | } |
| 638 | } |
| 639 | $cur = $parent; |
| 640 | } |
| 641 | return null; |
| 642 | } |
| 643 | |
| 644 | /** Look for an unread message in the thread rooted by the message |
| 645 | * @param id INTEGER message number |
| 646 | */ |
| 647 | private function _nextUnread($id) |
| 648 | { |
| 649 | if (!$this->overview[$id]->isread) { |
| 650 | return $id; |
| 651 | } |
| 652 | foreach ($this->overview[$id]->children as $child) { |
| 653 | $unread = $this->_nextUnread($child); |
| 654 | if (!is_null($unread)) { |
| 655 | return $unread; |
| 656 | } |
| 657 | } |
| 658 | return null; |
| 659 | } |
| 660 | |
| 661 | /** Find next unread message |
| 662 | * @param id INTEGER message number |
| 663 | */ |
| 664 | public function nextUnread($id = null) |
| 665 | { |
| 666 | if (!is_null($id)) { |
| 667 | // Look in message children |
| 668 | foreach ($this->overview[$id]->children as $child) { |
| 669 | $next = $this->_nextUnread($child); |
| 670 | if (!is_null($next)) { |
| 671 | return $next; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Look in current thread |
| 677 | $cur = $id; |
| 678 | do { |
| 679 | $parent = is_null($cur) ? null : $this->overview[$cur]->parent; |
| 680 | $ok = is_null($cur) ? true : false; |
| 681 | if (!is_null($parent)) { |
| 682 | $array = &$this->overview[$parent]->children; |
| 683 | } else { |
| 684 | $array = &$this->roots; |
| 685 | } |
| 686 | foreach ($array as $child) { |
| 687 | if ($ok) { |
| 688 | $next = $this->_nextUnread($child); |
| 689 | if (!is_null($next)) { |
| 690 | return $next; |
| 691 | } |
| 692 | } |
| 693 | if ($child == $cur) { |
| 694 | $ok = true; |
| 695 | } |
| 696 | } |
| 697 | $cur = $parent; |
| 698 | } while(!is_null($cur)); |
| 699 | return null; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
| 704 | ?> |