* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/
-class IMP_Imap_Tree implements ArrayAccess, Iterator
+class IMP_Imap_Tree implements ArrayAccess, Iterator, Serializable
{
+ /* Serialized version. */
+ const VERSION = 1;
+
/* Constants for mailboxElt attributes. */
const ELT_NOSELECT = 1;
const ELT_NAMESPACE = 2;
}
/**
- * Do cleanup prior to serialization and provide a list of variables
- * to serialize.
- */
- public function __sleep()
- {
- return array(
- '_tree', '_showunsub', '_parent', '_delimiter', '_namespaces'
- );
- }
-
- /**
* Initalize the tree.
*/
public function init()
);
}
+ /* Serializable methods. */
+
+ /**
+ * Serialization.
+ *
+ * @return string Serialized data.
+ */
+ public function serialize()
+ {
+ return serialize(array(
+ // Serialized data ID.
+ self::VERSION,
+ $this->_delimiter,
+ $this->_namespaces,
+ $this->_parent,
+ $this->_showunsub,
+ $this->_tree
+ ));
+ }
+
+ /**
+ * Unserialization.
+ *
+ * @param string $data Serialized data.
+ *
+ * @throws Exception
+ */
+ public function unserialize($data)
+ {
+ $data = @unserialize($data);
+ if (!is_array($data) ||
+ !isset($data[0]) ||
+ ($data[0] != self::VERSION)) {
+ throw new Exception('Cache version change');
+ }
+
+ $this->_delimiter = $data[1];
+ $this->_namespaces = $data[2];
+ $this->_parent = $data[3];
+ $this->_showunsub = $data[4];
+ $this->_tree = $data[5];
+ }
+
}
{
$this->_injector = $injector;
+ $instance = null;
+
if (empty($_SESSION['imp']['cache']['tree'])) {
$_SESSION['imp']['cache']['tree'] = strval(new Horde_Support_Randomid());
- $instance = null;
} else {
/* Since IMAP tree generation is so expensive/time-consuming,
* fallback to storing in the session even if no permanent cache
* backend is setup. */
$cache = $injector->getInstance('Horde_Cache_Factory')->getCache(array('session' => true));
- $instance = @unserialize($cache->get($_SESSION['imp']['cache']['tree'], 86400));
+ try {
+ $instance = @unserialize($cache->get($_SESSION['imp']['cache']['tree'], 86400));
+ } catch (Exception $e) {
+ Horde::logMessage('Could not unserialize stored IMP_Imap_Tree object.', 'DEBUG');
+ }
}
- if (empty($instance)) {
+ if (is_null($instance)) {
$instance = new IMP_Imap_Tree();
}