hpdl
|
1
|
1
|
<?php
|
|
2
|
/*
|
hpdl
|
153
|
3
|
$Id: session.php 153 2005-08-04 12:57:59Z hpdl $
|
hpdl
|
1
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
|
8
|
Copyright (c) 2004 osCommerce
|
|
9
|
|
|
10
|
Released under the GNU General Public License
|
|
11
|
*/
|
|
12
|
|
|
13
|
class osC_Session {
|
|
14
|
var $is_started,
|
|
15
|
$save_path,
|
|
16
|
$name,
|
|
17
|
$id;
|
|
18
|
|
|
19
|
/* Private variables */
|
|
20
|
var $_cookie_parameters;
|
|
21
|
|
|
22
|
// class constructor
|
|
23
|
function osC_Session() {
|
|
24
|
$this->setName('osCsid');
|
|
25
|
$this->setSavePath(DIR_FS_WORK);
|
|
26
|
$this->setCookieParameters();
|
|
27
|
|
|
28
|
if (STORE_SESSIONS == 'mysql') {
|
|
29
|
session_set_save_handler(array(&$this, '_open'),
|
|
30
|
array(&$this, '_close'),
|
|
31
|
array(&$this, '_read'),
|
|
32
|
array(&$this, '_write'),
|
|
33
|
array(&$this, '_destroy'),
|
|
34
|
array(&$this, '_gc'));
|
|
35
|
}
|
|
36
|
|
|
37
|
$this->setStarted(false);
|
|
38
|
}
|
|
39
|
|
|
40
|
// class methods
|
|
41
|
function start() {
|
|
42
|
$sane_session_id = true;
|
|
43
|
|
|
44
|
if (isset($_GET[$this->name])) {
|
|
45
|
if (preg_match('/^[a-zA-Z0-9]+$/', $_GET[$this->name]) == false) {
|
|
46
|
unset($_GET[$this->name]);
|
|
47
|
|
|
48
|
$sane_session_id = false;
|
|
49
|
}
|
|
50
|
} elseif (isset($_POST[$this->name])) {
|
|
51
|
if (preg_match('/^[a-zA-Z0-9]+$/', $_POST[$this->name]) == false) {
|
|
52
|
unset($_POST[$this->name]);
|
|
53
|
|
|
54
|
$sane_session_id = false;
|
|
55
|
}
|
|
56
|
} elseif (isset($_COOKIE[$this->name])) {
|
|
57
|
if (preg_match('/^[a-zA-Z0-9]+$/', $_COOKIE[$this->name]) == false) {
|
|
58
|
unset($_COOKIE[$this->name]);
|
|
59
|
|
|
60
|
$sane_session_id = false;
|
|
61
|
}
|
|
62
|
}
|
|
63
|
|
|
64
|
if ($sane_session_id == false) {
|
|
65
|
tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false));
|
|
66
|
} elseif (session_start()) {
|
|
67
|
$this->setStarted(true);
|
|
68
|
|
|
69
|
$this->setID();
|
|
70
|
|
|
71
|
return true;
|
|
72
|
}
|
|
73
|
|
|
74
|
return false;
|
|
75
|
}
|
|
76
|
|
|
77
|
function exists($variable) {
|
|
78
|
if (isset($_SESSION[$variable])) {
|
|
79
|
return true;
|
|
80
|
}
|
|
81
|
|
|
82
|
return false;
|
|
83
|
}
|
|
84
|
|
|
85
|
function set($variable, &$value) {
|
|
86
|
if ($this->is_started == true) {
|
|
87
|
$_SESSION[$variable] = $value;
|
|
88
|
|
|
89
|
return true;
|
|
90
|
}
|
|
91
|
|
|
92
|
return false;
|
|
93
|
}
|
|
94
|
|
|
95
|
function remove($variable) {
|
|
96
|
if ($this->exists($variable)) {
|
|
97
|
unset($_SESSION[$variable]);
|
|
98
|
|
|
99
|
return true;
|
|
100
|
}
|
|
101
|
|
|
102
|
return false;
|
|
103
|
}
|
|
104
|
|
|
105
|
function &value($variable) {
|
|
106
|
if (isset($_SESSION[$variable])) {
|
|
107
|
return $_SESSION[$variable];
|
|
108
|
}
|
|
109
|
|
|
110
|
return false;
|
|
111
|
}
|
|
112
|
|
|
113
|
function close() {
|
|
114
|
if (function_exists('session_write_close')) {
|
|
115
|
return session_write_close();
|
|
116
|
}
|
|
117
|
|
|
118
|
return true;
|
|
119
|
}
|
|
120
|
|
|
121
|
function destroy() {
|
|
122
|
if (isset($_COOKIE[$this->name])) {
|
|
123
|
unset($_COOKIE[$this->name]);
|
|
124
|
}
|
|
125
|
|
|
126
|
if (STORE_SESSIONS == '') {
|
|
127
|
if (file_exists($this->save_path . $this->id)) {
|
|
128
|
@unlink($this->save_path . $this->id);
|
|
129
|
}
|
|
130
|
}
|
|
131
|
|
|
132
|
return session_destroy();
|
|
133
|
}
|
|
134
|
|
|
135
|
function recreate() {
|
|
136
|
$session_backup = $_SESSION;
|
|
137
|
|
|
138
|
$this->destroy();
|
|
139
|
|
|
140
|
$this->osC_Session();
|
|
141
|
|
|
142
|
$this->start();
|
|
143
|
|
|
144
|
$_SESSION = $session_backup;
|
|
145
|
|
|
146
|
unset($session_backup);
|
|
147
|
}
|
|
148
|
|
|
149
|
function getSavePath() {
|
|
150
|
return $this->save_path;
|
|
151
|
}
|
|
152
|
|
|
153
|
function setName($name) {
|
|
154
|
session_name($name);
|
|
155
|
|
|
156
|
$this->name = session_name();
|
|
157
|
|
|
158
|
return true;
|
|
159
|
}
|
|
160
|
|
|
161
|
function setID() {
|
|
162
|
$this->id = session_id();
|
|
163
|
|
|
164
|
return true;
|
|
165
|
}
|
|
166
|
|
|
167
|
function setSavePath($path) {
|
|
168
|
if (substr($path, -1) == '/') {
|
|
169
|
$path = substr($path, 0, -1);
|
|
170
|
}
|
|
171
|
|
|
172
|
session_save_path($path);
|
|
173
|
|
|
174
|
$this->save_path = session_save_path();
|
|
175
|
|
|
176
|
return true;
|
|
177
|
}
|
|
178
|
|
|
179
|
function setStarted($state) {
|
|
180
|
if ($state == true) {
|
|
181
|
$this->is_started = true;
|
|
182
|
} else {
|
|
183
|
$this->is_started = false;
|
|
184
|
}
|
|
185
|
}
|
|
186
|
|
|
187
|
function setCookieParameters($lifetime = 0, $path = false, $domain = false, $secure = false) {
|
|
188
|
global $request_type;
|
|
189
|
|
|
190
|
if ($path === false) {
|
|
191
|
$path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
|
|
192
|
}
|
|
193
|
|
|
194
|
if ($domain === false) {
|
|
195
|
$domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
|
|
196
|
}
|
|
197
|
|
|
198
|
return session_set_cookie_params($lifetime, $path, $domain, $secure);
|
|
199
|
}
|
|
200
|
|
|
201
|
function getCookieParameters($key = '') {
|
|
202
|
if (isset($this->_cookie_parameters) === false) {
|
|
203
|
$this->_cookie_parameters = session_get_cookie_params();
|
|
204
|
}
|
|
205
|
|
|
206
|
if (isset($this->_cookie_parameters[$key])) {
|
|
207
|
return $this->_cookie_parameters[$key];
|
|
208
|
}
|
|
209
|
|
|
210
|
return $this->_cookie_parameters;
|
|
211
|
}
|
|
212
|
|
|
213
|
function _open() {
|
|
214
|
return true;
|
|
215
|
}
|
|
216
|
|
|
217
|
function _close() {
|
|
218
|
return true;
|
|
219
|
}
|
|
220
|
|
|
221
|
function _read($key) {
|
|
222
|
global $osC_Database;
|
|
223
|
|
|
224
|
$Qsession = $osC_Database->query('select value from :table_sessions where sesskey = :sesskey and expiry > :expiry');
|
|
225
|
$Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
|
|
226
|
$Qsession->bindValue(':sesskey', $key);
|
|
227
|
$Qsession->bindRaw(':expiry', time());
|
|
228
|
$Qsession->execute();
|
|
229
|
|
|
230
|
if ($Qsession->numberOfRows() > 0) {
|
|
231
|
$value = $Qsession->value('value');
|
|
232
|
|
|
233
|
$Qsession->freeResult();
|
|
234
|
|
|
235
|
return $value;
|
|
236
|
}
|
|
237
|
|
|
238
|
return false;
|
|
239
|
}
|
|
240
|
|
|
241
|
function _write($key, $value) {
|
|
242
|
global $osC_Database;
|
|
243
|
|
|
244
|
if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
|
|
245
|
$SESS_LIFE = 1440;
|
|
246
|
}
|
|
247
|
|
|
248
|
$expiry = time() + $SESS_LIFE;
|
|
249
|
|
|
250
|
$Qsession = $osC_Database->query('select count(*) as total from :table_sessions where sesskey = :sesskey');
|
|
251
|
$Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
|
|
252
|
$Qsession->bindValue(':sesskey', $key);
|
|
253
|
$Qsession->execute();
|
|
254
|
|
|
255
|
if ($Qsession->valueInt('total') > 0) {
|
|
256
|
$Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey');
|
|
257
|
} else {
|
|
258
|
$Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)');
|
|
259
|
}
|
|
260
|
$Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
|
|
261
|
$Qsession->bindValue(':sesskey', $key);
|
|
262
|
$Qsession->bindValue(':expiry', $expiry);
|
|
263
|
$Qsession->bindValue(':value', $value);
|
|
264
|
|
|
265
|
if ($Qsession->execute()) {
|
|
266
|
$write = true;
|
|
267
|
} else {
|
|
268
|
$write = false;
|
|
269
|
}
|
|
270
|
|
|
271
|
$Qsession->freeResult();
|
|
272
|
|
|
273
|
return $write;
|
|
274
|
}
|
|
275
|
|
|
276
|
function _destroy($key) {
|
|
277
|
global $osC_Database;
|
|
278
|
|
|
279
|
$Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey');
|
|
280
|
$Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
|
|
281
|
$Qsession->bindValue(':sesskey', $key);
|
|
282
|
$Qsession->execute();
|
|
283
|
|
|
284
|
$Qsession->freeResult();
|
|
285
|
}
|
|
286
|
|
|
287
|
function _gc($maxlifetime) {
|
|
288
|
global $osC_Database;
|
|
289
|
|
|
290
|
$Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry');
|
|
291
|
$Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
|
|
292
|
$Qsession->bindValue(':expiry', time());
|
|
293
|
$Qsession->execute();
|
|
294
|
|
|
295
|
$Qsession->freeResult();
|
|
296
|
}
|
|
297
|
}
|
|
298
|
?>
|