id = $id; $this->msgid = @$message['message-id']; $this->date = $message['date']; $this->subject = @$message['subject']; $this->from = $message['from']; $this->color = sprintf('#%06x', abs(crc32($this->from) % 0xffffff)); $this->desc = 1; $this->isread = true; $this->descunread = 0; if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from, $regs)) { $this->name = $regs[2]; } if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from, $regs)) { $this->name = preg_replace("/^'(.*)'$/", '\1', $regs[1]); $this->name = stripslashes($this->name); } if ($this->name) { $this->name = preg_replace("/\\\(\(|\))/","\\1", $this->name); } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from, $regs)) { $this->name = $regs[1]; } else { $this->name = 'Anonymous'; } } } class BananaSpool { private $version; private $mode; /** group name */ public $group; /** spool */ public $overview = array(); /** array msgid => msgnum */ public $ids = array(); /** thread starts */ public $roots = array(); /** thread trees (one tree per root node) */ public $trees = array(); /** protocole specific data */ public $storage = array(); private $unreadnb = 0; /** constructor * @param $_group STRING group name * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts * @param $_since INTEGER time stamp (used for read/unread) */ protected function __construct($group) { $this->version = BANANA_SPOOL_VERSION; $this->mode = Banana::SPOOL_ALL; $this->group = $group; } public static function &getSpool($group, $since = 0, $clean = false) { if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { $spool =& Banana::$spool; } else { $spool =& BananaSpool::readFromFile($group); } if (is_null($spool)) { $spool = new BananaSpool($group); } Banana::$spool =& $spool; $spool->build(); if ($clean) { $spool->markAllAsRead(); } $spool->updateUnread($since); //var_dump($spool->trees); return $spool; } private static function spoolFilename($group) { $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/'; if (!is_dir($file)) { mkdir($file); } return $file . Banana::$protocole->filename(); } private static function &readFromFile($group) { $spool = null; $file = BananaSpool::spoolFilename($group); if (!file_exists($file)) { return $spool; } $spool = unserialize(file_get_contents($file)); if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { $spool = null; return $spool; } $spool->markAllAsRead(); return $spool; } private function compare(&$a, &$b) { return ($b->date - $a->date); } private function saveToFile() { $file = BananaSpool::spoolFilename($this->group); $this->roots = Array(); foreach($this->overview as &$msg) { if (is_null($msg->parent)) { $this->roots[] =& $msg; } } usort($this->roots, array($this, 'compare')); if ($this->mode == Banana::SPOOL_ALL) { file_put_contents($file, serialize($this)); } } private function build() { $threshold = 0; // Compute the range of indexes list($msgnum, $first, $last) = Banana::$protocole->getIndexes(); if ($last < $first) { $threshold = $first + $msgnum - $last; $threshold = (int)(log($threshold)/log(2)); $threshold = (2 ^ ($threshold + 1)) - 1; } if (Banana::$spool_max && Banana::$spool_max < $msgnum) { $first = $last - Banana::$spool_max; if ($first < 0) { $first += $threshold; } } $clean = $this->clean($first, $last, $msgnum); $update = $this->update($first, $last, $msgnum); if ($clean || $update) { $this->saveToFile(); } } private function clean(&$first, &$last, $msgnum) { $do_save = false; if (!empty($this->overview)) { $mids = array_keys($this->overview); foreach ($mids as $id) { if (($first <= $last && ($id < $first || $id > $last)) || ($first > $last && $id < $first && $id > $last)) { $this->delid($id, false); $do_save = true; } } if (!empty($this->overview)) { $first = max(array_keys($this->overview))+1; } } return $do_save; } private function update(&$first, &$last, $msgnum) { if ($first > $last || !$msgnum) { return false; } $messages =& Banana::$protocole->getMessageHeaders($first, $last, array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); // Build all the new Spool Heads foreach ($messages as $id=>&$message) { if (!isset($this->overview[$id])) { $this->overview[$id] = new BananaSpoolHead($id, $message); $head =& $this->overview[$id]; $this->ids[$head->msgid] =& $head; } } // Build tree $updateTrees = array(); $null = null; foreach ($messages as $id=>&$message) { $msg =& $this->overview[$id]; $parents =& $this->getReferences($message); while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) { @$msg->parent =& array_pop($parents); } if (!is_null($msg->parent)) { $parent =& $msg->parent; $parent->children[] =& $msg; while (!is_null($parent)) { $parent->desc += $msg->desc; $prev =& $parent; if ($parent !== $parent->parent) { $parent =& $parent->parent; } else { $parent =& $null; } } $updateTrees[$prev->id] = true; } } foreach ($updateTrees as $root=>$t) { $this->trees[$root] =& $this->buildTree($root); } Banana::$protocole->updateSpool($messages); return true; } public function updateUnread($since) { if (empty($since)) { return; } $newpostsids = Banana::$protocole->getNewIndexes($since); if (empty($newpostsids)) { return; } $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); foreach ($newpostsids as $mid) { $overview =& $this->ids[$mid]; if ($overview->isread) { $overview->isread = false; while (!is_null($overview)) { $overview->descunread++; $overview =& $overview->parent; } } } $this->unreadnb += count($newpostsids); } public function setMode($mode) { $this->mode = $mode; switch ($mode) { case Banana::SPOOL_UNREAD: $num = max(array_keys($this->overview)); if ($this->overview[$num]->isread) { break; } foreach ($this->roots as &$root) { if ($root->descunread == 0) { $this->killdesc($root->id); } } break; } } /** Fetch list of references */ public function &getReferences(array &$refs) { $references = array(); if (isset($refs['references'])) { $text = preg_split('/\s/', str_replace('><', '> <', $refs['references'])); foreach ($text as $id=>&$value) { if (isset($this->ids[$value])) { $references[] =& $this->ids[$value]; } } } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) { $references[] =& $this->ids[$refs['in-reply-to']]; } return $references; } /** Mark the given id as read * @param id MSGNUM of post */ public function markAsRead($id) { if (!$this->overview[$id]->isread) { $this->overview[$id]->isread = true; $this->unreadnb--; while (isset($id)) { $this->overview[$id]->descunread--; $id = $this->overview[$id]->parent; } } } /** Mark all unread messages as read */ public function markAllAsRead(array &$array = null) { if (!$this->unreadnb) { return; } if (is_null($array) && !empty($this->roots)) { $array =& $this->roots; } elseif (is_null($array)) { return; } foreach ($array as &$msg) { if (!$msg->isread) { $this->markAsRead($msg->id); if (!$this->unreadnb) { return; } } if ($msg->descunread) { $this->markAllAsRead($msg->children); } } } /** kill post and childrens * @param $_id MSGNUM of post */ private function killdesc($_id) { $overview =& $this->overview[$_id]; $children =& $overview->children; if (sizeof($children)) { foreach ($children as &$c) { $this->killdesc($c->id); } } unset($this->overview[$_id]); foreach ($this->roots as $k=>&$root) { if ($root === $overview) { unset($this->roots[$k]); break; } } unset($this->ids[$overview->msgid]); unset($this->trees[$_id]); $overview = null; } /** delete a post from overview * @param $_id MSGNUM of post */ public function delid($_id, $write = true) { if (!isset($this->overview[$_id])) { return; } $overview =& $this->overview[$_id]; // Be sure it is not counted as unread if (!$overview->isread) { $this->markAsRead($_id); } $parent =& $overview->parent; // Remove from the message tree if (!is_null($parent)) { foreach ($parent->children as $key=>&$child) { if ($child === $overview) { unset($parent->children[$key]); break; } } if (sizeof($overview->children)) { $parent->children = array_merge($parent->children, $overview->children); foreach ($overview->children as &$child) { $child->parent =& $parent; } } while (!is_null($parent)) { $parent->desc--; $parent =& $parent->parent; } } // Remove all refenrences and assign null to the object unset($this->ids[$overview->msgid]); unset($this->overview[$_id]); unset($this->trees[$_id]); foreach ($this->roots as $k=>&$root) { if ($root === $overview) { unset($this->roots[$k]); break; } } $overview = null; if ($write) { $this->saveToFile(); } } public function formatDate(BananaSpoolHead &$head) { $stamp = $head->date; $today = intval(time() / (24*3600)); $dday = intval($stamp / (24*3600)); if ($today == $dday) { $format = "%H:%M"; } elseif ($today == 1 + $dday) { $format = _b_('hier')." %H:%M"; } elseif ($today < 7 + $dday) { $format = '%a %H:%M'; } elseif ($today < 90 + $dday) { $format = '%a %e %b'; } else { $format = '%a %e %b %Y'; } return strftime($format, $stamp); } public function formatSubject(BananaSpoolHead &$head) { $subject = $popup = $head->subject; $popup = $subject; if (function_exists('hook_formatDisplayHeader')) { list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); } else { $subject = banana_catchFormats(banana_entities(stripslashes($subject))); $link = null; } if (empty($subject)) { $subject = _b_('(pas de sujet)'); } if ($head->id != Banana::$artid) { $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id, 'text' => $subject, 'popup' => $popup)); } return $subject . $link; } public function formatFrom(BananaSpoolHead &$head) { return BananaMessage::formatFrom($head->from); } public function start() { if (Banana::$first) { return Banana::$first; } else { $first = array_search(Banana::$artid, $this->roots); return max(0, $first - Banana::$spool_tbefore); } } public function context() { return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext; } private function &_buildTree(BananaSpoolHead &$head) { static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f; if (!isset($spfx_f)) { $t_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 18, 'width' => 14)); $u_h = Banana::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18, 'width' => 14)); $u_ht = Banana::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14)); $u_vt = Banana::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14)); $u_l = Banana::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14)); $u_f = Banana::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14)); $r_h = Banana::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18, 'width' => 14)); $r_ht = Banana::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14)); $r_vt = Banana::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14)); $r_l = Banana::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14)); $r_f = Banana::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14)); } $style = 'background-color:' . $head->color . '; text-decoration: none'; $text = ''; $array = array($text); foreach ($head->children as $key=>&$msg) { $tree =& $this->_buildTree($msg); $last = $key == count($head->children) - 1; foreach ($tree as $kt=>&$line) { if ($kt === 0 && $key === 0 && !$last) { $array[0] .= ($msg->isread ? $r_ht : $u_ht) . $line; } else if($kt === 0 && $key === 0) { $array[0] .= ($msg->isread ? $r_h : $u_h) . $line; } else if ($kt === 0 && $last) { $array[] = $t_e . ($msg->isread ? $r_vt : $u_vt) . $line; } else if ($kt === 0) { $array[] = $t_e . ($msg->isread ? $r_f : $u_f) . $line; } else if ($last) { $array[] = $t_e . $t_e . $line; } else { $array[] = $t_e . ($msg->isread ? $r_l : $u_l) . $line; } } unset($tree); } return $array; } /** build the spool tree associated with the given message */ public function &buildTree($id, $force = false) { $root =& $this->root($id); $id = $root->id; if (!$force && isset($this->trees[$id])) { return $this->trees[$id]; } else { $tree =& $this->_buildTree($root); $tree = '
' . implode("
\n
", $tree) . '
'; return $tree; } } /** computes linear post index * @param $_id INTEGER MSGNUM of post * @return INTEGER linear index of post */ public function getNdX($_id) { $ndx = 1; $id_cur = $_id; while (true) { $id_parent = $this->overview[$id_cur]->parent; if (is_null($id_parent)) break; $pos = array_search($id_cur, $this->overview[$id_parent]->children); for ($i = 0; $i < $pos ; $i++) { $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; } $ndx++; //noeud père $id_cur = $id_parent; } foreach ($this->roots as $i) { if ($i==$id_cur) { break; } $ndx += $this->overview[$i]->desc; } return $ndx; } /** Return root message of the given thread * @param id INTEGER id of a message */ public function &root($id) { $parent =& $this->overview[$id]; while (!is_null($parent->parent)) { $parent =& $parent->parent; } return $parent; } /** Return the last post id with the given subject * @param subject */ public function getPostId($subject) { $subject = trim($subject); $id = max(array_keys($this->overview)); while (isset($this->overview[$id])) { $test = $this->overview[$id]->subject; if (function_exists('hook_formatDisplayHeader')) { $val = hook_formatDisplayHeader('subject', $test, true); if (is_array($val)) { $test = banana_html_entity_decode($val[0]); } else { $test = banana_html_entity_decode($val); } } $test = trim($test); if ($test == $subject) { return $id; } $id--; } return -1; } /** Returns previous thread root index * @param id INTEGER message number */ public function prevThread($id) { $root =& $this->root($id); $last = null; foreach ($this->roots as &$i) { if ($i === $root) { return $last; } $last = $i->id; } return $last; } /** Returns next thread root index * @param id INTEGER message number */ public function nextThread($id) { $root =& $this->root($id); $ok = false; foreach ($this->roots as &$i) { if ($ok) { return $i->id; } if ($i === $root) { $ok = true; } } return null; } /** Return prev post in the thread * @param id INTEGER message number */ public function prevPost($id) { $parent =& $this->overview[$id]->parent; if (is_null($parent)) { return null; } $last = $parent->id; foreach ($parent->children as &$child) { if ($child->id == $id) { return $last; } $last = $child->id; } return null; } /** Return next post in the thread * @param id INTEGER message number */ public function nextPost($id) { $cur =& $this->overview[$id]; if (count($cur->children) != 0) { return $cur->children[0]->id; } $parent =& $cur; while (true) { $parent =& $cur->parent; if (is_null($parent)) { return null; } $ok = false; foreach ($parent->children as &$child) { if ($ok) { return $child->id; } if ($child === $cur) { $ok = true; } } $cur =& $parent; } return null; } /** Look for an unread message in the thread rooted by the message * @param id INTEGER message number */ private function _nextUnread(BananaSpoolHead &$cur) { if (!$cur->isread) { return $cur->id; } foreach ($cur->children as &$child) { $unread = $this->_nextUnread($child); if (!is_null($unread)) { return $unread; } } return null; } /** Find next unread message * @param id INTEGER message number */ public function nextUnread($id = null) { if (!$this->unreadnb) { return null; } if (!is_null($id)) { // Look in message children foreach ($this->overview[$id]->children as &$child) { $next = $this->_nextUnread($child); if (!is_null($next)) { return $next; } } } // Look in current thread if (is_null($id)) { $cur = null; } else { $cur =& $this->overview[$id]; } do { if (is_null($cur)) { $parent =& $cur; $ok = true; } else { $parent =& $cur->parent; $ok = false; } if (!is_null($parent)) { $array =& $parent->children; } else { $array =& $this->roots; } foreach ($array as &$child) { if ($ok) { $next = $this->_nextUnread($child); if (!is_null($next)) { return $next; } } if ($child === $cur) { $ok = true; } } $cur =& $parent; } while(!is_null($cur)); return null; } } // vim:set et sw=4 sts=4 ts=4 enc=utf-8: ?>