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