dead code + css
[banana.git] / include / spool.inc.php
index 2522f04..f657d55 100644 (file)
@@ -8,7 +8,8 @@
 ********************************************************************************/
 
 if(!function_exists('_file_put_contents')) {
-    function file_put_contents($filename, $data) {
+    function file_put_contents($filename, $data)
+    {
         $fp = fopen($filename, 'w');
         if(!$fp) {
             trigger_error('file_put_contents cannot write in file '.$filename, E_USER_ERROR);
@@ -16,15 +17,15 @@ if(!function_exists('_file_put_contents')) {
         }
         fputs($fp, $data);
         fclose($fp);
-
     }
 }
 
+function spoolCompare($a,$b) { return ($b->date>=$a->date); }
 
 /** Class spoolhead
  *  class used in thread overviews
  */
-class SpoolHead
+class BananaSpoolHead
 {
     /** date (timestamp) */
     var $date;
@@ -54,7 +55,7 @@ class SpoolHead
      * @param $_descunread INTEGER descunread value (0 for a new post)
      */
 
-    function SpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0)
+    function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0)
     {
         $this->date       = $_date;
         $this->subject    = $_subject;
@@ -71,7 +72,7 @@ class SpoolHead
 
 define("BANANA_SPOOL_VERSION", '0.2');
 
-class spool
+class BananaSpool
 {
     var $version;
     /**  spool */
@@ -84,143 +85,139 @@ class spool
     var $roots;
 
     /** constructor
-     * @param $_nntp RESOURCE NNTP socket filehandle
      * @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)
      */
-
-    function spool(&$_nntp, $_group, $_display=0, $_since="")
+    function BananaSpool($_group, $_display=0, $_since="")
     {
-        global $news;
-
-        $do_save    = false;
-
-        $spool_path = dirname(dirname(__FILE__)).'/spool';
-        $spoolfile  = "$spool_path/spool-$_group.dat";
+        global $banana;
+        $this->group = $_group;
+        $groupinfo   = $banana->nntp->group($_group);
+        if (!$groupinfo) { return ($this = null); }
 
-        $groupinfo  = $_nntp->group($_group);
-        $first      = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
-        $last       = $groupinfo[2];
-
-        if (!$groupinfo) {
-            $this = null;
-            return false;
-        }
-        if (file_exists($spoolfile)) {
-            $this   = unserialize(file_get_contents($spoolfile));
-        }
+        $this->_readFromFile();
 
+        $do_save = false;
+        $first   = $banana->maxspool ? max($groupinfo[2]-$banana->maxspool, $groupinfo[1]) : $groupinfo[1];
+        $last    = $groupinfo[2];
         if ($this->version == BANANA_SPOOL_VERSION) {
-            $keys   = array_values($this->ids);
-            rsort($keys);
-            // remove expired messages
             for ($id = min(array_keys($this->overview)); $id<$first; $id++) { 
                 $this->delid($id, false);
                 $do_save = true;
             }
-            $start  = max(array_keys($this->overview))+1;
+            $first = max(array_keys($this->overview))+1;
         } else {
             unset($this->overview, $this->ids);
-            $this->group   = $_group;
             $this->version = BANANA_SPOOL_VERSION;
-            $start         = $first;
         }
 
-        if (($start<$last) && $groupinfo[0]) {
-            $do_save  = true;
+        if ($first<=$last && $groupinfo[0]) {
+            $do_save = true;
+            $this->_updateSpool("$first-$last");
+        }
+
+        if ($do_save) { $this->_saveToFile(); }
 
-            $dates    = array_map("strtotime",    $_nntp->xhdr("Date",    "$start-$last"));
-            $subjects = array_map("headerdecode", $_nntp->xhdr("Subject", "$start-$last"));
-            $froms    = array_map("headerdecode", $_nntp->xhdr("From",    "$start-$last"));
-            $msgids   = $_nntp->xhdr("Message-ID", "$start-$last");
-            $refs     = $_nntp->xhdr("References", "$start-$last");
+        $this->_updateUnread($_since, $_display);
+    }
 
-            if (isset($this->ids)) {
-                $this->ids = array_merge($this->ids, array_flip($msgids));
-            } else {
-                $this->ids = array_flip($msgids);
+    function _readFromFile()
+    {
+        $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
+        if (file_exists($file)) {
+            $this = unserialize(file_get_contents($file));
+        }
+    }
+
+    function _saveToFile()
+    {
+        $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
+        uasort($this->overview, "spoolcompare");
+
+        $this->roots = Array();
+        foreach($this->overview as $id=>$msg) {
+            if (is_null($msg->parent)) {
+                $this->roots[] = $id;
             }
+        }
+        
+        file_put_contents($file, serialize($this));
+    }
 
-            foreach ($msgids as $id=>$msgid) {
-                $msg                = new spoolhead($dates[$id], $subjects[$id], $froms[$id]);
-                $refs[$id]          = str_replace("><", "> <", $refs[$id]);
-                $msgrefs            = preg_split("/( |\t)/", strtr($refs[$id], $this->ids));
-                $parents            = preg_grep("/^\d+$/", $msgrefs);
-                $msg->parent        = array_pop($parents);
-                $msg->parent_direct = preg_match("/^\d+$/", array_pop($msgrefs));
-                
-                if (isset($this->overview[$id])) {
-                    $msg->desc     = $this->overview[$id]->desc;
-                    $msg->children = $this->overview[$id]->children;
+    function _updateSpool($arg)
+    {
+        global $banana;
+        $dates    = array_map(strtotime,    $banana->nntp->xhdr("Date",    $arg));
+        $subjects = array_map(headerdecode, $banana->nntp->xhdr("Subject", $arg));
+        $froms    = array_map(headerdecode, $banana->nntp->xhdr("From",    $arg));
+        $msgids   = $banana->nntp->xhdr("Message-ID", $arg);
+        $refs     = $banana->nntp->xhdr("References", $arg);
+
+        if (is_array($this->ids)) {
+            $this->ids = array_merge($this->ids, array_flip($msgids));
+        } else {
+            $this->ids = array_flip($msgids);
+        }
+
+        foreach ($msgids as $id=>$msgid) {
+            $msg                = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]);
+            $refs[$id]          = str_replace('><', '> <', $refs[$id]);
+            $msgrefs            = preg_split("/[ \t]/", strtr($refs[$id], $this->ids));
+            $parents            = preg_grep('/^\d+$/', $msgrefs);
+            $msg->parent        = array_pop($parents);
+            $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
+
+            if (isset($this->overview[$id])) {
+                $msg->desc     = $this->overview[$id]->desc;
+                $msg->children = $this->overview[$id]->children;
+            }
+            $this->overview[$id] = $msg;
+
+            if ($p = $msg->parent) {
+                if (empty($this->overview[$p])) {
+                    $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1);
                 }
-                $this->overview[$id] = $msg;
-           
-                if ($p = $msg->parent) {
-                    if (empty($this->overview[$p])) {
-                        $this->overview[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
-                    }
-                    $this->overview[$msg->parent]->children[] = $id;
+                $this->overview[$p]->children[] = $id;
 
-                    while ($p) {
-                        $this->overview[$p]->desc += $msg->desc;
-                        $p = $this->overview[$p]->parent;
-                    }
-                    
+                while ($p) {
+                    $this->overview[$p]->desc += $msg->desc;
+                    $p = $this->overview[$p]->parent;
                 }
             }
         }
+    }
 
-        if ($do_save) { $this->save($spoolfile); }
-
-        if ($_since) {
-            $newpostsids = $_nntp->newnews($_since, $_group);
-            if (sizeof($newpostsids)) {
-                $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
-                if ($newpostsids && !is_null($newpostsids)) {
-                    foreach ($newpostsids as $mid) {
-                        $this->overview[$this->ids[$mid]]->isread     = false;
-                        $this->overview[$this->ids[$mid]]->descunread = 1;
-                        $parentmid = $this->ids[$mid];
-                        while (isset($parentmid)) {
-                            $this->overview[$parentmid]->descunread ++;
-                            $parentmid = $this->overview[$parentmid]->parent;
-                        }
-                    }
+    function _updateUnread($since, $mode)
+    {
+        global $banana;
+        if (empty($since)) { return; }
+
+        if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) {
+            $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
+            foreach ($newpostsids as $mid) {
+                $this->overview[$this->ids[$mid]]->isread     = false;
+                $this->overview[$this->ids[$mid]]->descunread = 1;
+                $parentmid = $this->ids[$mid];
+                while (isset($parentmid)) {
+                    $this->overview[$parentmid]->descunread ++;
+                    $parentmid = $this->overview[$parentmid]->parent;
                 }
             }
-            if (sizeof($newpostsids)>0) {
-                switch ($_display) {
+
+            if (count($newpostsids)) {
+                switch ($mode) {
                     case 1:
-                        foreach ($this->roots as $i) {
+                        foreach ($this->roots as $k=>$i) {
                             if ($this->overview[$i]->descunread==0) {
                                 $this->killdesc($i);
+                                unset($this->roots[$k]);
                             }
                         }
                         break;
                 }
             }
         }
-        
-        return true;
-    }
-
-    function cmp($a, $b) {
-        return $this->overview[$a]->date < $this->overview[$b]->date;
-    }
-
-    function save($file)
-    {
-        uasort($this->overview, "spoolcompare");
-
-        $this->roots = Array();
-        foreach($this->overview as $id=>$msg) {
-            if (is_null($msg->parent)) {
-                $this->roots[] = $id;
-            }
-        }
-        
-        file_put_contents($file, serialize($this));
     }
 
     /** kill post and childrens
@@ -234,11 +231,8 @@ class spool
                 $this->killdesc($c);
             }
         }
-        $pos = array_search($_id, $this->roots);
-        unset($this->roots[$pos]);
         unset($this->overview[$_id]);
-        $msgid = array_search($_id, $this->ids);
-        if ($msgid) {
+        if (($msgid = array_search($_id, $this->ids)) !== false) {
             unset($this->ids[$msgid]);
         }
     }
@@ -277,9 +271,7 @@ class spool
                 unset($this->ids[$msgid]);
             }
             
-            if ($write) {
-                $this->save(dirname(dirname(__FILE__)).'/spool');
-            }
+            if ($write) { $this->_saveToFile(); }
         }
     }
 
@@ -294,8 +286,8 @@ class spool
      * @param $_head BOOLEAN true if first post in thread
      */
 
-    function disp_desc($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) {
-        global $css;
+    function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
+    {
         $spfx_f   = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; 
         $spfx_n   = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; 
         $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
@@ -311,48 +303,46 @@ class spool
             return;
         }
 
+        $res = '';
+
         if ($_index>=$_first) {
-            $us = ($_index == $_ref);
             $hc = empty($this->overview[$_id]->children);
 
-            echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n";
-            echo "<td class=\"{$css['date']}\">"
-                .formatSpoolHeader("date", $this->overview[$_id]->date, $_id,
-                        $this->group, $us, $this->overview[$_id]->isread)
-                ." </td>\n";
-            echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">$_pfx_node"
-                .($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n)
-                ."</div>"
-                .formatSpoolHeader("subject", $this->overview[$_id]->subject, $_id,
-                        $this->group, $us, $this->overview[$_id]->isread)
-                ." </td>\n";
-            echo "<td class=\"{$css['author']}\">"
-                .formatSpoolHeader("from", $this->overview[$_id]->from, $_id,
-                        $this->group, $us, $this->overview[$_id]->isread)
-                ." </td>\n</tr>";
-            if ($hc) {
-                return true;
+            $res .= '<tr class="'.($_index%2?'pair':'impair').($this->overview[$_id]->isread?'':' new')."\">\n";
+            $res .= "<td class='date'>".fancyDate($this->overview[$_id]->date)." </td>\n";
+            $res .= "<td class='subj'>"
+                ."<div class='tree'>$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n)
+                ."</div>";
+            if ($_index == $_ref) {
+                $res .= '<span class="cur">'.htmlentities($this->overview[$_id]->subject).'</span>';
+            } else {
+                $res .= "<a href='?group={$this->group}&amp;artid=$_id'>".htmlentities($this->overview[$_id]->subject).'</a>';
             }
+            $res .= "</td>\n<td class='from'>".formatFrom($this->overview[$_id]->from)."</td>\n</tr>";
+
+            if ($hc) { return $res; }
         } 
 
-        $index = $_index+1;
+        $_index ++;
 
         $children = $this->overview[$_id]->children;
         while ($child = array_shift($children)) {
-            if ($index > $_last) { return; }
-            if ($index+$this->overview[$child]->desc >= $_first) {
+            if ($_index > $_last) { return $res; }
+            if ($_index+$this->overview[$child]->desc >= $_first) {
                 if (sizeof($children)) {
-                    $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
-                            ($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
+                    $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
+                            $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
                             $_pfx_end.$spfx_I, false);
                 } else {
-                    $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
-                            ($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
+                    $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
+                            $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
                             $_pfx_end.$spfx_e, false);
                 }
             }
-            $index += $this->overview[$child]->desc;
+            $_index += $this->overview[$child]->desc;
         }
+
+        return $res;
     }
 
     /** Displays overview
@@ -361,24 +351,28 @@ class spool
      * @param $_ref STRING MSGNUM of current/selectionned post
      */
 
-    function disp($_first=0, $_last=0, $_ref="") {
-        global $css;
+    function to_html($_first=0, $_last=0, $_ref = null)
+    {
+        $res  = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">';
+       
+        if (is_null($_ref)) {
+            $res .= '<tr><th>'._b_('Date').'</th>';
+            $res .= '<th>'._b_('Sujet').'</th>';
+            $res .= '<th>'._b_('Auteur').'</th></tr>';
+        }
+
         $index = 1;
         if (sizeof($this->overview)) {
             foreach ($this->roots as $id) {
-                $this->disp_desc($id, $index, $_first, $_last, $_ref);
+                $res   .= $this->_to_html($id, $index, $_first, $_last, $_ref);
                 $index += $this->overview[$id]->desc ;
-                if ($index > $_last) {
-                    break;
-                }
+                if ($index > $_last) { break; }
             }
         } else {
-            echo "<tr class=\"{$css['pair']}\">\n";
-            echo "\t<td colspan=\"3\">\n";
-            echo "\t\tNo post in this newsgroup\n";
-            echo "\t</td>\n";
-            echo "</tr>\n";
+            $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>';
         }
+
+        return $res .= '</table>';
     }
 
     /** computes linear post index
@@ -386,7 +380,8 @@ class spool
      * @return INTEGER linear index of post
      */
 
-    function getndx($_id) {
+    function getndx($_id)
+    {
         $ndx    = 1;
         $id_cur = $_id;
         while (true) {