Fixes deprecated features in PHP 5.3.x.
[banana.git] / banana / mbox.inc.php
index 01c329e..f48c877 100644 (file)
@@ -18,25 +18,25 @@ class BananaMBox implements BananaProtocoleInterface
 
     private $_lasterrno = 0;
     private $_lasterror = null;
-    
+
     public function __construct()
     {
         $this->debug = Banana::$debug_mbox;
     }
-    
+
     public function isValid()
     {
         return true;
         //!Banana::$group || $this->file;
     }
-    
+
     /** Indicate last error n°
      */
     public function lastErrNo()
     {
         return $this->_lasterrno;;
     }
-    
+
     /** Indicate last error text
      */
     public function lastError()
@@ -66,10 +66,10 @@ class BananaMBox implements BananaProtocoleInterface
     {
         $message = null;
         if (!is_numeric($id)) {
-            if (!Banana::$spool) { 
+            if (!Banana::$spool) {
                 return $message;
             }
-            $id = Banana::$spool->ids[$id];
+            $id = Banana::$spool->ids[$id]->id;
         }
         $options = array ('-m ' . $id);
         $this->getMBoxPosition($options, $id);
@@ -85,29 +85,31 @@ class BananaMBox implements BananaProtocoleInterface
         $messages =& $this->getRawMessage($id);
         if ($messages) {
             $messages = new BananaMessage($messages);
+        } else {
+            $messages = null;
         }
-        return $messages;    
+        return $messages;
     }
 
     /** Return the sources of the given message
      */
     public function getMessageSource($id)
-    { 
+    {
         $message =& $this->getRawMessage($id);
         if ($message) {
-            $message = implode("\n", $message); 
+            $message = implode("\n", $message);
         }
         return $message;
-    }   
+    }
 
     /** Compute the number of messages of the box
      */
     private function getCount()
     {
         $options = array();
-        if (@filesize($this->getFileName()) == Banana::$spool->storage['size']) { 
-            return max(Banana::$spool->ids); 
-        } 
+        if (@filesize($this->getFileName()) == @Banana::$spool->storage['size']) {
+            return max(array_keys(Banana::$spool->overview)) + 1;
+        }
         $this->getMBoxPosition($options);
         $val =& $this->callHelper('-c', $options);
         if (!$val) {
@@ -139,31 +141,33 @@ class BananaMBox implements BananaProtocoleInterface
             return $headers;
         }
         $headers = array();
-        while ($lines) {
-            $id = array_shift($lines);
-            if ($id === '') {
-                continue;
-            }
-            $offset = array_shift($lines);
-            if ($offset === '') {
-                continue;
-            }
-            $id     = intval($id);
-            $headers[$id] = array('beginning' => intval($offset));
-            while (true) {
-                $hname = array_shift($lines);
-                if ($hname === '') {
-                    break;
-                }
-                $hval  = array_shift($lines);
-                if ($hname == 'date') {
-                    $headers[$id][$hname] = @strtotime($hval);
-                } else {
-                    $headers[$id][$hname] = $hval;
+        $in_message = false;
+        $get_pos    = true;
+        $hname      = null;
+        foreach ($lines as $key=>&$line) {
+            if (!$in_message) {
+                if (!empty($line)) {
+                    $id = intval($line);
+                    $in_message = true;
+                    $get_pos    = true;
                 }
+            } elseif ($get_pos) {
+                $headers[$id] = array('beginning' => intval($line));
+                $get_pos = false;
+            } elseif (empty($line) && empty($hname)) {
+                $in_message = false;
+            } elseif (empty($hname)) {
+                $hname = $line;
+            } elseif ($hname == 'date') {
+                $headers[$id][$hname] = @strtotime($line);
+                $hname = null;
+            } else {
+                BananaMimePart::decodeHeader($line, $hname);
+                $headers[$id][$hname] = $line;
+                $hname = null;
             }
+            unset($lines[$key]);
         }
-        array_walk_recursive($headers, array('BananaMimePart', 'decodeHeader'));
         return $headers;
     }
 
@@ -189,7 +193,7 @@ class BananaMBox implements BananaProtocoleInterface
             return array();
         }
         if (is_null($this->new_messages)) {
-            $this->getCount(); 
+            $this->getCount();
         }
         return range($this->count - $this->new_messages, $this->count - 1);
     }
@@ -222,7 +226,7 @@ class BananaMBox implements BananaProtocoleInterface
     /** Send a message
      * @return true if it was successfull
      */
-    public function send(BananaMessage &$message)
+    public function send(BananaMessage $message)
     {
         $headers = $message->getHeaders();
         $to      = $headers['To'];
@@ -233,7 +237,7 @@ class BananaMBox implements BananaProtocoleInterface
         foreach ($headers as $key=>$value) {
             if (!empty($value)) {
                 $hdrs .= "$key: $value\r\n";
-            }    
+            }
         }
         $body = $message->get(false);
         return mail($to, $subject, $body, $hdrs);
@@ -242,7 +246,7 @@ class BananaMBox implements BananaProtocoleInterface
     /** Cancel a message
      * @return true if it was successfull
      */
-    public function cancel(BananaMessage &$message)
+    public function cancel(BananaMessage $message)
     {
         return false;
     }
@@ -293,21 +297,21 @@ class BananaMBox implements BananaProtocoleInterface
 #######
 # MBox parser
 #######
-    
+
     /** Add the '-p' optioin for callHelper
      */
     private function getMBoxPosition(array &$options, $id = null)
     {
-        if (Banana::$spool->overview) {
+        if (Banana::$spool && Banana::$spool->overview) {
             if (!is_null($id) && isset(Banana::$spool->overview[$id])) {
                 $key = $id;
             } else {
-                $key = max(Banana::$spool->ids);
+                $key = max(array_keys(Banana::$spool->overview));
                 if (!is_null($id) && $key >= $id) {
                     return;
                 }
             }
-            if (isset(Banana::$spool->overview[$key]->storage['offset'])) { 
+            if (isset(Banana::$spool->overview[$key]->storage['offset'])) {
                 $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
             }
         }