1 |
6daefa8c
|
Petr Lukašík
|
<?php
|
2 |
|
|
/* SVN FILE: $Id: session.php 5129 2007-05-20 05:47:57Z phpnut $ */
|
3 |
|
|
/**
|
4 |
|
|
* Session class for Cake.
|
5 |
|
|
*
|
6 |
|
|
* Cake abstracts the handling of sessions.
|
7 |
|
|
* There are several convenient methods to access session information.
|
8 |
|
|
* This class is the implementation of those methods.
|
9 |
|
|
* They are mostly used by the Session Component.
|
10 |
|
|
*
|
11 |
|
|
* PHP versions 4 and 5
|
12 |
|
|
*
|
13 |
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
14 |
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
15 |
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
16 |
|
|
* Las Vegas, Nevada 89104
|
17 |
|
|
*
|
18 |
|
|
* Licensed under The MIT License
|
19 |
|
|
* Redistributions of files must retain the above copyright notice.
|
20 |
|
|
*
|
21 |
|
|
* @filesource
|
22 |
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
23 |
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
24 |
|
|
* @package cake
|
25 |
|
|
* @subpackage cake.cake.libs
|
26 |
|
|
* @since CakePHP(tm) v .0.10.0.1222
|
27 |
|
|
* @version $Revision: 5129 $
|
28 |
|
|
* @modifiedby $LastChangedBy: phpnut $
|
29 |
|
|
* @lastmodified $Date: 2007-05-20 00:47:57 -0500 (Sun, 20 May 2007) $
|
30 |
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
31 |
|
|
*/
|
32 |
|
|
/**
|
33 |
|
|
* Database name for cake sessions.
|
34 |
|
|
*
|
35 |
|
|
*/
|
36 |
|
|
if (!defined('CAKE_SESSION_TABLE')) {
|
37 |
|
|
define('CAKE_SESSION_TABLE', 'cake_sessions');
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
if (CAKE_SESSION_SAVE === 'database') {
|
41 |
|
|
uses('model' . DS . 'connection_manager');
|
42 |
|
|
}
|
43 |
|
|
uses('set');
|
44 |
|
|
/**
|
45 |
|
|
* Session class for Cake.
|
46 |
|
|
*
|
47 |
|
|
* Cake abstracts the handling of sessions. There are several convenient methods to access session information.
|
48 |
|
|
* This class is the implementation of those methods. They are mostly used by the Session Component.
|
49 |
|
|
*
|
50 |
|
|
* @package cake
|
51 |
|
|
* @subpackage cake.cake.libs
|
52 |
|
|
*/
|
53 |
|
|
class CakeSession extends Object {
|
54 |
|
|
/**
|
55 |
|
|
* True if the Session is still valid
|
56 |
|
|
*
|
57 |
|
|
* @var boolean
|
58 |
|
|
* @access public
|
59 |
|
|
*/
|
60 |
|
|
var $valid = false;
|
61 |
|
|
/**
|
62 |
|
|
* Error messages for this session
|
63 |
|
|
*
|
64 |
|
|
* @var array
|
65 |
|
|
* @access public
|
66 |
|
|
*/
|
67 |
|
|
var $error = false;
|
68 |
|
|
/**
|
69 |
|
|
* User agent string
|
70 |
|
|
*
|
71 |
|
|
* @var string
|
72 |
|
|
* @access protected
|
73 |
|
|
*/
|
74 |
|
|
var $_userAgent = false;
|
75 |
|
|
/**
|
76 |
|
|
* Path to where the session is active.
|
77 |
|
|
*
|
78 |
|
|
* @var string
|
79 |
|
|
* @access public
|
80 |
|
|
*/
|
81 |
|
|
var $path = false;
|
82 |
|
|
/**
|
83 |
|
|
* Error number of last occurred error
|
84 |
|
|
*
|
85 |
|
|
* @var integer
|
86 |
|
|
* @access public
|
87 |
|
|
*/
|
88 |
|
|
var $lastError = null;
|
89 |
|
|
/**
|
90 |
|
|
* CAKE_SECURITY setting, "high", "medium", or "low".
|
91 |
|
|
*
|
92 |
|
|
* @var string
|
93 |
|
|
* @access public
|
94 |
|
|
*/
|
95 |
|
|
var $security = null;
|
96 |
|
|
/**
|
97 |
|
|
* Start time for this session.
|
98 |
|
|
*
|
99 |
|
|
* @var integer
|
100 |
|
|
* @access public
|
101 |
|
|
*/
|
102 |
|
|
var $time = false;
|
103 |
|
|
/**
|
104 |
|
|
* Time when this session becomes invalid.
|
105 |
|
|
*
|
106 |
|
|
* @var integer
|
107 |
|
|
* @access public
|
108 |
|
|
*/
|
109 |
|
|
var $sessionTime = false;
|
110 |
|
|
/**
|
111 |
|
|
* Keeps track of keys to watch for writes on
|
112 |
|
|
*
|
113 |
|
|
* @var array
|
114 |
|
|
* @access public
|
115 |
|
|
*/
|
116 |
|
|
var $watchKeys = array();
|
117 |
|
|
/**
|
118 |
|
|
* Constructor.
|
119 |
|
|
*
|
120 |
|
|
* @param string $base The base path for the Session
|
121 |
|
|
* @param boolean $start
|
122 |
|
|
* @access public
|
123 |
|
|
*/
|
124 |
|
|
function __construct($base = null, $start = true) {
|
125 |
|
|
if (env('HTTP_USER_AGENT') != null) {
|
126 |
|
|
$this->_userAgent = md5(env('HTTP_USER_AGENT') . CAKE_SESSION_STRING);
|
127 |
|
|
} else {
|
128 |
|
|
$this->_userAgent = "";
|
129 |
|
|
}
|
130 |
|
|
$this->time = time();
|
131 |
|
|
|
132 |
|
|
if($start === true) {
|
133 |
|
|
$this->host = env('HTTP_HOST');
|
134 |
|
|
|
135 |
|
|
if (empty($base) || strpos($base, '?')) {
|
136 |
|
|
$this->path = '/';
|
137 |
|
|
} else {
|
138 |
|
|
$this->path = $base;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
if (strpos($this->host, ':') !== false) {
|
142 |
|
|
$this->host = substr($this->host, 0, strpos($this->host, ':'));
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
$this->sessionTime = $this->time + (Security::inactiveMins() * CAKE_SESSION_TIMEOUT);
|
146 |
|
|
$this->security = CAKE_SECURITY;
|
147 |
|
|
|
148 |
|
|
if (function_exists('session_write_close')) {
|
149 |
|
|
session_write_close();
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
$this->__initSession();
|
153 |
|
|
session_cache_limiter ("must-revalidate");
|
154 |
|
|
session_start();
|
155 |
|
|
header ('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
|
156 |
|
|
$this->__checkValid();
|
157 |
|
|
}
|
158 |
|
|
parent::__construct();
|
159 |
|
|
}
|
160 |
|
|
/**
|
161 |
|
|
* Returns true if given variable is set in session.
|
162 |
|
|
*
|
163 |
|
|
* @param string $name Variable name to check for
|
164 |
|
|
* @return boolean True if variable is there
|
165 |
|
|
* @access public
|
166 |
|
|
*/
|
167 |
|
|
function check($name) {
|
168 |
|
|
$var = $this->__validateKeys($name);
|
169 |
|
|
if (empty($var)) {
|
170 |
|
|
return false;
|
171 |
|
|
}
|
172 |
|
|
$result = Set::extract($_SESSION, $var);
|
173 |
|
|
return isset($result);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
/**
|
177 |
|
|
* Temp method until we are able to remove the last eval()
|
178 |
|
|
*/
|
179 |
|
|
function __sessionVarNames($name) {
|
180 |
|
|
if (is_string($name) && preg_match("/^[0-9a-zA-Z._-]+$/", $name)) {
|
181 |
|
|
if (strpos($name, ".")) {
|
182 |
|
|
$names = explode(".", $name);
|
183 |
|
|
} else {
|
184 |
|
|
$names = array($name);
|
185 |
|
|
}
|
186 |
|
|
$expression="\$_SESSION";
|
187 |
|
|
foreach($names as $item) {
|
188 |
|
|
$expression .= is_numeric($item) ? "[$item]" : "['$item']";
|
189 |
|
|
}
|
190 |
|
|
return $expression;
|
191 |
|
|
}
|
192 |
|
|
$this->__setError(3, "$name is not a string");
|
193 |
|
|
return false;
|
194 |
|
|
}
|
195 |
|
|
/**
|
196 |
|
|
* Removes a variable from session.
|
197 |
|
|
*
|
198 |
|
|
* @param string $name Session variable to remove
|
199 |
|
|
* @return boolean Success
|
200 |
|
|
* @access public
|
201 |
|
|
*/
|
202 |
|
|
function del($name) {
|
203 |
|
|
if ($this->check($name)) {
|
204 |
|
|
if ($var = $this->__validateKeys($name)) {
|
205 |
|
|
if (in_array($var, $this->watchKeys)) {
|
206 |
|
|
trigger_error('Deleting session key {' . $var . '}', E_USER_NOTICE);
|
207 |
|
|
}
|
208 |
|
|
$this->__overwrite($_SESSION, Set::remove($_SESSION, $var));
|
209 |
|
|
return ($this->check($var) == false);
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
$this->__setError(2, "$name doesn't exist");
|
213 |
|
|
return false;
|
214 |
|
|
}
|
215 |
|
|
/**
|
216 |
|
|
* Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself
|
217 |
|
|
*
|
218 |
|
|
* @param array $old
|
219 |
|
|
* @param array $new
|
220 |
|
|
* @return void
|
221 |
|
|
* @access private
|
222 |
|
|
*/
|
223 |
|
|
function __overwrite(&$old, $new) {
|
224 |
|
|
foreach ($old as $key => $var) {
|
225 |
|
|
if (!isset($new[$key])) {
|
226 |
|
|
unset($old[$key]);
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
foreach ($new as $key => $var) {
|
230 |
|
|
$old[$key] = $var;
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
/**
|
234 |
|
|
* Return error description for given error number.
|
235 |
|
|
*
|
236 |
|
|
* @param int $errorNumber
|
237 |
|
|
* @return string Error as string
|
238 |
|
|
* @access public
|
239 |
|
|
*/
|
240 |
|
|
function __error($errorNumber) {
|
241 |
|
|
if (!is_array($this->error) || !array_key_exists($errorNumber, $this->error)) {
|
242 |
|
|
return false;
|
243 |
|
|
} else {
|
244 |
|
|
return $this->error[$errorNumber];
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
/**
|
248 |
|
|
* Returns last occurred error as a string, if any.
|
249 |
|
|
*
|
250 |
|
|
* @return mixed Error description as a string, or false.
|
251 |
|
|
* @access public
|
252 |
|
|
*/
|
253 |
|
|
function error() {
|
254 |
|
|
if ($this->lastError) {
|
255 |
|
|
return $this->__error($this->lastError);
|
256 |
|
|
} else {
|
257 |
|
|
return false;
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
/**
|
261 |
|
|
* Returns true if session is valid.
|
262 |
|
|
*
|
263 |
|
|
* @return boolean
|
264 |
|
|
* @access public
|
265 |
|
|
*/
|
266 |
|
|
function valid() {
|
267 |
|
|
if ($this->read('Config')) {
|
268 |
|
|
if ($this->_userAgent == $this->read("Config.userAgent") && $this->time <= $this->read("Config.time")) {
|
269 |
|
|
$this->valid = true;
|
270 |
|
|
} else {
|
271 |
|
|
$this->valid = false;
|
272 |
|
|
$this->__setError(1, "Session Highjacking Attempted !!!");
|
273 |
|
|
}
|
274 |
|
|
}
|
275 |
|
|
return $this->valid;
|
276 |
|
|
}
|
277 |
|
|
/**
|
278 |
|
|
* Returns given session variable, or all of them, if no parameters given.
|
279 |
|
|
*
|
280 |
|
|
* @param mixed $name The name of the session variable
|
281 |
|
|
* @return mixed The value of the session variable
|
282 |
|
|
* @access public
|
283 |
|
|
*/
|
284 |
|
|
function read($name = null) {
|
285 |
|
|
if (is_null($name)) {
|
286 |
|
|
return $this->__returnSessionVars();
|
287 |
|
|
}
|
288 |
|
|
if (empty($name)) {
|
289 |
|
|
return false;
|
290 |
|
|
}
|
291 |
|
|
$result = Set::extract($_SESSION, $name);
|
292 |
|
|
|
293 |
|
|
if (!is_null($result)) {
|
294 |
|
|
return $result;
|
295 |
|
|
}
|
296 |
|
|
$this->__setError(2, "$name doesn't exist");
|
297 |
|
|
return null;
|
298 |
|
|
}
|
299 |
|
|
/**
|
300 |
|
|
* Returns all session variables.
|
301 |
|
|
*
|
302 |
|
|
* @return mixed Full $_SESSION array, or false on error.
|
303 |
|
|
* @access public
|
304 |
|
|
*/
|
305 |
|
|
function __returnSessionVars() {
|
306 |
|
|
if (!empty($_SESSION)) {
|
307 |
|
|
return $_SESSION;
|
308 |
|
|
}
|
309 |
|
|
$this->__setError(2, "No Session vars set");
|
310 |
|
|
return false;
|
311 |
|
|
}
|
312 |
|
|
/**
|
313 |
|
|
* Tells Session to write a notification when a certain session path or subpath is written to
|
314 |
|
|
*
|
315 |
|
|
* @param mixed $var The variable path to watch
|
316 |
|
|
* @return void
|
317 |
|
|
*/
|
318 |
|
|
function watch($var) {
|
319 |
|
|
$var = $this->__validateKeys($var);
|
320 |
|
|
if (empty($var)) {
|
321 |
|
|
return false;
|
322 |
|
|
}
|
323 |
|
|
$this->watchKeys[] = $var;
|
324 |
|
|
}
|
325 |
|
|
/**
|
326 |
|
|
* Tells Session to stop watching a given key path
|
327 |
|
|
*
|
328 |
|
|
* @param mixed $var The variable path to watch
|
329 |
|
|
* @return void
|
330 |
|
|
*/
|
331 |
|
|
function ignore($var) {
|
332 |
|
|
$var = $this->__validateKeys($var);
|
333 |
|
|
if (!in_array($var, $this->watchKeys)) {
|
334 |
|
|
return;
|
335 |
|
|
}
|
336 |
|
|
foreach ($this->watchKeys as $i => $key) {
|
337 |
|
|
if ($key == $var) {
|
338 |
|
|
unset($this->watchKeys[$i]);
|
339 |
|
|
$this->watchKeys = array_values($this->watchKeys);
|
340 |
|
|
return;
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
}
|
344 |
|
|
/**
|
345 |
|
|
* Writes value to given session variable name.
|
346 |
|
|
*
|
347 |
|
|
* @param mixed $name
|
348 |
|
|
* @param string $value
|
349 |
|
|
* @return boolean True if the write was successful, false if the write failed
|
350 |
|
|
*/
|
351 |
|
|
function write($name, $value) {
|
352 |
|
|
$var = $this->__validateKeys($name);
|
353 |
|
|
|
354 |
|
|
if (empty($var)) {
|
355 |
|
|
return false;
|
356 |
|
|
}
|
357 |
|
|
if (in_array($var, $this->watchKeys)) {
|
358 |
|
|
trigger_error('Writing session key {' . $var . '}: ' . Debugger::exportVar($value), E_USER_NOTICE);
|
359 |
|
|
}
|
360 |
|
|
$this->__overwrite($_SESSION, Set::insert($_SESSION, $var, $value));
|
361 |
|
|
return (Set::extract($_SESSION, $var) == $value);
|
362 |
|
|
}
|
363 |
|
|
/**
|
364 |
|
|
* Helper method to destroy invalid sessions.
|
365 |
|
|
*
|
366 |
|
|
* @return void
|
367 |
|
|
* @access private
|
368 |
|
|
*/
|
369 |
|
|
function destroy() {
|
370 |
|
|
$sessionpath = session_save_path();
|
371 |
|
|
if (empty($sessionpath)) {
|
372 |
|
|
$sessionpath = "/tmp";
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
if (isset($_COOKIE[session_name()])) {
|
376 |
|
|
setcookie(CAKE_SESSION_COOKIE, '', time() - 42000, $this->path);
|
377 |
|
|
}
|
378 |
|
|
|
379 |
|
|
$_SESSION = array();
|
380 |
|
|
$file = $sessionpath . DS . "sess_" . session_id();
|
381 |
|
|
@session_destroy();
|
382 |
|
|
@unlink ($file);
|
383 |
|
|
$this->__construct($this->path);
|
384 |
|
|
$this->renew();
|
385 |
|
|
}
|
386 |
|
|
/**
|
387 |
|
|
* Helper method to initialize a session, based on Cake core settings.
|
388 |
|
|
*
|
389 |
|
|
* @return void
|
390 |
|
|
* @access private
|
391 |
|
|
*/
|
392 |
|
|
function __initSession() {
|
393 |
|
|
switch($this->security) {
|
394 |
|
|
case 'high':
|
395 |
|
|
$this->cookieLifeTime=0;
|
396 |
|
|
if (function_exists('ini_set')) {
|
397 |
|
|
ini_set('session.referer_check', $this->host);
|
398 |
|
|
}
|
399 |
|
|
break;
|
400 |
|
|
case 'medium':
|
401 |
|
|
$this->cookieLifeTime = 7 * 86400;
|
402 |
|
|
break;
|
403 |
|
|
case 'low':
|
404 |
|
|
default:
|
405 |
|
|
$this->cookieLifeTime = 788940000;
|
406 |
|
|
break;
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
switch(CAKE_SESSION_SAVE) {
|
410 |
|
|
case 'cake':
|
411 |
|
|
if (!isset($_SESSION)) {
|
412 |
|
|
if (function_exists('ini_set')) {
|
413 |
|
|
ini_set('session.use_trans_sid', 0);
|
414 |
|
|
ini_set('url_rewriter.tags', '');
|
415 |
|
|
ini_set('session.serialize_handler', 'php');
|
416 |
|
|
ini_set('session.use_cookies', 1);
|
417 |
|
|
ini_set('session.name', CAKE_SESSION_COOKIE);
|
418 |
|
|
ini_set('session.cookie_lifetime', $this->cookieLifeTime);
|
419 |
|
|
ini_set('session.cookie_path', $this->path);
|
420 |
|
|
ini_set('session.auto_start', 0);
|
421 |
|
|
ini_set('session.save_path', TMP . 'sessions');
|
422 |
|
|
}
|
423 |
|
|
}
|
424 |
|
|
break;
|
425 |
|
|
case 'database':
|
426 |
|
|
if (!isset($_SESSION)) {
|
427 |
|
|
if (function_exists('ini_set')) {
|
428 |
|
|
ini_set('session.use_trans_sid', 0);
|
429 |
|
|
ini_set('url_rewriter.tags', '');
|
430 |
|
|
ini_set('session.save_handler', 'user');
|
431 |
|
|
ini_set('session.serialize_handler', 'php');
|
432 |
|
|
ini_set('session.use_cookies', 1);
|
433 |
|
|
ini_set('session.name', CAKE_SESSION_COOKIE);
|
434 |
|
|
ini_set('session.cookie_lifetime', $this->cookieLifeTime);
|
435 |
|
|
ini_set('session.cookie_path', $this->path);
|
436 |
|
|
ini_set('session.auto_start', 0);
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
session_set_save_handler(array('CakeSession','__open'),
|
440 |
|
|
array('CakeSession', '__close'),
|
441 |
|
|
array('CakeSession', '__read'),
|
442 |
|
|
array('CakeSession', '__write'),
|
443 |
|
|
array('CakeSession', '__destroy'),
|
444 |
|
|
array('CakeSession', '__gc'));
|
445 |
|
|
break;
|
446 |
|
|
case 'php':
|
447 |
|
|
if (!isset($_SESSION)) {
|
448 |
|
|
if (function_exists('ini_set')) {
|
449 |
|
|
ini_set('session.use_trans_sid', 0);
|
450 |
|
|
ini_set('session.name', CAKE_SESSION_COOKIE);
|
451 |
|
|
ini_set('session.cookie_lifetime', $this->cookieLifeTime);
|
452 |
|
|
ini_set('session.cookie_path', $this->path);
|
453 |
|
|
}
|
454 |
|
|
}
|
455 |
|
|
break;
|
456 |
|
|
default:
|
457 |
|
|
if (!isset($_SESSION)) {
|
458 |
|
|
$config = CONFIGS . CAKE_SESSION_SAVE . '.php';
|
459 |
|
|
|
460 |
|
|
if (is_file($config)) {
|
461 |
|
|
require_once ($config);
|
462 |
|
|
}
|
463 |
|
|
}
|
464 |
|
|
break;
|
465 |
|
|
}
|
466 |
|
|
}
|
467 |
|
|
/**
|
468 |
|
|
* Helper method to create a new session.
|
469 |
|
|
*
|
470 |
|
|
* @return void
|
471 |
|
|
* @access private
|
472 |
|
|
*
|
473 |
|
|
*/
|
474 |
|
|
function __checkValid() {
|
475 |
|
|
if ($this->read('Config')) {
|
476 |
|
|
if ($this->_userAgent == $this->read("Config.userAgent") && $this->time <= $this->read("Config.time")) {
|
477 |
|
|
$this->write("Config.time", $this->sessionTime);
|
478 |
|
|
$this->valid = true;
|
479 |
|
|
} else {
|
480 |
|
|
$this->valid = false;
|
481 |
|
|
$this->__setError(1, "Session Highjacking Attempted !!!");
|
482 |
|
|
$this->destroy();
|
483 |
|
|
}
|
484 |
|
|
} else {
|
485 |
|
|
srand ((double)microtime() * 1000000);
|
486 |
|
|
$this->write("Config.userAgent", $this->_userAgent);
|
487 |
|
|
$this->write("Config.time", $this->sessionTime);
|
488 |
|
|
$this->write('Config.rand', rand());
|
489 |
|
|
$this->valid = true;
|
490 |
|
|
$this->__setError(1, "Session is valid");
|
491 |
|
|
}
|
492 |
|
|
}
|
493 |
|
|
/**
|
494 |
|
|
* Helper method to restart a session.
|
495 |
|
|
*
|
496 |
|
|
* @return void
|
497 |
|
|
* @access private
|
498 |
|
|
*/
|
499 |
|
|
function __regenerateId() {
|
500 |
|
|
$oldSessionId = session_id();
|
501 |
|
|
$sessionpath = session_save_path();
|
502 |
|
|
if (empty($sessionpath)) {
|
503 |
|
|
$sessionpath = "/tmp";
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
if (isset($_COOKIE[session_name()])) {
|
507 |
|
|
setcookie(CAKE_SESSION_COOKIE, '', time() - 42000, $this->path);
|
508 |
|
|
}
|
509 |
|
|
session_regenerate_id();
|
510 |
|
|
$newSessid = session_id();
|
511 |
|
|
|
512 |
|
|
if (function_exists('session_write_close')) {
|
513 |
|
|
session_write_close();
|
514 |
|
|
}
|
515 |
|
|
$this->__initSession();
|
516 |
|
|
session_id($oldSessionId);
|
517 |
|
|
session_start();
|
518 |
|
|
session_destroy();
|
519 |
|
|
$file = $sessionpath . DS . "sess_$oldSessionId";
|
520 |
|
|
@unlink($file);
|
521 |
|
|
$this->__initSession();
|
522 |
|
|
session_id ($newSessid);
|
523 |
|
|
session_start();
|
524 |
|
|
}
|
525 |
|
|
/**
|
526 |
|
|
* Restarts this session.
|
527 |
|
|
*
|
528 |
|
|
* @return void
|
529 |
|
|
* @access public
|
530 |
|
|
*/
|
531 |
|
|
function renew() {
|
532 |
|
|
$this->__regenerateId();
|
533 |
|
|
}
|
534 |
|
|
/**
|
535 |
|
|
* Validate that the $name is in correct dot notation
|
536 |
|
|
* example: $name = 'ControllerName.key';
|
537 |
|
|
*
|
538 |
|
|
* @param string $name Session key names as string.
|
539 |
|
|
* @return boolean false is $name is not correct format
|
540 |
|
|
* @access private
|
541 |
|
|
*/
|
542 |
|
|
function __validateKeys($name) {
|
543 |
|
|
if (is_string($name) && preg_match("/^[0-9a-zA-Z._-]+$/", $name)) {
|
544 |
|
|
return $name;
|
545 |
|
|
}
|
546 |
|
|
$this->__setError(3, "$name is not a string");
|
547 |
|
|
return false;
|
548 |
|
|
}
|
549 |
|
|
/**
|
550 |
|
|
* Helper method to set an internal error message.
|
551 |
|
|
*
|
552 |
|
|
* @param int $errorNumber Number of the error
|
553 |
|
|
* @param string $errorMessage Description of the error
|
554 |
|
|
* @return void
|
555 |
|
|
* @access private
|
556 |
|
|
*/
|
557 |
|
|
function __setError($errorNumber, $errorMessage) {
|
558 |
|
|
if ($this->error === false) {
|
559 |
|
|
$this->error = array();
|
560 |
|
|
}
|
561 |
|
|
$this->error[$errorNumber] = $errorMessage;
|
562 |
|
|
$this->lastError = $errorNumber;
|
563 |
|
|
}
|
564 |
|
|
/**
|
565 |
|
|
* Method called on open of a database
|
566 |
|
|
* sesson
|
567 |
|
|
*
|
568 |
|
|
* @return boolean
|
569 |
|
|
* @access private
|
570 |
|
|
*
|
571 |
|
|
*/
|
572 |
|
|
function __open() {
|
573 |
|
|
return true;
|
574 |
|
|
}
|
575 |
|
|
/**
|
576 |
|
|
* Method called on close of a database
|
577 |
|
|
* session
|
578 |
|
|
*
|
579 |
|
|
* @return boolean
|
580 |
|
|
* @access private
|
581 |
|
|
*/
|
582 |
|
|
function __close() {
|
583 |
|
|
$probability = mt_rand(1, 150);
|
584 |
|
|
if($probability <= 3) {
|
585 |
|
|
CakeSession::__gc();
|
586 |
|
|
}
|
587 |
|
|
return true;
|
588 |
|
|
}
|
589 |
|
|
/**
|
590 |
|
|
* Method used to read from a database
|
591 |
|
|
* session
|
592 |
|
|
*
|
593 |
|
|
* @param mixed $key The key of the value to read
|
594 |
|
|
* @return mixed The value of the key or false if it does not exist
|
595 |
|
|
* @access private
|
596 |
|
|
*/
|
597 |
|
|
function __read($key) {
|
598 |
|
|
$db =& ConnectionManager::getDataSource('default');
|
599 |
|
|
$table = $db->fullTableName(CAKE_SESSION_TABLE, false);
|
600 |
|
|
$row = $db->query("SELECT " . $db->name($table.'.data') . " FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key), false);
|
601 |
|
|
|
602 |
|
|
if ($row && !isset($row[0][$table]) && isset($row[0][0])) {
|
603 |
|
|
$table = 0;
|
604 |
|
|
}
|
605 |
|
|
|
606 |
|
|
if ($row && $row[0][$table]['data']) {
|
607 |
|
|
return $row[0][$table]['data'];
|
608 |
|
|
} else {
|
609 |
|
|
return false;
|
610 |
|
|
}
|
611 |
|
|
}
|
612 |
|
|
/**
|
613 |
|
|
* Helper function called on write for database
|
614 |
|
|
* sessions
|
615 |
|
|
*
|
616 |
|
|
* @param mixed $key The name of the var
|
617 |
|
|
* @param mixed $value The value of the var
|
618 |
|
|
* @return boolean
|
619 |
|
|
* @access private
|
620 |
|
|
*/
|
621 |
|
|
function __write($key, $value) {
|
622 |
|
|
$db =& ConnectionManager::getDataSource('default');
|
623 |
|
|
$table = $db->fullTableName(CAKE_SESSION_TABLE);
|
624 |
|
|
|
625 |
|
|
switch(CAKE_SECURITY) {
|
626 |
|
|
case 'high':
|
627 |
|
|
$factor = 10;
|
628 |
|
|
break;
|
629 |
|
|
case 'medium':
|
630 |
|
|
$factor = 100;
|
631 |
|
|
break;
|
632 |
|
|
case 'low':
|
633 |
|
|
$factor = 300;
|
634 |
|
|
break;
|
635 |
|
|
default:
|
636 |
|
|
$factor = 10;
|
637 |
|
|
break;
|
638 |
|
|
}
|
639 |
|
|
$expires = time() + CAKE_SESSION_TIMEOUT * $factor;
|
640 |
|
|
$row = $db->query("SELECT COUNT(id) AS count FROM " . $db->name($table) . " WHERE "
|
641 |
|
|
. $db->name('id') . " = "
|
642 |
|
|
. $db->value($key), false);
|
643 |
|
|
|
644 |
|
|
if ($row[0][0]['count'] > 0) {
|
645 |
|
|
$db->execute("UPDATE " . $db->name($table) . " SET " . $db->name('data') . " = "
|
646 |
|
|
. $db->value($value) . ", " . $db->name('expires') . " = "
|
647 |
|
|
. $db->value($expires) . " WHERE " . $db->name('id') . " = "
|
648 |
|
|
. $db->value($key));
|
649 |
|
|
} else {
|
650 |
|
|
$db->execute("INSERT INTO " . $db->name($table) . " (" . $db->name('data') . ","
|
651 |
|
|
. $db->name('expires') . "," . $db->name('id')
|
652 |
|
|
. ") VALUES (" . $db->value($value) . ", " . $db->value($expires) . ", "
|
653 |
|
|
. $db->value($key) . ")");
|
654 |
|
|
}
|
655 |
|
|
return true;
|
656 |
|
|
}
|
657 |
|
|
/**
|
658 |
|
|
* Method called on the destruction of a
|
659 |
|
|
* database session
|
660 |
|
|
*
|
661 |
|
|
* @param integer $key
|
662 |
|
|
* @return boolean
|
663 |
|
|
* @access private
|
664 |
|
|
*/
|
665 |
|
|
function __destroy($key) {
|
666 |
|
|
$db =& ConnectionManager::getDataSource('default');
|
667 |
|
|
$table = $db->fullTableName(CAKE_SESSION_TABLE);
|
668 |
|
|
$db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key, 'integer'));
|
669 |
|
|
return true;
|
670 |
|
|
}
|
671 |
|
|
/**
|
672 |
|
|
* Helper function called on gc for
|
673 |
|
|
* database sessions
|
674 |
|
|
*
|
675 |
|
|
* @param unknown_type $expires
|
676 |
|
|
* @return boolean
|
677 |
|
|
* @access private
|
678 |
|
|
*/
|
679 |
|
|
function __gc($expires = null) {
|
680 |
|
|
$db =& ConnectionManager::getDataSource('default');
|
681 |
|
|
$table = $db->fullTableName(CAKE_SESSION_TABLE);
|
682 |
|
|
$db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.expires') . " < ". $db->value(time()));
|
683 |
|
|
return true;
|
684 |
|
|
}
|
685 |
|
|
}
|
686 |
|
|
?>
|