X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=banana%2Fspool.inc.php;h=ace19f7277dde077404d0aba500faba7a184384c;hb=ade32c5593c555591eb5cf859ea64b8ef7fdaaf8;hp=e65d05a1e6056dafdb71b07d5ebfe6dbac299f5f;hpb=06a59a10be523ac4396ab29fcbd00457f3e910da;p=banana.git diff --git a/banana/spool.inc.php b/banana/spool.inc.php index e65d05a..ace19f7 100644 --- a/banana/spool.inc.php +++ b/banana/spool.inc.php @@ -8,14 +8,18 @@ ********************************************************************************/ require_once dirname(__FILE__) . '/banana.inc.php'; +require_once dirname(__FILE__) . '/tree.inc.php'; -define('BANANA_SPOOL_VERSION', '0.5.9'); +define('BANANA_SPOOL_VERSION', '0.5.14'); /** Class spoolhead * class used in thread overviews */ class BananaSpoolHead { + public $id; + public $msgid; + /** date (timestamp) */ public $date; /** subject */ @@ -26,8 +30,6 @@ class BananaSpoolHead public $color; /** reference of parent */ public $parent = null; - /** paren is direct */ - public $parent_direct; /** array of children */ public $children = Array(); /** true if post is read */ @@ -36,6 +38,8 @@ class BananaSpoolHead public $desc; /** same as desc, but counts only unread posts */ public $descunread; + /** last time the number of children has been updated */ + public $time = 0; /** storage data */ public $storage = array(); @@ -48,28 +52,21 @@ class BananaSpoolHead * @param $_read BOOLEAN true if read * @param $_descunread INTEGER descunread value (0 for a new post) */ - public function __construct(array &$message) + public function __construct($id, array &$message) { + list($name, $from) = BananaMessage::extractMail($message['from']); + $this->id = $id; + $this->msgid = @$message['message-id']; $this->date = $message['date']; - $this->subject = $message['subject']; + $this->subject = @$message['subject']; $this->from = $message['from']; - $this->color = sprintf('#%06x', abs(crc32($this->from) % 0xffffff)); + $this->color = sprintf('#%06x', abs(crc32($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'; + $this->name = $name; + if ($name === $from) { + $this->name = 'Anonymous'; } } } @@ -83,13 +80,11 @@ class BananaSpool /** group name */ public $group; /** spool */ - public $overview; + public $overview = array(); /** array msgid => msgnum */ - public $ids; + public $ids = array(); /** thread starts */ - public $roots; - /** thread trees (one tree per root node) */ - public $trees = array(); + public $roots = array(); /** protocole specific data */ public $storage = array(); @@ -127,19 +122,27 @@ class BananaSpool return $spool; } - private static function spoolFilename($group) + public static function getPath($file = null) { - $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/'; - if (!is_dir($file)) { - mkdir($file); + $path = Banana::$spool_root . '/' . Banana::$protocole->name() . '/' . Banana::$protocole->filename(); + if (!is_dir($path)) { + if (file_exists($path)) { + @unlink($path); + } + mkdir($path, 0777, true); } - return $file . Banana::$protocole->filename(); + return $path . '/' . $file; + } + + private static function spoolFilename() + { + return BananaSpool::getPath('spool'); } private static function &readFromFile($group) { $spool = null; - $file = BananaSpool::spoolFilename($group); + $file = BananaSpool::spoolFilename(); if (!file_exists($file)) { return $spool; } @@ -154,17 +157,17 @@ class BananaSpool private function compare($a, $b) { - return ($this->overview[$b]->date >= $this->overview[$a]->date); + return ($b->date - $a->date); } private function saveToFile() { - $file = BananaSpool::spoolFilename($this->group); + $file = BananaSpool::spoolFilename(); $this->roots = Array(); - foreach($this->overview as $id=>&$msg) { + foreach($this->overview as &$msg) { if (is_null($msg->parent)) { - $this->roots[] = $id; + $this->roots[] =& $msg; } } usort($this->roots, array($this, 'compare')); @@ -202,7 +205,7 @@ class BananaSpool private function clean(&$first, &$last, $msgnum) { $do_save = false; - if (is_array($this->overview)) { + if (!empty($this->overview)) { $mids = array_keys($this->overview); foreach ($mids as $id) { if (($first <= $last && ($id < $first || $id > $last)) @@ -227,49 +230,41 @@ class BananaSpool $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(); - } - $updateTrees = array(); + // Build all the new Spool Heads + $time = time(); foreach ($messages as $id=>&$message) { if (!isset($this->overview[$id])) { - $this->overview[$id] = new BananaSpoolHead($message); + $this->overview[$id] = new BananaSpoolHead($id, $message); + $head =& $this->overview[$id]; + $this->ids[$head->msgid] =& $head; + $head->time = $time; } - $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; - $prev = $p; - if ($p != $this->overview[$p]->parent) { - $p = $this->overview[$p]->parent; + // Build tree + $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; + $parent->time = $time; + $prev =& $parent; + if ($parent !== $parent->parent) { + $parent =& $parent->parent; } else { - $p = null; - } - if (is_null($p)) { - $updateTrees[$prev] = true; + $parent =& $null; } } } } - foreach ($updateTrees as $root=>$t) { - $this->trees[$root] =& $this->buildTree($root); - } Banana::$protocole->updateSpool($messages); return true; } @@ -286,21 +281,34 @@ class BananaSpool 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; - $this->unreadnb++; - while (isset($id)) { - $this->overview[$id]->descunread++; - $id = $this->overview[$id]->parent; + $overview =& $this->ids[$mid]; + if ($overview->isread) { + $overview->isread = false; + while (!is_null($overview)) { + $overview->descunread++; + $overview =& $overview->parent; } } } + $this->unreadnb += count($newpostsids); + + if (function_exists('hook_listReadMessages')) { + $msgs = hook_listReadMessages($this->group); + if (!is_array($msgs)) { + return; + } + foreach ($msgs as $msg) { + if (!is_numeric($msg)) { + if (!isset($this->ids[$msg])) { + continue; + } + $msg = $this->ids[$msg]->id; + } + $this->markAsRead($msg); + } + } } public function setMode($mode) @@ -312,27 +320,52 @@ class BananaSpool if ($this->overview[$num]->isread) { break; } - foreach ($this->roots as $k=>$i) { - if ($this->overview[$i]->descunread == 0) { - $this->killdesc($i); - unset($this->roots[$k]); + 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; + } + + /** Get the tree associated to a given id + */ + public function &getTree($id) + { + return BananaTree::build($id)->show(); + } + /** 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; + $overview =& $this->overview[$id]; + if (!$overview->isread) { + $overview->isread = true; $this->unreadnb--; - while (isset($id)) { - $this->overview[$id]->descunread--; - $id = $this->overview[$id]->parent; + while (!is_null($overview)) { + $overview->descunread--; + $overview =& $overview->parent; } } } @@ -344,20 +377,20 @@ class BananaSpool if (!$this->unreadnb) { return; } - if (is_null($array) && is_array($this->roots)) { + if (is_null($array) && !empty($this->roots)) { $array =& $this->roots; } elseif (is_null($array)) { return; } - foreach ($array as $id) { - if (!$this->overview[$id]->isread) { - $this->markAsRead($id); + foreach ($array as &$msg) { + if (!$msg->isread) { + $this->markAsRead($msg->id); if (!$this->unreadnb) { return; } } - if ($this->overview[$id]->descunread) { - $this->markAllAsRead($this->overview[$id]->children); + if ($msg->descunread) { + $this->markAllAsRead($msg->children); } } } @@ -367,15 +400,22 @@ class BananaSpool */ private function killdesc($_id) { - if (sizeof($this->overview[$_id]->children)) { - foreach ($this->overview[$_id]->children as $c) { - $this->killdesc($c); + $overview =& $this->overview[$_id]; + $children =& $overview->children; + if (sizeof($children)) { + foreach ($children as &$c) { + $this->killdesc($c->id); } } unset($this->overview[$_id]); - if (($msgid = array_search($_id, $this->ids)) !== false) { - unset($this->ids[$msgid]); + foreach ($this->roots as $k=>&$root) { + if ($root === $overview) { + unset($this->roots[$k]); + break; + } } + unset($this->ids[$overview->msgid]); + $overview = null; } /** delete a post from overview @@ -383,51 +423,61 @@ class BananaSpool */ public function delid($_id, $write = true) { - if (isset($this->overview[$_id])) { - $overview =& $this->overview[$_id]; - if (!$overview->isread) { - $this->markAsRead($_id); - } - if ($overview->parent) { - $p = $overview->parent; - $parent =& $this->overview[$p]; - $parent->children = array_diff($parent->children, array($_id)); - if (sizeof($overview->children)) { - $parent->children = array_merge($parent->children, $overview->children); - foreach ($overview->children as $c) { - $this->overview[$c]->parent = $p; - $this->overview[$c]->parent_direct = false; - } - } - while (isset($p)) { - $this->overview[$p]->desc--; - $p = $this->overview[$p]->parent; - } - } elseif ($overview->children) { - foreach ($overview->children as $c) { - $this->overview[$c]->parent = null; + 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)) { + $time = time(); + foreach ($parent->children as $key=>&$child) { + if ($child === $overview) { + unset($parent->children[$key]); + break; } } - unset($overview); - unset($this->overview[$_id]); - unset($this->trees[$_id]); - $msgid = array_search($_id, $this->ids); - if ($msgid !== false) { - unset($this->ids[$msgid]); + if (sizeof($overview->children)) { + foreach ($overview->children as &$child) { + $parent->children[] =& $child; + $child->time = $time; + $child->parent =& $parent; + } } - $msgid = array_search($_id, $this->roots); - if ($msgid !== false) { - unset($this->roots[$msgid]); + while (!is_null($parent)) { + $parent->desc--; + $parent->time = $time; + $parent =& $parent->parent; } + } - if ($write) { - $this->saveToFile(); + // Remove all refenrences and assign null to the object + unset($this->ids[$overview->msgid]); + unset($this->overview[$_id]); + BananaTree::kill($_id); + foreach ($this->roots as $k=>&$root) { + if ($root === $overview) { + unset($this->roots[$k]); + break; } } + $overview = null; + + if ($write) { + $this->saveToFile(); + } } - public function formatDate($stamp) + public function formatDate(BananaSpoolHead $head) { + $stamp = $head->date; $today = intval(time() / (24*3600)); $dday = intval($stamp / (24*3600)); @@ -445,9 +495,9 @@ class BananaSpool return strftime($format, $stamp); } - public function formatSubject($id, $subject) + public function formatSubject(BananaSpoolHead $head) { - $subject = banana_html_entity_decode($subject); + $subject = $popup = $head->subject; $popup = $subject; if (function_exists('hook_formatDisplayHeader')) { list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); @@ -458,17 +508,19 @@ class BananaSpool if (empty($subject)) { $subject = _b_('(pas de sujet)'); } - if ($id != Banana::$artid) { - $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $id, + if (mb_strlen($subject) > 100) { + $subject = mb_substr($subject, 0, 99) . '…'; + } + 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($from) + public function formatFrom(BananaSpoolHead $head) { - $from = banana_html_entity_decode($from); - return BananaMessage::formatFrom($from); + return BananaMessage::formatFrom($head->from); } public function start() @@ -486,110 +538,16 @@ class BananaSpool return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext; } - - private function &_buildTree($id, 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 = 'makeURL(array('group' => $this->group, 'artid' => $id))) . '\'"' - : ' disabled="disabled"') - . ' />'; - $array = array($text); - foreach ($head->children as $key=>&$child) { - $msg =& $this->overview[$child]; - $tree =& $this->_buildTree($child, $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) { - $id = $this->root($id); - if (!$force && isset($this->trees[$id])) { - return $this->trees[$id]; - } else { - $tree =& $this->_buildTree($id, $this->overview[$id]); - $tree = '