internal.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @author cetra3 <peter@parashift.com.au>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Session;
  25. /**
  26. * Class Internal
  27. *
  28. * wrap php's internal session handling into the Session interface
  29. *
  30. * @package OC\Session
  31. */
  32. class Internal extends Session {
  33. public function __construct($name) {
  34. session_name($name);
  35. set_error_handler(array($this, 'trapError'));
  36. session_start();
  37. restore_error_handler();
  38. if (!isset($_SESSION)) {
  39. throw new \Exception('Failed to start session');
  40. }
  41. }
  42. public function __destruct() {
  43. $this->close();
  44. }
  45. /**
  46. * @param string $key
  47. * @param integer $value
  48. */
  49. public function set($key, $value) {
  50. $this->validateSession();
  51. $_SESSION[$key] = $value;
  52. }
  53. /**
  54. * @param string $key
  55. * @return mixed
  56. */
  57. public function get($key) {
  58. if (!$this->exists($key)) {
  59. return null;
  60. }
  61. return $_SESSION[$key];
  62. }
  63. /**
  64. * @param string $key
  65. * @return bool
  66. */
  67. public function exists($key) {
  68. return isset($_SESSION[$key]);
  69. }
  70. /**
  71. * @param string $key
  72. */
  73. public function remove($key) {
  74. if (isset($_SESSION[$key])) {
  75. unset($_SESSION[$key]);
  76. }
  77. }
  78. public function clear() {
  79. session_unset();
  80. @session_regenerate_id(true);
  81. @session_start();
  82. $_SESSION = array();
  83. }
  84. public function close() {
  85. session_write_close();
  86. parent::close();
  87. }
  88. public function reopen() {
  89. throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
  90. }
  91. public function trapError($errorNumber, $errorString) {
  92. throw new \ErrorException($errorString);
  93. }
  94. private function validateSession() {
  95. if ($this->sessionClosed) {
  96. throw new \Exception('Session has been closed - no further changes to the session as allowed');
  97. }
  98. }
  99. }