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