Fix deletion of message from spool
[banana.git] / banana / spool.inc.php
index 9514d6a..172e4aa 100644 (file)
@@ -49,7 +49,7 @@ class BananaSpoolHead
     public function __construct(array &$message)
     {
         $this->date       = $message['date'];
-        $this->subject    = stripslashes($message['subject']);
+        $this->subject    = $message['subject'];
         $this->from       = $message['from'];
         $this->desc       = 1;
         $this->isread     = true;
@@ -61,6 +61,7 @@ class BananaSpoolHead
 class BananaSpool
 {
     private $version;
+    private $mode;
 
     /**  group name */
     public $group;
@@ -80,13 +81,14 @@ class BananaSpool
     protected function __construct($group)
     {
         $this->version    = BANANA_SPOOL_VERSION;
+        $this->mode       = Banana::SPOOL_ALL;
         $this->group      = $group;
     }
 
-    public static function getSpool($group, $since = 0)
+    public static function getSpool($group, $since = 0, $clean = false)
     {
         if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
-            $spool = Banana::$spool;
+            $spool =& Banana::$spool;
         } else {
             $spool = BananaSpool::readFromFile($group);
         }        
@@ -95,6 +97,9 @@ class BananaSpool
         }
         Banana::$spool =& $spool;
         $spool->build();
+        if ($clean) {
+            $spool->markAllAsRead();
+        }
         $spool->updateUnread($since);
         return $spool;
     }
@@ -106,15 +111,7 @@ class BananaSpool
         if (!is_dir($file)) {
             mkdir($file);
         }
-        $url  = parse_url(Banana::$host);
-        if (isset($url['host'])) {
-            $file .= $url['host'] . '_';
-        }
-        if (isset($url['port'])) {
-            $file .= $url['port'] . '_';
-        }
-        $file .= $group;
-        return $file;
+        return $file . Banana::$protocole->filename();
     }
 
     private static function readFromFile($group)
@@ -124,7 +121,7 @@ class BananaSpool
             return null;
         }
         $spool =  unserialize(file_get_contents($file));
-        if ($spool->version != BANANA_SPOOL_VERSION) {
+        if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
             return null;
         }
         return $spool;
@@ -147,7 +144,9 @@ class BananaSpool
             }
         }
 
-        file_put_contents($file, serialize($this));
+        if ($this->mode == Banana::SPOOL_ALL) {
+            file_put_contents($file, serialize($this));
+        }    
     }
 
     private function build()
@@ -161,8 +160,8 @@ class BananaSpool
             $threshold = (int)(log($threshold)/log(2));
             $threshold = (2 ^ ($threshold + 1)) - 1;
         }
-        if (Banana::$maxspool && Banana::$maxspool < $msgnum) {
-            $first = $last - Banana::$maxspool;
+        if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
+            $first = $last - Banana::$spool_max;
             if ($first < 0) {
                 $first += $threshold;
             }
@@ -182,8 +181,7 @@ class BananaSpool
             $mids = array_keys($this->overview);
             foreach ($mids as $id) {
                 if (($first <= $last && ($id < $first || $id > $last))
-                        || ($first > $last && $id < $first && $id > $last))
-                {
+                        || ($first > $last && $id < $first && $id > $last)) {
                     $this->delid($id, false);
                     $do_save = true;
                 }
@@ -211,6 +209,9 @@ class BananaSpool
             $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);
@@ -248,7 +249,7 @@ class BananaSpool
         }
 
         $newpostsids = Banana::$protocole->getNewIndexes($since);
-        
+
         if (empty($newpostsids)) {
             return;
         }
@@ -261,7 +262,6 @@ class BananaSpool
             $id = $this->ids[$mid];
             if ($this->overview[$id]->isread) {
                 $this->overview[$id]->isread     = false;
-                $this->overview[$id]->descunread = 1;
                 while (isset($id)) {
                     $this->overview[$id]->descunread ++;
                     $id = $this->overview[$id]->parent;
@@ -272,6 +272,7 @@ class BananaSpool
 
     public function setMode($mode)
     {
+        $this->mode = $mode;
         switch ($mode) {
           case Banana::SPOOL_UNREAD:
             foreach ($this->roots as $k=>$i) {
@@ -284,6 +285,40 @@ class BananaSpool
         }
     }
 
+    /** 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
      */
@@ -329,11 +364,16 @@ class BananaSpool
             }
             unset($this->overview[$_id]);
             $msgid = array_search($_id, $this->ids);
-            if ($msgid) {
+            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();
             }
         }
@@ -366,9 +406,7 @@ class BananaSpool
      * @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_getSubject(&$subject) which
-     * take the subject as a reference parameter, transform this subject to be displaid in the spool
-     * view and return a string. This string will be put after the subject.
+     * 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)
     {
@@ -400,14 +438,15 @@ class BananaSpool
             $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">'
                 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n);
             $subject = $overview->subject;
+            if (function_exists('hook_formatDisplayHeader')) {
+                list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
+            } else {
+                $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
+                $link = null;
+            }
             if (empty($subject)) {
                 $subject = _b_('(pas de sujet)');
             }
-            $link = null;
-            if (function_exists('hook_getSubject')) {
-                $link = hook_getSubject($subject);
-            }
-            $subject = banana_catchFormats($subject);
             if ($_index != $_ref) {
                 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
                                                     'text'  => $subject, 'popup' => $subject));
@@ -455,12 +494,12 @@ class BananaSpool
 
         if (!$overview) {
             $_first = $first;
-            $_last  = $first + Banana::$tmax - 1;
+            $_last  = $first + Banana::$spool_tmax - 1;
             $_ref   = null;
         } else {
             $_ref   = $this->getNdx($first);
-            $_last  = $_ref + Banana::$tafter;
-            $_first = $_ref - Banana::$tbefore;
+            $_last  = $_ref + Banana::$spool_tafter;
+            $_first = $_ref - Banana::$spool_tbefore;
             if ($_first < 0) {
                 $_last -= $_first;
             }
@@ -492,7 +531,7 @@ class BananaSpool
             for ($i = 0; $i < $pos ; $i++) {
                 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
             }
-            $ndx++; //noeud père
+            $ndx++; //noeud père
 
             $id_cur = $id_parent;
         }
@@ -661,5 +700,5 @@ class BananaSpool
     }    
 }
 
-// vim:set et sw=4 sts=4 ts=4
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
 ?>