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