Overview

Packages

  • SessionHandler

Classes

  • Horde_SessionHandler
  • Horde_SessionHandler_Exception
  • Horde_SessionHandler_Storage
  • Horde_SessionHandler_Storage_Builtin
  • Horde_SessionHandler_Storage_External
  • Horde_SessionHandler_Storage_File
  • Horde_SessionHandler_Storage_Memcache
  • Horde_SessionHandler_Storage_Sql
  • Horde_SessionHandler_Storage_Stack
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * SessionHandler implementation for storage in text files.
  4:  *
  5:  * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Michael Slusarz <slusarz@horde.org>
 11:  * @category Horde
 12:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 13:  * @package  SessionHandler
 14:  */
 15: class Horde_SessionHandler_Storage_File extends Horde_SessionHandler_Storage
 16: {
 17:     /* File prefix. */
 18:     const PREFIX = 'horde_sh_';
 19: 
 20:     /**
 21:      * File stream.
 22:      *
 23:      * @var resource
 24:      */
 25:     protected $_fp;
 26: 
 27:     /**
 28:      * Constructor.
 29:      *
 30:      * @param array $params  Parameters:
 31:      * <pre>
 32:      * path - (string) [REQUIRED] The path to save the files.
 33:      * </pre>
 34:      *
 35:      * @throws InvalidArgumentException
 36:      */
 37:     public function __construct(array $params = array())
 38:     {
 39:         if (!isset($params['path'])) {
 40:             throw new InvalidArgumentException('Missing path parameter.');
 41:         }
 42:         $params['path'] = rtrim($params['path'], '/');
 43: 
 44:         parent::__construct($params);
 45:     }
 46: 
 47:     /**
 48:      */
 49:     public function open($save_path = null, $session_name = null)
 50:     {
 51:     }
 52: 
 53:     /**
 54:      * Open the file stream connection.
 55:      *
 56:      * @param string $id  The session ID.
 57:      */
 58:     protected function _open($id)
 59:     {
 60:         if (!empty($this->_fp)) {
 61:             return;
 62:         }
 63: 
 64:         $filename = $this->_params['path'] . '/' . self::PREFIX . $id;
 65: 
 66:         $this->_fp = fopen($filename, is_readable($filename) ? 'r+' : 'w+');
 67:         if ($this->_fp) {
 68:             flock($this->_fp, LOCK_EX);
 69:         }
 70:     }
 71: 
 72:     /**
 73:      */
 74:     public function close()
 75:     {
 76:         if (!empty($this->_fp)) {
 77:             flock($this->_fp, LOCK_UN);
 78:             fclose($this->_fp);
 79:             unset($this->_fp);
 80:         }
 81: 
 82:         return true;
 83:     }
 84: 
 85:     /**
 86:      */
 87:     public function read($id)
 88:     {
 89:         $this->_open($id);
 90: 
 91:         if (!$this->_fp) {
 92:             return '';
 93:         }
 94: 
 95:         rewind($this->_fp);
 96:         return strval(stream_get_contents($this->_fp));
 97:     }
 98: 
 99:     /**
100:      */
101:     public function write($id, $session_data)
102:     {
103:         $this->_open($id);
104: 
105:         if (!$this->_fp) {
106:             return false;
107:         }
108: 
109:         fseek($this->_fp, 0);
110:         ftruncate($this->_fp, 0);
111:         fwrite($this->_fp, $session_data);
112: 
113:         return true;
114:     }
115: 
116:     /**
117:      */
118:     public function destroy($id)
119:     {
120:         $this->close();
121: 
122:         $filename = $this->_params['path'] . '/' . self::PREFIX . $id;
123: 
124:         return @unlink($filename);
125:     }
126: 
127:     /**
128:      */
129:     public function gc($maxlifetime = 300)
130:     {
131:         try {
132:             $di = new DirectoryIterator($this->_params['path']);
133:         } catch (UnexpectedValueException $e) {
134:             return false;
135:         }
136: 
137:         $expire_time = time() - $maxlifetime;
138: 
139:         foreach ($di as $val) {
140:             if ($val->isFile() &&
141:                 (strpos($val->getFilename(), self::PREFIX) === 0) &&
142:                 ($val->getMTime() < $expire_time)) {
143:                 @unlink($val->getPathname());
144:             }
145:         }
146: 
147:         return true;
148:     }
149: 
150:     /**
151:      */
152:     public function getSessionIDs()
153:     {
154:         $ids = array();
155: 
156:         try {
157:             $di = new DirectoryIterator($this->_params['path']);
158:             foreach ($di as $val) {
159:                 if ($val->isFile() &&
160:                     (strpos($val->getFilename(), self::PREFIX) === 0)) {
161:                     $ids[] = substr($val->getFilename(), strlen(self::PREFIX));
162:                 }
163:             }
164:         } catch (UnexpectedValueException $e) {}
165: 
166:         return $ids;
167:     }
168: 
169: }
170: 
API documentation generated by ApiGen