diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2016-04-15 07:46:19 +0200 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-04-15 07:46:19 +0200 |
commit | e2c36c29032f482dc114bfe3b742d0c94164a808 (patch) | |
tree | 8505e49f3820c07e303ab578119be987492bb01e /lib/private/Session/Internal.php | |
parent | e3b90d7caa80bbcc569cbf042169c1ecbffa9e08 (diff) | |
download | nextcloud-server-e2c36c29032f482dc114bfe3b742d0c94164a808.tar.gz nextcloud-server-e2c36c29032f482dc114bfe3b742d0c94164a808.zip |
Move \OC\Session to PSR-4
Diffstat (limited to 'lib/private/Session/Internal.php')
-rw-r--r-- | lib/private/Session/Internal.php | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php new file mode 100644 index 00000000000..09175bf1f2f --- /dev/null +++ b/lib/private/Session/Internal.php @@ -0,0 +1,138 @@ +<?php +/** + * @author cetra3 <peter@parashift.com.au> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Phil Davis <phil.davis@inf.org> + * @author Robin Appelman <icewind@owncloud.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Session; + +/** + * Class Internal + * + * wrap php's internal session handling into the Session interface + * + * @package OC\Session + */ +class Internal extends Session { + /** + * @param string $name + * @throws \Exception + */ + public function __construct($name) { + session_name($name); + set_error_handler(array($this, 'trapError')); + try { + session_start(); + } catch (\Exception $e) { + setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/'); + } + restore_error_handler(); + if (!isset($_SESSION)) { + throw new \Exception('Failed to start session'); + } + } + + /** + * @param string $key + * @param integer $value + */ + public function set($key, $value) { + $this->validateSession(); + $_SESSION[$key] = $value; + } + + /** + * @param string $key + * @return mixed + */ + public function get($key) { + if (!$this->exists($key)) { + return null; + } + return $_SESSION[$key]; + } + + /** + * @param string $key + * @return bool + */ + public function exists($key) { + return isset($_SESSION[$key]); + } + + /** + * @param string $key + */ + public function remove($key) { + if (isset($_SESSION[$key])) { + unset($_SESSION[$key]); + } + } + + public function clear() { + session_unset(); + $this->regenerateId(); + @session_start(); + $_SESSION = array(); + } + + public function close() { + session_write_close(); + parent::close(); + } + + /** + * Wrapper around session_regenerate_id + * + * @param bool $deleteOldSession Whether to delete the old associated session file or not. + * @return void + */ + public function regenerateId($deleteOldSession = true) { + @session_regenerate_id($deleteOldSession); + } + + /** + * @throws \Exception + */ + public function reopen() { + throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.'); + } + + /** + * @param int $errorNumber + * @param string $errorString + * @throws \ErrorException + */ + public function trapError($errorNumber, $errorString) { + throw new \ErrorException($errorString); + } + + /** + * @throws \Exception + */ + private function validateSession() { + if ($this->sessionClosed) { + throw new \Exception('Session has been closed - no further changes to the session are allowed'); + } + } +} |