Quick Search:

View

Revision:

Diff

Diff from 1107 to:

Annotations

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

Annotated File View

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