Internal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Session;
  9. use OC\Authentication\Token\IProvider;
  10. use OCP\Authentication\Exceptions\InvalidTokenException;
  11. use OCP\Session\Exceptions\SessionNotAvailableException;
  12. /**
  13. * Class Internal
  14. *
  15. * wrap php's internal session handling into the Session interface
  16. *
  17. * @package OC\Session
  18. */
  19. class Internal extends Session {
  20. /**
  21. * @param string $name
  22. * @throws \Exception
  23. */
  24. public function __construct(string $name) {
  25. set_error_handler([$this, 'trapError']);
  26. $this->invoke('session_name', [$name]);
  27. try {
  28. $this->startSession();
  29. } catch (\Exception $e) {
  30. setcookie($this->invoke('session_name'), '', -1, \OC::$WEBROOT ?: '/');
  31. }
  32. restore_error_handler();
  33. if (!isset($_SESSION)) {
  34. throw new \Exception('Failed to start session');
  35. }
  36. }
  37. /**
  38. * @param string $key
  39. * @param integer $value
  40. */
  41. public function set(string $key, $value) {
  42. $reopened = $this->reopen();
  43. $_SESSION[$key] = $value;
  44. if ($reopened) {
  45. $this->close();
  46. }
  47. }
  48. /**
  49. * @param string $key
  50. * @return mixed
  51. */
  52. public function get(string $key) {
  53. if (!$this->exists($key)) {
  54. return null;
  55. }
  56. return $_SESSION[$key];
  57. }
  58. /**
  59. * @param string $key
  60. * @return bool
  61. */
  62. public function exists(string $key): bool {
  63. return isset($_SESSION[$key]);
  64. }
  65. /**
  66. * @param string $key
  67. */
  68. public function remove(string $key) {
  69. if (isset($_SESSION[$key])) {
  70. unset($_SESSION[$key]);
  71. }
  72. }
  73. public function clear() {
  74. $this->reopen();
  75. $this->invoke('session_unset');
  76. $this->regenerateId();
  77. $this->invoke('session_write_close');
  78. $this->startSession(true);
  79. $_SESSION = [];
  80. }
  81. public function close() {
  82. $this->invoke('session_write_close');
  83. parent::close();
  84. }
  85. /**
  86. * Wrapper around session_regenerate_id
  87. *
  88. * @param bool $deleteOldSession Whether to delete the old associated session file or not.
  89. * @param bool $updateToken Whether to update the associated auth token
  90. * @return void
  91. */
  92. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  93. $this->reopen();
  94. $oldId = null;
  95. if ($updateToken) {
  96. // Get the old id to update the token
  97. try {
  98. $oldId = $this->getId();
  99. } catch (SessionNotAvailableException $e) {
  100. // We can't update a token if there is no previous id
  101. $updateToken = false;
  102. }
  103. }
  104. try {
  105. @session_regenerate_id($deleteOldSession);
  106. } catch (\Error $e) {
  107. $this->trapError($e->getCode(), $e->getMessage());
  108. }
  109. if ($updateToken) {
  110. // Get the new id to update the token
  111. $newId = $this->getId();
  112. /** @var IProvider $tokenProvider */
  113. $tokenProvider = \OCP\Server::get(IProvider::class);
  114. try {
  115. $tokenProvider->renewSessionToken($oldId, $newId);
  116. } catch (InvalidTokenException $e) {
  117. // Just ignore
  118. }
  119. }
  120. }
  121. /**
  122. * Wrapper around session_id
  123. *
  124. * @return string
  125. * @throws SessionNotAvailableException
  126. * @since 9.1.0
  127. */
  128. public function getId(): string {
  129. $id = $this->invoke('session_id', [], true);
  130. if ($id === '') {
  131. throw new SessionNotAvailableException();
  132. }
  133. return $id;
  134. }
  135. /**
  136. * @throws \Exception
  137. */
  138. public function reopen(): bool {
  139. if ($this->sessionClosed) {
  140. $this->startSession(false, false);
  141. $this->sessionClosed = false;
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * @param int $errorNumber
  148. * @param string $errorString
  149. * @throws \ErrorException
  150. */
  151. public function trapError(int $errorNumber, string $errorString) {
  152. if ($errorNumber & E_ERROR) {
  153. throw new \ErrorException($errorString);
  154. }
  155. }
  156. /**
  157. * @param string $functionName the full session_* function name
  158. * @param array $parameters
  159. * @param bool $silence whether to suppress warnings
  160. * @throws \ErrorException via trapError
  161. * @return mixed
  162. */
  163. private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
  164. try {
  165. if ($silence) {
  166. return @call_user_func_array($functionName, $parameters);
  167. } else {
  168. return call_user_func_array($functionName, $parameters);
  169. }
  170. } catch (\Error $e) {
  171. $this->trapError($e->getCode(), $e->getMessage());
  172. }
  173. }
  174. private function startSession(bool $silence = false, bool $readAndClose = true) {
  175. $sessionParams = ['cookie_samesite' => 'Lax'];
  176. if (\OC::hasSessionRelaxedExpiry()) {
  177. $sessionParams['read_and_close'] = $readAndClose;
  178. }
  179. $this->invoke('session_start', [$sessionParams], $silence);
  180. }
  181. }