Quick Search:

View

Revision:

Diff

Diff from 151 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 /*
hpdl
19
3   $Id: session_compatible.php 151 2005-08-02 14:33:25Z mattice $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
19
8   Copyright (c) 2005 osCommerce
hpdl
1
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) {
hpdl
19
224       global $osC_Database;
hpdl
1
225
hpdl
19
226       $Qsession = $osC_Database->query('select value from :table_sessions where sesskey = :sesskey and expiry > :expiry');
227       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
228       $Qsession->bindValue(':sesskey', $key);
229       $Qsession->bindRaw(':expiry', time());
230       $Qsession->execute();
231
232       if ($Qsession->numberOfRows() > 0) {
233         $value = $Qsession->value('value');
234
235         $Qsession->freeResult();
236
237         return $value;
hpdl
1
238       }
239
240       return false;
241     }
242
243     function _write($key, $value) {
hpdl
19
244       global $osC_Database;
245
hpdl
1
246       if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
247         $SESS_LIFE = 1440;
248       }
249
250       $expiry = time() + $SESS_LIFE;
251
hpdl
19
252       $Qsession = $osC_Database->query('select count(*) as total from :table_sessions where sesskey = :sesskey');
253       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
254       $Qsession->bindValue(':sesskey', $key);
255       $Qsession->execute();
hpdl
1
256
hpdl
19
257       if ($Qsession->valueInt('total') > 0) {
258         $Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey');
hpdl
1
259       } else {
hpdl
19
260         $Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)');
hpdl
1
261       }
hpdl
19
262       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
263       $Qsession->bindValue(':sesskey', $key);
264       $Qsession->bindValue(':expiry', $expiry);
265       $Qsession->bindValue(':value', $value);
266
267       if ($Qsession->execute()) {
268         $write = true;
269       } else {
270         $write = false;
271       }
272
273       $Qsession->freeResult();
274
275       return $write;
hpdl
1
276     }
277
278     function _destroy($key) {
hpdl
19
279       global $osC_Database;
280
281       $Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey');
282       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
283       $Qsession->bindValue(':sesskey', $key);
284       $Qsession->execute();
285
286       $Qsession->freeResult();
hpdl
1
287     }
288
289     function _gc($maxlifetime) {
hpdl
19
290       global $osC_Database;
291
292       $Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry');
293       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
294       $Qsession->bindValue(':expiry', time());
295       $Qsession->execute();
296
297       $Qsession->freeResult();
hpdl
1
298     }
299   }
300 ?>