Work around broken DateTime serialization in PHP 5.2.x.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 13 Jul 2009 22:40:19 +0000 (16:40 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 13 Jul 2009 23:38:45 +0000 (17:38 -0600)
framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php
framework/Imap_Client/lib/Horde/Imap/Client/DateTime.php [new file with mode: 0644]
framework/Imap_Client/lib/Horde/Imap/Client/Socket.php
framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php
framework/Imap_Client/package.xml

index 542dbf8..9b4fdd5 100644 (file)
@@ -1212,7 +1212,7 @@ class Horde_Imap_Client_Cclient extends Horde_Imap_Client_Base
                 }
 
                 foreach ($options['ids'] as $id) {
-                    $ret[$id]['date'] = new DateTime($hdrinfo[$id]->MailDate);
+                    $ret[$id]['date'] = new Horde_Imap_Client_DateTime($hdrinfo[$id]->MailDate);
                 }
                 break;
 
diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/DateTime.php b/framework/Imap_Client/lib/Horde/Imap/Client/DateTime.php
new file mode 100644 (file)
index 0000000..8913987
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Horde_Imap_Client_DateTime:: is a wrapper around PHP's native DateTime
+ * class that works around the PHP 5.2.x issue that does not allow DateTime
+ * objects to be serialized.  See http://bugs.php.net/bug.php?id=41334
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Horde_Imap_Client
+ */
+class Horde_Imap_Client_DateTime
+{
+    /**
+     * The datetime string.
+     *
+     * @var string
+     */
+    private $_string;
+
+    /**
+     * The timezone string.
+     *
+     * @var string
+     */
+    private $_tz = null;
+
+    /**
+     * The DateTime object to use for function calls.
+     *
+     * @var DateTime
+     */
+    private $_datetime = null;
+
+    /**
+     * Constructor.
+     *
+     * @param string $time      String in a format accepted by strtotime().
+     * @param DateTimeZone $tz  Time zone of the time.
+     */
+    public function __construct($time, $tz = null)
+    {
+        $this->_string = $time;
+        if (!is_null($tz)) {
+            $this->_tz = $tz->getName();
+        }
+    }
+
+    /**
+     * Called on serialize().
+     */
+    public function __sleep()
+    {
+        return array('_string', '_tz');
+    }
+
+    /**
+     * Called on a function call.
+     */
+    public function __call($name, $arguments)
+    {
+        if (is_null($this->_datetime)) {
+            $this->_datetime = is_null($this->_tz)
+                ? new DateTime($this->_string)
+                : new DateTime($this->_string, $this->_tz);
+        }
+
+        call_user_func_array(array($this->_datetime, $name), $arguments);
+    }
+
+}
index 24b2ce1..62ec5b8 100644 (file)
@@ -2328,7 +2328,7 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
                 break;
 
             case 'INTERNALDATE':
-                $tmp['date'] = new DateTime($data[++$i]);
+                $tmp['date'] = new Horde_Imap_Client_DateTime($data[++$i]);
                 break;
 
             case 'RFC822.SIZE':
index b241b74..1c16f7e 100644 (file)
@@ -932,7 +932,7 @@ class Horde_Imap_Client_Socket_Pop3 extends Horde_Imap_Client_Base
             case Horde_Imap_Client::FETCH_DATE:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('hdrob', $id);
-                    $ret[$id]['date'] = new DateTime($tmp->getValue('date'));
+                    $ret[$id]['date'] = new Horde_Imap_Client_DateTime($tmp->getValue('date'));
                 }
                 break;
 
index 21fa966..9ecf673 100644 (file)
@@ -53,6 +53,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Base.php" role="php" />
        <file name="Cache.php" role="php" />
        <file name="Cclient.php" role="php" />
+       <file name="DateTime.php" role="php" />
        <file name="Exception.php" role="php" />
        <file name="Socket.php" role="php" />
        <file name="Sort.php" role="php" />
@@ -113,6 +114,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Imap/Client/Cache.php" as="Horde/Imap/Client/Cache.php" />
    <install name="lib/Horde/Imap/Client/Cclient/Pop3.php" as="Horde/Imap/Client/Cclient/Pop3.php" />
    <install name="lib/Horde/Imap/Client/Cclient.php" as="Horde/Imap/Client/Cclient.php" />
+   <install name="lib/Horde/Imap/Client/DateTime.php" as="Horde/Imap/Client/DateTime.php" />
    <install name="lib/Horde/Imap/Client/Exception.php" as="Horde/Imap/Client/Exception.php" />
    <install name="lib/Horde/Imap/Client/Search/Query.php" as="Horde/Imap/Client/Search/Query.php" />
    <install name="lib/Horde/Imap/Client/Socket.php" as="Horde/Imap/Client/Socket.php" />