|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Session |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Session\Handler; |
|
16
|
|
|
|
|
17
|
|
|
use SessionHandlerInterface; |
|
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
|
19
|
|
|
use Phossa2\Storage\Interfaces\StorageInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* StorageHandler |
|
23
|
|
|
* |
|
24
|
|
|
* Use of 'phossa2/storage' as session handler |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Session |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @version 2.1.0 |
|
29
|
|
|
* @since 2.1.0 added |
|
30
|
|
|
*/ |
|
31
|
|
|
class StorageHandler extends ObjectAbstract implements SessionHandlerInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var StorageInterface |
|
35
|
|
|
* @access protected |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $storage; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Path prefix in the storage |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
* @access protected |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $path_prefix; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param StorageInterface $storage |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @access public |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
StorageInterface $storage, |
|
54
|
|
|
$path = '/tmp/session' |
|
55
|
|
|
) { |
|
56
|
|
|
$this->storage = $storage; |
|
57
|
|
|
$this->path_prefix = rtrim($path, '/'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @see SessionHandlerInterface::close() |
|
65
|
|
|
*/ |
|
66
|
|
|
public function close()/*# : bool */ |
|
67
|
|
|
{ |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $session_id |
|
75
|
|
|
* @return bool |
|
76
|
|
|
* @see SessionHandlerInterface::destroy() |
|
77
|
|
|
*/ |
|
78
|
|
|
public function destroy(/*# string */ $session_id)/*# : bool */ |
|
79
|
|
|
{ |
|
80
|
|
|
$file = $this->getSessionFile($session_id); |
|
81
|
|
|
return $this->storage->del($file); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritDoc} |
|
86
|
|
|
* |
|
87
|
|
|
* @param int $maxlifetime |
|
88
|
|
|
* @return bool |
|
89
|
|
|
* @see SessionHandlerInterface::gc() |
|
90
|
|
|
*/ |
|
91
|
|
|
public function gc(/*# int */ $maxlifetime)/*# : bool */ |
|
92
|
|
|
{ |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritDoc} |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $save_path |
|
100
|
|
|
* @param string $session_name |
|
101
|
|
|
* @return bool |
|
102
|
|
|
* @see SessionHandlerInterface::open() |
|
103
|
|
|
*/ |
|
104
|
|
|
public function open( |
|
105
|
|
|
/*# string */ $save_path, |
|
106
|
|
|
/*# string */ $session_name |
|
107
|
|
|
)/*# : bool */ { |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritDoc} |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $session_id |
|
115
|
|
|
* @return string |
|
116
|
|
|
* @see SessionHandlerInterface::read() |
|
117
|
|
|
*/ |
|
118
|
|
|
public function read(/*# string */ $session_id)/*# : string */ |
|
119
|
|
|
{ |
|
120
|
|
|
$file = $this->getSessionFile($session_id); |
|
121
|
|
|
if ($this->storage->has($file)) { |
|
122
|
|
|
return $this->storage->get($file); |
|
123
|
|
|
} |
|
124
|
|
|
// empty array |
|
125
|
|
|
return 'a:0:{}'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $session_id |
|
132
|
|
|
* @param string $session_data |
|
133
|
|
|
* @return bool |
|
134
|
|
|
* @see SessionHandlerInterface::write() |
|
135
|
|
|
*/ |
|
136
|
|
|
public function write( |
|
137
|
|
|
/*# string */ $session_id, |
|
138
|
|
|
/*# string */ $session_data |
|
139
|
|
|
)/*# : bool */ { |
|
140
|
|
|
$file = $this->getSessionFile($session_id); |
|
141
|
|
|
return $this->storage->put($file, $session_data); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Return the path in the $this->storage of this session id |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $session_id |
|
148
|
|
|
* @return string |
|
149
|
|
|
* @access protected |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getSessionFile(/*# string */ $session_id)/*# : string */ |
|
152
|
|
|
{ |
|
153
|
|
|
// path |
|
154
|
|
|
$res = [$this->path_prefix]; |
|
155
|
|
|
|
|
156
|
|
|
// scatter evenly |
|
157
|
|
|
$md5 = md5($session_id); |
|
158
|
|
|
$res[] = $md5[0]; |
|
159
|
|
|
$res[] = $md5[1]; |
|
160
|
|
|
|
|
161
|
|
|
// append session id |
|
162
|
|
|
$res[] = $session_id; |
|
163
|
|
|
|
|
164
|
|
|
return join('/', $res); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|