Completed
Push — master ( fd844f...b84825 )
by Roeland
10:13
created

Internal::getId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/**
3
 * @author cetra3 <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Phil Davis <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud, Inc.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Session;
28
29
use OCP\Session\Exceptions\SessionNotAvailableException;
30
31
/**
32
 * Class Internal
33
 *
34
 * wrap php's internal session handling into the Session interface
35
 *
36
 * @package OC\Session
37
 */
38
class Internal extends Session {
39
	/**
40
	 * @param string $name
41
	 * @throws \Exception
42
	 */
43
	public function __construct($name) {
44
		session_name($name);
45
		set_error_handler(array($this, 'trapError'));
46
		try {
47
			session_start();
48
		} catch (\Exception $e) {
49
			setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/');
50
		}
51
		restore_error_handler();
52
		if (!isset($_SESSION)) {
53
			throw new \Exception('Failed to start session');
54
		}
55
	}
56
57
	/**
58
	 * @param string $key
59
	 * @param integer $value
60
	 */
61
	public function set($key, $value) {
62
		$this->validateSession();
63
		$_SESSION[$key] = $value;
64
	}
65
66
	/**
67
	 * @param string $key
68
	 * @return mixed
69
	 */
70
	public function get($key) {
71
		if (!$this->exists($key)) {
72
			return null;
73
		}
74
		return $_SESSION[$key];
75
	}
76
77
	/**
78
	 * @param string $key
79
	 * @return bool
80
	 */
81
	public function exists($key) {
82
		return isset($_SESSION[$key]);
83
	}
84
85
	/**
86
	 * @param string $key
87
	 */
88
	public function remove($key) {
89
		if (isset($_SESSION[$key])) {
90
			unset($_SESSION[$key]);
91
		}
92
	}
93
94
	public function clear() {
95
		session_unset();
96
		$this->regenerateId();
97
		@session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
98
		$_SESSION = array();
99
	}
100
101
	public function close() {
102
		session_write_close();
103
		parent::close();
104
	}
105
106
	/**
107
	 * Wrapper around session_regenerate_id
108
	 *
109
	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
110
	 * @return void
111
	 */
112
	public function regenerateId($deleteOldSession = true) {
113
		@session_regenerate_id($deleteOldSession);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
114
	}
115
116
	/**
117
	 * Wrapper around session_id
118
	 *
119
	 * @return string
120
	 * @throws SessionNotAvailableException
121
	 * @since 9.1.0
122
	 */
123
	public function getId() {
124
		$id = @session_id();
125
		if ($id === '') {
126
			throw new SessionNotAvailableException();
127
		}
128
		return $id;
129
	}
130
131
	/**
132
	 * @throws \Exception
133
	 */
134
	public function reopen() {
135
		throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
136
	}
137
138
	/**
139
	 * @param int $errorNumber
140
	 * @param string $errorString
141
	 * @throws \ErrorException
142
	 */
143
	public function trapError($errorNumber, $errorString) {
144
		throw new \ErrorException($errorString);
145
	}
146
147
	/**
148
	 * @throws \Exception
149
	 */
150
	private function validateSession() {
151
		if ($this->sessionClosed) {
152
			throw new \Exception('Session has been closed - no further changes to the session are allowed');
153
		}
154
	}
155
}
156