Fix references when replying to a post with artid = 0
[banana.git] / banana / nntpcore.inc.php
index 2d5434b..5d8bc63 100644 (file)
@@ -29,6 +29,7 @@ class BananaNNTPCore
 
     /** debug mode */
     private $debug = false;
+    private $bt    = array();
 
     /** constructor
      * @param $host STRING NNTP host
@@ -81,6 +82,15 @@ class BananaNNTPCore
         return $this->lasterrortext;
     }
 
+    public function backtrace()
+    {
+        if ($this->debug) {
+            return $this->bt;
+        } else {
+            return null;
+        }
+    }
+
 # Socket functions
 
     /** get a line from server
@@ -88,29 +98,20 @@ class BananaNNTPCore
      */
     private function getLine()
     {
-        return rtrim(fgets($this->ns, 1200));
+        return rtrim(@fgets($this->ns, 1200), "\r\n");
     }
 
     /** fetch data (and on delimitor)
      * @param STRING $delim string indicating and of transmission
      */
-    private function fetchResult($callback = null)
+    private function &fetchResult()
     {
         $array = Array();
         while (($result = $this->getLine()) != '.') {
-            if (!is_null($callback)) {
-                list($key, $result) = call_user_func($callback, $result);
-                if (is_null($result)) {
-                    continue;
-                }
-                if (is_null($key)) {
-                    $array[] = $result;
-                } else {
-                    $array[$key] = $result;
-                }
-            } else {
-                $array[] = $result;
-            }
+            $array[] = $result;
+        }
+        if ($this->debug && $this->bt) {
+            $this->bt[count($this->bt) - 1]['response'] = count($array);
         }
         return $array;
     }
@@ -126,9 +127,9 @@ class BananaNNTPCore
         }
         if ($this->debug) {
             $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
-            echo $db_line;
+            $this->bt[] = array('action' => $db_line, 'time' => microtime(true));
         }
-        return fputs($this->ns, $line, strlen($line));
+        return @fputs($this->ns, $line, strlen($line));
     }
 
     /** put a message (multiline)
@@ -163,11 +164,15 @@ class BananaNNTPCore
     private function checkState($strict = true)
     {
         $result = $this->getLine();
-        if ($this->debug) {
-            echo "$result\n";
-        }
         $this->lastresultcode = substr($result, 0, 3);
         $this->lastresulttext = substr($result, 4);
+        if ($this->debug && $this->bt) {
+            $trace =& $this->bt[count($this->bt) - 1];
+            $trace['time']     = microtime(true) - $trace['time'];
+            $trace['code']     = $this->lastresultcode;
+            $trace['message']  = $this->lastresulttext;
+            $trace['response'] = 0;
+        }
         $c = $this->lastresultcode{0};
         if ($c == '2' || (($c == '1' || $c == '3') && !$strict)) {
             return true;
@@ -335,7 +340,7 @@ class BananaNNTPCore
         $groups = array();
         foreach ($list as $result) {
             list($group, $last, $first, $p) = explode(' ', $result, 4);
-            if (!is_null(Banana::$boxpattern) || preg_match('@' . Banana::$boxpattern . '@i', $group)) {
+            if (!Banana::$boxpattern || preg_match('@' . Banana::$boxpattern . '@i', $group)) {
                 $groups[$group] = array(intval($last), intval($first), $p);
             }
         }
@@ -454,9 +459,9 @@ class BananaNNTPCore
         if (!$this->execLine("XGTITLE $pattern ")) {
             return false;
         }
-        $array  = $this->fetchResult();
+        $array  =& $this->fetchResult();
         $groups = array();
-        foreach ($array as $result) {
+        foreach ($array as &$result) {
             list($group, $desc) = split("[ \t]", $result, 2);
             $groups[$group] = $desc;
         }
@@ -478,7 +483,7 @@ class BananaNNTPCore
         if (!$this->execLine("XHDR $hdr $range ")) {
             return false;
         }
-        $array   = $this->fetchResult();
+        $array   =& $this->fetchResult();
         $headers = array();
         foreach ($array as &$result) {
             @list($head, $value) = explode(' ', $result, 2);
@@ -498,7 +503,7 @@ class BananaNNTPCore
         if (!$this->execLine("XPAT $hdr $range $pat")) {
             return false;
         }
-        $array   = $this->fetchResult();
+        $array   =& $this->fetchResult();
         $headers = array();
         foreach ($array as &$result) {
             list($head, $value) = explode(' ', $result, 2);