whole bunch of changes :
authorx2000habouzit <x2000habouzit>
Sun, 2 Jan 2005 03:00:25 +0000 (03:00 +0000)
committerx2000habouzit <x2000habouzit>
Sun, 2 Jan 2005 03:00:25 +0000 (03:00 +0000)
 - reorganize includes
 - a lot of speedups (I think we gain between 30 and 60% on the time used to create a page for threads and article)
 _ some code simplifications

article.php
include/encoding.inc.php [deleted file]
include/misc.inc.php [moved from include/wrapper.inc.php with 62% similarity]
include/spool.inc.php
locales/fr.inc.php
post.php
thread.php
xface.php

index 79d058e..58d9a60 100644 (file)
@@ -8,8 +8,7 @@
 ********************************************************************************/
 
 require_once("include/session.inc.php");
-require_once("include/encoding.inc.php");
-require_once("include/wrapper.inc.php");
+require_once("include/misc.inc.php");
 require_once("include/format.inc.php");
 require_once("include/config.inc.php");
 require_once("include/NetNNTP.inc.php");
diff --git a/include/encoding.inc.php b/include/encoding.inc.php
deleted file mode 100644 (file)
index 9d30000..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-/********************************************************************************
-* include/encoding.inc.php : decoding subroutines
-* --------------------------
-*
-* This file is part of the banana distribution
-* Copyright: See COPYING files that comes with this distribution
-********************************************************************************/
-
-/** Decodes quoted-printable and UTF8 headers
- * @param STRING $_value string to decode
- */
-
-function _headerdecode($charset, $c, $str) {
-    $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
-    $s = iconv($charset, 'iso-8859-15', $s);
-    return str_replace('_', ' ', $s);
-}
-function headerDecode($value) {
-    $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
-    return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
-}
-?>
similarity index 62%
rename from include/wrapper.inc.php
rename to include/misc.inc.php
index 7f657d9..9731d16 100644 (file)
@@ -7,7 +7,8 @@
 * Copyright: See COPYING files that comes with this distribution
 ********************************************************************************/
 
-function wrap($_text,$_prefix="",$_length=72) {
+function wrap($_text, $_prefix="", $_length=72)
+{
     $parts = preg_split("/\n-- ?\n/", $_text);
     if (count($parts)  >1) {
         $sign = "\n-- \n" . array_pop($parts);
@@ -23,4 +24,15 @@ function wrap($_text,$_prefix="",$_length=72) {
     return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
 }
 
+function _headerdecode($charset, $c, $str) {
+    $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
+    $s = iconv($charset, 'iso-8859-15', $s);
+    return str_replace('_', ' ', $s);
+}
+function headerDecode($value) {
+    $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
+    return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
+}
+
 ?>
index b0a73c6..2522f04 100644 (file)
@@ -80,6 +80,8 @@ class spool
     var $group;
     /**  array msgid => msgnum */
     var $ids;
+    /** thread starts */
+    var $roots;
 
     /** constructor
      * @param $_nntp RESOURCE NNTP socket filehandle
@@ -92,6 +94,8 @@ class spool
     {
         global $news;
 
+        $do_save    = false;
+
         $spool_path = dirname(dirname(__FILE__)).'/spool';
         $spoolfile  = "$spool_path/spool-$_group.dat";
 
@@ -111,8 +115,9 @@ class spool
             $keys   = array_values($this->ids);
             rsort($keys);
             // remove expired messages
-            for ($id=min(array_keys($this->overview)); $id<$first; $id++) { 
+            for ($id = min(array_keys($this->overview)); $id<$first; $id++) { 
                 $this->delid($id, false);
+                $do_save = true;
             }
             $start  = max(array_keys($this->overview))+1;
         } else {
@@ -123,6 +128,7 @@ class spool
         }
 
         if (($start<$last) && $groupinfo[0]) {
+            $do_save  = true;
 
             $dates    = array_map("strtotime",    $_nntp->xhdr("Date",    "$start-$last"));
             $subjects = array_map("headerdecode", $_nntp->xhdr("Subject", "$start-$last"));
@@ -163,10 +169,10 @@ class spool
                     
                 }
             }
-            uasort($this->overview, "spoolcompare");
-            file_put_contents($spoolfile, serialize($this));
         }
 
+        if ($do_save) { $this->save($spoolfile); }
+
         if ($_since) {
             $newpostsids = $_nntp->newnews($_since, $_group);
             if (sizeof($newpostsids)) {
@@ -186,26 +192,12 @@ class spool
             if (sizeof($newpostsids)>0) {
                 switch ($_display) {
                     case 1:
-                        foreach ($this->overview as $i=>$p) {
-                            if (isset($this->overview[$i]) &&
-                                    !isset($this->overview[$i]->parent) && 
-                                    ($this->overview[$i]->descunread==0))
-                            {
+                        foreach ($this->roots as $i) {
+                            if ($this->overview[$i]->descunread==0) {
                                 $this->killdesc($i);
                             }
                         }
                         break;
-
-                    case 2:
-                        $flipids = array_flip($this->ids);
-                        foreach ($this->overview as $i=>$p) {
-                            if ($p->isread) {
-                                unset($this->overview[$i]);
-                                unset($flipids[$i]);
-                            }
-                        }
-                        $this->ids = array_flip($flipids);
-                        break;
                 }
             }
         }
@@ -213,6 +205,24 @@ class spool
         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
      * @param $_id MSGNUM of post
      */
@@ -224,9 +234,11 @@ 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 ($msgids) {
+        if ($msgid) {
             unset($this->ids[$msgid]);
         }
     }
@@ -266,8 +278,7 @@ class spool
             }
             
             if ($write) {
-                $spool_path = dirname(dirname(__FILE__)).'/spool';
-                file_put_contents("$spool_path/spool-$_group.dat", serialize($this));
+                $this->save(dirname(dirname(__FILE__)).'/spool');
             }
         }
     }
@@ -283,9 +294,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) {
+    function disp_desc($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) {
         global $css;
-        $debug    = false;
         $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="+" />';
@@ -297,52 +307,40 @@ class spool
         $spfx_e   = '<img src="img/e.gif" height="21" width="12" alt="&nbsp;" />';
         $spfx_I   = '<img src="img/I.gif" height="21" width="12"alt="|" />';
 
-        if ($_index == "") {
-            $_index = $this->getndx($_id);
+        if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) {
+            return;
         }
 
-        if (!sizeof($this->overview[$_id]->children) && ($_index<=$_last) && ($_index>=$_first)) {
+        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, ($_index==$_ref), $this->overview[$_id]->isread)
+                        $this->group, $us, $this->overview[$_id]->isread)
                 ." </td>\n";
-            echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
-                .$_pfx_node.($_head?$spfx_f:
-                        ($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd))
+            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, ($_index==$_ref), $this->overview[$_id]->isread)
-                .($debug?" $_id $_index ".
-                        $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
-            echo "<td class=\"{$css['author']}\">"
-                .formatSpoolHeader("from", $this->overview[$_id]->from, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                ." </td>\n</tr>";
-            return true;
-        } 
-        $children = $this->overview[$_id]->children;
-        if (($_index<=$_last) && ($_index>=$_first)) {
-            echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n";
-            echo "<td class=\"{$css['date']}\">"
-                .formatSpoolHeader("date", $this->overview[$_id]->date, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
+                        $this->group, $us, $this->overview[$_id]->isread)
                 ." </td>\n";
-            echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
-                .$_pfx_node.$spfx_n."</div>"
-                .formatSpoolHeader("subject", $this->overview[$_id]->subject, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                .($debug?" $_id $_index ".
-                        $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
             echo "<td class=\"{$css['author']}\">"
                 .formatSpoolHeader("from", $this->overview[$_id]->from, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
+                        $this->group, $us, $this->overview[$_id]->isread)
                 ." </td>\n</tr>";
-        }
+            if ($hc) {
+                return true;
+            }
+        } 
+
         $index = $_index+1;
+
+        $children = $this->overview[$_id]->children;
         while ($child = array_shift($children)) {
-            if (($index+$this->overview[$child]->desc-1>=$_first)
-                    ||($index<$_last)){
+            if ($index > $_last) { return; }
+            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),
@@ -367,10 +365,11 @@ class spool
         global $css;
         $index = 1;
         if (sizeof($this->overview)) {
-            foreach ($this->overview as $id=>$msg) {
-                if (is_null($msg->parent)) {
-                    $this->disp_desc($id, $index, $_first, $_last, $_ref);
-                    $index += $msg->desc ;
+            foreach ($this->roots as $id) {
+                $this->disp_desc($id, $index, $_first, $_last, $_ref);
+                $index += $this->overview[$id]->desc ;
+                if ($index > $_last) {
+                    break;
                 }
             }
         } else {
@@ -388,27 +387,26 @@ class spool
      */
 
     function getndx($_id) {
-        $ndx = 1;
-        // on remonte l'arbre
+        $ndx    = 1;
+        $id_cur = $_id;
         while (true) {
-            $id_parent = $this->overview[$_id]->parent;
-            $id_curr   = $_id;
+            $id_parent = $this->overview[$id_cur]->parent;
             if (is_null($id_parent)) break;
-            $pos       = array_search($id_curr, $this->overview[$id_parent]->children);
+            $pos       = array_search($id_cur, $this->overview[$id_parent]->children);
         
-            for ($i = 0; $i < $_pos ; $i++) {
+            for ($i = 0; $i < $pos ; $i++) {
                 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
             }
             $ndx++; //noeud père
+
+            $id_cur = $id_parent;
         }
-        // on compte les threads précédents
-        foreach ($this->overview as $i=>$p) {
-            if ($i==$id_curr) {
+
+        foreach ($this->roots as $i) {
+            if ($i==$id_cur) {
                 break;
             }
-            if (is_null($p->parent)) {
-                $ndx += $this->overview[$i]->desc;
-            }
+            $ndx += $this->overview[$i]->desc;
         }
         return $ndx;
     }
index 2482d6d..3b71209 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 function locale_date($_text) {
-    return strftime("%A %d %B %Y, %H:%m (fuseau serveur)", strtotime($_text));
+    return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
 }
 
 function locale_header_date($_text) {
index 81450d8..ff1084c 100644 (file)
--- a/post.php
+++ b/post.php
@@ -8,7 +8,7 @@
 ********************************************************************************/
 
 require_once("include/session.inc.php");
-require_once("include/encoding.inc.php");
+require_once("include/misc.inc.php");
 require_once("include/format.inc.php");
 require_once("include/config.inc.php");
 require_once("include/NetNNTP.inc.php");
@@ -16,7 +16,6 @@ require_once("include/post.inc.php");
 require_once("include/spool.inc.php");
 require_once("include/password.inc.php");
 require_once("include/profile.inc.php");
-require_once("include/wrapper.inc.php");
 require_once("include/error.inc.php");
 
 $profile = getprofile();
index f37f60d..b649083 100644 (file)
@@ -8,7 +8,7 @@
 ********************************************************************************/
 
 require_once("include/session.inc.php");
-require_once("include/encoding.inc.php");
+require_once("include/misc.inc.php");
 require_once("include/format.inc.php");
 require_once("include/config.inc.php");
 require_once("include/NetNNTP.inc.php");
@@ -16,7 +16,6 @@ require_once("include/post.inc.php");
 require_once("include/spool.inc.php");
 require_once("include/password.inc.php");
 require_once("include/profile.inc.php");
-require_once("include/wrapper.inc.php");
 require_once("include/error.inc.php");
 
 $profile=getprofile();
index 4edd725..98078c3 100644 (file)
--- a/xface.php
+++ b/xface.php
@@ -1,8 +1,9 @@
 <?php
 
 $face = base64_decode($_REQUEST['face']);
-$face = ereg_replace("'", "'\\''", $face);
+$face = escapeshellarg($face);
 
 header("Content-Type: image/png");
-passthru("echo '$face'|uncompface -X |convert xbm:- png:-");
+passthru("echo $face|uncompface -X |convert xbm:- png:-");
+
 ?>