Completed
Push — master ( 8ccc2e...5bf447 )
by Morris
16:27
created

Internal::invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author cetra3 <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Phil Davis <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Thomas Müller <[email protected]>
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
29
namespace OC\Session;
30
31
use OCP\Session\Exceptions\SessionNotAvailableException;
32
33
/**
34
 * Class Internal
35
 *
36
 * wrap php's internal session handling into the Session interface
37
 *
38
 * @package OC\Session
39
 */
40
class Internal extends Session {
41
	/**
42
	 * @param string $name
43
	 * @throws \Exception
44
	 */
45
	public function __construct($name) {
46
		set_error_handler(array($this, 'trapError'));
47
		$this->invoke('session_name', [$name]);
48
		try {
49
			$this->invoke('session_start');
50
		} catch (\Exception $e) {
51
			setcookie($this->invoke('session_name'), null, -1, \OC::$WEBROOT ?: '/');
52
		}
53
		restore_error_handler();
54
		if (!isset($_SESSION)) {
55
			throw new \Exception('Failed to start session');
56
		}
57
	}
58
59
	/**
60
	 * @param string $key
61
	 * @param integer $value
62
	 */
63
	public function set($key, $value) {
64
		$this->validateSession();
65
		$_SESSION[$key] = $value;
66
	}
67
68
	/**
69
	 * @param string $key
70
	 * @return mixed
71
	 */
72
	public function get($key) {
73
		if (!$this->exists($key)) {
74
			return null;
75
		}
76
		return $_SESSION[$key];
77
	}
78
79
	/**
80
	 * @param string $key
81
	 * @return bool
82
	 */
83
	public function exists($key) {
84
		return isset($_SESSION[$key]);
85
	}
86
87
	/**
88
	 * @param string $key
89
	 */
90
	public function remove($key) {
91
		if (isset($_SESSION[$key])) {
92
			unset($_SESSION[$key]);
93
		}
94
	}
95
96
	public function clear() {
97
		$this->invoke('session_unset');
98
		$this->regenerateId();
99
		$this->invoke('session_start', [], true);
100
		$_SESSION = [];
101
	}
102
103
	public function close() {
104
		$this->invoke('session_write_close');
105
		parent::close();
106
	}
107
108
	/**
109
	 * Wrapper around session_regenerate_id
110
	 *
111
	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
112
	 * @return void
113
	 */
114
	public function regenerateId($deleteOldSession = true) {
115
		try {
116
			@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...
117
		} catch (\Error $e) {
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
118
			$this->trapError($e->getCode(), $e->getMessage());
119
		}
120
	}
121
122
	/**
123
	 * Wrapper around session_id
124
	 *
125
	 * @return string
126
	 * @throws SessionNotAvailableException
127
	 * @since 9.1.0
128
	 */
129
	public function getId() {
130
		$id = $this->invoke('session_id', [], true);
131
		if ($id === '') {
132
			throw new SessionNotAvailableException();
133
		}
134
		return $id;
135
	}
136
137
	/**
138
	 * @throws \Exception
139
	 */
140
	public function reopen() {
141
		throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
142
	}
143
144
	/**
145
	 * @param int $errorNumber
146
	 * @param string $errorString
147
	 * @throws \ErrorException
148
	 */
149
	public function trapError($errorNumber, $errorString) {
150
		throw new \ErrorException($errorString);
151
	}
152
153
	/**
154
	 * @throws \Exception
155
	 */
156
	private function validateSession() {
157
		if ($this->sessionClosed) {
158
			throw new SessionNotAvailableException('Session has been closed - no further changes to the session are allowed');
159
		}
160
	}
161
162
	/**
163
	 * @param string $functionName the full session_* function name
164
	 * @param array $parameters
165
	 * @param bool $silence whether to suppress warnings
166
	 * @throws \ErrorException via trapError
167
	 * @return mixed
168
	 */
169
	private function invoke($functionName, array $parameters = [], $silence = false) {
170
		try {
171
			if($silence) {
172
				return @call_user_func_array($functionName, $parameters);
173
			} else {
174
				return call_user_func_array($functionName, $parameters);
175
			}
176
		} catch(\Error $e) {
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
177
			$this->trapError($e->getCode(), $e->getMessage());
178
		}
179
	}
180
}
181