Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Internal.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author cetra3 <peter@parashift.com.au>
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Phil Davis <phil.davis@inf.org>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Session;
  29. use OCP\Session\Exceptions\SessionNotAvailableException;
  30. /**
  31. * Class Internal
  32. *
  33. * wrap php's internal session handling into the Session interface
  34. *
  35. * @package OC\Session
  36. */
  37. class Internal extends Session {
  38. /**
  39. * @param string $name
  40. * @throws \Exception
  41. */
  42. public function __construct($name) {
  43. session_name($name);
  44. set_error_handler(array($this, 'trapError'));
  45. try {
  46. session_start();
  47. } catch (\Exception $e) {
  48. setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/');
  49. }
  50. restore_error_handler();
  51. if (!isset($_SESSION)) {
  52. throw new \Exception('Failed to start session');
  53. }
  54. }
  55. /**
  56. * @param string $key
  57. * @param integer $value
  58. */
  59. public function set($key, $value) {
  60. $this->validateSession();
  61. $_SESSION[$key] = $value;
  62. }
  63. /**
  64. * @param string $key
  65. * @return mixed
  66. */
  67. public function get($key) {
  68. if (!$this->exists($key)) {
  69. return null;
  70. }
  71. return $_SESSION[$key];
  72. }
  73. /**
  74. * @param string $key
  75. * @return bool
  76. */
  77. public function exists($key) {
  78. return isset($_SESSION[$key]);
  79. }
  80. /**
  81. * @param string $key
  82. */
  83. public function remove($key) {
  84. if (isset($_SESSION[$key])) {
  85. unset($_SESSION[$key]);
  86. }
  87. }
  88. public function clear() {
  89. session_unset();
  90. $this->regenerateId();
  91. @session_start();
  92. $_SESSION = array();
  93. }
  94. public function close() {
  95. session_write_close();
  96. parent::close();
  97. }
  98. /**
  99. * Wrapper around session_regenerate_id
  100. *
  101. * @param bool $deleteOldSession Whether to delete the old associated session file or not.
  102. * @return void
  103. */
  104. public function regenerateId($deleteOldSession = true) {
  105. @session_regenerate_id($deleteOldSession);
  106. }
  107. /**
  108. * Wrapper around session_id
  109. *
  110. * @return string
  111. * @throws SessionNotAvailableException
  112. * @since 9.1.0
  113. */
  114. public function getId() {
  115. $id = @session_id();
  116. if ($id === '') {
  117. throw new SessionNotAvailableException();
  118. }
  119. return $id;
  120. }
  121. /**
  122. * @throws \Exception
  123. */
  124. public function reopen() {
  125. throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
  126. }
  127. /**
  128. * @param int $errorNumber
  129. * @param string $errorString
  130. * @throws \ErrorException
  131. */
  132. public function trapError($errorNumber, $errorString) {
  133. throw new \ErrorException($errorString);
  134. }
  135. /**
  136. * @throws \Exception
  137. */
  138. private function validateSession() {
  139. if ($this->sessionClosed) {
  140. throw new \Exception('Session has been closed - no further changes to the session are allowed');
  141. }
  142. }
  143. }