Quick Search:

View

Revision:

Diff

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