Completed
Push — master ( d0a636...8d8b22 )
by Morris
27:59 queued 11:19
created
lib/public/ISession.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
Please login to merge, or discard this patch.
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -45,68 +45,68 @@
 block discarded – undo
45 45
  */
46 46
 interface ISession {
47 47
 
48
-	/**
49
-	 * Set a value in the session
50
-	 *
51
-	 * @param string $key
52
-	 * @param mixed $value
53
-	 * @since 6.0.0
54
-	 */
55
-	public function set(string $key, $value);
48
+    /**
49
+     * Set a value in the session
50
+     *
51
+     * @param string $key
52
+     * @param mixed $value
53
+     * @since 6.0.0
54
+     */
55
+    public function set(string $key, $value);
56 56
 
57
-	/**
58
-	 * Get a value from the session
59
-	 *
60
-	 * @param string $key
61
-	 * @return mixed should return null if $key does not exist
62
-	 * @since 6.0.0
63
-	 */
64
-	public function get(string $key);
57
+    /**
58
+     * Get a value from the session
59
+     *
60
+     * @param string $key
61
+     * @return mixed should return null if $key does not exist
62
+     * @since 6.0.0
63
+     */
64
+    public function get(string $key);
65 65
 
66
-	/**
67
-	 * Check if a named key exists in the session
68
-	 *
69
-	 * @param string $key
70
-	 * @return bool
71
-	 * @since 6.0.0
72
-	 */
73
-	public function exists(string $key): bool;
66
+    /**
67
+     * Check if a named key exists in the session
68
+     *
69
+     * @param string $key
70
+     * @return bool
71
+     * @since 6.0.0
72
+     */
73
+    public function exists(string $key): bool;
74 74
 
75
-	/**
76
-	 * Remove a $key/$value pair from the session
77
-	 *
78
-	 * @param string $key
79
-	 * @since 6.0.0
80
-	 */
81
-	public function remove(string $key);
75
+    /**
76
+     * Remove a $key/$value pair from the session
77
+     *
78
+     * @param string $key
79
+     * @since 6.0.0
80
+     */
81
+    public function remove(string $key);
82 82
 
83
-	/**
84
-	 * Reset and recreate the session
85
-	 * @since 6.0.0
86
-	 */
87
-	public function clear();
83
+    /**
84
+     * Reset and recreate the session
85
+     * @since 6.0.0
86
+     */
87
+    public function clear();
88 88
 
89
-	/**
90
-	 * Close the session and release the lock
91
-	 * @since 7.0.0
92
-	 */
93
-	public function close();
89
+    /**
90
+     * Close the session and release the lock
91
+     * @since 7.0.0
92
+     */
93
+    public function close();
94 94
 
95
-	/**
96
-	 * Wrapper around session_regenerate_id
97
-	 *
98
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
99
-	 * @return void
100
-	 * @since 9.0.0
101
-	 */
102
-	public function regenerateId(bool $deleteOldSession = true);
95
+    /**
96
+     * Wrapper around session_regenerate_id
97
+     *
98
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
99
+     * @return void
100
+     * @since 9.0.0
101
+     */
102
+    public function regenerateId(bool $deleteOldSession = true);
103 103
 
104
-	/**
105
-	 * Wrapper around session_id
106
-	 *
107
-	 * @return string
108
-	 * @throws SessionNotAvailableException
109
-	 * @since 9.1.0
110
-	 */
111
-	public function getId(): string;
104
+    /**
105
+     * Wrapper around session_id
106
+     *
107
+     * @return string
108
+     * @throws SessionNotAvailableException
109
+     * @since 9.1.0
110
+     */
111
+    public function getId(): string;
112 112
 }
Please login to merge, or discard this patch.
lib/private/Session/Session.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
Please login to merge, or discard this patch.
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -29,53 +29,53 @@
 block discarded – undo
29 29
 
30 30
 abstract class Session implements \ArrayAccess, ISession {
31 31
 
32
-	/**
33
-	 * @var bool
34
-	 */
35
-	protected $sessionClosed = false;
32
+    /**
33
+     * @var bool
34
+     */
35
+    protected $sessionClosed = false;
36 36
 
37
-	/**
38
-	 * $name serves as a namespace for the session keys
39
-	 *
40
-	 * @param string $name
41
-	 */
42
-	abstract public function __construct(string $name);
37
+    /**
38
+     * $name serves as a namespace for the session keys
39
+     *
40
+     * @param string $name
41
+     */
42
+    abstract public function __construct(string $name);
43 43
 
44
-	/**
45
-	 * @param mixed $offset
46
-	 * @return bool
47
-	 */
48
-	public function offsetExists($offset): bool {
49
-		return $this->exists($offset);
50
-	}
44
+    /**
45
+     * @param mixed $offset
46
+     * @return bool
47
+     */
48
+    public function offsetExists($offset): bool {
49
+        return $this->exists($offset);
50
+    }
51 51
 
52
-	/**
53
-	 * @param mixed $offset
54
-	 * @return mixed
55
-	 */
56
-	public function offsetGet($offset) {
57
-		return $this->get($offset);
58
-	}
52
+    /**
53
+     * @param mixed $offset
54
+     * @return mixed
55
+     */
56
+    public function offsetGet($offset) {
57
+        return $this->get($offset);
58
+    }
59 59
 
60
-	/**
61
-	 * @param mixed $offset
62
-	 * @param mixed $value
63
-	 */
64
-	public function offsetSet($offset, $value) {
65
-		$this->set($offset, $value);
66
-	}
60
+    /**
61
+     * @param mixed $offset
62
+     * @param mixed $value
63
+     */
64
+    public function offsetSet($offset, $value) {
65
+        $this->set($offset, $value);
66
+    }
67 67
 
68
-	/**
69
-	 * @param mixed $offset
70
-	 */
71
-	public function offsetUnset($offset) {
72
-		$this->remove($offset);
73
-	}
68
+    /**
69
+     * @param mixed $offset
70
+     */
71
+    public function offsetUnset($offset) {
72
+        $this->remove($offset);
73
+    }
74 74
 
75
-	/**
76
-	 * Close the session and release the lock
77
-	 */
78
-	public function close() {
79
-		$this->sessionClosed = true;
80
-	}
75
+    /**
76
+     * Close the session and release the lock
77
+     */
78
+    public function close() {
79
+        $this->sessionClosed = true;
80
+    }
81 81
 }
Please login to merge, or discard this patch.
lib/private/Session/Memory.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
Please login to merge, or discard this patch.
Indentation   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -39,86 +39,86 @@
 block discarded – undo
39 39
  * @package OC\Session
40 40
  */
41 41
 class Memory extends Session {
42
-	protected $data;
42
+    protected $data;
43 43
 
44
-	public function __construct(string $name) {
45
-		//no need to use $name since all data is already scoped to this instance
46
-		$this->data = [];
47
-	}
44
+    public function __construct(string $name) {
45
+        //no need to use $name since all data is already scoped to this instance
46
+        $this->data = [];
47
+    }
48 48
 
49
-	/**
50
-	 * @param string $key
51
-	 * @param integer $value
52
-	 */
53
-	public function set(string $key, $value) {
54
-		$this->validateSession();
55
-		$this->data[$key] = $value;
56
-	}
49
+    /**
50
+     * @param string $key
51
+     * @param integer $value
52
+     */
53
+    public function set(string $key, $value) {
54
+        $this->validateSession();
55
+        $this->data[$key] = $value;
56
+    }
57 57
 
58
-	/**
59
-	 * @param string $key
60
-	 * @return mixed
61
-	 */
62
-	public function get(string $key) {
63
-		if (!$this->exists($key)) {
64
-			return null;
65
-		}
66
-		return $this->data[$key];
67
-	}
58
+    /**
59
+     * @param string $key
60
+     * @return mixed
61
+     */
62
+    public function get(string $key) {
63
+        if (!$this->exists($key)) {
64
+            return null;
65
+        }
66
+        return $this->data[$key];
67
+    }
68 68
 
69
-	/**
70
-	 * @param string $key
71
-	 * @return bool
72
-	 */
73
-	public function exists(string $key): bool {
74
-		return isset($this->data[$key]);
75
-	}
69
+    /**
70
+     * @param string $key
71
+     * @return bool
72
+     */
73
+    public function exists(string $key): bool {
74
+        return isset($this->data[$key]);
75
+    }
76 76
 
77
-	/**
78
-	 * @param string $key
79
-	 */
80
-	public function remove(string $key) {
81
-		$this->validateSession();
82
-		unset($this->data[$key]);
83
-	}
77
+    /**
78
+     * @param string $key
79
+     */
80
+    public function remove(string $key) {
81
+        $this->validateSession();
82
+        unset($this->data[$key]);
83
+    }
84 84
 
85
-	public function clear() {
86
-		$this->data = [];
87
-	}
85
+    public function clear() {
86
+        $this->data = [];
87
+    }
88 88
 
89
-	/**
90
-	 * Stub since the session ID does not need to get regenerated for the cache
91
-	 *
92
-	 * @param bool $deleteOldSession
93
-	 */
94
-	public function regenerateId(bool $deleteOldSession = true) {}
89
+    /**
90
+     * Stub since the session ID does not need to get regenerated for the cache
91
+     *
92
+     * @param bool $deleteOldSession
93
+     */
94
+    public function regenerateId(bool $deleteOldSession = true) {}
95 95
 
96
-	/**
97
-	 * Wrapper around session_id
98
-	 *
99
-	 * @return string
100
-	 * @throws SessionNotAvailableException
101
-	 * @since 9.1.0
102
-	 */
103
-	public function getId(): string {
104
-		throw new SessionNotAvailableException('Memory session does not have an ID');
105
-	}
96
+    /**
97
+     * Wrapper around session_id
98
+     *
99
+     * @return string
100
+     * @throws SessionNotAvailableException
101
+     * @since 9.1.0
102
+     */
103
+    public function getId(): string {
104
+        throw new SessionNotAvailableException('Memory session does not have an ID');
105
+    }
106 106
 
107
-	/**
108
-	 * Helper function for PHPUnit execution - don't use in non-test code
109
-	 */
110
-	public function reopen() {
111
-		$this->sessionClosed = false;
112
-	}
107
+    /**
108
+     * Helper function for PHPUnit execution - don't use in non-test code
109
+     */
110
+    public function reopen() {
111
+        $this->sessionClosed = false;
112
+    }
113 113
 
114
-	/**
115
-	 * In case the session has already been locked an exception will be thrown
116
-	 *
117
-	 * @throws Exception
118
-	 */
119
-	private function validateSession() {
120
-		if ($this->sessionClosed) {
121
-			throw new Exception('Session has been closed - no further changes to the session are allowed');
122
-		}
123
-	}
114
+    /**
115
+     * In case the session has already been locked an exception will be thrown
116
+     *
117
+     * @throws Exception
118
+     */
119
+    private function validateSession() {
120
+        if ($this->sessionClosed) {
121
+            throw new Exception('Session has been closed - no further changes to the session are allowed');
122
+        }
123
+    }
124 124
 }
Please login to merge, or discard this patch.
lib/private/Session/Internal.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -166,7 +166,7 @@
 block discarded – undo
166 166
 	 * @param array $parameters
167 167
 	 * @param bool $silence whether to suppress warnings
168 168
 	 * @throws \ErrorException via trapError
169
-	 * @return mixed
169
+	 * @return string
170 170
 	 */
171 171
 	private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
172 172
 		try {
Please login to merge, or discard this patch.
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -40,143 +40,143 @@
 block discarded – undo
40 40
  * @package OC\Session
41 41
  */
42 42
 class Internal extends Session {
43
-	/**
44
-	 * @param string $name
45
-	 * @throws \Exception
46
-	 */
47
-	public function __construct(string $name) {
48
-		set_error_handler([$this, 'trapError']);
49
-		$this->invoke('session_name', [$name]);
50
-		try {
51
-			$this->invoke('session_start');
52
-		} catch (\Exception $e) {
53
-			setcookie($this->invoke('session_name'), null, -1, \OC::$WEBROOT ?: '/');
54
-		}
55
-		restore_error_handler();
56
-		if (!isset($_SESSION)) {
57
-			throw new \Exception('Failed to start session');
58
-		}
59
-	}
43
+    /**
44
+     * @param string $name
45
+     * @throws \Exception
46
+     */
47
+    public function __construct(string $name) {
48
+        set_error_handler([$this, 'trapError']);
49
+        $this->invoke('session_name', [$name]);
50
+        try {
51
+            $this->invoke('session_start');
52
+        } catch (\Exception $e) {
53
+            setcookie($this->invoke('session_name'), null, -1, \OC::$WEBROOT ?: '/');
54
+        }
55
+        restore_error_handler();
56
+        if (!isset($_SESSION)) {
57
+            throw new \Exception('Failed to start session');
58
+        }
59
+    }
60 60
 
61
-	/**
62
-	 * @param string $key
63
-	 * @param integer $value
64
-	 */
65
-	public function set(string $key, $value) {
66
-		$this->validateSession();
67
-		$_SESSION[$key] = $value;
68
-	}
61
+    /**
62
+     * @param string $key
63
+     * @param integer $value
64
+     */
65
+    public function set(string $key, $value) {
66
+        $this->validateSession();
67
+        $_SESSION[$key] = $value;
68
+    }
69 69
 
70
-	/**
71
-	 * @param string $key
72
-	 * @return mixed
73
-	 */
74
-	public function get(string $key) {
75
-		if (!$this->exists($key)) {
76
-			return null;
77
-		}
78
-		return $_SESSION[$key];
79
-	}
70
+    /**
71
+     * @param string $key
72
+     * @return mixed
73
+     */
74
+    public function get(string $key) {
75
+        if (!$this->exists($key)) {
76
+            return null;
77
+        }
78
+        return $_SESSION[$key];
79
+    }
80 80
 
81
-	/**
82
-	 * @param string $key
83
-	 * @return bool
84
-	 */
85
-	public function exists(string $key): bool {
86
-		return isset($_SESSION[$key]);
87
-	}
81
+    /**
82
+     * @param string $key
83
+     * @return bool
84
+     */
85
+    public function exists(string $key): bool {
86
+        return isset($_SESSION[$key]);
87
+    }
88 88
 
89
-	/**
90
-	 * @param string $key
91
-	 */
92
-	public function remove(string $key) {
93
-		if (isset($_SESSION[$key])) {
94
-			unset($_SESSION[$key]);
95
-		}
96
-	}
89
+    /**
90
+     * @param string $key
91
+     */
92
+    public function remove(string $key) {
93
+        if (isset($_SESSION[$key])) {
94
+            unset($_SESSION[$key]);
95
+        }
96
+    }
97 97
 
98
-	public function clear() {
99
-		$this->invoke('session_unset');
100
-		$this->regenerateId();
101
-		$this->invoke('session_start', [], true);
102
-		$_SESSION = [];
103
-	}
98
+    public function clear() {
99
+        $this->invoke('session_unset');
100
+        $this->regenerateId();
101
+        $this->invoke('session_start', [], true);
102
+        $_SESSION = [];
103
+    }
104 104
 
105
-	public function close() {
106
-		$this->invoke('session_write_close');
107
-		parent::close();
108
-	}
105
+    public function close() {
106
+        $this->invoke('session_write_close');
107
+        parent::close();
108
+    }
109 109
 
110
-	/**
111
-	 * Wrapper around session_regenerate_id
112
-	 *
113
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
114
-	 * @return void
115
-	 */
116
-	public function regenerateId(bool $deleteOldSession = true) {
117
-		try {
118
-			@session_regenerate_id($deleteOldSession);
119
-		} catch (\Error $e) {
120
-			$this->trapError($e->getCode(), $e->getMessage());
121
-		}
122
-	}
110
+    /**
111
+     * Wrapper around session_regenerate_id
112
+     *
113
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
114
+     * @return void
115
+     */
116
+    public function regenerateId(bool $deleteOldSession = true) {
117
+        try {
118
+            @session_regenerate_id($deleteOldSession);
119
+        } catch (\Error $e) {
120
+            $this->trapError($e->getCode(), $e->getMessage());
121
+        }
122
+    }
123 123
 
124
-	/**
125
-	 * Wrapper around session_id
126
-	 *
127
-	 * @return string
128
-	 * @throws SessionNotAvailableException
129
-	 * @since 9.1.0
130
-	 */
131
-	public function getId(): string {
132
-		$id = $this->invoke('session_id', [], true);
133
-		if ($id === '') {
134
-			throw new SessionNotAvailableException();
135
-		}
136
-		return $id;
137
-	}
124
+    /**
125
+     * Wrapper around session_id
126
+     *
127
+     * @return string
128
+     * @throws SessionNotAvailableException
129
+     * @since 9.1.0
130
+     */
131
+    public function getId(): string {
132
+        $id = $this->invoke('session_id', [], true);
133
+        if ($id === '') {
134
+            throw new SessionNotAvailableException();
135
+        }
136
+        return $id;
137
+    }
138 138
 
139
-	/**
140
-	 * @throws \Exception
141
-	 */
142
-	public function reopen() {
143
-		throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
144
-	}
139
+    /**
140
+     * @throws \Exception
141
+     */
142
+    public function reopen() {
143
+        throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
144
+    }
145 145
 
146
-	/**
147
-	 * @param int $errorNumber
148
-	 * @param string $errorString
149
-	 * @throws \ErrorException
150
-	 */
151
-	public function trapError(int $errorNumber, string $errorString) {
152
-		throw new \ErrorException($errorString);
153
-	}
146
+    /**
147
+     * @param int $errorNumber
148
+     * @param string $errorString
149
+     * @throws \ErrorException
150
+     */
151
+    public function trapError(int $errorNumber, string $errorString) {
152
+        throw new \ErrorException($errorString);
153
+    }
154 154
 
155
-	/**
156
-	 * @throws \Exception
157
-	 */
158
-	private function validateSession() {
159
-		if ($this->sessionClosed) {
160
-			throw new SessionNotAvailableException('Session has been closed - no further changes to the session are allowed');
161
-		}
162
-	}
155
+    /**
156
+     * @throws \Exception
157
+     */
158
+    private function validateSession() {
159
+        if ($this->sessionClosed) {
160
+            throw new SessionNotAvailableException('Session has been closed - no further changes to the session are allowed');
161
+        }
162
+    }
163 163
 
164
-	/**
165
-	 * @param string $functionName the full session_* function name
166
-	 * @param array $parameters
167
-	 * @param bool $silence whether to suppress warnings
168
-	 * @throws \ErrorException via trapError
169
-	 * @return mixed
170
-	 */
171
-	private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
172
-		try {
173
-			if($silence) {
174
-				return @call_user_func_array($functionName, $parameters);
175
-			} else {
176
-				return call_user_func_array($functionName, $parameters);
177
-			}
178
-		} catch(\Error $e) {
179
-			$this->trapError($e->getCode(), $e->getMessage());
180
-		}
181
-	}
164
+    /**
165
+     * @param string $functionName the full session_* function name
166
+     * @param array $parameters
167
+     * @param bool $silence whether to suppress warnings
168
+     * @throws \ErrorException via trapError
169
+     * @return mixed
170
+     */
171
+    private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
172
+        try {
173
+            if($silence) {
174
+                return @call_user_func_array($functionName, $parameters);
175
+            } else {
176
+                return call_user_func_array($functionName, $parameters);
177
+            }
178
+        } catch(\Error $e) {
179
+            $this->trapError($e->getCode(), $e->getMessage());
180
+        }
181
+    }
182 182
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
@@ -170,12 +170,12 @@  discard block
 block discarded – undo
170 170
 	 */
171 171
 	private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
172 172
 		try {
173
-			if($silence) {
173
+			if ($silence) {
174 174
 				return @call_user_func_array($functionName, $parameters);
175 175
 			} else {
176 176
 				return call_user_func_array($functionName, $parameters);
177 177
 			}
178
-		} catch(\Error $e) {
178
+		} catch (\Error $e) {
179 179
 			$this->trapError($e->getCode(), $e->getMessage());
180 180
 		}
181 181
 	}
Please login to merge, or discard this patch.
lib/private/Session/CryptoSessionData.php 2 patches
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -37,176 +37,176 @@
 block discarded – undo
37 37
  * @package OC\Session
38 38
  */
39 39
 class CryptoSessionData implements \ArrayAccess, ISession {
40
-	/** @var ISession */
41
-	protected $session;
42
-	/** @var \OCP\Security\ICrypto */
43
-	protected $crypto;
44
-	/** @var string */
45
-	protected $passphrase;
46
-	/** @var array */
47
-	protected $sessionValues;
48
-	/** @var bool */
49
-	protected $isModified = false;
50
-	CONST encryptedSessionName = 'encrypted_session_data';
51
-
52
-	/**
53
-	 * @param ISession $session
54
-	 * @param ICrypto $crypto
55
-	 * @param string $passphrase
56
-	 */
57
-	public function __construct(ISession $session,
58
-								ICrypto $crypto,
59
-								string $passphrase) {
60
-		$this->crypto = $crypto;
61
-		$this->session = $session;
62
-		$this->passphrase = $passphrase;
63
-		$this->initializeSession();
64
-	}
65
-
66
-	/**
67
-	 * Close session if class gets destructed
68
-	 */
69
-	public function __destruct() {
70
-		try {
71
-			$this->close();
72
-		} catch (SessionNotAvailableException $e){
73
-			// This exception can occur if session is already closed
74
-			// So it is safe to ignore it and let the garbage collector to proceed
75
-		}
76
-	}
77
-
78
-	protected function initializeSession() {
79
-		$encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
80
-		try {
81
-			$this->sessionValues = json_decode(
82
-				$this->crypto->decrypt($encryptedSessionData, $this->passphrase),
83
-				true
84
-			);
85
-		} catch (\Exception $e) {
86
-			$this->sessionValues = [];
87
-		}
88
-	}
89
-
90
-	/**
91
-	 * Set a value in the session
92
-	 *
93
-	 * @param string $key
94
-	 * @param mixed $value
95
-	 */
96
-	public function set(string $key, $value) {
97
-		$this->sessionValues[$key] = $value;
98
-		$this->isModified = true;
99
-	}
100
-
101
-	/**
102
-	 * Get a value from the session
103
-	 *
104
-	 * @param string $key
105
-	 * @return string|null Either the value or null
106
-	 */
107
-	public function get(string $key) {
108
-		if(isset($this->sessionValues[$key])) {
109
-			return $this->sessionValues[$key];
110
-		}
111
-
112
-		return null;
113
-	}
114
-
115
-	/**
116
-	 * Check if a named key exists in the session
117
-	 *
118
-	 * @param string $key
119
-	 * @return bool
120
-	 */
121
-	public function exists(string $key): bool {
122
-		return isset($this->sessionValues[$key]);
123
-	}
124
-
125
-	/**
126
-	 * Remove a $key/$value pair from the session
127
-	 *
128
-	 * @param string $key
129
-	 */
130
-	public function remove(string $key) {
131
-		$this->isModified = true;
132
-		unset($this->sessionValues[$key]);
133
-		$this->session->remove(self::encryptedSessionName);
134
-	}
135
-
136
-	/**
137
-	 * Reset and recreate the session
138
-	 */
139
-	public function clear() {
140
-		$requesttoken = $this->get('requesttoken');
141
-		$this->sessionValues = [];
142
-		if ($requesttoken !== null) {
143
-			$this->set('requesttoken', $requesttoken);
144
-		}
145
-		$this->isModified = true;
146
-		$this->session->clear();
147
-	}
148
-
149
-	/**
150
-	 * Wrapper around session_regenerate_id
151
-	 *
152
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
153
-	 * @return void
154
-	 */
155
-	public function regenerateId(bool $deleteOldSession = true) {
156
-		$this->session->regenerateId($deleteOldSession);
157
-	}
158
-
159
-	/**
160
-	 * Wrapper around session_id
161
-	 *
162
-	 * @return string
163
-	 * @throws SessionNotAvailableException
164
-	 * @since 9.1.0
165
-	 */
166
-	public function getId(): string {
167
-		return $this->session->getId();
168
-	}
169
-
170
-	/**
171
-	 * Close the session and release the lock, also writes all changed data in batch
172
-	 */
173
-	public function close() {
174
-		if($this->isModified) {
175
-			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
176
-			$this->session->set(self::encryptedSessionName, $encryptedValue);
177
-			$this->isModified = false;
178
-		}
179
-		$this->session->close();
180
-	}
181
-
182
-	/**
183
-	 * @param mixed $offset
184
-	 * @return bool
185
-	 */
186
-	public function offsetExists($offset): bool {
187
-		return $this->exists($offset);
188
-	}
189
-
190
-	/**
191
-	 * @param mixed $offset
192
-	 * @return mixed
193
-	 */
194
-	public function offsetGet($offset) {
195
-		return $this->get($offset);
196
-	}
197
-
198
-	/**
199
-	 * @param mixed $offset
200
-	 * @param mixed $value
201
-	 */
202
-	public function offsetSet($offset, $value) {
203
-		$this->set($offset, $value);
204
-	}
205
-
206
-	/**
207
-	 * @param mixed $offset
208
-	 */
209
-	public function offsetUnset($offset) {
210
-		$this->remove($offset);
211
-	}
40
+    /** @var ISession */
41
+    protected $session;
42
+    /** @var \OCP\Security\ICrypto */
43
+    protected $crypto;
44
+    /** @var string */
45
+    protected $passphrase;
46
+    /** @var array */
47
+    protected $sessionValues;
48
+    /** @var bool */
49
+    protected $isModified = false;
50
+    CONST encryptedSessionName = 'encrypted_session_data';
51
+
52
+    /**
53
+     * @param ISession $session
54
+     * @param ICrypto $crypto
55
+     * @param string $passphrase
56
+     */
57
+    public function __construct(ISession $session,
58
+                                ICrypto $crypto,
59
+                                string $passphrase) {
60
+        $this->crypto = $crypto;
61
+        $this->session = $session;
62
+        $this->passphrase = $passphrase;
63
+        $this->initializeSession();
64
+    }
65
+
66
+    /**
67
+     * Close session if class gets destructed
68
+     */
69
+    public function __destruct() {
70
+        try {
71
+            $this->close();
72
+        } catch (SessionNotAvailableException $e){
73
+            // This exception can occur if session is already closed
74
+            // So it is safe to ignore it and let the garbage collector to proceed
75
+        }
76
+    }
77
+
78
+    protected function initializeSession() {
79
+        $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
80
+        try {
81
+            $this->sessionValues = json_decode(
82
+                $this->crypto->decrypt($encryptedSessionData, $this->passphrase),
83
+                true
84
+            );
85
+        } catch (\Exception $e) {
86
+            $this->sessionValues = [];
87
+        }
88
+    }
89
+
90
+    /**
91
+     * Set a value in the session
92
+     *
93
+     * @param string $key
94
+     * @param mixed $value
95
+     */
96
+    public function set(string $key, $value) {
97
+        $this->sessionValues[$key] = $value;
98
+        $this->isModified = true;
99
+    }
100
+
101
+    /**
102
+     * Get a value from the session
103
+     *
104
+     * @param string $key
105
+     * @return string|null Either the value or null
106
+     */
107
+    public function get(string $key) {
108
+        if(isset($this->sessionValues[$key])) {
109
+            return $this->sessionValues[$key];
110
+        }
111
+
112
+        return null;
113
+    }
114
+
115
+    /**
116
+     * Check if a named key exists in the session
117
+     *
118
+     * @param string $key
119
+     * @return bool
120
+     */
121
+    public function exists(string $key): bool {
122
+        return isset($this->sessionValues[$key]);
123
+    }
124
+
125
+    /**
126
+     * Remove a $key/$value pair from the session
127
+     *
128
+     * @param string $key
129
+     */
130
+    public function remove(string $key) {
131
+        $this->isModified = true;
132
+        unset($this->sessionValues[$key]);
133
+        $this->session->remove(self::encryptedSessionName);
134
+    }
135
+
136
+    /**
137
+     * Reset and recreate the session
138
+     */
139
+    public function clear() {
140
+        $requesttoken = $this->get('requesttoken');
141
+        $this->sessionValues = [];
142
+        if ($requesttoken !== null) {
143
+            $this->set('requesttoken', $requesttoken);
144
+        }
145
+        $this->isModified = true;
146
+        $this->session->clear();
147
+    }
148
+
149
+    /**
150
+     * Wrapper around session_regenerate_id
151
+     *
152
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
153
+     * @return void
154
+     */
155
+    public function regenerateId(bool $deleteOldSession = true) {
156
+        $this->session->regenerateId($deleteOldSession);
157
+    }
158
+
159
+    /**
160
+     * Wrapper around session_id
161
+     *
162
+     * @return string
163
+     * @throws SessionNotAvailableException
164
+     * @since 9.1.0
165
+     */
166
+    public function getId(): string {
167
+        return $this->session->getId();
168
+    }
169
+
170
+    /**
171
+     * Close the session and release the lock, also writes all changed data in batch
172
+     */
173
+    public function close() {
174
+        if($this->isModified) {
175
+            $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
176
+            $this->session->set(self::encryptedSessionName, $encryptedValue);
177
+            $this->isModified = false;
178
+        }
179
+        $this->session->close();
180
+    }
181
+
182
+    /**
183
+     * @param mixed $offset
184
+     * @return bool
185
+     */
186
+    public function offsetExists($offset): bool {
187
+        return $this->exists($offset);
188
+    }
189
+
190
+    /**
191
+     * @param mixed $offset
192
+     * @return mixed
193
+     */
194
+    public function offsetGet($offset) {
195
+        return $this->get($offset);
196
+    }
197
+
198
+    /**
199
+     * @param mixed $offset
200
+     * @param mixed $value
201
+     */
202
+    public function offsetSet($offset, $value) {
203
+        $this->set($offset, $value);
204
+    }
205
+
206
+    /**
207
+     * @param mixed $offset
208
+     */
209
+    public function offsetUnset($offset) {
210
+        $this->remove($offset);
211
+    }
212 212
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	public function __destruct() {
70 70
 		try {
71 71
 			$this->close();
72
-		} catch (SessionNotAvailableException $e){
72
+		} catch (SessionNotAvailableException $e) {
73 73
 			// This exception can occur if session is already closed
74 74
 			// So it is safe to ignore it and let the garbage collector to proceed
75 75
 		}
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
 	 * @return string|null Either the value or null
106 106
 	 */
107 107
 	public function get(string $key) {
108
-		if(isset($this->sessionValues[$key])) {
108
+		if (isset($this->sessionValues[$key])) {
109 109
 			return $this->sessionValues[$key];
110 110
 		}
111 111
 
@@ -171,7 +171,7 @@  discard block
 block discarded – undo
171 171
 	 * Close the session and release the lock, also writes all changed data in batch
172 172
 	 */
173 173
 	public function close() {
174
-		if($this->isModified) {
174
+		if ($this->isModified) {
175 175
 			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
176 176
 			$this->session->set(self::encryptedSessionName, $encryptedValue);
177 177
 			$this->isModified = false;
Please login to merge, or discard this patch.