Add Horde_SessionHandler_Storage_File
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 12 Nov 2010 20:49:50 +0000 (13:49 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 12 Nov 2010 20:57:30 +0000 (13:57 -0700)
framework/SessionHandler/lib/Horde/SessionHandler/Storage.php
framework/SessionHandler/lib/Horde/SessionHandler/Storage/File.php [new file with mode: 0644]
framework/SessionHandler/package.xml

index 78fe885..05290b8 100644 (file)
@@ -42,7 +42,7 @@ abstract class Horde_SessionHandler_Storage
      */
     public function __construct(array $params = array())
     {
-        $params = array_merge($this->_params, $params);
+        $this->_params = array_merge($this->_params, $params);
     }
 
     /**
diff --git a/framework/SessionHandler/lib/Horde/SessionHandler/Storage/File.php b/framework/SessionHandler/lib/Horde/SessionHandler/Storage/File.php
new file mode 100644 (file)
index 0000000..6cc7f5b
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+/**
+ * SessionHandler implementation for storage in text files.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  SessionHandler
+ */
+class Horde_SessionHandler_Storage_File extends Horde_SessionHandler_Storage
+{
+    const PREFIX = 'horde_sh_';
+
+    /**
+     * Constructor.
+     *
+     * @param array $params  Parameters:
+     * <pre>
+     * path - (string) [REQUIRED] The path to save the files.
+     * </pre>
+     *
+     * @throws InvalidArgumentException
+     */
+    public function __construct(array $params = array())
+    {
+        if (!isset($params['path'])) {
+            throw new InvalidArgumentException('Missing path parameter.');
+        }
+        $params['path'] = rtrim($params['path'], '/');
+
+        parent::__construct($params);
+    }
+
+    /**
+     */
+    public function open($save_path = null, $session_name = null)
+    {
+    }
+
+    /**
+     */
+    public function close()
+    {
+    }
+
+    /**
+     */
+    public function read($id)
+    {
+        $filename = $this->_params['path'] . '/' . self::PREFIX . $id;
+
+        return file_exists($filename)
+            ? file_get_contents($filename)
+            : '';
+    }
+
+    /**
+     */
+    public function write($id, $session_data)
+    {
+        $filename = $this->_params['path'] . '/' . self::PREFIX . $id;
+
+        return @file_put_contents($filename, $session_data);
+    }
+
+    /**
+     */
+    public function destroy($id)
+    {
+        $filename = $this->_params['path'] . '/' . self::PREFIX . $id;
+
+        return @unlink($filename);
+    }
+
+    /**
+     */
+    public function gc($maxlifetime = 300)
+    {
+        try {
+            $di = new DirectoryIterator($this->_params['path']);
+        } catch (UnexpectedValueException $e) {
+            return false;
+        }
+
+        $expire_time = time() - $maxlifetime;
+
+        foreach ($di as $val) {
+            if ($val->isFile() &&
+                (strpos($val->getFilename(), self::PREFIX) === 0) &&
+                ($val->getMTime() < $expire_time)) {
+                @unlink($val->getPathname());
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     */
+    public function getSessionIDs()
+    {
+        $ids = array();
+
+        try {
+            $di = new DirectoryIterator($this->_params['path']);
+            foreach ($di as $val) {
+                if ($val->isFile() &&
+                    (strpos($val->getFilename(), self::PREFIX) === 0)) {
+                    $ids[] = substr($val->getFilename(), strlen(self::PREFIX));
+                }
+            }
+        } catch (UnexpectedValueException $e) {}
+
+        return $ids;
+    }
+
+}
index e86b7ff..8cf41b8 100644 (file)
@@ -34,7 +34,8 @@
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Abstracted storage-specific code into &apos;Storage&apos; drivers.
+ <notes>* Added &apos;File&apos; driver.
+* Abstracted storage-specific code into &apos;Storage&apos; drivers.
 * Removed LDAP driver
 * Abstracted memcache persistent-backend code into &apos;Stack&apos; driver.
 * Renamed &apos;none&apos; driver to &apos;Builtin&apos;.
@@ -53,6 +54,7 @@
       <dir name="Storage">
        <file name="Builtin.php" role="php" />
        <file name="External.php" role="php" />
+       <file name="File.php" role="php" />
        <file name="Memcache.php" role="php" />
        <file name="Sql.php" role="php" />
        <file name="Stack.php" role="php" />
    <install as="Horde/SessionHandler.php" name="lib/Horde/SessionHandler.php" />
    <install as="Horde/SessionHandler/Storage/Builtin.php" name="lib/Horde/SessionHandler/Storage/Builtin.php" />
    <install as="Horde/SessionHandler/Storage/External.php" name="lib/Horde/SessionHandler/Storage/External.php" />
+   <install as="Horde/SessionHandler/Storage/File.php" name="lib/Horde/SessionHandler/Storage/File.php" />
    <install as="Horde/SessionHandler/Storage/Memcache.php" name="lib/Horde/SessionHandler/Storage/Memcache.php" />
    <install as="Horde/SessionHandler/Storage/Sql.php" name="lib/Horde/SessionHandler/Storage/Sql.php" />
    <install as="Horde/SessionHandler/Storage/Stack.php" name="lib/Horde/SessionHandler/Storage/Stack.php" />