Quick Search:

View

Revision:

Diff

Diff from 725 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 725 2006-08-18 12:51:04Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
199
8   Copyright (c) 2005 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
13   class osC_Session {
14
15 /* Private variables */
hpdl
199
16     var $_cookie_parameters,
17         $_is_started = false,
18         $_id,
19         $_name,
20         $_save_path;
hpdl
1
21
22 // class constructor
hpdl
199
23     function osC_Session($name = 'sid') {
24       $this->setName($name);
hpdl
1
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
38 // class methods
39     function start() {
40       $sane_session_id = true;
41
hpdl
227
42       if (isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false))) {
hpdl
199
43         $sane_session_id = false;
hpdl
227
44       } elseif (isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false))) {
hpdl
199
45         $sane_session_id = false;
hpdl
227
46       } elseif (isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false))) {
hpdl
199
47         $sane_session_id = false;
48       }
hpdl
1
49
hpdl
199
50       if ($sane_session_id === false) {
51         if (isset($_COOKIE[$this->_name])) {
52           setcookie($this->getName(), '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
hpdl
1
53         }
54
hpdl
725
55         osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false));
hpdl
1
56       } elseif (session_start()) {
57         $this->setStarted(true);
58         $this->setID();
59
60         return true;
61       }
62
63       return false;
64     }
65
hpdl
199
66     function hasStarted() {
67       return $this->_is_started;
hpdl
1
68     }
69
70     function close() {
hpdl
199
71       return session_write_close();
hpdl
1
72     }
73
74     function destroy() {
hpdl
199
75       if (isset($_COOKIE[$this->_name])) {
76         unset($_COOKIE[$this->_name]);
hpdl
1
77       }
78
79       if (STORE_SESSIONS == '') {
hpdl
199
80         if (file_exists($this->_save_path . $this->_id)) {
81           @unlink($this->_save_path . $this->_id);
hpdl
1
82         }
83       }
84
85       return session_destroy();
86     }
87
88     function recreate() {
89       $session_backup = $_SESSION;
90
91       $this->destroy();
92
93       $this->osC_Session();
94
95       $this->start();
96
97       $_SESSION = $session_backup;
98
99       unset($session_backup);
100     }
101
102     function getSavePath() {
hpdl
199
103       return $this->_save_path;
hpdl
1
104     }
105
hpdl
199
106     function getID() {
107       return $this->_id;
108     }
109
110     function getName() {
111       return $this->_name;
112     }
113
hpdl
1
114     function setName($name) {
115       session_name($name);
116
hpdl
199
117       $this->_name = session_name();
hpdl
1
118     }
119
120     function setID() {
hpdl
199
121       $this->_id = session_id();
hpdl
1
122     }
123
124     function setSavePath($path) {
125       if (substr($path, -1) == '/') {
126         $path = substr($path, 0, -1);
127       }
128
129       session_save_path($path);
130
hpdl
199
131       $this->_save_path = session_save_path();
hpdl
1
132     }
133
134     function setStarted($state) {
hpdl
199
135       if ($state === true) {
136         $this->_is_started = true;
hpdl
1
137       } else {
hpdl
199
138         $this->_is_started = false;
hpdl
1
139       }
140     }
141
142     function setCookieParameters($lifetime = 0, $path = false, $domain = false, $secure = false) {
143       global $request_type;
144
145       if ($path === false) {
146         $path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH);
147       }
148
149       if ($domain === false) {
150         $domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN);
151       }
152
153       return session_set_cookie_params($lifetime, $path, $domain, $secure);
154     }
155
156     function getCookieParameters($key = '') {
157       if (isset($this->_cookie_parameters) === false) {
158         $this->_cookie_parameters = session_get_cookie_params();
159       }
160
161       if (isset($this->_cookie_parameters[$key])) {
162         return $this->_cookie_parameters[$key];
163       }
164
165       return $this->_cookie_parameters;
166     }
167
168     function _open() {
169       return true;
170     }
171
172     function _close() {
173       return true;
174     }
175
176     function _read($key) {
177       global $osC_Database;
178
179       $Qsession = $osC_Database->query('select value from :table_sessions where sesskey = :sesskey and expiry > :expiry');
180       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
181       $Qsession->bindValue(':sesskey', $key);
182       $Qsession->bindRaw(':expiry', time());
183       $Qsession->execute();
184
185       if ($Qsession->numberOfRows() > 0) {
186         $value = $Qsession->value('value');
187
188         $Qsession->freeResult();
189
190         return $value;
191       }
192
193       return false;
194     }
195
196     function _write($key, $value) {
197       global $osC_Database;
198
199       if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
200         $SESS_LIFE = 1440;
201       }
202
203       $expiry = time() + $SESS_LIFE;
204
205       $Qsession = $osC_Database->query('select count(*) as total from :table_sessions where sesskey = :sesskey');
206       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
207       $Qsession->bindValue(':sesskey', $key);
208       $Qsession->execute();
209
210       if ($Qsession->valueInt('total') > 0) {
211         $Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey');
212       } else {
213         $Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)');
214       }
215       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
216       $Qsession->bindValue(':sesskey', $key);
217       $Qsession->bindValue(':expiry', $expiry);
218       $Qsession->bindValue(':value', $value);
219
220       if ($Qsession->execute()) {
221         $write = true;
222       } else {
223         $write = false;
224       }
225
226       $Qsession->freeResult();
227
228       return $write;
229     }
230
231     function _destroy($key) {
232       global $osC_Database;
233
234       $Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey');
235       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
236       $Qsession->bindValue(':sesskey', $key);
237       $Qsession->execute();
238
239       $Qsession->freeResult();
240     }
241
242     function _gc($maxlifetime) {
243       global $osC_Database;
244
245       $Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry');
246       $Qsession->bindRaw(':table_sessions', TABLE_SESSIONS);
247       $Qsession->bindValue(':expiry', time());
248       $Qsession->execute();
249
250       $Qsession->freeResult();
251     }
252   }
253 ?>