Completed
Pull Request — master (#7824)
by Roeland
28:09
created
lib/private/Session/CryptoSessionData.php 1 patch
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -36,176 +36,176 @@
 block discarded – undo
36 36
  * @package OC\Session
37 37
  */
38 38
 class CryptoSessionData implements \ArrayAccess, ISession {
39
-	/** @var ISession */
40
-	protected $session;
41
-	/** @var \OCP\Security\ICrypto */
42
-	protected $crypto;
43
-	/** @var string */
44
-	protected $passphrase;
45
-	/** @var array */
46
-	protected $sessionValues;
47
-	/** @var bool */
48
-	protected $isModified = false;
49
-	CONST encryptedSessionName = 'encrypted_session_data';
50
-
51
-	/**
52
-	 * @param ISession $session
53
-	 * @param ICrypto $crypto
54
-	 * @param string $passphrase
55
-	 */
56
-	public function __construct(ISession $session,
57
-								ICrypto $crypto,
58
-								$passphrase) {
59
-		$this->crypto = $crypto;
60
-		$this->session = $session;
61
-		$this->passphrase = $passphrase;
62
-		$this->initializeSession();
63
-	}
64
-
65
-	/**
66
-	 * Close session if class gets destructed
67
-	 */
68
-	public function __destruct() {
69
-		try {
70
-			$this->close();
71
-		} catch (SessionNotAvailableException $e){
72
-			// This exception can occur if session is already closed
73
-			// So it is safe to ignore it and let the garbage collector to proceed
74
-		}
75
-	}
76
-
77
-	protected function initializeSession() {
78
-		$encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
79
-		try {
80
-			$this->sessionValues = json_decode(
81
-				$this->crypto->decrypt($encryptedSessionData, $this->passphrase),
82
-				true
83
-			);
84
-		} catch (\Exception $e) {
85
-			$this->sessionValues = [];
86
-		}
87
-	}
88
-
89
-	/**
90
-	 * Set a value in the session
91
-	 *
92
-	 * @param string $key
93
-	 * @param mixed $value
94
-	 */
95
-	public function set($key, $value) {
96
-		$this->sessionValues[$key] = $value;
97
-		$this->isModified = true;
98
-	}
99
-
100
-	/**
101
-	 * Get a value from the session
102
-	 *
103
-	 * @param string $key
104
-	 * @return string|null Either the value or null
105
-	 */
106
-	public function get($key) {
107
-		if(isset($this->sessionValues[$key])) {
108
-			return $this->sessionValues[$key];
109
-		}
110
-
111
-		return null;
112
-	}
113
-
114
-	/**
115
-	 * Check if a named key exists in the session
116
-	 *
117
-	 * @param string $key
118
-	 * @return bool
119
-	 */
120
-	public function exists($key) {
121
-		return isset($this->sessionValues[$key]);
122
-	}
123
-
124
-	/**
125
-	 * Remove a $key/$value pair from the session
126
-	 *
127
-	 * @param string $key
128
-	 */
129
-	public function remove($key) {
130
-		$this->isModified = true;
131
-		unset($this->sessionValues[$key]);
132
-		$this->session->remove(self::encryptedSessionName);
133
-	}
134
-
135
-	/**
136
-	 * Reset and recreate the session
137
-	 */
138
-	public function clear() {
139
-		$requesttoken = $this->get('requesttoken');
140
-		$this->sessionValues = [];
141
-		if ($requesttoken !== null) {
142
-			$this->set('requesttoken', $requesttoken);
143
-		}
144
-		$this->isModified = true;
145
-		$this->session->clear();
146
-	}
147
-
148
-	/**
149
-	 * Wrapper around session_regenerate_id
150
-	 *
151
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
152
-	 * @return void
153
-	 */
154
-	public function regenerateId($deleteOldSession = true) {
155
-		$this->session->regenerateId($deleteOldSession);
156
-	}
157
-
158
-	/**
159
-	 * Wrapper around session_id
160
-	 *
161
-	 * @return string
162
-	 * @throws SessionNotAvailableException
163
-	 * @since 9.1.0
164
-	 */
165
-	public function getId() {
166
-		return $this->session->getId();
167
-	}
168
-
169
-	/**
170
-	 * Close the session and release the lock, also writes all changed data in batch
171
-	 */
172
-	public function close() {
173
-		if($this->isModified) {
174
-			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
175
-			$this->session->set(self::encryptedSessionName, $encryptedValue);
176
-			$this->isModified = false;
177
-		}
178
-		$this->session->close();
179
-	}
180
-
181
-	/**
182
-	 * @param mixed $offset
183
-	 * @return bool
184
-	 */
185
-	public function offsetExists($offset) {
186
-		return $this->exists($offset);
187
-	}
188
-
189
-	/**
190
-	 * @param mixed $offset
191
-	 * @return mixed
192
-	 */
193
-	public function offsetGet($offset) {
194
-		return $this->get($offset);
195
-	}
196
-
197
-	/**
198
-	 * @param mixed $offset
199
-	 * @param mixed $value
200
-	 */
201
-	public function offsetSet($offset, $value) {
202
-		$this->set($offset, $value);
203
-	}
204
-
205
-	/**
206
-	 * @param mixed $offset
207
-	 */
208
-	public function offsetUnset($offset) {
209
-		$this->remove($offset);
210
-	}
39
+    /** @var ISession */
40
+    protected $session;
41
+    /** @var \OCP\Security\ICrypto */
42
+    protected $crypto;
43
+    /** @var string */
44
+    protected $passphrase;
45
+    /** @var array */
46
+    protected $sessionValues;
47
+    /** @var bool */
48
+    protected $isModified = false;
49
+    CONST encryptedSessionName = 'encrypted_session_data';
50
+
51
+    /**
52
+     * @param ISession $session
53
+     * @param ICrypto $crypto
54
+     * @param string $passphrase
55
+     */
56
+    public function __construct(ISession $session,
57
+                                ICrypto $crypto,
58
+                                $passphrase) {
59
+        $this->crypto = $crypto;
60
+        $this->session = $session;
61
+        $this->passphrase = $passphrase;
62
+        $this->initializeSession();
63
+    }
64
+
65
+    /**
66
+     * Close session if class gets destructed
67
+     */
68
+    public function __destruct() {
69
+        try {
70
+            $this->close();
71
+        } catch (SessionNotAvailableException $e){
72
+            // This exception can occur if session is already closed
73
+            // So it is safe to ignore it and let the garbage collector to proceed
74
+        }
75
+    }
76
+
77
+    protected function initializeSession() {
78
+        $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
79
+        try {
80
+            $this->sessionValues = json_decode(
81
+                $this->crypto->decrypt($encryptedSessionData, $this->passphrase),
82
+                true
83
+            );
84
+        } catch (\Exception $e) {
85
+            $this->sessionValues = [];
86
+        }
87
+    }
88
+
89
+    /**
90
+     * Set a value in the session
91
+     *
92
+     * @param string $key
93
+     * @param mixed $value
94
+     */
95
+    public function set($key, $value) {
96
+        $this->sessionValues[$key] = $value;
97
+        $this->isModified = true;
98
+    }
99
+
100
+    /**
101
+     * Get a value from the session
102
+     *
103
+     * @param string $key
104
+     * @return string|null Either the value or null
105
+     */
106
+    public function get($key) {
107
+        if(isset($this->sessionValues[$key])) {
108
+            return $this->sessionValues[$key];
109
+        }
110
+
111
+        return null;
112
+    }
113
+
114
+    /**
115
+     * Check if a named key exists in the session
116
+     *
117
+     * @param string $key
118
+     * @return bool
119
+     */
120
+    public function exists($key) {
121
+        return isset($this->sessionValues[$key]);
122
+    }
123
+
124
+    /**
125
+     * Remove a $key/$value pair from the session
126
+     *
127
+     * @param string $key
128
+     */
129
+    public function remove($key) {
130
+        $this->isModified = true;
131
+        unset($this->sessionValues[$key]);
132
+        $this->session->remove(self::encryptedSessionName);
133
+    }
134
+
135
+    /**
136
+     * Reset and recreate the session
137
+     */
138
+    public function clear() {
139
+        $requesttoken = $this->get('requesttoken');
140
+        $this->sessionValues = [];
141
+        if ($requesttoken !== null) {
142
+            $this->set('requesttoken', $requesttoken);
143
+        }
144
+        $this->isModified = true;
145
+        $this->session->clear();
146
+    }
147
+
148
+    /**
149
+     * Wrapper around session_regenerate_id
150
+     *
151
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
152
+     * @return void
153
+     */
154
+    public function regenerateId($deleteOldSession = true) {
155
+        $this->session->regenerateId($deleteOldSession);
156
+    }
157
+
158
+    /**
159
+     * Wrapper around session_id
160
+     *
161
+     * @return string
162
+     * @throws SessionNotAvailableException
163
+     * @since 9.1.0
164
+     */
165
+    public function getId() {
166
+        return $this->session->getId();
167
+    }
168
+
169
+    /**
170
+     * Close the session and release the lock, also writes all changed data in batch
171
+     */
172
+    public function close() {
173
+        if($this->isModified) {
174
+            $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
175
+            $this->session->set(self::encryptedSessionName, $encryptedValue);
176
+            $this->isModified = false;
177
+        }
178
+        $this->session->close();
179
+    }
180
+
181
+    /**
182
+     * @param mixed $offset
183
+     * @return bool
184
+     */
185
+    public function offsetExists($offset) {
186
+        return $this->exists($offset);
187
+    }
188
+
189
+    /**
190
+     * @param mixed $offset
191
+     * @return mixed
192
+     */
193
+    public function offsetGet($offset) {
194
+        return $this->get($offset);
195
+    }
196
+
197
+    /**
198
+     * @param mixed $offset
199
+     * @param mixed $value
200
+     */
201
+    public function offsetSet($offset, $value) {
202
+        $this->set($offset, $value);
203
+    }
204
+
205
+    /**
206
+     * @param mixed $offset
207
+     */
208
+    public function offsetUnset($offset) {
209
+        $this->remove($offset);
210
+    }
211 211
 }
Please login to merge, or discard this patch.