Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 28 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 29 | /** |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 30 | * CodeIgniter Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 31 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Sessions |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 35 | * @author Andrey Andreev |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 37 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 38 | class CI_Session { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 39 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 40 | protected $_driver = 'files'; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 41 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 42 | // ------------------------------------------------------------------------ |
| 43 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 44 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 45 | * Class constructor |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 46 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 47 | * @param array $params Configuration parameters |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 48 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 49 | */ |
| 50 | public function __construct(array $params = array()) |
| 51 | { |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 52 | // No sessions under CLI |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 53 | if (is_cli()) |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 54 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 55 | log_message('debug', 'Session: Initialization under CLI aborted.'); |
| 56 | return; |
| 57 | } |
| 58 | elseif ((bool) ini_get('session.auto_start')) |
| 59 | { |
| 60 | log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.'); |
| 61 | return; |
| 62 | } |
| 63 | elseif ( ! empty($params['driver'])) |
| 64 | { |
| 65 | $this->_driver = $params['driver']; |
| 66 | unset($params['driver']); |
| 67 | } |
Andrey Andreev | 34b1ef5 | 2014-05-31 21:23:41 +0300 | [diff] [blame^] | 68 | // Note: Make the autoloader pass sess_* params to this constructor |
| 69 | elseif (empty($params) && $driver = config_item('sess_driver')) |
| 70 | { |
| 71 | $this->_driver = $driver; |
| 72 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 73 | |
| 74 | if (($class = $this->_ci_load_classes($this->_driver)) === FALSE) |
| 75 | { |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 79 | $class = new $class($params); |
| 80 | if ($class instanceof SessionHandlerInterface) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 81 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 82 | if (is_php('5.4')) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 83 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 84 | session_set_save_handler($class, TRUE); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 85 | } |
| 86 | else |
| 87 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 88 | session_set_save_handler( |
| 89 | array($class, 'open'), |
| 90 | array($class, 'close'), |
| 91 | array($class, 'read'), |
| 92 | array($class, 'write'), |
| 93 | array($class, 'destroy'), |
| 94 | array($class, 'gc') |
| 95 | ); |
| 96 | |
| 97 | register_shutdown_function('session_write_close'); |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting."); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | session_start(); |
| 107 | $this->_ci_init_vars(); |
| 108 | |
| 109 | log_message('debug', "Session: Class initialized using '".$this->_driver."' driver."); |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | |
| 114 | protected function _ci_load_classes($driver) |
| 115 | { |
| 116 | // PHP 5.4 compatibility |
| 117 | interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php'); |
| 118 | |
| 119 | $prefix = config_item('subclass_prefix'); |
| 120 | |
| 121 | if ( ! class_exists('CI_Session_driver', FALSE)) |
| 122 | { |
| 123 | if (file_exists($file_path = APPPATH.'libraries/Session/Session_driver.php') OR file_exists($file_path = BASEPATH.'libraries/Session/Session_driver.php')) |
| 124 | { |
| 125 | require_once($file_path); |
| 126 | } |
| 127 | |
| 128 | if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php')) |
| 129 | { |
| 130 | require_once($file_path); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | $class = 'Session_'.$driver.'_driver'; |
| 135 | |
| 136 | if ( ! class_exists('CI_'.$class, FALSE)) |
| 137 | { |
| 138 | if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php')) |
| 139 | { |
| 140 | require_once($file_path); |
| 141 | } |
| 142 | |
| 143 | if ( ! class_exists('CI_'.$class, FALSE)) |
| 144 | { |
| 145 | log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting."); |
| 146 | return FALSE; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php')) |
| 151 | { |
| 152 | require_once($file_path); |
| 153 | if (class_exists($prefix.$class, FALSE)) |
| 154 | { |
| 155 | return $prefix.$class; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.'); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return 'CI_'.$class; |
| 164 | } |
| 165 | |
| 166 | // ------------------------------------------------------------------------ |
| 167 | |
| 168 | /** |
| 169 | * Handle temporary variables |
| 170 | * |
| 171 | * Clears old "flash" data, marks the new one for deletion and handles |
| 172 | * "temp" data deletion. |
| 173 | * |
| 174 | * @return void |
| 175 | */ |
| 176 | protected function _ci_init_vars() |
| 177 | { |
| 178 | if ( ! empty($_SESSION['__ci_vars'])) |
| 179 | { |
| 180 | $current_time = time(); |
| 181 | |
| 182 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
| 183 | { |
| 184 | if ($value === 'new') |
| 185 | { |
| 186 | $_SESSION['__ci_vars'][$key] = 'old'; |
| 187 | } |
| 188 | // Hacky, but 'old' will (implicitly) always be less than time() ;) |
| 189 | // DO NOT move this above the 'new' check! |
| 190 | elseif ($value < $current_time) |
| 191 | { |
| 192 | unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (empty($_SESSION['__ci_vars'])) |
| 197 | { |
| 198 | unset($_SESSION['__ci_vars']); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 203 | // ------------------------------------------------------------------------ |
| 204 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 205 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 206 | * Mark as flash |
| 207 | * |
| 208 | * @param mixed $key Session data key(s) |
| 209 | * @return bool |
| 210 | */ |
| 211 | public function mark_as_flash($key) |
| 212 | { |
| 213 | if (is_array($key)) |
| 214 | { |
| 215 | for ($i = 0, $c = count($key); $i < $c; $i++) |
| 216 | { |
| 217 | if ( ! isset($_SESSION[$key[$i]])) |
| 218 | { |
| 219 | return FALSE; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | $new = array_fill_keys($key, 'new'); |
| 224 | |
| 225 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 226 | ? array_merge($_SESSION['__ci_vars'], $new) |
| 227 | : $new; |
| 228 | |
| 229 | return TRUE; |
| 230 | } |
| 231 | |
| 232 | if ( ! isset($_SESSION[$key])) |
| 233 | { |
| 234 | return FALSE; |
| 235 | } |
| 236 | |
| 237 | $_SESSION['__ci_vars'][$key] = 'new'; |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | // ------------------------------------------------------------------------ |
| 242 | |
| 243 | /** |
| 244 | * Get flash keys |
| 245 | * |
| 246 | * @return array |
| 247 | */ |
| 248 | public function get_flash_keys() |
| 249 | { |
| 250 | if ( ! isset($_SESSION['__ci_vars'])) |
| 251 | { |
| 252 | return array(); |
| 253 | } |
| 254 | |
| 255 | $keys = array(); |
| 256 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 257 | { |
| 258 | is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key; |
| 259 | } |
| 260 | |
| 261 | return $keys; |
| 262 | } |
| 263 | |
| 264 | // ------------------------------------------------------------------------ |
| 265 | |
| 266 | /** |
| 267 | * Unmark flash |
| 268 | * |
| 269 | * @param mixed $key Session data key(s) |
| 270 | * @return void |
| 271 | */ |
| 272 | public function unmark_flash($key) |
| 273 | { |
| 274 | if (empty($_SESSION['__ci_vars'])) |
| 275 | { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | is_array($key) OR $key = array($key); |
| 280 | |
| 281 | foreach ($key as $k) |
| 282 | { |
| 283 | if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) |
| 284 | { |
| 285 | unset($_SESSION['__ci_vars'][$k]); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (empty($_SESSION['__ci_vars'])) |
| 290 | { |
| 291 | unset($_SESSION['__ci_vars']); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // ------------------------------------------------------------------------ |
| 296 | |
| 297 | /** |
| 298 | * Mark as temp |
| 299 | * |
| 300 | * @param mixed $key Session data key(s) |
| 301 | * @param int $ttl Time-to-live in seconds |
| 302 | * @return bool |
| 303 | */ |
| 304 | public function mark_as_temp($key, $ttl = 300) |
| 305 | { |
| 306 | $ttl += time(); |
| 307 | |
| 308 | if (is_array($key)) |
| 309 | { |
| 310 | $temp = array(); |
| 311 | |
| 312 | foreach ($key as $k => $v) |
| 313 | { |
| 314 | // Do we have a key => ttl pair, or just a key? |
| 315 | if (is_int($k)) |
| 316 | { |
| 317 | $k = $v; |
| 318 | $v = $ttl; |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | $v += time(); |
| 323 | } |
| 324 | |
| 325 | if ( ! isset($_SESSION[$k])) |
| 326 | { |
| 327 | return FALSE; |
| 328 | } |
| 329 | |
| 330 | $temp[$k] = $ts; |
| 331 | } |
| 332 | |
| 333 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 334 | ? array_merge($_SESSION['__ci_vars'], $temp) |
| 335 | : $temp; |
| 336 | |
| 337 | return TRUE; |
| 338 | } |
| 339 | |
| 340 | if ( ! isset($_SESSION[$key])) |
| 341 | { |
| 342 | return FALSE; |
| 343 | } |
| 344 | |
| 345 | $_SESSION['__ci_vars'][$key] = $ttl; |
| 346 | return TRUE; |
| 347 | } |
| 348 | |
| 349 | // ------------------------------------------------------------------------ |
| 350 | |
| 351 | /** |
| 352 | * Get temp keys |
| 353 | * |
| 354 | * @return array |
| 355 | */ |
| 356 | public function get_temp_keys() |
| 357 | { |
| 358 | if ( ! isset($_SESSION['__ci_vars'])) |
| 359 | { |
| 360 | return array(); |
| 361 | } |
| 362 | |
| 363 | $keys = array(); |
| 364 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 365 | { |
| 366 | is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key; |
| 367 | } |
| 368 | |
| 369 | return $keys; |
| 370 | } |
| 371 | |
| 372 | // ------------------------------------------------------------------------ |
| 373 | |
| 374 | /** |
| 375 | * Unmark flash |
| 376 | * |
| 377 | * @param mixed $key Session data key(s) |
| 378 | * @return void |
| 379 | */ |
| 380 | public function unmark_temp($key) |
| 381 | { |
| 382 | if (empty($_SESSION['__ci_vars'])) |
| 383 | { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | is_array($key) OR $key = array($key); |
| 388 | |
| 389 | foreach ($key as $k) |
| 390 | { |
| 391 | if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) |
| 392 | { |
| 393 | unset($_SESSION['__ci_vars'][$k]); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (empty($_SESSION['__ci_vars'])) |
| 398 | { |
| 399 | unset($_SESSION['__ci_vars']); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // ------------------------------------------------------------------------ |
| 404 | |
| 405 | /** |
| 406 | * __get() |
| 407 | * |
| 408 | * @param string $key 'session_id' or a session data key |
| 409 | * @return mixed |
| 410 | */ |
| 411 | public function __get($key) |
| 412 | { |
| 413 | // Note: Keep this order the same, just in case somebody wants to |
| 414 | // use 'session_id' as a session data key, for whatever reason |
| 415 | if (isset($_SESSION[$key])) |
| 416 | { |
| 417 | return $_SESSION[$key]; |
| 418 | } |
| 419 | elseif ($key === 'session_id') |
| 420 | { |
| 421 | return session_id(); |
| 422 | } |
| 423 | |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | // ------------------------------------------------------------------------ |
| 428 | |
| 429 | /** |
| 430 | * __set() |
| 431 | * |
| 432 | * @param string $key Session data key |
| 433 | * @param mixed $value Session data value |
| 434 | * @return void |
| 435 | */ |
| 436 | public function __set($key, $value) |
| 437 | { |
| 438 | $_SESSION[$key] = $value; |
| 439 | } |
| 440 | |
| 441 | // ------------------------------------------------------------------------ |
| 442 | |
| 443 | /** |
| 444 | * Session destroy |
| 445 | * |
| 446 | * Legacy CI_Session compatibility method |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 447 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 448 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 449 | */ |
| 450 | public function sess_destroy() |
| 451 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 452 | session_destroy(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 453 | } |
| 454 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 455 | // ------------------------------------------------------------------------ |
| 456 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 457 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 458 | * Session regenerate |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 459 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 460 | * Legacy CI_Session compatibility method |
| 461 | * |
| 462 | * @param bool $destroy Destroy old session data flag |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 463 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 464 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 465 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 466 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 467 | session_regenerate_id($destroy); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 470 | // ------------------------------------------------------------------------ |
| 471 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 472 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 473 | * Get userdata reference |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 474 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 475 | * Legacy CI_Session compatibility method |
| 476 | * |
| 477 | * @returns array |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 478 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 479 | public function &get_userdata() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 480 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 481 | return $_SESSION; |
| 482 | } |
| 483 | |
| 484 | // ------------------------------------------------------------------------ |
| 485 | |
| 486 | /** |
| 487 | * Userdata (fetch) |
| 488 | * |
| 489 | * Legacy CI_Session compatibility method |
| 490 | * |
| 491 | * @param string $key Session data key |
| 492 | * @return mixed Session data value or NULL if not found |
| 493 | */ |
| 494 | public function userdata($key = NULL) |
| 495 | { |
| 496 | if (isset($key)) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 497 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 498 | return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL; |
| 499 | } |
| 500 | elseif (empty($_SESSION)) |
| 501 | { |
| 502 | return array(); |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 505 | $userdata = array(); |
| 506 | $_exclude = array_merge( |
| 507 | array('__ci_f', '__ci_t'), |
| 508 | $this->get_flash_keys(), |
| 509 | $this->get_temp_keys() |
| 510 | ); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 511 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 512 | foreach (array_keys($_SESSION) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 513 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 514 | if ( ! in_array($key, $_exclude, TRUE)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 515 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 516 | $userdata[$key] = $_SESSION[$key]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 520 | return $userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 523 | // ------------------------------------------------------------------------ |
| 524 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 525 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 526 | * Set userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 527 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 528 | * Legacy CI_Session compatibility method |
| 529 | * |
| 530 | * @param mixed $data Session data key or an associative array |
| 531 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 532 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 533 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 534 | public function set_userdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 535 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 536 | if (is_array($data)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 537 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 538 | foreach ($data as $key => &$value) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 539 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 540 | $_SESSION[$key] = $value; |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return; |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 544 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 545 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 546 | $_SESSION[$data] = $value; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 549 | // ------------------------------------------------------------------------ |
| 550 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 551 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 552 | * Unset userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 553 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 554 | * Legacy CI_Session compatibility method |
| 555 | * |
| 556 | * @param mixed $data Session data key(s) |
| 557 | * @return void |
| 558 | */ |
| 559 | public function unset_userdata($key) |
| 560 | { |
| 561 | if (is_array($key)) |
| 562 | { |
| 563 | foreach ($key as $k) |
| 564 | { |
| 565 | unset($_SESSION[$key]); |
| 566 | } |
| 567 | |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | unset($_SESSION[$key]); |
| 572 | } |
| 573 | |
| 574 | // ------------------------------------------------------------------------ |
| 575 | |
| 576 | /** |
| 577 | * All userdata (fetch) |
| 578 | * |
| 579 | * Legacy CI_Session compatibility method |
| 580 | * |
| 581 | * @return array $_SESSION, excluding flash data items |
| 582 | */ |
| 583 | public function all_userdata() |
| 584 | { |
| 585 | return $this->userdata(); |
| 586 | } |
| 587 | |
| 588 | // ------------------------------------------------------------------------ |
| 589 | |
| 590 | /** |
| 591 | * Has userdata |
| 592 | * |
| 593 | * Legacy CI_Session compatibility method |
| 594 | * |
| 595 | * @param string $key Session data key |
| 596 | * @return bool |
| 597 | */ |
| 598 | public function has_userdata($key) |
| 599 | { |
| 600 | return isset($_SESSION[$key]); |
| 601 | } |
| 602 | |
| 603 | // ------------------------------------------------------------------------ |
| 604 | |
| 605 | /** |
| 606 | * Flashdata (fetch) |
| 607 | * |
| 608 | * Legacy CI_Session compatibility method |
| 609 | * |
| 610 | * @param string $key Session data key |
| 611 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 612 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 613 | public function flashdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 614 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 615 | if (isset($key)) |
| 616 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 617 | return isset($_SESSION['__ci_f'], $_SESSION['__ci_f'][$key], $_SESSION[$key]) |
| 618 | ? $_SESSION[$key] |
| 619 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 622 | $flashdata = array(); |
| 623 | |
| 624 | if ( ! empty($_SESSION['__ci_f'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 625 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 626 | foreach (array_keys($_SESSION['__ci_f']) as $key) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 627 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 628 | $flashdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 632 | return $flashdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 635 | // ------------------------------------------------------------------------ |
| 636 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 637 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 638 | * Set flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 639 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 640 | * Legacy CI_Session compatibiliy method |
| 641 | * |
| 642 | * @param mixed $data Session data key or an associative array |
| 643 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 644 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 645 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 646 | public function set_flashdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 647 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 648 | $this->set_userdata($data, $value); |
| 649 | $this->mark_as_flash($data); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 652 | // ------------------------------------------------------------------------ |
| 653 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 654 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 655 | * Keep flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 656 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 657 | * Legacy CI_Session compatibility method |
| 658 | * |
| 659 | * @param mixed $key Session data key(s) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 660 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 661 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 662 | public function keep_flashdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 663 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 664 | $this->mark_as_flash($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 667 | // ------------------------------------------------------------------------ |
| 668 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 669 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 670 | * Temp data (fetch) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 671 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 672 | * Legacy CI_Session compatibility method |
| 673 | * |
| 674 | * @param string $key Session data key |
| 675 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 676 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 677 | public function tempdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 678 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 679 | if (isset($key)) |
| 680 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 681 | return isset($_SESSION['__ci_t'], $_SESSION['__ci_t'][$key], $_SESSION[$key]) |
| 682 | ? $_SESSION[$key] |
| 683 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 686 | $tempdata = array(); |
| 687 | |
| 688 | if ( ! empty($_SESSION['__ci_t'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 689 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 690 | foreach (array_keys($_SESSION['__ci_t']) as $key) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 691 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 692 | $tempdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 696 | return $tempdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 697 | } |
| 698 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 699 | // ------------------------------------------------------------------------ |
| 700 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 701 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 702 | * Set tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 703 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 704 | * Legacy CI_Session compatibility method |
| 705 | * |
| 706 | * @param mixed $data Session data key or an associative array of items |
| 707 | * @param mixed $value Value to store |
| 708 | * @param int $ttl Time-to-live in seconds |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 709 | * @return void |
| 710 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 711 | public function set_tempdata($data, $value = NULL, $ttl = 300) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 712 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 713 | $this->set_userdata($data, $value); |
| 714 | $this->mark_as_temp($data, $ttl); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 715 | } |
| 716 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 717 | // ------------------------------------------------------------------------ |
| 718 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 719 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 720 | * Unset tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 721 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 722 | * Legacy CI_Session compatibility method |
| 723 | * |
| 724 | * @param mixed $data Session data key(s) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 725 | * @return void |
| 726 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 727 | public function unset_tempdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 728 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 729 | $this->unmark_temp($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 732 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 733 | |
| 734 | /* End of file Session.php */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 735 | /* Location: ./system/libraries/Session/Session.php */ |