78cd27b3 |
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 | |
ab02e8a9 |
10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
78cd27b3 |
11 | |
ab02e8a9 |
12 | define('BANANA_SPOOL_VERSION', '0.3'); |
78cd27b3 |
13 | |
14 | /** Class spoolhead |
15 | * class used in thread overviews |
16 | */ |
17 | class BananaSpoolHead |
18 | { |
19 | /** date (timestamp) */ |
ab02e8a9 |
20 | public $date; |
78cd27b3 |
21 | /** subject */ |
ab02e8a9 |
22 | public $subject; |
78cd27b3 |
23 | /** author */ |
ab02e8a9 |
24 | public $from; |
78cd27b3 |
25 | /** reference of parent */ |
ab02e8a9 |
26 | public $parent = null; |
78cd27b3 |
27 | /** paren is direct */ |
ab02e8a9 |
28 | public $parent_direct; |
78cd27b3 |
29 | /** array of children */ |
ab02e8a9 |
30 | public $children = Array(); |
78cd27b3 |
31 | /** true if post is read */ |
ab02e8a9 |
32 | public $isread; |
78cd27b3 |
33 | /** number of posts deeper in this branch of tree */ |
ab02e8a9 |
34 | public $desc; |
78cd27b3 |
35 | /** same as desc, but counts only unread posts */ |
ab02e8a9 |
36 | public $descunread; |
37 | |
38 | /** storage data */ |
39 | public $storage = array(); |
78cd27b3 |
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 | */ |
ab02e8a9 |
49 | public function __construct(array &$message) |
78cd27b3 |
50 | { |
ab02e8a9 |
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; |
78cd27b3 |
57 | } |
58 | } |
59 | |
78cd27b3 |
60 | |
61 | class BananaSpool |
62 | { |
ab02e8a9 |
63 | private $version; |
64 | |
78cd27b3 |
65 | /** group name */ |
ab02e8a9 |
66 | public $group; |
67 | /** spool */ |
68 | public $overview; |
78cd27b3 |
69 | /** array msgid => msgnum */ |
ab02e8a9 |
70 | public $ids; |
78cd27b3 |
71 | /** thread starts */ |
ab02e8a9 |
72 | public $roots; |
73 | |
78cd27b3 |
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 | */ |
ab02e8a9 |
80 | protected function __construct($group) |
78cd27b3 |
81 | { |
ab02e8a9 |
82 | $this->version = BANANA_SPOOL_VERSION; |
83 | $this->group = $group; |
84 | } |
9d18c838 |
85 | |
ab02e8a9 |
86 | public static function getSpool($group, $since = 0) |
87 | { |
88 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { |
89 | $spool = Banana::$spool; |
78cd27b3 |
90 | } else { |
ab02e8a9 |
91 | $spool = BananaSpool::readFromFile($group); |
92 | } |
93 | if (is_null($spool)) { |
94 | $spool = new BananaSpool($group); |
78cd27b3 |
95 | } |
ab02e8a9 |
96 | Banana::$spool =& $spool; |
97 | $spool->build(); |
98 | $spool->updateUnread($since); |
99 | return $spool; |
100 | } |
78cd27b3 |
101 | |
ab02e8a9 |
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); |
78cd27b3 |
108 | } |
ab02e8a9 |
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; |
78cd27b3 |
118 | } |
119 | |
ab02e8a9 |
120 | private static function readFromFile($group) |
78cd27b3 |
121 | { |
ab02e8a9 |
122 | $file = BananaSpool::spoolFilename($group); |
123 | if (!file_exists($file)) { |
124 | return null; |
78cd27b3 |
125 | } |
ab02e8a9 |
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); |
78cd27b3 |
136 | } |
137 | |
ab02e8a9 |
138 | private function saveToFile() |
78cd27b3 |
139 | { |
ab02e8a9 |
140 | $file = BananaSpool::spoolFilename($this->group); |
141 | uasort($this->overview, array($this, 'compare')); |
78cd27b3 |
142 | |
143 | $this->roots = Array(); |
144 | foreach($this->overview as $id=>$msg) { |
145 | if (is_null($msg->parent)) { |
146 | $this->roots[] = $id; |
147 | } |
148 | } |
01972ace |
149 | |
78cd27b3 |
150 | file_put_contents($file, serialize($this)); |
151 | } |
152 | |
ab02e8a9 |
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) |
78cd27b3 |
179 | { |
ab02e8a9 |
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; |
78cd27b3 |
196 | } |
197 | |
ab02e8a9 |
198 | private function update(&$first, &$last, $msgnum) |
78cd27b3 |
199 | { |
ab02e8a9 |
200 | if ($first >= $last || !$msgnum) { |
201 | return false; |
78cd27b3 |
202 | } |
203 | |
ab02e8a9 |
204 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
205 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); |
78cd27b3 |
206 | |
ab02e8a9 |
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); |
78cd27b3 |
217 | } |
ab02e8a9 |
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)); |
78cd27b3 |
223 | |
ab02e8a9 |
224 | if (!is_null($p = $msg->parent)) { |
78cd27b3 |
225 | if (empty($this->overview[$p])) { |
ab02e8a9 |
226 | $this->overview[$p] = new BananaSpoolHead($messages[$p]); |
78cd27b3 |
227 | } |
228 | $this->overview[$p]->children[] = $id; |
229 | |
ab02e8a9 |
230 | while (!is_null($p)) { |
78cd27b3 |
231 | $this->overview[$p]->desc += $msg->desc; |
ab02e8a9 |
232 | if ($p != $this->overview[$p]->parent) { |
233 | $p = $this->overview[$p]->parent; |
234 | } else { |
235 | $p = null; |
236 | } |
78cd27b3 |
237 | } |
238 | } |
239 | } |
ab02e8a9 |
240 | Banana::$protocole->updateSpool($messages); |
241 | return true; |
78cd27b3 |
242 | } |
243 | |
ab02e8a9 |
244 | private function updateUnread($since) |
78cd27b3 |
245 | { |
ab02e8a9 |
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; |
78cd27b3 |
268 | } |
269 | } |
ab02e8a9 |
270 | } |
271 | } |
78cd27b3 |
272 | |
ab02e8a9 |
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]); |
78cd27b3 |
281 | } |
282 | } |
ab02e8a9 |
283 | break; |
78cd27b3 |
284 | } |
285 | } |
286 | |
287 | /** kill post and childrens |
288 | * @param $_id MSGNUM of post |
289 | */ |
ab02e8a9 |
290 | private function killdesc($_id) |
78cd27b3 |
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 | */ |
ab02e8a9 |
306 | public function delid($_id, $write = true) |
78cd27b3 |
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 | |
ab02e8a9 |
336 | if ($write) { |
337 | $this->saveToFile(); |
338 | } |
78cd27b3 |
339 | } |
340 | } |
341 | |
ab02e8a9 |
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 | |
78cd27b3 |
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 |
e2347ffc |
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. |
78cd27b3 |
372 | */ |
ab02e8a9 |
373 | private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
78cd27b3 |
374 | { |
ab02e8a9 |
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)); |
78cd27b3 |
387 | } |
388 | |
ab02e8a9 |
389 | $overview =& $this->overview[$_id]; |
390 | if ($_index + $overview->desc < $_first || $_index > $_last) { |
391 | return ''; |
392 | } |
78cd27b3 |
393 | |
ab02e8a9 |
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)) { |
71d6c865 |
404 | $subject = _b_('(pas de sujet)'); |
405 | } |
e2347ffc |
406 | $link = null; |
407 | if (function_exists('hook_getSubject')) { |
408 | $link = hook_getSubject($subject); |
409 | } |
ab02e8a9 |
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)); |
78cd27b3 |
414 | } |
ab02e8a9 |
415 | $res .= ' ' . $subject . $link; |
416 | $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>"; |
78cd27b3 |
417 | |
ab02e8a9 |
418 | if ($hc) { |
419 | return $res; |
420 | } |
78cd27b3 |
421 | } |
422 | |
423 | $_index ++; |
ab02e8a9 |
424 | $children = $overview->children; |
78cd27b3 |
425 | while ($child = array_shift($children)) { |
ab02e8a9 |
426 | $overview =& $this->overview[$child]; |
427 | if ($_index > $_last) { |
428 | return $res; |
429 | } |
430 | if ($_index + $overview->desc >= $_first) { |
78cd27b3 |
431 | if (sizeof($children)) { |
432 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
433 | $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd), |
434 | $_pfx_end . $spfx_I, false); |
78cd27b3 |
435 | } else { |
436 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
437 | $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd), |
438 | $_pfx_end . $spfx_e, false); |
78cd27b3 |
439 | } |
440 | } |
ab02e8a9 |
441 | $_index += $overview->desc; |
78cd27b3 |
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 | */ |
ab02e8a9 |
452 | public function toHtml($first = 0, $overview = false) |
78cd27b3 |
453 | { |
ab02e8a9 |
454 | $res = ''; |
455 | |
456 | if (!$overview) { |
457 | $_first = $first; |
458 | $_last = $first + Banana::$tmax - 1; |
459 | $_ref = null; |
2ba32e89 |
460 | } else { |
ab02e8a9 |
461 | $_ref = $this->getNdx($first); |
462 | $_last = $_ref + Banana::$tafter; |
463 | $_first = $_ref - Banana::$tbefore; |
464 | if ($_first < 0) { |
465 | $_last -= $_first; |
466 | } |
78cd27b3 |
467 | } |
78cd27b3 |
468 | $index = 1; |
ab02e8a9 |
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; |
78cd27b3 |
474 | } |
e230b41a |
475 | } |
ab02e8a9 |
476 | return $res; |
78cd27b3 |
477 | } |
478 | |
479 | /** computes linear post index |
480 | * @param $_id INTEGER MSGNUM of post |
481 | * @return INTEGER linear index of post |
482 | */ |
ab02e8a9 |
483 | public function getNdX($_id) |
78cd27b3 |
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 | } |
2ba32e89 |
508 | |
509 | /** Return root message of the given thread |
510 | * @param id INTEGER id of a message |
511 | */ |
ab02e8a9 |
512 | public function root($id) |
513 | { |
2ba32e89 |
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 | */ |
ab02e8a9 |
526 | public function prevThread($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
542 | public function nextThread($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
560 | public function prevPost($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
579 | public function nextPost($id) |
2ba32e89 |
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 | } |
dc12019c |
604 | |
605 | /** Look for an unread message in the thread rooted by the message |
606 | * @param id INTEGER message number |
607 | */ |
ab02e8a9 |
608 | private function _nextUnread($id) |
dc12019c |
609 | { |
610 | if (!$this->overview[$id]->isread) { |
611 | return $id; |
612 | } |
613 | foreach ($this->overview[$id]->children as $child) { |
6aa1f5e4 |
614 | $unread = $this->_nextUnread($child); |
615 | if (!is_null($unread)) { |
616 | return $unread; |
617 | } |
dc12019c |
618 | } |
619 | return null; |
620 | } |
621 | |
622 | /** Find next unread message |
623 | * @param id INTEGER message number |
624 | */ |
ab02e8a9 |
625 | public function nextUnread($id = null) |
dc12019c |
626 | { |
27db62eb |
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 | } |
dc12019c |
634 | } |
635 | } |
636 | |
637 | // Look in current thread |
638 | $cur = $id; |
27db62eb |
639 | do { |
640 | $parent = is_null($cur) ? null : $this->overview[$cur]->parent; |
641 | $ok = is_null($cur) ? true : false; |
a02dd64a |
642 | if (!is_null($parent)) { |
dc12019c |
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; |
27db62eb |
659 | } while(!is_null($cur)); |
dc12019c |
660 | return null; |
661 | } |
78cd27b3 |
662 | } |
663 | |
f6df9eb2 |
664 | // vim:set et sw=4 sts=4 ts=4 |
78cd27b3 |
665 | ?> |