date = $message['date']; $this->subject = $message['subject']; $this->from = $message['from']; $this->desc = 1; $this->isread = true; $this->descunread = 0; } } class BananaSpool { private $version; private $mode; /** group name */ public $group; /** spool */ public $overview; /** array msgid => msgnum */ public $ids; /** thread starts */ public $roots; /** 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); 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) { $file = BananaSpool::spoolFilename($group); if (!file_exists($file)) { return null; } $spool = unserialize(file_get_contents($file)); if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { return null; } return $spool; } private function compare($a, $b) { return ($b->date >= $a->date); } private function saveToFile() { $file = BananaSpool::spoolFilename($this->group); uasort($this->overview, array($this, 'compare')); $this->roots = Array(); foreach($this->overview as $id=>$msg) { if (is_null($msg->parent)) { $this->roots[] = $id; } } 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 = $firt + $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 (is_array($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')); if (!is_array($this->ids)) { $this->ids = array(); } foreach ($messages as $id=>&$message) { $this->ids[$message['message-id']] = $id; } if (!is_array($this->overview)) { $this->overview = array(); } foreach ($messages as $id=>&$message) { if (!isset($this->overview[$id])) { $this->overview[$id] = new BananaSpoolHead($message); } $msg =& $this->overview[$id]; $msgrefs = BananaMessage::formatReferences($message); $parents = preg_grep('/^\d+$/', $msgrefs); $msg->parent = array_pop($parents); $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); if (!is_null($p = $msg->parent)) { if (empty($this->overview[$p])) { $this->overview[$p] = new BananaSpoolHead($messages[$p]); } $this->overview[$p]->children[] = $id; while (!is_null($p)) { $this->overview[$p]->desc += $msg->desc; if ($p != $this->overview[$p]->parent) { $p = $this->overview[$p]->parent; } else { $p = null; } } } } Banana::$protocole->updateSpool($messages); return true; } private function updateUnread($since) { if (empty($since)) { return; } $newpostsids = Banana::$protocole->getNewIndexes($since); if (empty($newpostsids)) { return; } if (!is_array($this->ids)) { $this->ids = array(); } $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); foreach ($newpostsids as $mid) { $id = $this->ids[$mid]; if ($this->overview[$id]->isread) { $this->overview[$id]->isread = false; while (isset($id)) { $this->overview[$id]->descunread ++; $id = $this->overview[$id]->parent; } } } } public function setMode($mode) { $this->mode = $mode; switch ($mode) { case Banana::SPOOL_UNREAD: foreach ($this->roots as $k=>$i) { if ($this->overview[$i]->descunread == 0) { $this->killdesc($i); unset($this->roots[$k]); } } break; } } /** 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; 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 (is_null($array)) { $array =& $this->roots; } if (!is_array($this->roots)) { return; } foreach ($array as $id) { if (!$this->overview[$id]->isread) { $this->markAsRead($id); } if ($this->overview[$id]->descunread) { $this->markAllAsRead($this->overview[$id]->children); } } } /** kill post and childrens * @param $_id MSGNUM of post */ private function killdesc($_id) { if (sizeof($this->overview[$_id]->children)) { foreach ($this->overview[$_id]->children as $c) { $this->killdesc($c); } } unset($this->overview[$_id]); if (($msgid = array_search($_id, $this->ids)) !== false) { unset($this->ids[$msgid]); } } /** delete a post from overview * @param $_id MSGNUM of post */ public function delid($_id, $write = true) { if (isset($this->overview[$_id])) { if (sizeof($this->overview[$_id]->parent)) { $this->overview[$this->overview[$_id]->parent]->children = array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); if (sizeof($this->overview[$_id]->children)) { $this->overview[$this->overview[$_id]->parent]->children = array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); foreach ($this->overview[$_id]->children as $c) { $this->overview[$c]->parent = $this->overview[$_id]->parent; $this->overview[$c]->parent_direct = false; } } $p = $this->overview[$_id]->parent; while ($p) { $this->overview[$p]->desc--; $p = $this->overview[$p]->parent; } } elseif (sizeof($this->overview[$_id]->children)) { foreach ($this->overview[$_id]->children as $c) { $this->overview[$c]->parent = null; } } unset($this->overview[$_id]); $msgid = array_search($_id, $this->ids); if ($msgid !== false) { unset($this->ids[$msgid]); } $msgid = array_search($_id, $this->roots); if ($msgid !== false) { unset($this->roots[$msgid]); } if ($write) { $this->markAllAsRead(); $this->saveToFile(); } } } private function formatDate($stamp) { $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'; } else { $format = '%a %e %b'; } return utf8_encode(strftime($format, $stamp)); } /** displays children tree of a post * @param $_id INTEGER MSGNUM of post * @param $_index INTEGER linear number of post in the tree * @param $_first INTEGER linear number of first post displayed * @param $_last INTEGER linear number of last post displayed * @param $_ref STRING MSGNUM of current post * @param $_pfx_node STRING prefix used for current node * @param $_pfx_end STRING prefix used for children of current node * @param $_head BOOLEAN true if first post in thread * * If you want to analyse subject, you can define the function hook_formatDisplayHeader */ private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) { static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I; if (!isset($spfx_f)) { $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9)); $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9)); $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12)); $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12)); $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5)); $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12)); $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12)); $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5)); $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12)); $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12)); } $overview =& $this->overview[$_id]; if ($_index + $overview->desc < $_first || $_index > $_last) { return ''; } $res = ''; if ($_index >= $_first) { $hc = empty($overview->children); $res .= '