hpdl
|
1
|
1
|
<?php
|
|
2
|
/*
|
mattice
|
151
|
3
|
$Id: session.php 1859 2009-03-06 23:21:50Z hpdl $
|
hpdl
|
1
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
hpdl
|
1859
|
8
|
Copyright (c) 2007 osCommerce
|
hpdl
|
1
|
9
|
|
hpdl
|
1498
|
10
|
This program is free software; you can redistribute it and/or modify
|
|
11
|
it under the terms of the GNU General Public License v2 (1991)
|
|
12
|
as published by the Free Software Foundation.
|
hpdl
|
1
|
13
|
*/
|
|
14
|
|
hpdl
|
1859
|
15
|
/**
|
|
16
|
* The osC_Session class manages the session data and custom storage handlers
|
|
17
|
*/
|
|
18
|
|
hpdl
|
1
|
19
|
class osC_Session {
|
|
20
|
|
hpdl
|
1859
|
21
|
/**
|
|
22
|
* Holds the session cookie parameters (lifetime, path, domain, secure, httponly)
|
|
23
|
*
|
|
24
|
* @var array
|
|
25
|
* @access protected
|
|
26
|
*/
|
hpdl
|
1
|
27
|
|
hpdl
|
1859
|
28
|
protected $_cookie_parameters = array();
|
|
29
|
|
|
30
|
/**
|
|
31
|
* Defines if the session has been started or not
|
|
32
|
*
|
|
33
|
* @var boolean
|
|
34
|
* @access protected
|
|
35
|
*/
|
|
36
|
|
|
37
|
protected $_is_started = false;
|
|
38
|
|
|
39
|
/**
|
|
40
|
* Holds the name of the session
|
|
41
|
*
|
|
42
|
* @var string
|
|
43
|
* @access protected
|
|
44
|
*/
|
|
45
|
|
|
46
|
protected $_name = 'osCsid';
|
|
47
|
|
|
48
|
/**
|
|
49
|
* Holds the session id
|
|
50
|
*
|
|
51
|
* @var string
|
|
52
|
* @access protected
|
|
53
|
*/
|
|
54
|
|
|
55
|
protected $_id = null;
|
|
56
|
|
|
57
|
/**
|
|
58
|
* Holds the file system save path for file based session storage
|
|
59
|
*
|
|
60
|
* @var string
|
|
61
|
* @access protected
|
|
62
|
*/
|
|
63
|
|
|
64
|
protected $_save_path = DIR_FS_WORK;
|
|
65
|
|
|
66
|
/**
|
|
67
|
* Constructor, loads custom session handle module if defined
|
|
68
|
*
|
|
69
|
* @param string $name The name of the session
|
|
70
|
* @access public
|
|
71
|
*/
|
|
72
|
|
|
73
|
public function __construct($name = null) {
|
hpdl
|
368
|
74
|
$this->setName($name);
|
hpdl
|
1
|
75
|
$this->setCookieParameters();
|
|
76
|
|
hpdl
|
1859
|
77
|
if ( SERVICE_SESSION_EXPIRATION_TIME > 0 ) {
|
|
78
|
ini_set('session.gc_maxlifetime', SERVICE_SESSION_EXPIRATION_TIME * 60);
|
|
79
|
}
|
|
80
|
}
|
hpdl
|
1107
|
81
|
|
hpdl
|
1859
|
82
|
/**
|
|
83
|
* Destructor, closes the session
|
|
84
|
*
|
|
85
|
* @access public
|
|
86
|
*/
|
|
87
|
|
|
88
|
public function __destruct() {
|
|
89
|
$this->close();
|
|
90
|
}
|
|
91
|
|
|
92
|
/**
|
|
93
|
* Loads the session storage handler
|
|
94
|
*
|
|
95
|
* @param string $name The name of the session
|
|
96
|
* @access public
|
|
97
|
*/
|
|
98
|
|
|
99
|
public static function load($name = null) {
|
|
100
|
$class_name = 'osC_Session';
|
|
101
|
|
|
102
|
if ( !osc_empty(basename(STORE_SESSIONS)) && file_exists(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php') ) {
|
|
103
|
include(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php');
|
|
104
|
|
|
105
|
$class_name = 'osC_Session_' . basename(STORE_SESSIONS);
|
hpdl
|
1
|
106
|
}
|
hpdl
|
1859
|
107
|
|
|
108
|
return new $class_name($name);
|
hpdl
|
1
|
109
|
}
|
|
110
|
|
hpdl
|
1859
|
111
|
/**
|
|
112
|
* Verify an existing session ID and create or resume the session if the existing session ID is valid
|
|
113
|
*
|
|
114
|
* @access public
|
|
115
|
* @return boolean
|
|
116
|
*/
|
|
117
|
|
|
118
|
public function start() {
|
hpdl
|
1
|
119
|
$sane_session_id = true;
|
|
120
|
|
hpdl
|
1859
|
121
|
if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) {
|
hpdl
|
368
|
122
|
$sane_session_id = false;
|
hpdl
|
1859
|
123
|
} elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) {
|
hpdl
|
368
|
124
|
$sane_session_id = false;
|
hpdl
|
1859
|
125
|
} elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) {
|
hpdl
|
368
|
126
|
$sane_session_id = false;
|
|
127
|
}
|
hpdl
|
1
|
128
|
|
hpdl
|
1859
|
129
|
if ( $sane_session_id === false ) {
|
|
130
|
if ( isset($_COOKIE[$this->_name]) ) {
|
|
131
|
setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
|
hpdl
|
1
|
132
|
}
|
|
133
|
|
hpdl
|
757
|
134
|
osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false));
|
hpdl
|
1859
|
135
|
} elseif ( session_start() ) {
|
|
136
|
$this->_is_started = true;
|
|
137
|
$this->_id = session_id();
|
hpdl
|
1
|
138
|
|
|
139
|
return true;
|
|
140
|
}
|
|
141
|
|
|
142
|
return false;
|
|
143
|
}
|
|
144
|
|
hpdl
|
1859
|
145
|
/**
|
|
146
|
* Checks if the session has been started or not
|
|
147
|
*
|
|
148
|
* @access public
|
|
149
|
* @return boolean
|
|
150
|
*/
|
|
151
|
|
|
152
|
public function hasStarted() {
|
hpdl
|
368
|
153
|
return $this->_is_started;
|
hpdl
|
1
|
154
|
}
|
|
155
|
|
hpdl
|
1859
|
156
|
/**
|
|
157
|
* Closes the session and writes the session data to the storage handler
|
|
158
|
*
|
|
159
|
* @access public
|
|
160
|
*/
|
hpdl
|
1
|
161
|
|
hpdl
|
1859
|
162
|
public function close() {
|
|
163
|
if ( $this->_is_started === true ) {
|
|
164
|
$this->_is_started = false;
|
|
165
|
|
|
166
|
return session_write_close();
|
hpdl
|
1
|
167
|
}
|
hpdl
|
1859
|
168
|
}
|
hpdl
|
1
|
169
|
|
hpdl
|
1859
|
170
|
/**
|
|
171
|
* Deletes an existing session
|
|
172
|
*
|
|
173
|
* @access public
|
|
174
|
*/
|
|
175
|
|
|
176
|
public function destroy() {
|
|
177
|
if ( $this->_is_started === true ) {
|
|
178
|
if ( isset($_COOKIE[$this->_name]) ) {
|
|
179
|
setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
|
hpdl
|
1
|
180
|
}
|
hpdl
|
1859
|
181
|
|
|
182
|
$this->delete();
|
|
183
|
|
|
184
|
return session_destroy();
|
hpdl
|
1
|
185
|
}
|
|
186
|
}
|
|
187
|
|
hpdl
|
1859
|
188
|
/**
|
|
189
|
* Deletes an existing session from the storage handler
|
|
190
|
*
|
|
191
|
* @param string $id The ID of the session
|
|
192
|
* @access public
|
|
193
|
*/
|
hpdl
|
1
|
194
|
|
hpdl
|
1859
|
195
|
public function delete($id = null) {
|
|
196
|
if ( empty($id) ) {
|
|
197
|
$id = $this->_id;
|
|
198
|
}
|
hpdl
|
1
|
199
|
|
hpdl
|
1859
|
200
|
if ( file_exists($this->_save_path . '/' . $id) ) {
|
|
201
|
@unlink($this->_save_path . '/' . $id);
|
|
202
|
}
|
|
203
|
}
|
hpdl
|
1
|
204
|
|
hpdl
|
1859
|
205
|
/**
|
|
206
|
* Delete an existing session and move the session data to a new session with a new session ID
|
|
207
|
*
|
|
208
|
* @access public
|
|
209
|
*/
|
hpdl
|
1
|
210
|
|
hpdl
|
1859
|
211
|
public function recreate() {
|
|
212
|
if ( $this->_is_started === true ) {
|
|
213
|
return session_regenerate_id(true);
|
|
214
|
}
|
hpdl
|
1
|
215
|
}
|
|
216
|
|
hpdl
|
1859
|
217
|
/**
|
|
218
|
* Return the session file based storage location
|
|
219
|
*
|
|
220
|
* @access public
|
|
221
|
* @return string
|
|
222
|
*/
|
|
223
|
|
|
224
|
public function getSavePath() {
|
hpdl
|
368
|
225
|
return $this->_save_path;
|
hpdl
|
1
|
226
|
}
|
|
227
|
|
hpdl
|
1859
|
228
|
/**
|
|
229
|
* Return the session ID
|
|
230
|
*
|
|
231
|
* @access public
|
|
232
|
* @return string
|
|
233
|
*/
|
|
234
|
|
|
235
|
public function getID() {
|
hpdl
|
368
|
236
|
return $this->_id;
|
|
237
|
}
|
|
238
|
|
hpdl
|
1859
|
239
|
/**
|
|
240
|
* Return the name of the session
|
|
241
|
*
|
|
242
|
* @access public
|
|
243
|
* @return string
|
|
244
|
*/
|
|
245
|
|
|
246
|
public function getName() {
|
hpdl
|
368
|
247
|
return $this->_name;
|
|
248
|
}
|
|
249
|
|
hpdl
|
1859
|
250
|
/**
|
|
251
|
* Sets the name of the session
|
|
252
|
*
|
|
253
|
* @param string $name The name of the session
|
|
254
|
* @access public
|
|
255
|
*/
|
|
256
|
|
|
257
|
public function setName($name) {
|
|
258
|
if ( empty($name) ) {
|
|
259
|
$name = 'osCsid';
|
|
260
|
}
|
|
261
|
|
hpdl
|
1
|
262
|
session_name($name);
|
|
263
|
|
hpdl
|
368
|
264
|
$this->_name = session_name();
|
hpdl
|
1
|
265
|
}
|
|
266
|
|
hpdl
|
1859
|
267
|
/**
|
|
268
|
* Sets the storage location for the file based storage handler
|
|
269
|
*
|
|
270
|
* @param string $path The file path to store the session data in
|
|
271
|
* @access public
|
|
272
|
*/
|
hpdl
|
1
|
273
|
|
hpdl
|
1859
|
274
|
public function setSavePath($path) {
|
|
275
|
if ( substr($path, -1) == '/' ) {
|
hpdl
|
1
|
276
|
$path = substr($path, 0, -1);
|
|
277
|
}
|
|
278
|
|
|
279
|
session_save_path($path);
|
|
280
|
|
hpdl
|
368
|
281
|
$this->_save_path = session_save_path();
|
hpdl
|
1
|
282
|
}
|
|
283
|
|
hpdl
|
1859
|
284
|
/**
|
|
285
|
* Sets the cookie parameters for the session (lifetime, path, domain, secure, httponly)
|
|
286
|
*
|
|
287
|
* @param integer $lifetime The amount of minutes to keep a cookie active for
|
|
288
|
* @param string $path The web path of the online store to limit cookies to
|
|
289
|
* @param string $domain The domain of the online store to limit cookies to
|
|
290
|
* @param boolean $secure Only access cookies over a secure HTTPS connection
|
|
291
|
* @param boolean $httponly Only access cookies over a HTTP protocol (disallows javascript access to cookies)
|
|
292
|
* @access public
|
|
293
|
*/
|
hpdl
|
1
|
294
|
|
hpdl
|
1859
|
295
|
public function setCookieParameters($lifetime = null, $path = null, $domain = null, $secure = false, $httponly = false) {
|
hpdl
|
1
|
296
|
global $request_type;
|
|
297
|
|
hpdl
|
1859
|
298
|
if ( !is_numeric($lifetime) ) {
|
|
299
|
$lifetime = SERVICE_SESSION_EXPIRATION_TIME * 60;
|
|
300
|
}
|
|
301
|
|
|
302
|
if ( empty($path) ) {
|
hpdl
|
1
|
303
|
$path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
|
|
304
|
}
|
|
305
|
|
hpdl
|
1859
|
306
|
if ( empty($domain) ) {
|
hpdl
|
1
|
307
|
$domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
|
|
308
|
}
|
|
309
|
|
hpdl
|
1859
|
310
|
return session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
|
hpdl
|
1
|
311
|
}
|
|
312
|
|
hpdl
|
1859
|
313
|
/**
|
|
314
|
* Returns the cookie parameters for the session (lifetime, path, domain, secure, httponly)
|
|
315
|
*
|
|
316
|
* @param string $key If specified, return only the value of this cookie parameter setting
|
|
317
|
* @access public
|
|
318
|
*/
|
|
319
|
|
|
320
|
public function getCookieParameters($key = null) {
|
|
321
|
if ( empty($this->_cookie_parameters) ) {
|
hpdl
|
1
|
322
|
$this->_cookie_parameters = session_get_cookie_params();
|
|
323
|
}
|
|
324
|
|
hpdl
|
1859
|
325
|
if ( !empty($key) ) {
|
hpdl
|
1
|
326
|
return $this->_cookie_parameters[$key];
|
|
327
|
}
|
|
328
|
|
|
329
|
return $this->_cookie_parameters;
|
|
330
|
}
|
|
331
|
}
|
|
332
|
?>
|