Internal.php 4.4 KB

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