0b604a0
From ae1adf9f1549f6bb826b541274949cfd0e77457e Mon Sep 17 00:00:00 2001
0b604a0
From: Michael M Slusarz <slusarz@horde.org>
0b604a0
Date: Tue, 13 Jan 2015 03:00:10 -0700
0b604a0
Subject: [PATCH] [mms] Workaround broken in-memory stream filter handling.
0b604a0
0b604a0
Needed to work on PHP 5.5.21+ and 5.6.5+
0b604a0
---
0b604a0
 .../Horde/Imap/Client/Data/Format/Filter/Quote.php | 25 ++++++++++++++++++++--
0b604a0
 framework/Imap_Client/package.xml                  |  4 ++--
0b604a0
 2 files changed, 25 insertions(+), 4 deletions(-)
0b604a0
0b604a0
diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Data/Format/Filter/Quote.php b/framework/Imap_Client/lib/Horde/Imap/Client/Data/Format/Filter/Quote.php
0b604a0
index a3f1788..3964f69 100644
0b604a0
--- a/framework/Imap_Client/lib/Horde/Imap/Client/Data/Format/Filter/Quote.php
0b604a0
+++ b/framework/Imap_Client/lib/Horde/Imap/Client/Data/Format/Filter/Quote.php
0b604a0
@@ -23,11 +23,28 @@
0b604a0
 class Horde_Imap_Client_Data_Format_Filter_Quote extends php_user_filter
0b604a0
 {
0b604a0
     /**
0b604a0
+     * Has the initial quote been prepended?
0b604a0
+     *
0b604a0
+     * @var boolean
0b604a0
+     */
0b604a0
+    protected $_prepend;
0b604a0
+
0b604a0
+    /**
0b604a0
+     */
0b604a0
+    public function onCreate()
0b604a0
+    {
0b604a0
+        $this->_prepend = false;
0b604a0
+    }
0b604a0
+
0b604a0
+    /**
0b604a0
      * @see stream_filter_register()
0b604a0
      */
0b604a0
     public function filter($in, $out, &$consumed, $closing)
0b604a0
     {
0b604a0
-        stream_bucket_append($out, stream_bucket_new($this->stream, '"'));
0b604a0
+        if (!$this->_prepend) {
0b604a0
+            stream_bucket_append($out, stream_bucket_new($this->stream, '"'));
0b604a0
+            $this->_prepend = true;
0b604a0
+        }
0b604a0
 
0b604a0
         while ($bucket = stream_bucket_make_writeable($in)) {
0b604a0
             $consumed += $bucket->datalen;
0b604a0
@@ -35,7 +52,11 @@ public function filter($in, $out, &$consumed, $closing)
0b604a0
             stream_bucket_append($out, $bucket);
0b604a0
         }
0b604a0
 
0b604a0
-        stream_bucket_append($out, stream_bucket_new($this->stream, '"'));
0b604a0
+        /* feof() call needed due to:
0b604a0
+         * http://news.php.net/php.internals/80363 */
0b604a0
+        if ($closing || feof($this->stream)) {
0b604a0
+            stream_bucket_append($out, stream_bucket_new($this->stream, '"'));
0b604a0
+        }
0b604a0
 
0b604a0
         return PSFS_PASS_ON;
0b604a0
     }