Commit | Line | Data |
---|---|---|
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 | 10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
d3b026ab | 11 | require_once dirname(__FILE__) . '/tree.inc.php'; |
3ca86dfe | 12 | |
d3b026ab | 13 | define('BANANA_SPOOL_VERSION', '0.5.14'); |
01681efd | 14 | |
810ac1df | 15 | /** Class spoolhead |
16 | * class used in thread overviews | |
17 | */ | |
d4c19591 | 18 | class BananaSpoolHead |
e785d91c | 19 | { |
0580f927 | 20 | public $id; |
21 | public $msgid; | |
22 | ||
e785d91c | 23 | /** date (timestamp) */ |
7027794f | 24 | public $date; |
e785d91c | 25 | /** subject */ |
7027794f | 26 | public $subject; |
e785d91c | 27 | /** author */ |
7027794f | 28 | public $from; |
66e81236 | 29 | public $name; |
dce7d862 | 30 | public $color; |
e785d91c | 31 | /** reference of parent */ |
7027794f | 32 | public $parent = null; |
e785d91c | 33 | /** array of children */ |
7027794f | 34 | public $children = Array(); |
e785d91c | 35 | /** true if post is read */ |
7027794f | 36 | public $isread; |
e785d91c | 37 | /** number of posts deeper in this branch of tree */ |
7027794f | 38 | public $desc; |
e785d91c | 39 | /** same as desc, but counts only unread posts */ |
7027794f | 40 | public $descunread; |
d3b026ab | 41 | /** last time the number of children has been updated */ |
42 | public $time = 0; | |
7027794f | 43 | |
44 | /** storage data */ | |
45 | public $storage = array(); | |
810ac1df | 46 | |
e785d91c | 47 | /** constructor |
48 | * @param $_date INTEGER timestamp of post | |
49 | * @param $_subject STRING subject of post | |
50 | * @param $_from STRING author of post | |
51 | * @param $_desc INTEGER desc value (1 for a new post) | |
52 | * @param $_read BOOLEAN true if read | |
53 | * @param $_descunread INTEGER descunread value (0 for a new post) | |
54 | */ | |
0580f927 | 55 | public function __construct($id, array &$message) |
e785d91c | 56 | { |
0580f927 | 57 | $this->id = $id; |
954b9478 | 58 | $this->msgid = @$message['message-id']; |
7027794f | 59 | $this->date = $message['date']; |
954b9478 | 60 | $this->subject = @$message['subject']; |
7027794f | 61 | $this->from = $message['from']; |
dce7d862 | 62 | $this->color = sprintf('#%06x', abs(crc32($this->from) % 0xffffff)); |
7027794f | 63 | $this->desc = 1; |
64 | $this->isread = true; | |
65 | $this->descunread = 0; | |
66e81236 | 66 | if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from, $regs)) { |
67 | $this->name = $regs[2]; | |
68 | } | |
69 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from, $regs)) { | |
70 | $this->name = preg_replace("/^'(.*)'$/", '\1', $regs[1]); | |
71 | $this->name = stripslashes($this->name); | |
72 | } | |
73 | if ($this->name) { | |
74 | $this->name = preg_replace("/\\\(\(|\))/","\\1", $this->name); | |
75 | } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from, $regs)) { | |
76 | $this->name = $regs[1]; | |
77 | } else { | |
78 | $this->name = 'Anonymous'; | |
79 | } | |
e785d91c | 80 | } |
810ac1df | 81 | } |
82 | ||
3ca86dfe | 83 | |
d4c19591 | 84 | class BananaSpool |
e785d91c | 85 | { |
7027794f | 86 | private $version; |
e9360b11 | 87 | private $mode; |
7027794f | 88 | |
e785d91c | 89 | /** group name */ |
7027794f | 90 | public $group; |
91 | /** spool */ | |
0580f927 | 92 | public $overview = array(); |
e785d91c | 93 | /** array msgid => msgnum */ |
0580f927 | 94 | public $ids = array(); |
cced14b6 | 95 | /** thread starts */ |
0580f927 | 96 | public $roots = array(); |
66e81236 | 97 | |
dc5f77ad | 98 | /** protocole specific data */ |
99 | public $storage = array(); | |
7027794f | 100 | |
d265608a | 101 | private $unreadnb = 0; |
810ac1df | 102 | |
e785d91c | 103 | /** constructor |
e785d91c | 104 | * @param $_group STRING group name |
105 | * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts | |
106 | * @param $_since INTEGER time stamp (used for read/unread) | |
107 | */ | |
7027794f | 108 | protected function __construct($group) |
e785d91c | 109 | { |
7027794f | 110 | $this->version = BANANA_SPOOL_VERSION; |
e9360b11 | 111 | $this->mode = Banana::SPOOL_ALL; |
7027794f | 112 | $this->group = $group; |
113 | } | |
fb6428c8 | 114 | |
e02edbfe | 115 | public static function &getSpool($group, $since = 0, $clean = false) |
7027794f | 116 | { |
117 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { | |
b3d11736 | 118 | $spool =& Banana::$spool; |
82c17a91 | 119 | } else { |
e02edbfe | 120 | $spool =& BananaSpool::readFromFile($group); |
64f89fae | 121 | } |
7027794f | 122 | if (is_null($spool)) { |
123 | $spool = new BananaSpool($group); | |
82c17a91 | 124 | } |
7027794f | 125 | Banana::$spool =& $spool; |
126 | $spool->build(); | |
b3d11736 | 127 | if ($clean) { |
128 | $spool->markAllAsRead(); | |
129 | } | |
7027794f | 130 | $spool->updateUnread($since); |
131 | return $spool; | |
132 | } | |
e785d91c | 133 | |
dcb7f48d | 134 | public static function getPath($file = null) |
7027794f | 135 | { |
dcb7f48d | 136 | $path = Banana::$spool_root . '/' . Banana::$protocole->name() . '/' . Banana::$protocole->filename(); |
137 | if (!is_dir($path)) { | |
138 | if (file_exists($path)) { | |
139 | @unlink($path); | |
140 | } | |
141 | mkdir($path, 0777, true); | |
dd7d1c59 | 142 | } |
dcb7f48d | 143 | return $path . '/' . $file; |
144 | } | |
145 | ||
146 | private static function spoolFilename() | |
147 | { | |
148 | return BananaSpool::getPath('spool'); | |
dd7d1c59 | 149 | } |
150 | ||
e02edbfe | 151 | private static function &readFromFile($group) |
dd7d1c59 | 152 | { |
e02edbfe | 153 | $spool = null; |
dcb7f48d | 154 | $file = BananaSpool::spoolFilename(); |
7027794f | 155 | if (!file_exists($file)) { |
e02edbfe | 156 | return $spool; |
dd7d1c59 | 157 | } |
7027794f | 158 | $spool = unserialize(file_get_contents($file)); |
e9360b11 | 159 | if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { |
e02edbfe | 160 | $spool = null; |
161 | return $spool; | |
7027794f | 162 | } |
d265608a | 163 | $spool->markAllAsRead(); |
7027794f | 164 | return $spool; |
165 | } | |
166 | ||
8a30c7d6 | 167 | private function compare($a, $b) |
7027794f | 168 | { |
0580f927 | 169 | return ($b->date - $a->date); |
dd7d1c59 | 170 | } |
171 | ||
7027794f | 172 | private function saveToFile() |
dd7d1c59 | 173 | { |
dcb7f48d | 174 | $file = BananaSpool::spoolFilename(); |
dd7d1c59 | 175 | |
176 | $this->roots = Array(); | |
0580f927 | 177 | foreach($this->overview as &$msg) { |
dd7d1c59 | 178 | if (is_null($msg->parent)) { |
0580f927 | 179 | $this->roots[] =& $msg; |
dd7d1c59 | 180 | } |
181 | } | |
345c3a85 | 182 | usort($this->roots, array($this, 'compare')); |
2c606d23 | 183 | |
e9360b11 | 184 | if ($this->mode == Banana::SPOOL_ALL) { |
185 | file_put_contents($file, serialize($this)); | |
64f89fae | 186 | } |
dd7d1c59 | 187 | } |
188 | ||
7027794f | 189 | private function build() |
190 | { | |
191 | $threshold = 0; | |
192 | ||
193 | // Compute the range of indexes | |
194 | list($msgnum, $first, $last) = Banana::$protocole->getIndexes(); | |
195 | if ($last < $first) { | |
6bd49b4b | 196 | $threshold = $first + $msgnum - $last; |
7027794f | 197 | $threshold = (int)(log($threshold)/log(2)); |
198 | $threshold = (2 ^ ($threshold + 1)) - 1; | |
199 | } | |
e9360b11 | 200 | if (Banana::$spool_max && Banana::$spool_max < $msgnum) { |
201 | $first = $last - Banana::$spool_max; | |
7027794f | 202 | if ($first < 0) { |
203 | $first += $threshold; | |
204 | } | |
205 | } | |
206 | $clean = $this->clean($first, $last, $msgnum); | |
207 | $update = $this->update($first, $last, $msgnum); | |
64f89fae | 208 | |
7027794f | 209 | if ($clean || $update) { |
210 | $this->saveToFile(); | |
211 | } | |
212 | } | |
64f89fae | 213 | |
7027794f | 214 | private function clean(&$first, &$last, $msgnum) |
9090c673 | 215 | { |
7027794f | 216 | $do_save = false; |
0580f927 | 217 | if (!empty($this->overview)) { |
7027794f | 218 | $mids = array_keys($this->overview); |
219 | foreach ($mids as $id) { | |
220 | if (($first <= $last && ($id < $first || $id > $last)) | |
e9360b11 | 221 | || ($first > $last && $id < $first && $id > $last)) { |
7027794f | 222 | $this->delid($id, false); |
223 | $do_save = true; | |
224 | } | |
225 | } | |
226 | if (!empty($this->overview)) { | |
227 | $first = max(array_keys($this->overview))+1; | |
228 | } | |
229 | } | |
230 | return $do_save; | |
9090c673 PHM |
231 | } |
232 | ||
7027794f | 233 | private function update(&$first, &$last, $msgnum) |
dd7d1c59 | 234 | { |
64f89fae | 235 | if ($first > $last || !$msgnum) { |
7027794f | 236 | return false; |
dd7d1c59 | 237 | } |
238 | ||
7027794f | 239 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
240 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); | |
dd7d1c59 | 241 | |
0580f927 | 242 | // Build all the new Spool Heads |
d3b026ab | 243 | $time = time(); |
7027794f | 244 | foreach ($messages as $id=>&$message) { |
0580f927 | 245 | if (!isset($this->overview[$id])) { |
246 | $this->overview[$id] = new BananaSpoolHead($id, $message); | |
247 | $head =& $this->overview[$id]; | |
248 | $this->ids[$head->msgid] =& $head; | |
d3b026ab | 249 | $head->time = $time; |
0580f927 | 250 | } |
7027794f | 251 | } |
252 | ||
0580f927 | 253 | // Build tree |
0580f927 | 254 | $null = null; |
7027794f | 255 | foreach ($messages as $id=>&$message) { |
0580f927 | 256 | $msg =& $this->overview[$id]; |
257 | $parents =& $this->getReferences($message); | |
258 | while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) { | |
954b9478 | 259 | @$msg->parent =& array_pop($parents); |
3316e34e | 260 | } |
0580f927 | 261 | |
262 | if (!is_null($msg->parent)) { | |
263 | $parent =& $msg->parent; | |
264 | $parent->children[] =& $msg; | |
265 | while (!is_null($parent)) { | |
266 | $parent->desc += $msg->desc; | |
d3b026ab | 267 | $parent->time = $time; |
190f587f | 268 | $prev =& $parent; |
0580f927 | 269 | if ($parent !== $parent->parent) { |
270 | $parent =& $parent->parent; | |
7027794f | 271 | } else { |
0580f927 | 272 | $parent =& $null; |
66e81236 | 273 | } |
e785d91c | 274 | } |
810ac1df | 275 | } |
810ac1df | 276 | } |
7027794f | 277 | Banana::$protocole->updateSpool($messages); |
278 | return true; | |
dd7d1c59 | 279 | } |
e785d91c | 280 | |
bffb37b4 | 281 | public function updateUnread($since) |
75ff2f64 | 282 | { |
7027794f | 283 | if (empty($since)) { |
284 | return; | |
285 | } | |
286 | ||
287 | $newpostsids = Banana::$protocole->getNewIndexes($since); | |
b3d11736 | 288 | |
7027794f | 289 | if (empty($newpostsids)) { |
290 | return; | |
291 | } | |
292 | ||
7027794f | 293 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
294 | foreach ($newpostsids as $mid) { | |
0580f927 | 295 | $overview =& $this->ids[$mid]; |
296 | if ($overview->isread) { | |
297 | $overview->isread = false; | |
298 | while (!is_null($overview)) { | |
299 | $overview->descunread++; | |
300 | $overview =& $overview->parent; | |
e785d91c | 301 | } |
810ac1df | 302 | } |
7027794f | 303 | } |
0580f927 | 304 | $this->unreadnb += count($newpostsids); |
2b974d09 FB |
305 | |
306 | if (function_exists('hook_listReadMessages')) { | |
307 | $msgs = hook_listReadMessages($this->group); | |
308 | if (!is_array($msgs)) { | |
309 | return; | |
310 | } | |
311 | foreach ($msgs as $msg) { | |
312 | if (!is_numeric($msg)) { | |
313 | if (!isset($this->ids[$msg])) { | |
314 | continue; | |
315 | } | |
05114a3e | 316 | $msg = $this->ids[$msg]->id; |
2b974d09 FB |
317 | } |
318 | $this->markAsRead($msg); | |
319 | } | |
320 | } | |
7027794f | 321 | } |
dd7d1c59 | 322 | |
7027794f | 323 | public function setMode($mode) |
324 | { | |
e9360b11 | 325 | $this->mode = $mode; |
7027794f | 326 | switch ($mode) { |
327 | case Banana::SPOOL_UNREAD: | |
64f89fae | 328 | $num = max(array_keys($this->overview)); |
329 | if ($this->overview[$num]->isread) { | |
330 | break; | |
331 | } | |
0580f927 | 332 | foreach ($this->roots as &$root) { |
333 | if ($root->descunread == 0) { | |
334 | $this->killdesc($root->id); | |
e785d91c | 335 | } |
810ac1df | 336 | } |
7027794f | 337 | break; |
810ac1df | 338 | } |
cced14b6 | 339 | } |
340 | ||
0580f927 | 341 | /** Fetch list of references |
342 | */ | |
343 | public function &getReferences(array &$refs) | |
344 | { | |
345 | $references = array(); | |
346 | if (isset($refs['references'])) { | |
347 | $text = preg_split('/\s/', str_replace('><', '> <', $refs['references'])); | |
348 | foreach ($text as $id=>&$value) { | |
349 | if (isset($this->ids[$value])) { | |
350 | $references[] =& $this->ids[$value]; | |
351 | } | |
352 | } | |
353 | } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) { | |
354 | $references[] =& $this->ids[$refs['in-reply-to']]; | |
355 | } | |
356 | return $references; | |
357 | } | |
358 | ||
d3b026ab | 359 | /** Get the tree associated to a given id |
360 | */ | |
361 | public function &getTree($id) | |
362 | { | |
1d521296 | 363 | return BananaTree::build($id)->show(); |
d3b026ab | 364 | } |
365 | ||
e9360b11 | 366 | /** Mark the given id as read |
367 | * @param id MSGNUM of post | |
368 | */ | |
369 | public function markAsRead($id) | |
370 | { | |
1d521296 | 371 | $overview =& $this->overview[$id]; |
372 | if (!$overview->isread) { | |
373 | $overview->isread = true; | |
d265608a | 374 | $this->unreadnb--; |
1d521296 | 375 | while (!is_null($overview)) { |
376 | $overview->descunread--; | |
377 | $overview =& $overview->parent; | |
e9360b11 | 378 | } |
379 | } | |
380 | } | |
381 | ||
382 | /** Mark all unread messages as read | |
383 | */ | |
384 | public function markAllAsRead(array &$array = null) | |
385 | { | |
d265608a | 386 | if (!$this->unreadnb) { |
387 | return; | |
388 | } | |
0580f927 | 389 | if (is_null($array) && !empty($this->roots)) { |
e9360b11 | 390 | $array =& $this->roots; |
c4f176d8 | 391 | } elseif (is_null($array)) { |
c28d3016 | 392 | return; |
393 | } | |
0580f927 | 394 | foreach ($array as &$msg) { |
395 | if (!$msg->isread) { | |
396 | $this->markAsRead($msg->id); | |
d265608a | 397 | if (!$this->unreadnb) { |
398 | return; | |
399 | } | |
e9360b11 | 400 | } |
0580f927 | 401 | if ($msg->descunread) { |
402 | $this->markAllAsRead($msg->children); | |
e9360b11 | 403 | } |
404 | } | |
405 | } | |
406 | ||
e785d91c | 407 | /** kill post and childrens |
408 | * @param $_id MSGNUM of post | |
409 | */ | |
7027794f | 410 | private function killdesc($_id) |
e785d91c | 411 | { |
0580f927 | 412 | $overview =& $this->overview[$_id]; |
413 | $children =& $overview->children; | |
414 | if (sizeof($children)) { | |
415 | foreach ($children as &$c) { | |
416 | $this->killdesc($c->id); | |
e785d91c | 417 | } |
418 | } | |
419 | unset($this->overview[$_id]); | |
0580f927 | 420 | foreach ($this->roots as $k=>&$root) { |
421 | if ($root === $overview) { | |
422 | unset($this->roots[$k]); | |
423 | break; | |
424 | } | |
e785d91c | 425 | } |
0580f927 | 426 | unset($this->ids[$overview->msgid]); |
0580f927 | 427 | $overview = null; |
810ac1df | 428 | } |
e785d91c | 429 | |
430 | /** delete a post from overview | |
431 | * @param $_id MSGNUM of post | |
432 | */ | |
7027794f | 433 | public function delid($_id, $write = true) |
e785d91c | 434 | { |
0580f927 | 435 | if (!isset($this->overview[$_id])) { |
436 | return; | |
437 | } | |
438 | ||
439 | $overview =& $this->overview[$_id]; | |
440 | // Be sure it is not counted as unread | |
441 | if (!$overview->isread) { | |
442 | $this->markAsRead($_id); | |
443 | } | |
444 | ||
445 | $parent =& $overview->parent; | |
446 | ||
447 | // Remove from the message tree | |
448 | if (!is_null($parent)) { | |
694a26dd | 449 | $time = time(); |
0580f927 | 450 | foreach ($parent->children as $key=>&$child) { |
451 | if ($child === $overview) { | |
452 | unset($parent->children[$key]); | |
453 | break; | |
e785d91c | 454 | } |
455 | } | |
0580f927 | 456 | if (sizeof($overview->children)) { |
0580f927 | 457 | foreach ($overview->children as &$child) { |
694a26dd | 458 | $parent->children[] =& $child; |
459 | $child->time = $time; | |
0580f927 | 460 | $child->parent =& $parent; |
461 | } | |
e785d91c | 462 | } |
0580f927 | 463 | while (!is_null($parent)) { |
464 | $parent->desc--; | |
d3b026ab | 465 | $parent->time = $time; |
0580f927 | 466 | $parent =& $parent->parent; |
cfb7fe5d | 467 | } |
0580f927 | 468 | } |
64f89fae | 469 | |
0580f927 | 470 | // Remove all refenrences and assign null to the object |
471 | unset($this->ids[$overview->msgid]); | |
472 | unset($this->overview[$_id]); | |
d3b026ab | 473 | BananaTree::kill($_id); |
0580f927 | 474 | foreach ($this->roots as $k=>&$root) { |
475 | if ($root === $overview) { | |
476 | unset($this->roots[$k]); | |
477 | break; | |
7027794f | 478 | } |
e785d91c | 479 | } |
0580f927 | 480 | $overview = null; |
481 | ||
482 | if ($write) { | |
483 | $this->saveToFile(); | |
484 | } | |
35ca8036 | 485 | } |
810ac1df | 486 | |
8a30c7d6 | 487 | public function formatDate(BananaSpoolHead $head) |
7027794f | 488 | { |
0580f927 | 489 | $stamp = $head->date; |
7027794f | 490 | $today = intval(time() / (24*3600)); |
491 | $dday = intval($stamp / (24*3600)); | |
492 | ||
493 | if ($today == $dday) { | |
494 | $format = "%H:%M"; | |
495 | } elseif ($today == 1 + $dday) { | |
496 | $format = _b_('hier')." %H:%M"; | |
497 | } elseif ($today < 7 + $dday) { | |
498 | $format = '%a %H:%M'; | |
d924433f | 499 | } elseif ($today < 90 + $dday) { |
7027794f | 500 | $format = '%a %e %b'; |
d924433f | 501 | } else { |
502 | $format = '%a %e %b %Y'; | |
7027794f | 503 | } |
22b95309 | 504 | return strftime($format, $stamp); |
7027794f | 505 | } |
506 | ||
8a30c7d6 | 507 | public function formatSubject(BananaSpoolHead $head) |
66e81236 | 508 | { |
0580f927 | 509 | $subject = $popup = $head->subject; |
66e81236 | 510 | $popup = $subject; |
511 | if (function_exists('hook_formatDisplayHeader')) { | |
512 | list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); | |
513 | } else { | |
514 | $subject = banana_catchFormats(banana_entities(stripslashes($subject))); | |
515 | $link = null; | |
516 | } | |
517 | if (empty($subject)) { | |
518 | $subject = _b_('(pas de sujet)'); | |
519 | } | |
212ada1b SJ |
520 | if (mb_strlen($subject) > 100) { |
521 | $subject = mb_substr($subject, 0, 99) . '…'; | |
d2e741cb | 522 | } |
bd05fff5 | 523 | if ($head->id !== Banana::$artid) { |
0580f927 | 524 | $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id, |
66e81236 | 525 | 'text' => $subject, 'popup' => $popup)); |
526 | } | |
527 | return $subject . $link; | |
528 | } | |
529 | ||
8a30c7d6 | 530 | public function formatFrom(BananaSpoolHead $head) |
66e81236 | 531 | { |
0580f927 | 532 | return BananaMessage::formatFrom($head->from); |
66e81236 | 533 | } |
534 | ||
535 | public function start() | |
536 | { | |
537 | if (Banana::$first) { | |
538 | return Banana::$first; | |
539 | } else { | |
540 | $first = array_search(Banana::$artid, $this->roots); | |
541 | return max(0, $first - Banana::$spool_tbefore); | |
542 | } | |
543 | } | |
544 | ||
545 | public function context() | |
546 | { | |
547 | return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext; | |
548 | } | |
549 | ||
d8e2470c | 550 | /** Return root message of the given thread |
551 | * @param id INTEGER id of a message | |
552 | */ | |
190f587f | 553 | public function &root($id) |
7027794f | 554 | { |
190f587f | 555 | $parent =& $this->overview[$id]; |
556 | while (!is_null($parent->parent)) { | |
557 | $parent =& $parent->parent; | |
d8e2470c | 558 | } |
190f587f | 559 | return $parent; |
d8e2470c | 560 | } |
561 | ||
bffb37b4 | 562 | /** Return the last post id with the given subject |
563 | * @param subject | |
564 | */ | |
565 | public function getPostId($subject) | |
566 | { | |
567 | $subject = trim($subject); | |
568 | $id = max(array_keys($this->overview)); | |
569 | while (isset($this->overview[$id])) { | |
570 | $test = $this->overview[$id]->subject; | |
571 | if (function_exists('hook_formatDisplayHeader')) { | |
572 | $val = hook_formatDisplayHeader('subject', $test, true); | |
573 | if (is_array($val)) { | |
0a5b736c | 574 | $test = banana_html_entity_decode($val[0]); |
bffb37b4 | 575 | } else { |
0a5b736c | 576 | $test = banana_html_entity_decode($val); |
bffb37b4 | 577 | } |
578 | } | |
579 | $test = trim($test); | |
bffb37b4 | 580 | if ($test == $subject) { |
581 | return $id; | |
582 | } | |
583 | $id--; | |
584 | } | |
585 | return -1; | |
586 | } | |
587 | ||
d8e2470c | 588 | /** Returns previous thread root index |
589 | * @param id INTEGER message number | |
590 | */ | |
7027794f | 591 | public function prevThread($id) |
d8e2470c | 592 | { |
190f587f | 593 | $root =& $this->root($id); |
d8e2470c | 594 | $last = null; |
190f587f | 595 | foreach ($this->roots as &$i) { |
596 | if ($i === $root) { | |
d8e2470c | 597 | return $last; |
598 | } | |
190f587f | 599 | $last = $i->id; |
d8e2470c | 600 | } |
601 | return $last; | |
602 | } | |
603 | ||
604 | /** Returns next thread root index | |
605 | * @param id INTEGER message number | |
606 | */ | |
7027794f | 607 | public function nextThread($id) |
d8e2470c | 608 | { |
190f587f | 609 | $root =& $this->root($id); |
d8e2470c | 610 | $ok = false; |
190f587f | 611 | foreach ($this->roots as &$i) { |
d8e2470c | 612 | if ($ok) { |
190f587f | 613 | return $i->id; |
d8e2470c | 614 | } |
190f587f | 615 | if ($i === $root) { |
d8e2470c | 616 | $ok = true; |
617 | } | |
618 | } | |
619 | return null; | |
620 | } | |
621 | ||
622 | /** Return prev post in the thread | |
623 | * @param id INTEGER message number | |
624 | */ | |
7027794f | 625 | public function prevPost($id) |
d8e2470c | 626 | { |
190f587f | 627 | $parent =& $this->overview[$id]->parent; |
d8e2470c | 628 | if (is_null($parent)) { |
629 | return null; | |
630 | } | |
190f587f | 631 | $last = $parent->id; |
632 | foreach ($parent->children as &$child) { | |
633 | if ($child->id == $id) { | |
d8e2470c | 634 | return $last; |
635 | } | |
190f587f | 636 | $last = $child->id; |
d8e2470c | 637 | } |
638 | return null; | |
639 | } | |
640 | ||
641 | /** Return next post in the thread | |
642 | * @param id INTEGER message number | |
643 | */ | |
7027794f | 644 | public function nextPost($id) |
d8e2470c | 645 | { |
190f587f | 646 | $cur =& $this->overview[$id]; |
647 | if (count($cur->children) != 0) { | |
648 | return $cur->children[0]->id; | |
d8e2470c | 649 | } |
64f89fae | 650 | |
190f587f | 651 | $parent =& $cur; |
d8e2470c | 652 | while (true) { |
190f587f | 653 | $parent =& $cur->parent; |
d8e2470c | 654 | if (is_null($parent)) { |
655 | return null; | |
656 | } | |
657 | $ok = false; | |
190f587f | 658 | foreach ($parent->children as &$child) { |
d8e2470c | 659 | if ($ok) { |
190f587f | 660 | return $child->id; |
d8e2470c | 661 | } |
190f587f | 662 | if ($child === $cur) { |
d8e2470c | 663 | $ok = true; |
664 | } | |
665 | } | |
190f587f | 666 | $cur =& $parent; |
d8e2470c | 667 | } |
668 | return null; | |
669 | } | |
d634c13c | 670 | |
671 | /** Look for an unread message in the thread rooted by the message | |
672 | * @param id INTEGER message number | |
673 | */ | |
8a30c7d6 | 674 | private function _nextUnread(BananaSpoolHead $cur) |
d634c13c | 675 | { |
190f587f | 676 | if (!$cur->isread) { |
677 | return $cur->id; | |
d634c13c | 678 | } |
190f587f | 679 | foreach ($cur->children as &$child) { |
bdad7c9d | 680 | $unread = $this->_nextUnread($child); |
681 | if (!is_null($unread)) { | |
682 | return $unread; | |
64f89fae | 683 | } |
d634c13c | 684 | } |
685 | return null; | |
686 | } | |
687 | ||
688 | /** Find next unread message | |
689 | * @param id INTEGER message number | |
690 | */ | |
7027794f | 691 | public function nextUnread($id = null) |
d634c13c | 692 | { |
d265608a | 693 | if (!$this->unreadnb) { |
694 | return null; | |
695 | } | |
696 | ||
b7d59a47 | 697 | if (!is_null($id)) { |
698 | // Look in message children | |
190f587f | 699 | foreach ($this->overview[$id]->children as &$child) { |
b7d59a47 | 700 | $next = $this->_nextUnread($child); |
701 | if (!is_null($next)) { | |
702 | return $next; | |
703 | } | |
d634c13c | 704 | } |
705 | } | |
706 | ||
707 | // Look in current thread | |
190f587f | 708 | if (is_null($id)) { |
709 | $cur = null; | |
710 | } else { | |
711 | $cur =& $this->overview[$id]; | |
712 | } | |
b7d59a47 | 713 | do { |
190f587f | 714 | if (is_null($cur)) { |
715 | $parent =& $cur; | |
716 | $ok = true; | |
717 | } else { | |
718 | $parent =& $cur->parent; | |
719 | $ok = false; | |
720 | } | |
1e016f3a | 721 | if (!is_null($parent)) { |
190f587f | 722 | $array =& $parent->children; |
d634c13c | 723 | } else { |
190f587f | 724 | $array =& $this->roots; |
d634c13c | 725 | } |
190f587f | 726 | foreach ($array as &$child) { |
d634c13c | 727 | if ($ok) { |
728 | $next = $this->_nextUnread($child); | |
729 | if (!is_null($next)) { | |
730 | return $next; | |
731 | } | |
732 | } | |
190f587f | 733 | if ($child === $cur) { |
d634c13c | 734 | $ok = true; |
735 | } | |
736 | } | |
190f587f | 737 | $cur =& $parent; |
b7d59a47 | 738 | } while(!is_null($cur)); |
d634c13c | 739 | return null; |
64f89fae | 740 | } |
810ac1df | 741 | } |
598a1c53 | 742 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
810ac1df | 743 | ?> |