You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

985 lines
23 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 2.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Session Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Sessions
  45. * @author Andrey Andreev
  46. * @link https://codeigniter.com/user_guide/libraries/sessions.html
  47. */
  48. class CI_Session {
  49. /**
  50. * Userdata array
  51. *
  52. * Just a reference to $_SESSION, for BC purposes.
  53. */
  54. public $userdata;
  55. protected $_driver = 'files';
  56. protected $_config;
  57. protected $_sid_regexp;
  58. // ------------------------------------------------------------------------
  59. /**
  60. * Class constructor
  61. *
  62. * @param array $params Configuration parameters
  63. * @return void
  64. */
  65. public function __construct(array $params = array())
  66. {
  67. // No sessions under CLI
  68. if (is_cli())
  69. {
  70. log_message('debug', 'Session: Initialization under CLI aborted.');
  71. return;
  72. }
  73. elseif ((bool) ini_get('session.auto_start'))
  74. {
  75. log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
  76. return;
  77. }
  78. elseif ( ! empty($params['driver']))
  79. {
  80. $this->_driver = $params['driver'];
  81. unset($params['driver']);
  82. }
  83. elseif ($driver = config_item('sess_driver'))
  84. {
  85. $this->_driver = $driver;
  86. }
  87. // Note: BC workaround
  88. elseif (config_item('sess_use_database'))
  89. {
  90. log_message('debug', 'Session: "sess_driver" is empty; using BC fallback to "sess_use_database".');
  91. $this->_driver = 'database';
  92. }
  93. $class = $this->_ci_load_classes($this->_driver);
  94. // Configuration ...
  95. $this->_configure($params);
  96. $this->_config['_sid_regexp'] = $this->_sid_regexp;
  97. $class = new $class($this->_config);
  98. if ($class instanceof SessionHandlerInterface)
  99. {
  100. if (is_php('5.4'))
  101. {
  102. session_set_save_handler($class, TRUE);
  103. }
  104. else
  105. {
  106. session_set_save_handler(
  107. array($class, 'open'),
  108. array($class, 'close'),
  109. array($class, 'read'),
  110. array($class, 'write'),
  111. array($class, 'destroy'),
  112. array($class, 'gc')
  113. );
  114. register_shutdown_function('session_write_close');
  115. }
  116. }
  117. else
  118. {
  119. log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting.");
  120. return;
  121. }
  122. // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
  123. if (isset($_COOKIE[$this->_config['cookie_name']])
  124. && (
  125. ! is_string($_COOKIE[$this->_config['cookie_name']])
  126. OR ! preg_match('#\A'.$this->_sid_regexp.'\z#', $_COOKIE[$this->_config['cookie_name']])
  127. )
  128. )
  129. {
  130. unset($_COOKIE[$this->_config['cookie_name']]);
  131. }
  132. session_start();
  133. // Is session ID auto-regeneration configured? (ignoring ajax requests)
  134. if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
  135. && ($regenerate_time = config_item('sess_time_to_update')) > 0
  136. )
  137. {
  138. if ( ! isset($_SESSION['__ci_last_regenerate']))
  139. {
  140. $_SESSION['__ci_last_regenerate'] = time();
  141. }
  142. elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
  143. {
  144. $this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
  145. }
  146. }
  147. // Another work-around ... PHP doesn't seem to send the session cookie
  148. // unless it is being currently created or regenerated
  149. elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
  150. {
  151. setcookie(
  152. $this->_config['cookie_name'],
  153. session_id(),
  154. (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
  155. $this->_config['cookie_path'],
  156. $this->_config['cookie_domain'],
  157. $this->_config['cookie_secure'],
  158. TRUE
  159. );
  160. }
  161. $this->_ci_init_vars();
  162. log_message('info', "Session: Class initialized using '".$this->_driver."' driver.");
  163. }
  164. // ------------------------------------------------------------------------
  165. /**
  166. * CI Load Classes
  167. *
  168. * An internal method to load all possible dependency and extension
  169. * classes. It kind of emulates the CI_Driver library, but is
  170. * self-sufficient.
  171. *
  172. * @param string $driver Driver name
  173. * @return string Driver class name
  174. */
  175. protected function _ci_load_classes($driver)
  176. {
  177. // PHP 5.4 compatibility
  178. interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
  179. $prefix = config_item('subclass_prefix');
  180. if ( ! class_exists('CI_Session_driver', FALSE))
  181. {
  182. require_once(
  183. file_exists(APPPATH.'libraries/Session/Session_driver.php')
  184. ? APPPATH.'libraries/Session/Session_driver.php'
  185. : BASEPATH.'libraries/Session/Session_driver.php'
  186. );
  187. if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
  188. {
  189. require_once($file_path);
  190. }
  191. }
  192. $class = 'Session_'.$driver.'_driver';
  193. // Allow custom drivers without the CI_ or MY_ prefix
  194. if ( ! class_exists($class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php'))
  195. {
  196. require_once($file_path);
  197. if (class_exists($class, FALSE))
  198. {
  199. return $class;
  200. }
  201. }
  202. if ( ! class_exists('CI_'.$class, FALSE))
  203. {
  204. if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
  205. {
  206. require_once($file_path);
  207. }
  208. if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
  209. {
  210. throw new UnexpectedValueException("Session: Configured driver '".$driver."' was not found. Aborting.");
  211. }
  212. }
  213. if ( ! class_exists($prefix.$class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
  214. {
  215. require_once($file_path);
  216. if (class_exists($prefix.$class, FALSE))
  217. {
  218. return $prefix.$class;
  219. }
  220. else
  221. {
  222. log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
  223. }
  224. }
  225. return 'CI_'.$class;
  226. }
  227. // ------------------------------------------------------------------------
  228. /**
  229. * Configuration
  230. *
  231. * Handle input parameters and configuration defaults
  232. *
  233. * @param array &$params Input parameters
  234. * @return void
  235. */
  236. protected function _configure(&$params)
  237. {
  238. $expiration = config_item('sess_expiration');
  239. if (isset($params['cookie_lifetime']))
  240. {
  241. $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
  242. }
  243. else
  244. {
  245. $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
  246. ? 0 : (int) $expiration;
  247. }
  248. isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
  249. if (empty($params['cookie_name']))
  250. {
  251. $params['cookie_name'] = ini_get('session.name');
  252. }
  253. else
  254. {
  255. ini_set('session.name', $params['cookie_name']);
  256. }
  257. isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
  258. isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
  259. isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
  260. session_set_cookie_params(
  261. $params['cookie_lifetime'],
  262. $params['cookie_path'],
  263. $params['cookie_domain'],
  264. $params['cookie_secure'],
  265. TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
  266. );
  267. if (empty($expiration))
  268. {
  269. $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
  270. }
  271. else
  272. {
  273. $params['expiration'] = (int) $expiration;
  274. ini_set('session.gc_maxlifetime', $expiration);
  275. }
  276. $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
  277. isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
  278. $this->_config = $params;
  279. // Security is king
  280. ini_set('session.use_trans_sid', 0);
  281. ini_set('session.use_strict_mode', 1);
  282. ini_set('session.use_cookies', 1);
  283. ini_set('session.use_only_cookies', 1);
  284. $this->_configure_sid_length();
  285. }
  286. // ------------------------------------------------------------------------
  287. /**
  288. * Configure session ID length
  289. *
  290. * To make life easier, we used to force SHA-1 and 4 bits per
  291. * character on everyone. And of course, someone was unhappy.
  292. *
  293. * Then PHP 7.1 broke backwards-compatibility because ext/session
  294. * is such a mess that nobody wants to touch it with a pole stick,
  295. * and the one guy who does, nobody has the energy to argue with.
  296. *
  297. * So we were forced to make changes, and OF COURSE something was
  298. * going to break and now we have this pile of shit. -- Narf
  299. *
  300. * @return void
  301. */
  302. protected function _configure_sid_length()
  303. {
  304. if (PHP_VERSION_ID < 70100)
  305. {
  306. $hash_function = ini_get('session.hash_function');
  307. if (ctype_digit($hash_function))
  308. {
  309. if ($hash_function !== '1')
  310. {
  311. ini_set('session.hash_function', 1);
  312. }
  313. $bits = 160;
  314. }
  315. elseif ( ! in_array($hash_function, hash_algos(), TRUE))
  316. {
  317. ini_set('session.hash_function', 1);
  318. $bits = 160;
  319. }
  320. elseif (($bits = strlen(hash($hash_function, 'dummy', false)) * 4) < 160)
  321. {
  322. ini_set('session.hash_function', 1);
  323. $bits = 160;
  324. }
  325. $bits_per_character = (int) ini_get('session.hash_bits_per_character');
  326. $sid_length = (int) ceil($bits / $bits_per_character);
  327. }
  328. else
  329. {
  330. $bits_per_character = (int) ini_get('session.sid_bits_per_character');
  331. $sid_length = (int) ini_get('session.sid_length');
  332. if (($bits = $sid_length * $bits_per_character) < 160)
  333. {
  334. // Add as many more characters as necessary to reach at least 160 bits
  335. $sid_length += (int) ceil((160 % $bits) / $bits_per_character);
  336. ini_set('session.sid_length', $sid_length);
  337. }
  338. }
  339. // Yes, 4,5,6 are the only known possible values as of 2016-10-27
  340. switch ($bits_per_character)
  341. {
  342. case 4:
  343. $this->_sid_regexp = '[0-9a-f]';
  344. break;
  345. case 5:
  346. $this->_sid_regexp = '[0-9a-v]';
  347. break;
  348. case 6:
  349. $this->_sid_regexp = '[0-9a-zA-Z,-]';
  350. break;
  351. }
  352. $this->_sid_regexp .= '{'.$sid_length.'}';
  353. }
  354. // ------------------------------------------------------------------------
  355. /**
  356. * Handle temporary variables
  357. *
  358. * Clears old "flash" data, marks the new one for deletion and handles
  359. * "temp" data deletion.
  360. *
  361. * @return void
  362. */
  363. protected function _ci_init_vars()
  364. {
  365. if ( ! empty($_SESSION['__ci_vars']))
  366. {
  367. $current_time = time();
  368. foreach ($_SESSION['__ci_vars'] as $key => &$value)
  369. {
  370. if ($value === 'new')
  371. {
  372. $_SESSION['__ci_vars'][$key] = 'old';
  373. }
  374. // Hacky, but 'old' will (implicitly) always be less than time() ;)
  375. // DO NOT move this above the 'new' check!
  376. elseif ($value < $current_time)
  377. {
  378. unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
  379. }
  380. }
  381. if (empty($_SESSION['__ci_vars']))
  382. {
  383. unset($_SESSION['__ci_vars']);
  384. }
  385. }
  386. $this->userdata =& $_SESSION;
  387. }
  388. // ------------------------------------------------------------------------
  389. /**
  390. * Mark as flash
  391. *
  392. * @param mixed $key Session data key(s)
  393. * @return bool
  394. */
  395. public function mark_as_flash($key)
  396. {
  397. if (is_array($key))
  398. {
  399. for ($i = 0, $c = count($key); $i < $c; $i++)
  400. {
  401. if ( ! isset($_SESSION[$key[$i]]))
  402. {
  403. return FALSE;
  404. }
  405. }
  406. $new = array_fill_keys($key, 'new');
  407. $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
  408. ? array_merge($_SESSION['__ci_vars'], $new)
  409. : $new;
  410. return TRUE;
  411. }
  412. if ( ! isset($_SESSION[$key]))
  413. {
  414. return FALSE;
  415. }
  416. $_SESSION['__ci_vars'][$key] = 'new';
  417. return TRUE;
  418. }
  419. // ------------------------------------------------------------------------
  420. /**
  421. * Get flash keys
  422. *
  423. * @return array
  424. */
  425. public function get_flash_keys()
  426. {
  427. if ( ! isset($_SESSION['__ci_vars']))
  428. {
  429. return array();
  430. }
  431. $keys = array();
  432. foreach (array_keys($_SESSION['__ci_vars']) as $key)
  433. {
  434. is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
  435. }
  436. return $keys;
  437. }
  438. // ------------------------------------------------------------------------
  439. /**
  440. * Unmark flash
  441. *
  442. * @param mixed $key Session data key(s)
  443. * @return void
  444. */
  445. public function unmark_flash($key)
  446. {
  447. if (empty($_SESSION['__ci_vars']))
  448. {
  449. return;
  450. }
  451. is_array($key) OR $key = array($key);
  452. foreach ($key as $k)
  453. {
  454. if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
  455. {
  456. unset($_SESSION['__ci_vars'][$k]);
  457. }
  458. }
  459. if (empty($_SESSION['__ci_vars']))
  460. {
  461. unset($_SESSION['__ci_vars']);
  462. }
  463. }
  464. // ------------------------------------------------------------------------
  465. /**
  466. * Mark as temp
  467. *
  468. * @param mixed $key Session data key(s)
  469. * @param int $ttl Time-to-live in seconds
  470. * @return bool
  471. */
  472. public function mark_as_temp($key, $ttl = 300)
  473. {
  474. $ttl += time();
  475. if (is_array($key))
  476. {
  477. $temp = array();
  478. foreach ($key as $k => $v)
  479. {
  480. // Do we have a key => ttl pair, or just a key?
  481. if (is_int($k))
  482. {
  483. $k = $v;
  484. $v = $ttl;
  485. }
  486. else
  487. {
  488. $v += time();
  489. }
  490. if ( ! isset($_SESSION[$k]))
  491. {
  492. return FALSE;
  493. }
  494. $temp[$k] = $v;
  495. }
  496. $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
  497. ? array_merge($_SESSION['__ci_vars'], $temp)
  498. : $temp;
  499. return TRUE;
  500. }
  501. if ( ! isset($_SESSION[$key]))
  502. {
  503. return FALSE;
  504. }
  505. $_SESSION['__ci_vars'][$key] = $ttl;
  506. return TRUE;
  507. }
  508. // ------------------------------------------------------------------------
  509. /**
  510. * Get temp keys
  511. *
  512. * @return array
  513. */
  514. public function get_temp_keys()
  515. {
  516. if ( ! isset($_SESSION['__ci_vars']))
  517. {
  518. return array();
  519. }
  520. $keys = array();
  521. foreach (array_keys($_SESSION['__ci_vars']) as $key)
  522. {
  523. is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
  524. }
  525. return $keys;
  526. }
  527. // ------------------------------------------------------------------------
  528. /**
  529. * Unmark flash
  530. *
  531. * @param mixed $key Session data key(s)
  532. * @return void
  533. */
  534. public function unmark_temp($key)
  535. {
  536. if (empty($_SESSION['__ci_vars']))
  537. {
  538. return;
  539. }
  540. is_array($key) OR $key = array($key);
  541. foreach ($key as $k)
  542. {
  543. if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
  544. {
  545. unset($_SESSION['__ci_vars'][$k]);
  546. }
  547. }
  548. if (empty($_SESSION['__ci_vars']))
  549. {
  550. unset($_SESSION['__ci_vars']);
  551. }
  552. }
  553. // ------------------------------------------------------------------------
  554. /**
  555. * __get()
  556. *
  557. * @param string $key 'session_id' or a session data key
  558. * @return mixed
  559. */
  560. public function __get($key)
  561. {
  562. // Note: Keep this order the same, just in case somebody wants to
  563. // use 'session_id' as a session data key, for whatever reason
  564. if (isset($_SESSION[$key]))
  565. {
  566. return $_SESSION[$key];
  567. }
  568. elseif ($key === 'session_id')
  569. {
  570. return session_id();
  571. }
  572. return NULL;
  573. }
  574. // ------------------------------------------------------------------------
  575. /**
  576. * __isset()
  577. *
  578. * @param string $key 'session_id' or a session data key
  579. * @return bool
  580. */
  581. public function __isset($key)
  582. {
  583. if ($key === 'session_id')
  584. {
  585. return (session_status() === PHP_SESSION_ACTIVE);
  586. }
  587. return isset($_SESSION[$key]);
  588. }
  589. // ------------------------------------------------------------------------
  590. /**
  591. * __set()
  592. *
  593. * @param string $key Session data key
  594. * @param mixed $value Session data value
  595. * @return void
  596. */
  597. public function __set($key, $value)
  598. {
  599. $_SESSION[$key] = $value;
  600. }
  601. // ------------------------------------------------------------------------
  602. /**
  603. * Session destroy
  604. *
  605. * Legacy CI_Session compatibility method
  606. *
  607. * @return void
  608. */
  609. public function sess_destroy()
  610. {
  611. session_destroy();
  612. }
  613. // ------------------------------------------------------------------------
  614. /**
  615. * Session regenerate
  616. *
  617. * Legacy CI_Session compatibility method
  618. *
  619. * @param bool $destroy Destroy old session data flag
  620. * @return void
  621. */
  622. public function sess_regenerate($destroy = FALSE)
  623. {
  624. $_SESSION['__ci_last_regenerate'] = time();
  625. session_regenerate_id($destroy);
  626. }
  627. // ------------------------------------------------------------------------
  628. /**
  629. * Get userdata reference
  630. *
  631. * Legacy CI_Session compatibility method
  632. *
  633. * @returns array
  634. */
  635. public function &get_userdata()
  636. {
  637. return $_SESSION;
  638. }
  639. // ------------------------------------------------------------------------
  640. /**
  641. * Userdata (fetch)
  642. *
  643. * Legacy CI_Session compatibility method
  644. *
  645. * @param string $key Session data key
  646. * @return mixed Session data value or NULL if not found
  647. */
  648. public function userdata($key = NULL)
  649. {
  650. if (isset($key))
  651. {
  652. return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
  653. }
  654. elseif (empty($_SESSION))
  655. {
  656. return array();
  657. }
  658. $userdata = array();
  659. $_exclude = array_merge(
  660. array('__ci_vars'),
  661. $this->get_flash_keys(),
  662. $this->get_temp_keys()
  663. );
  664. foreach (array_keys($_SESSION) as $key)
  665. {
  666. if ( ! in_array($key, $_exclude, TRUE))
  667. {
  668. $userdata[$key] = $_SESSION[$key];
  669. }
  670. }
  671. return $userdata;
  672. }
  673. // ------------------------------------------------------------------------
  674. /**
  675. * Set userdata
  676. *
  677. * Legacy CI_Session compatibility method
  678. *
  679. * @param mixed $data Session data key or an associative array
  680. * @param mixed $value Value to store
  681. * @return void
  682. */
  683. public function set_userdata($data, $value = NULL)
  684. {
  685. if (is_array($data))
  686. {
  687. foreach ($data as $key => &$value)
  688. {
  689. $_SESSION[$key] = $value;
  690. }
  691. return;
  692. }
  693. $_SESSION[$data] = $value;
  694. }
  695. // ------------------------------------------------------------------------
  696. /**
  697. * Unset userdata
  698. *
  699. * Legacy CI_Session compatibility method
  700. *
  701. * @param mixed $key Session data key(s)
  702. * @return void
  703. */
  704. public function unset_userdata($key)
  705. {
  706. if (is_array($key))
  707. {
  708. foreach ($key as $k)
  709. {
  710. unset($_SESSION[$k]);
  711. }
  712. return;
  713. }
  714. unset($_SESSION[$key]);
  715. }
  716. // ------------------------------------------------------------------------
  717. /**
  718. * All userdata (fetch)
  719. *
  720. * Legacy CI_Session compatibility method
  721. *
  722. * @return array $_SESSION, excluding flash data items
  723. */
  724. public function all_userdata()
  725. {
  726. return $this->userdata();
  727. }
  728. // ------------------------------------------------------------------------
  729. /**
  730. * Has userdata
  731. *
  732. * Legacy CI_Session compatibility method
  733. *
  734. * @param string $key Session data key
  735. * @return bool
  736. */
  737. public function has_userdata($key)
  738. {
  739. return isset($_SESSION[$key]);
  740. }
  741. // ------------------------------------------------------------------------
  742. /**
  743. * Flashdata (fetch)
  744. *
  745. * Legacy CI_Session compatibility method
  746. *
  747. * @param string $key Session data key
  748. * @return mixed Session data value or NULL if not found
  749. */
  750. public function flashdata($key = NULL)
  751. {
  752. if (isset($key))
  753. {
  754. return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
  755. ? $_SESSION[$key]
  756. : NULL;
  757. }
  758. $flashdata = array();
  759. if ( ! empty($_SESSION['__ci_vars']))
  760. {
  761. foreach ($_SESSION['__ci_vars'] as $key => &$value)
  762. {
  763. is_int($value) OR $flashdata[$key] = $_SESSION[$key];
  764. }
  765. }
  766. return $flashdata;
  767. }
  768. // ------------------------------------------------------------------------
  769. /**
  770. * Set flashdata
  771. *
  772. * Legacy CI_Session compatibility method
  773. *
  774. * @param mixed $data Session data key or an associative array
  775. * @param mixed $value Value to store
  776. * @return void
  777. */
  778. public function set_flashdata($data, $value = NULL)
  779. {
  780. $this->set_userdata($data, $value);
  781. $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
  782. }
  783. // ------------------------------------------------------------------------
  784. /**
  785. * Keep flashdata
  786. *
  787. * Legacy CI_Session compatibility method
  788. *
  789. * @param mixed $key Session data key(s)
  790. * @return void
  791. */
  792. public function keep_flashdata($key)
  793. {
  794. $this->mark_as_flash($key);
  795. }
  796. // ------------------------------------------------------------------------
  797. /**
  798. * Temp data (fetch)
  799. *
  800. * Legacy CI_Session compatibility method
  801. *
  802. * @param string $key Session data key
  803. * @return mixed Session data value or NULL if not found
  804. */
  805. public function tempdata($key = NULL)
  806. {
  807. if (isset($key))
  808. {
  809. return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
  810. ? $_SESSION[$key]
  811. : NULL;
  812. }
  813. $tempdata = array();
  814. if ( ! empty($_SESSION['__ci_vars']))
  815. {
  816. foreach ($_SESSION['__ci_vars'] as $key => &$value)
  817. {
  818. is_int($value) && $tempdata[$key] = $_SESSION[$key];
  819. }
  820. }
  821. return $tempdata;
  822. }
  823. // ------------------------------------------------------------------------
  824. /**
  825. * Set tempdata
  826. *
  827. * Legacy CI_Session compatibility method
  828. *
  829. * @param mixed $data Session data key or an associative array of items
  830. * @param mixed $value Value to store
  831. * @param int $ttl Time-to-live in seconds
  832. * @return void
  833. */
  834. public function set_tempdata($data, $value = NULL, $ttl = 300)
  835. {
  836. $this->set_userdata($data, $value);
  837. $this->mark_as_temp(is_array($data) ? array_keys($data) : $data, $ttl);
  838. }
  839. // ------------------------------------------------------------------------
  840. /**
  841. * Unset tempdata
  842. *
  843. * Legacy CI_Session compatibility method
  844. *
  845. * @param mixed $data Session data key(s)
  846. * @return void
  847. */
  848. public function unset_tempdata($key)
  849. {
  850. $this->unmark_temp($key);
  851. }
  852. }