Quick Search:

View

Revision:

Diff

Diff from 1668 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/session.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
153
3   $Id: session.php 1668 2007-07-20 14:53:24Z 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       $this->setName($name);
hpdl
1668
75       $this->_setCookieParameters(SERVICE_SESSION_EXPIRATION_TIME);
76
77       if ( SERVICE_SESSION_EXPIRATION_TIME > 0 ) {
78         ini_set('session.gc_maxlifetime', SERVICE_SESSION_EXPIRATION_TIME * 60);
79       }
hpdl
1
80     }
81
hpdl
1666
82 /**
83  * Destructor, closes the session
84  *
85  * @access public
86  */
87
88     public function __destruct() {
89       $this->close();
90     }
91
92 /**
hpdl
1667
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);
106       }
107
108       return new $class_name($name);
109     }
110
111 /**
hpdl
1666
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
1666
121       if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) {
hpdl
199
122         $sane_session_id = false;
hpdl
1666
123       } elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) {
hpdl
199
124         $sane_session_id = false;
hpdl
1666
125       } elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) {
hpdl
199
126         $sane_session_id = false;
127       }
hpdl
1
128
hpdl
1666
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
725
134         osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false));
hpdl
1666
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
1666
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
199
153       return $this->_is_started;
hpdl
1
154     }
155
hpdl
1666
156 /**
157  * Closes the session and writes the session data to the storage handler
158  *
159  * @access public
160  */
hpdl
1
161
hpdl
1666
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
1666
168     }
hpdl
1
169
hpdl
1666
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
1666
181
hpdl
1667
182         $this->delete();
hpdl
1666
183
184         return session_destroy();
hpdl
1
185       }
186     }
187
hpdl
1666
188 /**
189  * Deletes an existing session from the storage handler
190  *
hpdl
1667
191  * @param string $id The ID of the session
192  * @access public
hpdl
1666
193  */
hpdl
1
194
hpdl
1667
195     public function delete($id = null) {
196       if ( empty($id) ) {
197         $id = $this->_id;
hpdl
1666
198       }
hpdl
1667
199
200       if ( file_exists($this->_save_path . '/' . $id) ) {
201         @unlink($this->_save_path . '/' . $id);
202       }
hpdl
1666
203     }
hpdl
1
204
hpdl
1666
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
1666
211     public function recreate() {
212       if ( $this->_is_started === true ) {
213         return session_regenerate_id(true);
214       }
215     }
hpdl
1
216
hpdl
1666
217 /**
218  * Return the session file based storage location
219  *
220  * @access public
221  * @return string
222  */
hpdl
1
223
hpdl
1666
224     public function getSavePath() {
hpdl
199
225       return $this->_save_path;
hpdl
1
226     }
227
hpdl
1666
228 /**
229  * Return the session ID
230  *
231  * @access public
232  * @return string
233  */
234
235     public function getID() {
hpdl
199
236       return $this->_id;
237     }
238
hpdl
1666
239 /**
240  * Return the name of the session
241  *
242  * @access public
243  * @return string
244  */
245
246     public function getName() {
hpdl
199
247       return $this->_name;
248     }
249
hpdl
1666
250 /**
251  * Sets the name of the session
252  *
253  * @access public
254  */
255
256     public function setName($name) {
257       if ( empty($name) ) {
258         $name = 'osCsid';
259       }
260
hpdl
1
261       session_name($name);
262
hpdl
199
263       $this->_name = session_name();
hpdl
1
264     }
265
hpdl
1666
266 /**
267  * Sets the storage location for the file based storage handler
268  *
269  * @access public
270  */
hpdl
1
271
hpdl
1666
272     public function setSavePath($path) {
273       if ( substr($path, -1) == '/' ) {
hpdl
1
274         $path = substr($path, 0, -1);
275       }
276
277       session_save_path($path);
278
hpdl
199
279       $this->_save_path = session_save_path();
hpdl
1
280     }
281
hpdl
1666
282 /**
283  * Sets the cookie parameters for the session (lifetime, path, domain, secure, httponly)
284  *
285  * @access protected
286  */
hpdl
1
287
hpdl
1666
288     protected function _setCookieParameters($lifetime = 0, $path = null, $domain = null, $secure = false, $httponly = false) {
hpdl
1
289       global $request_type;
290
hpdl
1668
291       if ( !is_numeric($lifetime) ) {
292         $lifetime = 0;
293       }
294
hpdl
1666
295       if ( empty($path) ) {
hpdl
1
296         $path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
297       }
298
hpdl
1666
299       if ( empty($domain) ) {
hpdl
1
300         $domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
301       }
302
hpdl
1666
303       return session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
hpdl
1
304     }
305
hpdl
1666
306 /**
307  * Returns the cookie parameters for the session (lifetime, path, domain, secure, httponly)
308  *
309  * @access public
310  */
311
312     public function getCookieParameters($key = null) {
313       if ( empty($this->_cookie_parameters) ) {
hpdl
1
314         $this->_cookie_parameters = session_get_cookie_params();
315       }
316
hpdl
1666
317       if ( !empty($key) && isset($this->_cookie_parameters[$key]) ) {
hpdl
1
318         return $this->_cookie_parameters[$key];
hpdl
1666
319       } else {
320         return false;
hpdl
1
321       }
322
323       return $this->_cookie_parameters;
324     }
325   }
326 ?>