1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_SessionHandler_Storage_File extends Horde_SessionHandler_Storage
16: {
17:
18: const PREFIX = 'horde_sh_';
19:
20: 21: 22: 23: 24:
25: protected $_fp;
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 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: 55: 56: 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: