internal.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Session;
  9. /**
  10. * Class Internal
  11. *
  12. * wrap php's internal session handling into the Session interface
  13. *
  14. * @package OC\Session
  15. */
  16. class Internal extends Memory {
  17. public function __construct($name) {
  18. session_name($name);
  19. session_start();
  20. if (!isset($_SESSION)) {
  21. throw new \Exception('Failed to start session');
  22. }
  23. $this->data = $_SESSION;
  24. }
  25. public function __destruct() {
  26. $this->close();
  27. }
  28. /**
  29. * @param string $key
  30. */
  31. public function remove($key) {
  32. // also remove it from $_SESSION to prevent re-setting the old value during the merge
  33. if (isset($_SESSION[$key])) {
  34. unset($_SESSION[$key]);
  35. }
  36. parent::remove($key);
  37. }
  38. public function clear() {
  39. session_unset();
  40. @session_regenerate_id(true);
  41. @session_start();
  42. $this->data = $_SESSION = array();
  43. }
  44. public function close() {
  45. $_SESSION = array_merge($_SESSION, $this->data);
  46. session_write_close();
  47. parent::close();
  48. }
  49. public function reopen() {
  50. throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
  51. }
  52. }