|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Appserver\ServletEngine\Session\AbstractSessionHandler |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/appserver |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Appserver\ServletEngine\Session; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Psr\Servlet\ServletSessionInterface; |
|
24
|
|
|
use AppserverIo\Appserver\ServletEngine\Http\Session; |
|
25
|
|
|
use AppserverIo\Appserver\ServletEngine\SessionSettingsInterface; |
|
26
|
|
|
use AppserverIo\Appserver\ServletEngine\SessionMarshallerInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* A abstract session handler implementation. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Tim Wagner <[email protected]> |
|
32
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
34
|
|
|
* @link https://github.com/appserver-io/appserver |
|
35
|
|
|
* @link http://www.appserver.io |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract class AbstractSessionHandler implements SessionHandlerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The default session marshaller type. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
const DEFAULT_SESSION_MARSHALLER_TYPE = 'AppserverIo\Appserver\ServletEngine\StandardSessionMarshaller'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The settings for the session handling. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \AppserverIo\Appserver\ServletEngine\SessionSettingsInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $sessionSettings; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The session marshaller instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @var \AppserverIo\Appserver\ServletEngine\SessionMarshallerInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $sessionMarshaller; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Initializes the session handler with the configured params. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $sessionMarshallerType The session marshaller type to use |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct($sessionMarshallerType = FilesystemSessionHandler::DEFAULT_SESSION_MARSHALLER_TYPE) |
|
67
|
|
|
{ |
|
68
|
|
|
// create and inject an instance of the session marshaller to use |
|
69
|
|
|
$this->injectSessionMarshaller(new $sessionMarshallerType()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Injects the session settings. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \AppserverIo\Appserver\ServletEngine\SessionSettingsInterface $sessionSettings Settings for the session handling |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function injectSessionSettings(SessionSettingsInterface $sessionSettings) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->sessionSettings = $sessionSettings; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the session settings. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \AppserverIo\Appserver\ServletEngine\SessionSettingsInterface The session settings |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getSessionSettings() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->sessionSettings; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Injects the session marshaller. |
|
96
|
|
|
* |
|
97
|
|
|
* @param \AppserverIo\Appserver\ServletEngine\SessionMarshallerInterface $sessionMarshaller The session marshaller instance |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function injectSessionMarshaller(SessionMarshallerInterface $sessionMarshaller) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->sessionMarshaller = $sessionMarshaller; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the session marshaller. |
|
108
|
|
|
* |
|
109
|
|
|
* @return \AppserverIo\Appserver\ServletEngine\SessionMarshallerInterface The session marshaller |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getSessionMarshaller() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->sessionMarshaller; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Transforms the passed session instance into a JSON encoded string. If the data contains |
|
118
|
|
|
* objects, each of them will be serialized before store them to the persistence layer. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletSessionInterface $servletSession The servlet session to be transformed |
|
121
|
|
|
* |
|
122
|
|
|
* @return string The marshalled servlet session representation |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function marshall(ServletSessionInterface $servletSession) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getSessionMarshaller()->marshall($servletSession); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Initializes the session instance from the passed JSON string. If the encoded |
|
131
|
|
|
* data contains objects, they will be unserialized before reattached to the |
|
132
|
|
|
* session instance. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $marshalled The marshaled session representation |
|
135
|
|
|
* |
|
136
|
|
|
* @return \AppserverIo\Psr\Servlet\ServletSessionInterface The un-marshaled servlet session instance |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function unmarshall($marshalled) |
|
139
|
|
|
{ |
|
140
|
|
|
|
|
141
|
|
|
// create a new and empty servlet session instance |
|
142
|
|
|
$servletSession = Session::emptyInstance(); |
|
143
|
|
|
|
|
144
|
|
|
// unmarshall the session data |
|
145
|
|
|
$this->getSessionMarshaller()->unmarshall($servletSession, $marshalled); |
|
146
|
|
|
|
|
147
|
|
|
// returns the initialized servlet session instance |
|
148
|
|
|
return $servletSession; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Querys whether or not the passed session has timed out. |
|
153
|
|
|
* |
|
154
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletSessionInterface $session The session to query for |
|
155
|
|
|
* |
|
156
|
|
|
* @return boolean TRUE if the session has timed out, else FALSE |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function sessionTimedOut(ServletSessionInterface $session) |
|
159
|
|
|
{ |
|
160
|
|
|
|
|
161
|
|
|
// we want to know what inactivity timeout we've to check the sessions for |
|
162
|
|
|
$inactivityTimeout = $this->getSessionSettings()->getInactivityTimeout(); |
|
163
|
|
|
|
|
164
|
|
|
// load the sessions last activity timestamp |
|
165
|
|
|
$lastActivitySecondsAgo = time() - $session->getLastActivityTimestamp(); |
|
166
|
|
|
|
|
167
|
|
|
// query whether or not an inactivity timeout has been set |
|
168
|
|
|
if ($inactivityTimeout < 1) { |
|
169
|
|
|
// if NOT, the inactivity timeout has to be higher than |
|
170
|
|
|
// the last activity timestamp to avoid session timeout |
|
171
|
|
|
$inactivityTimeout = $lastActivitySecondsAgo + 1; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// return TRUE it the last ativity timeout OR the session lifetime has been reached |
|
175
|
|
|
return $lastActivitySecondsAgo > $inactivityTimeout || $session->getLifetime() < time(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|