  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: session.php 1497 2007-03-29 13:40:05Z hpdl $ |
| |
| 3 | + | $Id: session.php 1666 2007-07-19 11:55:15Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
7 | 7 | | |
  |
8 | | - | Copyright (c) 2005 osCommerce |
| |
| 8 | + | Copyright (c) 2007 osCommerce |
|
9 | 9 | | |
| |
10 | 10 | | This program is free software; you can redistribute it and/or modify |
| |
11 | 11 | | it under the terms of the GNU General Public License v2 (1991) |
| |
12 | 12 | | as published by the Free Software Foundation. |
| |
13 | 13 | | */ |
| |
14 | 14 | | |
  |
| 15 | + | /** |
| |
| 16 | + | * The osC_Session class manages the session data and custom storage handlers |
| |
| 17 | + | */ |
| |
| 18 | + | |
|
15 | 19 | | class osC_Session { |
| |
16 | 20 | | |
  |
17 | | - | /* Private variables */ |
| |
18 | | - | var $_cookie_parameters, |
| |
19 | | - | $_is_started = false, |
| |
20 | | - | $_id, |
| |
21 | | - | $_name, |
| |
22 | | - | $_save_path; |
| |
| 21 | + | /** |
| |
| 22 | + | * Holds the session cookie parameters (lifetime, path, domain, secure, httponly) |
| |
| 23 | + | * |
| |
| 24 | + | * @var array |
| |
| 25 | + | * @access protected |
| |
| 26 | + | */ |
|
23 | 27 | | |
  |
24 | | - | // class constructor |
| |
25 | | - | function osC_Session($name = 'sid') { |
| |
26 | | - | $this->setName($name); |
| |
27 | | - | $this->setSavePath(DIR_FS_WORK); |
| |
28 | | - | $this->setCookieParameters(); |
| |
| 28 | + | protected $_cookie_parameters = array(); |
|
29 | 29 | | |
  |
30 | | - | if (STORE_SESSIONS == 'mysql') { |
| |
31 | | - | session_set_save_handler(array(&$this, '_open'), |
| |
32 | | - | array(&$this, '_close'), |
| |
33 | | - | array(&$this, '_read'), |
| |
34 | | - | array(&$this, '_write'), |
| |
35 | | - | array(&$this, '_destroy'), |
| |
36 | | - | array(&$this, '_gc')); |
| |
| 30 | + | /** |
| |
| 31 | + | * Defines if the session has been started or not |
| |
| 32 | + | * |
| |
| 33 | + | * @var boolean |
| |
| 34 | + | * @access protected |
| |
| 35 | + | */ |
|
37 | 36 | | |
  |
38 | | - | register_shutdown_function('session_write_close'); |
| |
| 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); |
|
39 | 80 | | } |
  |
| 81 | + | |
| |
| 82 | + | $this->setName($name); |
| |
| 83 | + | $this->_setCookieParameters(); |
|
40 | 84 | | } |
| |
41 | 85 | | |
  |
42 | | - | // class methods |
| |
43 | | - | function start() { |
| |
| 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() { |
|
44 | 104 | | $sane_session_id = true; |
| |
45 | 105 | | |
  |
46 | | - | if (isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false))) { |
| |
| 106 | + | if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) { |
|
47 | 107 | | $sane_session_id = false; |
  |
48 | | - | } elseif (isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false))) { |
| |
| 108 | + | } elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) { |
|
49 | 109 | | $sane_session_id = false; |
  |
50 | | - | } elseif (isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false))) { |
| |
| 110 | + | } elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) { |
|
51 | 111 | | $sane_session_id = false; |
| |
52 | 112 | | } |
| |
53 | 113 | | |
  |
54 | | - | if ($sane_session_id === false) { |
| |
55 | | - | if (isset($_COOKIE[$this->_name])) { |
| |
56 | | - | setcookie($this->getName(), '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain')); |
| |
| 114 | + | if ( $sane_session_id === false ) { |
| |
| 115 | + | if ( isset($_COOKIE[$this->_name]) ) { |
| |
| 116 | + | setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain')); |
|
57 | 117 | | } |
| |
58 | 118 | | |
| |
59 | 119 | | osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false)); |
  |
60 | | - | } elseif (session_start()) { |
| |
61 | | - | $this->setStarted(true); |
| |
62 | | - | $this->setID(); |
| |
| 120 | + | } elseif ( session_start() ) { |
| |
| 121 | + | $this->_is_started = true; |
| |
| 122 | + | $this->_id = session_id(); |
|
63 | 123 | | |
| |
64 | 124 | | return true; |
| |
65 | 125 | | } |
| |
66 | 126 | | |
| |
67 | 127 | | return false; |
| |
68 | 128 | | } |
| |
69 | 129 | | |
  |
70 | | - | function hasStarted() { |
| |
| 130 | + | /** |
| |
| 131 | + | * Checks if the session has been started or not |
| |
| 132 | + | * |
| |
| 133 | + | * @access public |
| |
| 134 | + | * @return boolean |
| |
| 135 | + | */ |
| |
| 136 | + | |
| |
| 137 | + | public function hasStarted() { |
|
71 | 138 | | return $this->_is_started; |
| |
72 | 139 | | } |
| |
73 | 140 | | |
  |
74 | | - | function close() { |
| |
75 | | - | return session_write_close(); |
| |
76 | | - | } |
| |
| 141 | + | /** |
| |
| 142 | + | * Closes the session and writes the session data to the storage handler |
| |
| 143 | + | * |
| |
| 144 | + | * @access public |
| |
| 145 | + | */ |
|
77 | 146 | | |
  |
78 | | - | function destroy() { |
| |
79 | | - | if (isset($_COOKIE[$this->_name])) { |
| |
80 | | - | unset($_COOKIE[$this->_name]); |
| |
81 | | - | } |
| |
| 147 | + | public function close() { |
| |
| 148 | + | if ( $this->_is_started === true ) { |
| |
| 149 | + | $this->_is_started = false; |
|
82 | 150 | | |
  |
83 | | - | if (STORE_SESSIONS == '') { |
| |
84 | | - | if (file_exists($this->_save_path . $this->_id)) { |
| |
85 | | - | @unlink($this->_save_path . $this->_id); |
| |
86 | | - | } |
| |
| 151 | + | return session_write_close(); |
|
87 | 152 | | } |
  |
88 | | - | |
| |
89 | | - | return session_destroy(); |
|
90 | 153 | | } |
| |
91 | 154 | | |
  |
92 | | - | function recreate() { |
| |
93 | | - | $session_backup = $_SESSION; |
| |
| 155 | + | /** |
| |
| 156 | + | * Deletes an existing session |
| |
| 157 | + | * |
| |
| 158 | + | * @access public |
| |
| 159 | + | */ |
|
94 | 160 | | |
  |
95 | | - | $this->destroy(); |
| |
| 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')); |
| |
| 165 | + | } |
|
96 | 166 | | |
  |
97 | | - | $this->osC_Session(); |
| |
| 167 | + | $this->_delete(); |
|
98 | 168 | | |
  |
99 | | - | $this->start(); |
| |
| 169 | + | return session_destroy(); |
| |
| 170 | + | } |
| |
| 171 | + | } |
|
100 | 172 | | |
  |
101 | | - | $_SESSION = $session_backup; |
| |
| 173 | + | /** |
| |
| 174 | + | * Deletes an existing session from the storage handler |
| |
| 175 | + | * |
| |
| 176 | + | * @access protected |
| |
| 177 | + | */ |
|
102 | 178 | | |
  |
103 | | - | unset($session_backup); |
| |
| 179 | + | protected function _delete() { |
| |
| 180 | + | if ( file_exists($this->_save_path . '/' . $this->_id) ) { |
| |
| 181 | + | @unlink($this->_save_path . '/' . $this->_id); |
| |
| 182 | + | } |
|
104 | 183 | | } |
| |
105 | 184 | | |
  |
106 | | - | function getSavePath() { |
| |
| 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 | + | */ |
| |
| 190 | + | |
| |
| 191 | + | public function recreate() { |
| |
| 192 | + | if ( $this->_is_started === true ) { |
| |
| 193 | + | return session_regenerate_id(true); |
| |
| 194 | + | } |
| |
| 195 | + | } |
| |
| 196 | + | |
| |
| 197 | + | /** |
| |
| 198 | + | * Return the session file based storage location |
| |
| 199 | + | * |
| |
| 200 | + | * @access public |
| |
| 201 | + | * @return string |
| |
| 202 | + | */ |
| |
| 203 | + | |
| |
| 204 | + | public function getSavePath() { |
|
107 | 205 | | return $this->_save_path; |
| |
108 | 206 | | } |
| |
109 | 207 | | |
  |
110 | | - | function getID() { |
| |
| 208 | + | /** |
| |
| 209 | + | * Return the session ID |
| |
| 210 | + | * |
| |
| 211 | + | * @access public |
| |
| 212 | + | * @return string |
| |
| 213 | + | */ |
| |
| 214 | + | |
| |
| 215 | + | public function getID() { |
|
111 | 216 | | return $this->_id; |
| |
112 | 217 | | } |
| |
113 | 218 | | |
  |
114 | | - | function getName() { |
| |
| 219 | + | /** |
| |
| 220 | + | * Return the name of the session |
| |
| 221 | + | * |
| |
| 222 | + | * @access public |
| |
| 223 | + | * @return string |
| |
| 224 | + | */ |
| |
| 225 | + | |
| |
| 226 | + | public function getName() { |
|
115 | 227 | | return $this->_name; |
| |
116 | 228 | | } |
| |
117 | 229 | | |
  |
118 | | - | function setName($name) { |
| |
| 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 | + | |
|
119 | 241 | | session_name($name); |
| |
120 | 242 | | |
| |
121 | 243 | | $this->_name = session_name(); |
| |
122 | 244 | | } |
| |
123 | 245 | | |
  |
124 | | - | function setID() { |
| |
125 | | - | $this->_id = session_id(); |
| |
126 | | - | } |
| |
| 246 | + | /** |
| |
| 247 | + | * Sets the storage location for the file based storage handler |
| |
| 248 | + | * |
| |
| 249 | + | * @access public |
| |
| 250 | + | */ |
|
127 | 251 | | |
  |
128 | | - | function setSavePath($path) { |
| |
129 | | - | if (substr($path, -1) == '/') { |
| |
| 252 | + | public function setSavePath($path) { |
| |
| 253 | + | if ( substr($path, -1) == '/' ) { |
|
130 | 254 | | $path = substr($path, 0, -1); |
| |
131 | 255 | | } |
| |
132 | 256 | | |
| |
|
|
 |
… |
|
135 | 259 | | $this->_save_path = session_save_path(); |
| |
136 | 260 | | } |
| |
137 | 261 | | |
  |
138 | | - | function setStarted($state) { |
| |
139 | | - | if ($state === true) { |
| |
140 | | - | $this->_is_started = true; |
| |
141 | | - | } else { |
| |
142 | | - | $this->_is_started = false; |
| |
143 | | - | } |
| |
144 | | - | } |
| |
| 262 | + | /** |
| |
| 263 | + | * Sets the cookie parameters for the session (lifetime, path, domain, secure, httponly) |
| |
| 264 | + | * |
| |
| 265 | + | * @access protected |
| |
| 266 | + | */ |
|
145 | 267 | | |
  |
146 | | - | function setCookieParameters($lifetime = 0, $path = false, $domain = false, $secure = false) { |
| |
| 268 | + | protected function _setCookieParameters($lifetime = 0, $path = null, $domain = null, $secure = false, $httponly = false) { |
|
147 | 269 | | global $request_type; |
| |
148 | 270 | | |
  |
149 | | - | if ($path === false) { |
| |
| 271 | + | if ( empty($path) ) { |
|
150 | 272 | | $path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH); |
| |
151 | 273 | | } |
| |
152 | 274 | | |
  |
153 | | - | if ($domain === false) { |
| |
| 275 | + | if ( empty($domain) ) { |
|
154 | 276 | | $domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN); |
| |
155 | 277 | | } |
| |
156 | 278 | | |
  |
157 | | - | return session_set_cookie_params($lifetime, $path, $domain, $secure); |
| |
| 279 | + | return session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly); |
|
158 | 280 | | } |
| |
159 | 281 | | |
  |
160 | | - | function getCookieParameters($key = '') { |
| |
161 | | - | if (isset($this->_cookie_parameters) === false) { |
| |
| 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) ) { |
|
162 | 290 | | $this->_cookie_parameters = session_get_cookie_params(); |
| |
163 | 291 | | } |
| |
164 | 292 | | |
  |
165 | | - | if (isset($this->_cookie_parameters[$key])) { |
| |
| 293 | + | if ( !empty($key) && isset($this->_cookie_parameters[$key]) ) { |
|
166 | 294 | | return $this->_cookie_parameters[$key]; |
  |
| 295 | + | } else { |
| |
| 296 | + | return false; |
|
167 | 297 | | } |
| |
168 | 298 | | |
| |
169 | 299 | | return $this->_cookie_parameters; |
| |
170 | 300 | | } |
  |
171 | | - | |
| |
172 | | - | function _open() { |
| |
173 | | - | return true; |
| |
174 | | - | } |
| |
175 | | - | |
| |
176 | | - | function _close() { |
| |
177 | | - | return true; |
| |
178 | | - | } |
| |
179 | | - | |
| |
180 | | - | function _read($key) { |
| |
181 | | - | global $osC_Database; |
| |
182 | | - | |
| |
183 | | - | $Qsession = $osC_Database->query('select value from :table_sessions where sesskey = :sesskey and expiry > :expiry'); |
| |
184 | | - | $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS); |
| |
185 | | - | $Qsession->bindValue(':sesskey', $key); |
| |
186 | | - | $Qsession->bindRaw(':expiry', time()); |
| |
187 | | - | $Qsession->execute(); |
| |
188 | | - | |
| |
189 | | - | if ($Qsession->numberOfRows() > 0) { |
| |
190 | | - | $value = $Qsession->value('value'); |
| |
191 | | - | |
| |
192 | | - | $Qsession->freeResult(); |
| |
193 | | - | |
| |
194 | | - | return $value; |
| |
195 | | - | } |
| |
196 | | - | |
| |
197 | | - | return false; |
| |
198 | | - | } |
| |
199 | | - | |
| |
200 | | - | function _write($key, $value) { |
| |
201 | | - | global $osC_Database; |
| |
202 | | - | |
| |
203 | | - | if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) { |
| |
204 | | - | $SESS_LIFE = 1440; |
| |
205 | | - | } |
| |
206 | | - | |
| |
207 | | - | $expiry = time() + $SESS_LIFE; |
| |
208 | | - | |
| |
209 | | - | $Qsession = $osC_Database->query('select count(*) as total from :table_sessions where sesskey = :sesskey'); |
| |
210 | | - | $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS); |
| |
211 | | - | $Qsession->bindValue(':sesskey', $key); |
| |
212 | | - | $Qsession->execute(); |
| |
213 | | - | |
| |
214 | | - | if ($Qsession->valueInt('total') > 0) { |
| |
215 | | - | $Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey'); |
| |
216 | | - | } else { |
| |
217 | | - | $Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)'); |
| |
218 | | - | } |
| |
219 | | - | $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS); |
| |
220 | | - | $Qsession->bindValue(':sesskey', $key); |
| |
221 | | - | $Qsession->bindValue(':expiry', $expiry); |
| |
222 | | - | $Qsession->bindValue(':value', $value); |
| |
223 | | - | |
| |
224 | | - | if ($Qsession->execute()) { |
| |
225 | | - | $write = true; |
| |
226 | | - | } else { |
| |
227 | | - | $write = false; |
| |
228 | | - | } |
| |
229 | | - | |
| |
230 | | - | $Qsession->freeResult(); |
| |
231 | | - | |
| |
232 | | - | return $write; |
| |
233 | | - | } |
| |
234 | | - | |
| |
235 | | - | function _destroy($key) { |
| |
236 | | - | global $osC_Database; |
| |
237 | | - | |
| |
238 | | - | $Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey'); |
| |
239 | | - | $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS); |
| |
240 | | - | $Qsession->bindValue(':sesskey', $key); |
| |
241 | | - | $Qsession->execute(); |
| |
242 | | - | |
| |
243 | | - | $Qsession->freeResult(); |
| |
244 | | - | } |
| |
245 | | - | |
| |
246 | | - | function _gc($maxlifetime) { |
| |
247 | | - | global $osC_Database; |
| |
248 | | - | |
| |
249 | | - | $Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry'); |
| |
250 | | - | $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS); |
| |
251 | | - | $Qsession->bindValue(':expiry', time()); |
| |
252 | | - | $Qsession->execute(); |
| |
253 | | - | |
| |
254 | | - | $Qsession->freeResult(); |
| |
255 | | - | } |
  |
256 | 301 | | } |
| |
257 | 302 | | ?> |