hpdl
|
1
|
1
|
<?php
|
|
2
|
/*
|
hpdl
|
153
|
3
|
$Id: session.php 1666 2007-07-19 11:55:15Z hpdl $
|
hpdl
|
1
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
hpdl
|
1666
|
8
|
Copyright (c) 2007 osCommerce
|
hpdl
|
1
|
9
|
|
hpdl
|
1497
|
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
|
1666
|
15
|
/**
|
|
16
|
* The osC_Session class manages the session data and custom storage handlers
|
|
17
|
*/
|
|
18
|
|
hpdl
|
1
|
19
|
class osC_Session {
|
|
20
|
|
hpdl
|
1666
|
21
|
/**
|
|
22
|
* Holds the session cookie parameters (lifetime, path, domain, secure, httponly)
|
|
23
|
*
|
|
24
|
* @var array
|
|
25
|
* @access protected
|
|
26
|
*/
|
hpdl
|
1
|
27
|
|
hpdl
|
1666
|
28
|
protected $_cookie_parameters = array();
|
hpdl
|
1
|
29
|
|
hpdl
|
1666
|
30
|
/**
|
|
31
|
* Defines if the session has been started or not
|
|
32
|
*
|
|
33
|
* @var boolean
|
|
34
|
* @access protected
|
|
35
|
*/
|
hpdl
|
1106
|
36
|
|
hpdl
|
1666
|
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) {
|
|
74
|
if ( !osc_empty(basename(STORE_SESSIONS)) && file_exists(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php') ) {
|
|
75
|
include(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php');
|
|
76
|
|
|
77
|
$class_name = 'osC_Session_' . basename(STORE_SESSIONS);
|
|
78
|
|
|
79
|
return new $class_name($name);
|
hpdl
|
1
|
80
|
}
|
hpdl
|
1666
|
81
|
|
|
82
|
$this->setName($name);
|
|
83
|
$this->_setCookieParameters();
|
hpdl
|
1
|
84
|
}
|
|
85
|
|
hpdl
|
1666
|
86
|
/**
|
|
87
|
* Destructor, closes the session
|
|
88
|
*
|
|
89
|
* @access public
|
|
90
|
*/
|
|
91
|
|
|
92
|
public function __destruct() {
|
|
93
|
$this->close();
|
|
94
|
}
|
|
95
|
|
|
96
|
/**
|
|
97
|
* Verify an existing session ID and create or resume the session if the existing session ID is valid
|
|
98
|
*
|
|
99
|
* @access public
|
|
100
|
* @return boolean
|
|
101
|
*/
|
|
102
|
|
|
103
|
public function start() {
|
hpdl
|
1
|
104
|
$sane_session_id = true;
|
|
105
|
|
hpdl
|
1666
|
106
|
if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) {
|
hpdl
|
199
|
107
|
$sane_session_id = false;
|
hpdl
|
1666
|
108
|
} elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) {
|
hpdl
|
199
|
109
|
$sane_session_id = false;
|
hpdl
|
1666
|
110
|
} elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) {
|
hpdl
|
199
|
111
|
$sane_session_id = false;
|
|
112
|
}
|
hpdl
|
1
|
113
|
|
hpdl
|
1666
|
114
|
if ( $sane_session_id === false ) {
|
|
115
|
if ( isset($_COOKIE[$this->_name]) ) {
|
|
116
|
setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
|
hpdl
|
1
|
117
|
}
|
|
118
|
|
hpdl
|
725
|
119
|
osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false));
|
hpdl
|
1666
|
120
|
} elseif ( session_start() ) {
|
|
121
|
$this->_is_started = true;
|
|
122
|
$this->_id = session_id();
|
hpdl
|
1
|
123
|
|
|
124
|
return true;
|
|
125
|
}
|
|
126
|
|
|
127
|
return false;
|
|
128
|
}
|
|
129
|
|
hpdl
|
1666
|
130
|
/**
|
|
131
|
* Checks if the session has been started or not
|
|
132
|
*
|
|
133
|
* @access public
|
|
134
|
* @return boolean
|
|
135
|
*/
|
|
136
|
|
|
137
|
public function hasStarted() {
|
hpdl
|
199
|
138
|
return $this->_is_started;
|
hpdl
|
1
|
139
|
}
|
|
140
|
|
hpdl
|
1666
|
141
|
/**
|
|
142
|
* Closes the session and writes the session data to the storage handler
|
|
143
|
*
|
|
144
|
* @access public
|
|
145
|
*/
|
hpdl
|
1
|
146
|
|
hpdl
|
1666
|
147
|
public function close() {
|
|
148
|
if ( $this->_is_started === true ) {
|
|
149
|
$this->_is_started = false;
|
|
150
|
|
|
151
|
return session_write_close();
|
hpdl
|
1
|
152
|
}
|
hpdl
|
1666
|
153
|
}
|
hpdl
|
1
|
154
|
|
hpdl
|
1666
|
155
|
/**
|
|
156
|
* Deletes an existing session
|
|
157
|
*
|
|
158
|
* @access public
|
|
159
|
*/
|
|
160
|
|
|
161
|
public function destroy() {
|
|
162
|
if ( $this->_is_started === true ) {
|
|
163
|
if ( isset($_COOKIE[$this->_name]) ) {
|
|
164
|
setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
|
hpdl
|
1
|
165
|
}
|
hpdl
|
1666
|
166
|
|
|
167
|
$this->_delete();
|
|
168
|
|
|
169
|
return session_destroy();
|
hpdl
|
1
|
170
|
}
|
|
171
|
}
|
|
172
|
|
hpdl
|
1666
|
173
|
/**
|
|
174
|
* Deletes an existing session from the storage handler
|
|
175
|
*
|
|
176
|
* @access protected
|
|
177
|
*/
|
hpdl
|
1
|
178
|
|
hpdl
|
1666
|
179
|
protected function _delete() {
|
|
180
|
if ( file_exists($this->_save_path . '/' . $this->_id) ) {
|
|
181
|
@unlink($this->_save_path . '/' . $this->_id);
|
|
182
|
}
|
|
183
|
}
|
hpdl
|
1
|
184
|
|
hpdl
|
1666
|
185
|
/**
|
|
186
|
* Delete an existing session and move the session data to a new session with a new session ID
|
|
187
|
*
|
|
188
|
* @access public
|
|
189
|
*/
|
hpdl
|
1
|
190
|
|
hpdl
|
1666
|
191
|
public function recreate() {
|
|
192
|
if ( $this->_is_started === true ) {
|
|
193
|
return session_regenerate_id(true);
|
|
194
|
}
|
|
195
|
}
|
hpdl
|
1
|
196
|
|
hpdl
|
1666
|
197
|
/**
|
|
198
|
* Return the session file based storage location
|
|
199
|
*
|
|
200
|
* @access public
|
|
201
|
* @return string
|
|
202
|
*/
|
hpdl
|
1
|
203
|
|
hpdl
|
1666
|
204
|
public function getSavePath() {
|
hpdl
|
199
|
205
|
return $this->_save_path;
|
hpdl
|
1
|
206
|
}
|
|
207
|
|
hpdl
|
1666
|
208
|
/**
|
|
209
|
* Return the session ID
|
|
210
|
*
|
|
211
|
* @access public
|
|
212
|
* @return string
|
|
213
|
*/
|
|
214
|
|
|
215
|
public function getID() {
|
hpdl
|
199
|
216
|
return $this->_id;
|
|
217
|
}
|
|
218
|
|
hpdl
|
1666
|
219
|
/**
|
|
220
|
* Return the name of the session
|
|
221
|
*
|
|
222
|
* @access public
|
|
223
|
* @return string
|
|
224
|
*/
|
|
225
|
|
|
226
|
public function getName() {
|
hpdl
|
199
|
227
|
return $this->_name;
|
|
228
|
}
|
|
229
|
|
hpdl
|
1666
|
230
|
/**
|
|
231
|
* Sets the name of the session
|
|
232
|
*
|
|
233
|
* @access public
|
|
234
|
*/
|
|
235
|
|
|
236
|
public function setName($name) {
|
|
237
|
if ( empty($name) ) {
|
|
238
|
$name = 'osCsid';
|
|
239
|
}
|
|
240
|
|
hpdl
|
1
|
241
|
session_name($name);
|
|
242
|
|
hpdl
|
199
|
243
|
$this->_name = session_name();
|
hpdl
|
1
|
244
|
}
|
|
245
|
|
hpdl
|
1666
|
246
|
/**
|
|
247
|
* Sets the storage location for the file based storage handler
|
|
248
|
*
|
|
249
|
* @access public
|
|
250
|
*/
|
hpdl
|
1
|
251
|
|
hpdl
|
1666
|
252
|
public function setSavePath($path) {
|
|
253
|
if ( substr($path, -1) == '/' ) {
|
hpdl
|
1
|
254
|
$path = substr($path, 0, -1);
|
|
255
|
}
|
|
256
|
|
|
257
|
session_save_path($path);
|
|
258
|
|
hpdl
|
199
|
259
|
$this->_save_path = session_save_path();
|
hpdl
|
1
|
260
|
}
|
|
261
|
|
hpdl
|
1666
|
262
|
/**
|
|
263
|
* Sets the cookie parameters for the session (lifetime, path, domain, secure, httponly)
|
|
264
|
*
|
|
265
|
* @access protected
|
|
266
|
*/
|
hpdl
|
1
|
267
|
|
hpdl
|
1666
|
268
|
protected function _setCookieParameters($lifetime = 0, $path = null, $domain = null, $secure = false, $httponly = false) {
|
hpdl
|
1
|
269
|
global $request_type;
|
|
270
|
|
hpdl
|
1666
|
271
|
if ( empty($path) ) {
|
hpdl
|
1
|
272
|
$path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
|
|
273
|
}
|
|
274
|
|
hpdl
|
1666
|
275
|
if ( empty($domain) ) {
|
hpdl
|
1
|
276
|
$domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
|
|
277
|
}
|
|
278
|
|
hpdl
|
1666
|
279
|
return session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
|
hpdl
|
1
|
280
|
}
|
|
281
|
|
hpdl
|
1666
|
282
|
/**
|
|
283
|
* Returns the cookie parameters for the session (lifetime, path, domain, secure, httponly)
|
|
284
|
*
|
|
285
|
* @access public
|
|
286
|
*/
|
|
287
|
|
|
288
|
public function getCookieParameters($key = null) {
|
|
289
|
if ( empty($this->_cookie_parameters) ) {
|
hpdl
|
1
|
290
|
$this->_cookie_parameters = session_get_cookie_params();
|
|
291
|
}
|
|
292
|
|
hpdl
|
1666
|
293
|
if ( !empty($key) && isset($this->_cookie_parameters[$key]) ) {
|
hpdl
|
1
|
294
|
return $this->_cookie_parameters[$key];
|
hpdl
|
1666
|
295
|
} else {
|
|
296
|
return false;
|
hpdl
|
1
|
297
|
}
|
|
298
|
|
|
299
|
return $this->_cookie_parameters;
|
|
300
|
}
|
|
301
|
}
|
|
302
|
?>
|