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