Quick Search:

View

Revision:

Diff

Diff from 1 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/trunk/oscommerce/includes/classes/session_compatible.php

Annotated File View

hpdl
1
1 <?php
2 /*
3   $Id: session_compatible.php,v 1.7 2004/11/24 16:43:18 hpdl Exp $
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       global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS;
43
44       $sane_session_id = true;
45
46       if (isset($HTTP_GET_VARS[$this->name])) {
47         if (preg_match('/^[a-zA-Z0-9]+$/', $HTTP_GET_VARS[$this->name]) == false) {
48           unset($HTTP_GET_VARS[$this->name]);
49
50           $sane_session_id = false;
51         }
52       } elseif (isset($HTTP_POST_VARS[$this->name])) {
53         if (preg_match('/^[a-zA-Z0-9]+$/', $HTTP_POST_VARS[$this->name]) == false) {
54           unset($HTTP_POST_VARS[$this->name]);
55
56           $sane_session_id = false;
57         }
58       } elseif (isset($HTTP_COOKIE_VARS[$this->name])) {
59         if (preg_match('/^[a-zA-Z0-9]+$/', $HTTP_COOKIE_VARS[$this->name]) == false) {
60           unset($HTTP_COOKIE_VARS[$this->name]);
61
62           $sane_session_id = false;
63         }
64       }
65
66       if ($sane_session_id == false) {
67         tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false));
68       } elseif (session_start()) {
69         $this->setStarted(true);
70
71         $this->setID();
72
73         return true;
74       }
75
76       return false;
77     }
78
79     function exists($variable) {
80       global $HTTP_SESSION_VARS;
81
82       if (isset($HTTP_SESSION_VARS[$variable])) {
83         return true;
84       }
85
86       return false;
87     }
88
89     function set($variable, &$value) {
90       global $HTTP_SESSION_VARS;
91
92       if ($this->is_started == true) {
93         $HTTP_SESSION_VARS[$variable] = $value;
94
95         return true;
96       }
97
98       return false;
99     }
100
101     function remove($variable) {
102       global $HTTP_SESSION_VARS;
103
104       if ($this->exists($variable)) {
105         unset($HTTP_SESSION_VARS[$variable]);
106
107         return true;
108       }
109
110       return false;
111     }
112
113     function &value($variable) {
114       global $HTTP_SESSION_VARS;
115
116       if (isset($HTTP_SESSION_VARS[$variable])) {
117         return $HTTP_SESSION_VARS[$variable];
118       }
119
120       return false;
121     }
122
123     function close() {
124       if (function_exists('session_write_close')) {
125         return session_write_close();
126       }
127
128       return true;
129     }
130
131     function destroy() {
132       global $_COOKIE;
133
134       if (isset($_COOKIE[$this->name])) {
135         unset($_COOKIE[$this->name]);
136       }
137
138       if (STORE_SESSIONS == '') {
139         if (file_exists($this->save_path . $this->id)) {
140           @unlink($this->save_path . $this->id);
141         }
142       }
143
144       return session_destroy();
145     }
146
147     function recreate() {
148       return false;
149     }
150
151     function getSavePath() {
152       return $this->save_path;
153     }
154
155     function setName($name) {
156       session_name($name);
157
158       $this->name = session_name();
159
160       return true;
161     }
162
163     function setID() {
164       $this->id = session_id();
165
166       return true;
167     }
168
169     function setSavePath($path) {
170       if (substr($path, -1) == '/') {
171         $path = substr($path, 0, -1);
172       }
173
174       session_save_path($path);
175
176       $this->save_path = session_save_path();
177
178       return true;
179     }
180
181     function setStarted($state) {
182       if ($state == true) {
183         $this->is_started = true;
184       } else {
185         $this->is_started = false;
186       }
187     }
188
189     function setCookieParameters($lifetime = 0, $path = false, $domain = false, $secure = false) {
190       global $request_type;
191
192       if ($path === false) {
193         $path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
194       }
195
196       if ($domain === false) {
197         $domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
198       }
199
200       return session_set_cookie_params($lifetime, $path, $domain, $secure);
201     }
202
203     function getCookieParameters($key = '') {
204       if (isset($this->_cookie_parameters) === false) {
205         $this->_cookie_parameters = session_get_cookie_params();
206       }
207
208       if (isset($this->_cookie_parameters[$key])) {
209         return $this->_cookie_parameters[$key];
210       }
211
212       return $this->_cookie_parameters;
213     }
214
215     function _open() {
216       return true;
217     }
218
219     function _close() {
220       return true;
221     }
222
223     function _read($key) {
224       $value_query = tep_db_query("select value from " . TABLE_SESSIONS . " where sesskey = '" . tep_db_input($key) . "' and expiry > '" . time() . "'");
225       if (tep_db_num_rows($value_query)) {
226         $value = tep_db_fetch_array($value_query);
227
228         return $value['value'];
229       }
230
231       return false;
232     }
233
234     function _write($key, $value) {
235       if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
236         $SESS_LIFE = 1440;
237       }
238
239       $expiry = time() + $SESS_LIFE;
240
241       $check_query = tep_db_query("select count(*) as total from " . TABLE_SESSIONS . " where sesskey = '" . tep_db_input($key) . "'");
242       $check = tep_db_fetch_array($check_query);
243
244       if ($check['total'] > 0) {
245         return tep_db_query("update " . TABLE_SESSIONS . " set expiry = '" . tep_db_input($expiry) . "', value = '" . tep_db_input($value) . "' where sesskey = '" . tep_db_input($key) . "'");
246       } else {
247         return tep_db_query("insert into " . TABLE_SESSIONS . " values ('" . tep_db_input($key) . "', '" . tep_db_input($expiry) . "', '" . tep_db_input($value) . "')");
248       }
249     }
250
251     function _destroy($key) {
252       return tep_db_query("delete from " . TABLE_SESSIONS . " where sesskey = '" . tep_db_input($key) . "'");
253     }
254
255     function _gc($maxlifetime) {
256       return tep_db_query("delete from " . TABLE_SESSIONS . " where expiry < '" . time() . "'");
257     }
258   }
259 ?>