Completed
Pull Request — master (#27)
by Will
01:23
created
tests/CookieStoreTest.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -10,69 +10,69 @@
 block discarded – undo
10 10
 
11 11
 class CookieStoreTest extends AbstractTest
12 12
 {
13
-    protected function getStore()
14
-    {
15
-        $store = Injector::inst()->get(CookieStore::class);
16
-        $store->setKey(uniqid());
17
-        $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE');
13
+	protected function getStore()
14
+	{
15
+		$store = Injector::inst()->get(CookieStore::class);
16
+		$store->setKey(uniqid());
17
+		$store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE');
18 18
 
19
-        return $store;
20
-    }
19
+		return $store;
20
+	}
21 21
 
22
-    public function testStoreLargeData()
23
-    {
24
-        $session = uniqid();
25
-        $store = $this->getStore();
22
+	public function testStoreLargeData()
23
+	{
24
+		$session = uniqid();
25
+		$store = $this->getStore();
26 26
 
27
-        // Test new session is blank
28
-        $result = $store->read($session);
29
-        $this->assertEmpty($result);
27
+		// Test new session is blank
28
+		$result = $store->read($session);
29
+		$this->assertEmpty($result);
30 30
 
31
-        // Save data against session
32
-        $data1 = array(
33
-            'Large' => str_repeat('A', 600),
34
-            'Content' => str_repeat('B', 600)
35
-        );
36
-        $store->write($session, serialize($data1));
37
-        $result = $store->read($session);
31
+		// Save data against session
32
+		$data1 = array(
33
+			'Large' => str_repeat('A', 600),
34
+			'Content' => str_repeat('B', 600)
35
+		);
36
+		$store->write($session, serialize($data1));
37
+		$result = $store->read($session);
38 38
 
39
-        // Cookies should not try to store data that large
40
-        $this->assertEmpty($result);
41
-    }
39
+		// Cookies should not try to store data that large
40
+		$this->assertEmpty($result);
41
+	}
42 42
 
43
-    /**
44
-     * Ensure that subsequent reads without the necessary write do not report data
45
-     */
46
-    public function testReadInvalidatesData()
47
-    {
48
-        $session = uniqid();
49
-        $store = $this->getStore();
43
+	/**
44
+	 * Ensure that subsequent reads without the necessary write do not report data
45
+	 */
46
+	public function testReadInvalidatesData()
47
+	{
48
+		$session = uniqid();
49
+		$store = $this->getStore();
50 50
 
51
-        // Test new session is blank
52
-        $result = $store->read($session);
53
-        $this->assertEmpty($result);
51
+		// Test new session is blank
52
+		$result = $store->read($session);
53
+		$this->assertEmpty($result);
54 54
 
55
-        // Save data against session
56
-        $data1 = array(
57
-            'Color' => 'red',
58
-            'Animal' => 'elephant'
59
-        );
60
-        $store->write($session, serialize($data1));
61
-        $result = $store->read($session);
62
-        $this->assertEquals($data1, unserialize($result));
55
+		// Save data against session
56
+		$data1 = array(
57
+			'Color' => 'red',
58
+			'Animal' => 'elephant'
59
+		);
60
+		$store->write($session, serialize($data1));
61
+		$result = $store->read($session);
62
+		$this->assertEquals($data1, unserialize($result));
63 63
 
64
-        // Since we have read the data into the result, the application could modify this content
65
-        // and be unable to write it back due to headers being sent. We should thus assume
66
-        // that subsequent reads without a successful write do not purport to have valid data
67
-        $data1['Color'] = 'blue';
68
-        $result = $store->read($session);
69
-        $this->assertEmpty($result);
64
+		// Since we have read the data into the result, the application could modify this content
65
+		// and be unable to write it back due to headers being sent. We should thus assume
66
+		// that subsequent reads without a successful write do not purport to have valid data
67
+		$data1['Color'] = 'blue';
68
+		$result = $store->read($session);
69
+		$this->assertEmpty($result);
70 70
 
71
-        // Check that writing to cookie fails after headers are sent and these results remain
72
-        // invalidated
73
-        TestCookieStore::$override_headers_sent = true;
74
-        $store->write($session, serialize($data1));
75
-        $result = $store->read($session);
76
-        $this->assertEmpty($result);
77
-    }
71
+		// Check that writing to cookie fails after headers are sent and these results remain
72
+		// invalidated
73
+		TestCookieStore::$override_headers_sent = true;
74
+		$store->write($session, serialize($data1));
75
+		$result = $store->read($session);
76
+		$this->assertEmpty($result);
77
+	}
78 78
 }
Please login to merge, or discard this patch.
tests/DatabaseStoreTest.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -7,11 +7,11 @@
 block discarded – undo
7 7
 
8 8
 class DatabaseStoreTest extends AbstractTest
9 9
 {
10
-    protected function getStore()
11
-    {
12
-        $store = Injector::inst()->get(DatabaseStore::class);
13
-        $store->setKey(uniqid());
10
+	protected function getStore()
11
+	{
12
+		$store = Injector::inst()->get(DatabaseStore::class);
13
+		$store->setKey(uniqid());
14 14
 
15
-        return $store;
16
-    }
15
+		return $store;
16
+	}
17 17
 }
Please login to merge, or discard this patch.
tests/Store/TestCookieStore.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -9,21 +9,21 @@
 block discarded – undo
9 9
 {
10 10
 
11 11
 
12
-    /**
13
-     * Override value of 'headers_sent' but only if running tests.
14
-     *
15
-     * Set to true or false, or null to not override
16
-     *
17
-     * @var string
18
-     */
19
-    public static $override_headers_sent = null;
12
+	/**
13
+	 * Override value of 'headers_sent' but only if running tests.
14
+	 *
15
+	 * Set to true or false, or null to not override
16
+	 *
17
+	 * @var string
18
+	 */
19
+	public static $override_headers_sent = null;
20 20
 
21
-    protected function canWrite()
22
-    {
23
-        if (self::$override_headers_sent !== null) {
24
-            return !self::$override_headers_sent;
25
-        }
21
+	protected function canWrite()
22
+	{
23
+		if (self::$override_headers_sent !== null) {
24
+			return !self::$override_headers_sent;
25
+		}
26 26
 
27
-        parent::canWrite();
28
-    }
27
+		parent::canWrite();
28
+	}
29 29
 }
Please login to merge, or discard this patch.
tests/HybridSessionTest.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -10,15 +10,15 @@
 block discarded – undo
10 10
 class HybridSessionTest extends AbstractTest
11 11
 {
12 12
 
13
-    /**
14
-     * @return HybridSessionStore_Cookie
15
-     */
16
-    protected function getStore()
17
-    {
18
-        $store = Injector::inst()->create(HybridSession::class);
19
-        $store->setKey(uniqid());
20
-        $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE');
13
+	/**
14
+	 * @return HybridSessionStore_Cookie
15
+	 */
16
+	protected function getStore()
17
+	{
18
+		$store = Injector::inst()->create(HybridSession::class);
19
+		$store->setKey(uniqid());
20
+		$store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE');
21 21
 
22
-        return $store;
23
-    }
22
+		return $store;
23
+	}
24 24
 }
Please login to merge, or discard this patch.
tests/AbstractTest.php 1 patch
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -10,109 +10,109 @@
 block discarded – undo
10 10
 
11 11
 abstract class AbstractTest extends SapphireTest
12 12
 {
13
-    protected $usesDatabase = true;
14
-
15
-    protected function setUp()
16
-    {
17
-        parent::setUp();
18
-
19
-        TestCookieStore::$override_headers_sent = false;
20
-
21
-        Injector::inst()->registerService(
22
-            new TestCookieStore(),
23
-            CookieStore::class
24
-        );
25
-
26
-        DBDatetime::set_mock_now('2010-03-15 12:00:00');
27
-
28
-        if (get_class() === get_class($this)) {
29
-            $this->markTestSkipped("Skipping abstract test");
30
-            $this->skipTest = true;
31
-        }
32
-    }
33
-
34
-    protected function tearDown()
35
-    {
36
-        DBDatetime::clear_mock_now();
37
-
38
-        parent::tearDown();
39
-    }
40
-
41
-    abstract protected function getStore();
42
-
43
-    /**
44
-     * Test how this store handles large volumes of data (>1000 characters)
45
-     */
46
-    public function testStoreLargeData()
47
-    {
48
-        $session = uniqid();
49
-        $store = $this->getStore();
50
-
51
-        // Test new session is blank
52
-        $result = $store->read($session);
53
-        $this->assertEmpty($result);
54
-
55
-        // Save data against session
56
-        $data1 = array(
57
-            'Large' => str_repeat('A', 600),
58
-            'Content' => str_repeat('B', 600)
59
-        );
60
-        $store->write($session, serialize($data1));
61
-        $result = $store->read($session);
62
-        $this->assertEquals($data1, unserialize($result));
63
-    }
64
-
65
-    /**
66
-     * Test storage of data
67
-     */
68
-    public function testStoreData()
69
-    {
70
-        $session = uniqid();
71
-        $store = $this->getStore();
72
-
73
-        // Test new session is blank
74
-        $result = $store->read($session);
75
-        $this->assertEmpty($result);
76
-
77
-        // Save data against session
78
-        $data1 = array(
79
-            'Color' => 'red',
80
-            'Animal' => 'elephant'
81
-        );
82
-        $store->write($session, serialize($data1));
83
-        $result = $store->read($session);
84
-        $this->assertEquals($data1, unserialize($result));
85
-
86
-        // Save larger data
87
-        $data2 = array(
88
-            'Color' => 'blue',
89
-            'Animal' => str_repeat('bat', 100)
90
-        );
91
-        $store->write($session, serialize($data2));
92
-        $result = $store->read($session);
93
-        $this->assertEquals($data2, unserialize($result));
94
-    }
95
-
96
-    /**
97
-     * Test expiry of data
98
-     */
99
-    public function testExpiry()
100
-    {
101
-        $session1 = uniqid();
102
-        $store = $this->getStore();
103
-
104
-        // Store data now
105
-        $data1 = array(
106
-            'Food' => 'Pizza'
107
-        );
108
-        $store->write($session1, serialize($data1));
109
-        $result1 = $store->read($session1);
110
-        $this->assertEquals($data1, unserialize($result1));
111
-
112
-        // Go to the future and test that the expiry is accurate
113
-        DBDatetime::set_mock_now('2040-03-16 12:00:00');
114
-        $result2 = $store->read($session1);
115
-
116
-        $this->assertEmpty($result2);
117
-    }
13
+	protected $usesDatabase = true;
14
+
15
+	protected function setUp()
16
+	{
17
+		parent::setUp();
18
+
19
+		TestCookieStore::$override_headers_sent = false;
20
+
21
+		Injector::inst()->registerService(
22
+			new TestCookieStore(),
23
+			CookieStore::class
24
+		);
25
+
26
+		DBDatetime::set_mock_now('2010-03-15 12:00:00');
27
+
28
+		if (get_class() === get_class($this)) {
29
+			$this->markTestSkipped("Skipping abstract test");
30
+			$this->skipTest = true;
31
+		}
32
+	}
33
+
34
+	protected function tearDown()
35
+	{
36
+		DBDatetime::clear_mock_now();
37
+
38
+		parent::tearDown();
39
+	}
40
+
41
+	abstract protected function getStore();
42
+
43
+	/**
44
+	 * Test how this store handles large volumes of data (>1000 characters)
45
+	 */
46
+	public function testStoreLargeData()
47
+	{
48
+		$session = uniqid();
49
+		$store = $this->getStore();
50
+
51
+		// Test new session is blank
52
+		$result = $store->read($session);
53
+		$this->assertEmpty($result);
54
+
55
+		// Save data against session
56
+		$data1 = array(
57
+			'Large' => str_repeat('A', 600),
58
+			'Content' => str_repeat('B', 600)
59
+		);
60
+		$store->write($session, serialize($data1));
61
+		$result = $store->read($session);
62
+		$this->assertEquals($data1, unserialize($result));
63
+	}
64
+
65
+	/**
66
+	 * Test storage of data
67
+	 */
68
+	public function testStoreData()
69
+	{
70
+		$session = uniqid();
71
+		$store = $this->getStore();
72
+
73
+		// Test new session is blank
74
+		$result = $store->read($session);
75
+		$this->assertEmpty($result);
76
+
77
+		// Save data against session
78
+		$data1 = array(
79
+			'Color' => 'red',
80
+			'Animal' => 'elephant'
81
+		);
82
+		$store->write($session, serialize($data1));
83
+		$result = $store->read($session);
84
+		$this->assertEquals($data1, unserialize($result));
85
+
86
+		// Save larger data
87
+		$data2 = array(
88
+			'Color' => 'blue',
89
+			'Animal' => str_repeat('bat', 100)
90
+		);
91
+		$store->write($session, serialize($data2));
92
+		$result = $store->read($session);
93
+		$this->assertEquals($data2, unserialize($result));
94
+	}
95
+
96
+	/**
97
+	 * Test expiry of data
98
+	 */
99
+	public function testExpiry()
100
+	{
101
+		$session1 = uniqid();
102
+		$store = $this->getStore();
103
+
104
+		// Store data now
105
+		$data1 = array(
106
+			'Food' => 'Pizza'
107
+		);
108
+		$store->write($session1, serialize($data1));
109
+		$result1 = $store->read($session1);
110
+		$this->assertEquals($data1, unserialize($result1));
111
+
112
+		// Go to the future and test that the expiry is accurate
113
+		DBDatetime::set_mock_now('2040-03-16 12:00:00');
114
+		$result2 = $store->read($session1);
115
+
116
+		$this->assertEmpty($result2);
117
+	}
118 118
 }
Please login to merge, or discard this patch.
src/HybridSession.php 1 patch
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -9,161 +9,161 @@
 block discarded – undo
9 9
 class HybridSession extends BaseStore
10 10
 {
11 11
 
12
-    /**
13
-     * List of session handlers
14
-     *
15
-     * @var array
16
-     */
17
-    protected $handlers = [];
18
-
19
-    /**
20
-     * True if this session store has been initialised
21
-     *
22
-     * @var bool
23
-     */
24
-    protected static $enabled = false;
25
-
26
-    /**
27
-     * @param SessionHandlerInterface[]
28
-     *
29
-     * @return $this
30
-     */
31
-    public function setHandlers($handlers)
32
-    {
33
-        $this->handlers = $handlers;
34
-        $this->setKey($this->getKey());
35
-
36
-        return $this;
37
-    }
38
-
39
-    /**
40
-     * @param string
41
-     *
42
-     * @return $this
43
-     */
44
-    public function setKey($key)
45
-    {
46
-        parent::setKey($key);
47
-
48
-        foreach ($this->handlers as $handler) {
49
-            $handler->setKey($key);
50
-        }
51
-
52
-        return $this;
53
-    }
54
-
55
-    /**
56
-     * @return SessionHandlerInterface[]
57
-     */
58
-    public function getHandlers()
59
-    {
60
-        return $this->handlers;
61
-    }
62
-
63
-    /**
64
-     * @param string $save_path
65
-     * @param string $name
66
-     *
67
-     * @return bool
68
-     */
69
-    public function open($save_path, $name)
70
-    {
71
-        if ($this->handlers) {
72
-            foreach ($this->handlers as $handler) {
73
-                $handler->open($save_path, $name);
74
-            }
75
-        }
76
-
77
-        return true;
78
-    }
79
-
80
-    /**
81
-     * @return bool
82
-     */
83
-    public function close()
84
-    {
85
-        if ($this->handlers) {
86
-            foreach ($this->handlers as $handler) {
87
-                $handler->close();
88
-            }
89
-        }
90
-
91
-        return true;
92
-    }
93
-
94
-    /**
95
-     * @param string $session_id
96
-     *
97
-     * @return string
98
-     */
99
-    public function read($session_id)
100
-    {
101
-        if ($this->handlers) {
102
-            foreach ($this->handlers as $handler) {
103
-                if ($data = $handler->read($session_id)) {
104
-                    return $data;
105
-                }
106
-            }
107
-        }
108
-
109
-        return '';
110
-    }
111
-
112
-
113
-    public function write($session_id, $session_data)
114
-    {
115
-        if ($this->handlers) {
116
-            foreach ($this->handlers as $handler) {
117
-                if ($handler->write($session_id, $session_data)) {
118
-                    return;
119
-                }
120
-            }
121
-        }
122
-    }
123
-
124
-    public function destroy($session_id)
125
-    {
126
-        if ($this->handlers) {
127
-            foreach ($this->handlers as $handler) {
128
-                $handler->destroy($session_id);
129
-            }
130
-        }
131
-    }
132
-
133
-    public function gc($maxlifetime)
134
-    {
135
-        if ($this->handlers) {
136
-            foreach ($this->handlers as $handler) {
137
-                $handler->gc($maxlifetime);
138
-            }
139
-        }
140
-    }
141
-
142
-    /**
143
-     * Register the session handler as the default
144
-     *
145
-     * @param string $key Desired session key
146
-     */
147
-    public static function init($key = null)
148
-    {
149
-        $instance = Injector::inst()->get(__CLASS__);
150
-
151
-        if (empty($key)) {
152
-            user_error(
153
-                'HybridSession::init() was not given a $key. Disabling cookie-based storage',
154
-                E_USER_WARNING
155
-            );
156
-        } else {
157
-            $instance->setKey($key);
158
-        }
159
-
160
-        session_set_save_handler($instance, true);
161
-
162
-        self::$enabled = true;
163
-    }
164
-
165
-    public static function is_enabled()
166
-    {
167
-        return self::$enabled;
168
-    }
12
+	/**
13
+	 * List of session handlers
14
+	 *
15
+	 * @var array
16
+	 */
17
+	protected $handlers = [];
18
+
19
+	/**
20
+	 * True if this session store has been initialised
21
+	 *
22
+	 * @var bool
23
+	 */
24
+	protected static $enabled = false;
25
+
26
+	/**
27
+	 * @param SessionHandlerInterface[]
28
+	 *
29
+	 * @return $this
30
+	 */
31
+	public function setHandlers($handlers)
32
+	{
33
+		$this->handlers = $handlers;
34
+		$this->setKey($this->getKey());
35
+
36
+		return $this;
37
+	}
38
+
39
+	/**
40
+	 * @param string
41
+	 *
42
+	 * @return $this
43
+	 */
44
+	public function setKey($key)
45
+	{
46
+		parent::setKey($key);
47
+
48
+		foreach ($this->handlers as $handler) {
49
+			$handler->setKey($key);
50
+		}
51
+
52
+		return $this;
53
+	}
54
+
55
+	/**
56
+	 * @return SessionHandlerInterface[]
57
+	 */
58
+	public function getHandlers()
59
+	{
60
+		return $this->handlers;
61
+	}
62
+
63
+	/**
64
+	 * @param string $save_path
65
+	 * @param string $name
66
+	 *
67
+	 * @return bool
68
+	 */
69
+	public function open($save_path, $name)
70
+	{
71
+		if ($this->handlers) {
72
+			foreach ($this->handlers as $handler) {
73
+				$handler->open($save_path, $name);
74
+			}
75
+		}
76
+
77
+		return true;
78
+	}
79
+
80
+	/**
81
+	 * @return bool
82
+	 */
83
+	public function close()
84
+	{
85
+		if ($this->handlers) {
86
+			foreach ($this->handlers as $handler) {
87
+				$handler->close();
88
+			}
89
+		}
90
+
91
+		return true;
92
+	}
93
+
94
+	/**
95
+	 * @param string $session_id
96
+	 *
97
+	 * @return string
98
+	 */
99
+	public function read($session_id)
100
+	{
101
+		if ($this->handlers) {
102
+			foreach ($this->handlers as $handler) {
103
+				if ($data = $handler->read($session_id)) {
104
+					return $data;
105
+				}
106
+			}
107
+		}
108
+
109
+		return '';
110
+	}
111
+
112
+
113
+	public function write($session_id, $session_data)
114
+	{
115
+		if ($this->handlers) {
116
+			foreach ($this->handlers as $handler) {
117
+				if ($handler->write($session_id, $session_data)) {
118
+					return;
119
+				}
120
+			}
121
+		}
122
+	}
123
+
124
+	public function destroy($session_id)
125
+	{
126
+		if ($this->handlers) {
127
+			foreach ($this->handlers as $handler) {
128
+				$handler->destroy($session_id);
129
+			}
130
+		}
131
+	}
132
+
133
+	public function gc($maxlifetime)
134
+	{
135
+		if ($this->handlers) {
136
+			foreach ($this->handlers as $handler) {
137
+				$handler->gc($maxlifetime);
138
+			}
139
+		}
140
+	}
141
+
142
+	/**
143
+	 * Register the session handler as the default
144
+	 *
145
+	 * @param string $key Desired session key
146
+	 */
147
+	public static function init($key = null)
148
+	{
149
+		$instance = Injector::inst()->get(__CLASS__);
150
+
151
+		if (empty($key)) {
152
+			user_error(
153
+				'HybridSession::init() was not given a $key. Disabling cookie-based storage',
154
+				E_USER_WARNING
155
+			);
156
+		} else {
157
+			$instance->setKey($key);
158
+		}
159
+
160
+		session_set_save_handler($instance, true);
161
+
162
+		self::$enabled = true;
163
+	}
164
+
165
+	public static function is_enabled()
166
+	{
167
+		return self::$enabled;
168
+	}
169 169
 }
Please login to merge, or discard this patch.
src/Store/DatabaseStore.php 1 patch
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -12,93 +12,93 @@
 block discarded – undo
12 12
 class DatabaseStore extends BaseStore
13 13
 {
14 14
 
15
-    /**
16
-     * Determine if the DB is ready to use.
17
-     *
18
-     * @return bool
19
-     * @throws Exception
20
-     */
21
-    protected function isDatabaseReady()
22
-    {
23
-        // Such as during setup of testsession prior to DB connection.
24
-        if (!DB::is_active()) {
25
-            return false;
26
-        }
27
-
28
-        // If we have a DB of the wrong type then complain
29
-        if (!(DB::get_conn() instanceof MySQLDatabase)) {
30
-            throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases');
31
-        }
32
-
33
-        // Prevent freakout during dev/build
34
-        return ClassInfo::hasTable('HybridSessionDataObject');
35
-    }
36
-
37
-    public function open($save_path, $name)
38
-    {
39
-        // no-op
40
-    }
41
-
42
-    public function close()
43
-    {
44
-        // no-op
45
-    }
46
-
47
-    public function read($session_id)
48
-    {
49
-        if (!$this->isDatabaseReady()) {
50
-            return null;
51
-        }
52
-
53
-        $query = sprintf(
54
-            'SELECT "Data" FROM "HybridSessionDataObject"
15
+	/**
16
+	 * Determine if the DB is ready to use.
17
+	 *
18
+	 * @return bool
19
+	 * @throws Exception
20
+	 */
21
+	protected function isDatabaseReady()
22
+	{
23
+		// Such as during setup of testsession prior to DB connection.
24
+		if (!DB::is_active()) {
25
+			return false;
26
+		}
27
+
28
+		// If we have a DB of the wrong type then complain
29
+		if (!(DB::get_conn() instanceof MySQLDatabase)) {
30
+			throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases');
31
+		}
32
+
33
+		// Prevent freakout during dev/build
34
+		return ClassInfo::hasTable('HybridSessionDataObject');
35
+	}
36
+
37
+	public function open($save_path, $name)
38
+	{
39
+		// no-op
40
+	}
41
+
42
+	public function close()
43
+	{
44
+		// no-op
45
+	}
46
+
47
+	public function read($session_id)
48
+	{
49
+		if (!$this->isDatabaseReady()) {
50
+			return null;
51
+		}
52
+
53
+		$query = sprintf(
54
+			'SELECT "Data" FROM "HybridSessionDataObject"
55 55
             WHERE "SessionID" = \'%s\' AND "Expiry" >= %s',
56
-            Convert::raw2sql($session_id),
57
-            $this->getNow()
58
-        );
56
+			Convert::raw2sql($session_id),
57
+			$this->getNow()
58
+		);
59 59
 
60
-        $result = DB::query($query);
60
+		$result = DB::query($query);
61 61
 
62
-        if ($result && $result->numRecords()) {
63
-            $data = $result->first();
64
-            return $data['Data'];
65
-        }
66
-    }
62
+		if ($result && $result->numRecords()) {
63
+			$data = $result->first();
64
+			return $data['Data'];
65
+		}
66
+	}
67 67
 
68
-    public function write($session_id, $session_data)
69
-    {
70
-        if (!$this->isDatabaseReady()) {
71
-            return false;
72
-        }
68
+	public function write($session_id, $session_data)
69
+	{
70
+		if (!$this->isDatabaseReady()) {
71
+			return false;
72
+		}
73 73
 
74
-        $expiry = $this->getNow() + $this->getLifetime();
74
+		$expiry = $this->getNow() + $this->getLifetime();
75 75
 
76
-        DB::query($str = sprintf(
77
-            'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data")
76
+		DB::query($str = sprintf(
77
+			'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data")
78 78
             VALUES (\'%1$s\', %2$u, \'%3$s\')
79 79
             ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'',
80
-            Convert::raw2sql($session_id),
81
-            $expiry,
82
-            Convert::raw2sql($session_data)
83
-        ));
84
-
85
-        return true;
86
-    }
87
-
88
-    public function destroy($session_id)
89
-    {
90
-        // NOP
91
-    }
92
-
93
-    public function gc($maxlifetime)
94
-    {
95
-        if (!$this->isDatabaseReady()) {
96
-            return;
97
-        }
98
-
99
-        DB::query(sprintf(
100
-            'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u',
101
-            $this->getNow()
102
-        ));
103
-    }
80
+			Convert::raw2sql($session_id),
81
+			$expiry,
82
+			Convert::raw2sql($session_data)
83
+		));
84
+
85
+		return true;
86
+	}
87
+
88
+	public function destroy($session_id)
89
+	{
90
+		// NOP
91
+	}
92
+
93
+	public function gc($maxlifetime)
94
+	{
95
+		if (!$this->isDatabaseReady()) {
96
+			return;
97
+		}
98
+
99
+		DB::query(sprintf(
100
+			'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u',
101
+			$this->getNow()
102
+		));
103
+	}
104 104
 }
Please login to merge, or discard this patch.
src/Store/CookieStore.php 1 patch
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -22,168 +22,168 @@
 block discarded – undo
22 22
 class CookieStore extends BaseStore
23 23
 {
24 24
 
25
-    /**
26
-     * Maximum length of a cookie value in characters
27
-     *
28
-     * @var int
29
-     * @config
30
-     */
31
-    private static $max_length = 1024;
32
-
33
-    /**
34
-     * Encryption service
35
-     *
36
-     * @var HybridSessionStore_Crypto
37
-     */
38
-    protected $crypto;
39
-
40
-    /**
41
-     * Name of cookie
42
-     *
43
-     * @var string
44
-     */
45
-    protected $cookie;
46
-
47
-    /**
48
-     * Known unmodified value of this cookie. If the cookie backend has been read into the application,
49
-     * then the backend is unable to verify the modification state of this value internally within the
50
-     * system, so this will be left null unless written back.
51
-     *
52
-     * If the content exceeds max_length then the backend can also not maintain this cookie, also
53
-     * setting this variable to null.
54
-     *
55
-     * @var string
56
-     */
57
-    protected $currentCookieData;
58
-
59
-    public function open($save_path, $name)
60
-    {
61
-        $this->cookie = $name.'_2';
62
-
63
-        // Read the incoming value, then clear the cookie - we might not be able
64
-        // to do so later if write() is called after headers are sent
65
-        // This is intended to force a failover to the database store if the
66
-        // modified session cannot be emitted.
67
-        $this->currentCookieData = Cookie::get($this->cookie);
68
-
69
-        if ($this->currentCookieData) {
70
-            Cookie::set($this->cookie, '');
71
-        }
72
-    }
73
-
74
-    public function close()
75
-    {
76
-    }
77
-
78
-    /**
79
-     * Get the cryptography store for the specified session
80
-     *
81
-     * @param string $session_id
82
-     * @return HybridSessionStore_Crypto
83
-     */
84
-    protected function getCrypto($session_id)
85
-    {
86
-        $key = $this->getKey();
87
-
88
-        if (!$key) {
89
-            return null;
90
-        }
91
-
92
-        if (!$this->crypto || $this->crypto->getSalt() != $session_id) {
93
-            $this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id);
94
-        }
95
-
96
-        return $this->crypto;
97
-    }
98
-
99
-    public function read($session_id)
100
-    {
101
-        // Check ability to safely decrypt content
102
-        if (!$this->currentCookieData
103
-            || !($crypto = $this->getCrypto($session_id))
104
-        ) {
105
-            return;
106
-        }
107
-
108
-        // Decrypt and invalidate old data
109
-        $cookieData = $crypto->decrypt($this->currentCookieData);
110
-        $this->currentCookieData = null;
111
-
112
-        // Verify expiration
113
-        if ($cookieData) {
114
-            $expiry = (int)substr($cookieData, 0, 10);
115
-            $data = substr($cookieData, 10);
116
-
117
-            if ($expiry > $this->getNow()) {
118
-                return $data;
119
-            }
120
-        }
121
-    }
122
-
123
-    /**
124
-     * Determine if the session could be verifably written to cookie storage
125
-     *
126
-     * @return bool
127
-     */
128
-    protected function canWrite()
129
-    {
130
-        return !headers_sent();
131
-    }
132
-
133
-    public function write($session_id, $session_data)
134
-    {
135
-        // Check ability to safely encrypt and write content
136
-        if (!$this->canWrite()
137
-            || (strlen($session_data) > static::config()->get('max_length'))
138
-            || !($crypto = $this->getCrypto($session_id))
139
-        ) {
140
-            return false;
141
-        }
142
-
143
-        // Prepare content for write
144
-        $params = session_get_cookie_params();
145
-        // Total max lifetime, stored internally
146
-        $lifetime = $this->getLifetime();
147
-        $expiry = $this->getNow() + $lifetime;
148
-
149
-        // Restore the known good cookie value
150
-        $this->currentCookieData = $this->crypto->encrypt(
151
-            sprintf('%010u', $expiry) . $session_data
152
-        );
153
-
154
-        // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero)
155
-        $cookieLifetime = min((int)$params['lifetime'], $lifetime);
156
-
157
-        Cookie::set(
158
-            $this->cookie,
159
-            $this->currentCookieData,
160
-            $cookieLifetime / 86400,
161
-            $params['path'],
162
-            $params['domain'],
163
-            $params['secure'],
164
-            $params['httponly']
165
-        );
166
-
167
-        return true;
168
-    }
169
-
170
-    public function destroy($session_id)
171
-    {
172
-        $this->currentCookieData = null;
173
-
174
-        $params = session_get_cookie_params();
175
-
176
-        Cookie::force_expiry(
177
-            $this->cookie,
178
-            $params['path'],
179
-            $params['domain'],
180
-            $params['secure'],
181
-            $params['httponly']
182
-        );
183
-    }
184
-
185
-    public function gc($maxlifetime)
186
-    {
187
-        // NOP
188
-    }
25
+	/**
26
+	 * Maximum length of a cookie value in characters
27
+	 *
28
+	 * @var int
29
+	 * @config
30
+	 */
31
+	private static $max_length = 1024;
32
+
33
+	/**
34
+	 * Encryption service
35
+	 *
36
+	 * @var HybridSessionStore_Crypto
37
+	 */
38
+	protected $crypto;
39
+
40
+	/**
41
+	 * Name of cookie
42
+	 *
43
+	 * @var string
44
+	 */
45
+	protected $cookie;
46
+
47
+	/**
48
+	 * Known unmodified value of this cookie. If the cookie backend has been read into the application,
49
+	 * then the backend is unable to verify the modification state of this value internally within the
50
+	 * system, so this will be left null unless written back.
51
+	 *
52
+	 * If the content exceeds max_length then the backend can also not maintain this cookie, also
53
+	 * setting this variable to null.
54
+	 *
55
+	 * @var string
56
+	 */
57
+	protected $currentCookieData;
58
+
59
+	public function open($save_path, $name)
60
+	{
61
+		$this->cookie = $name.'_2';
62
+
63
+		// Read the incoming value, then clear the cookie - we might not be able
64
+		// to do so later if write() is called after headers are sent
65
+		// This is intended to force a failover to the database store if the
66
+		// modified session cannot be emitted.
67
+		$this->currentCookieData = Cookie::get($this->cookie);
68
+
69
+		if ($this->currentCookieData) {
70
+			Cookie::set($this->cookie, '');
71
+		}
72
+	}
73
+
74
+	public function close()
75
+	{
76
+	}
77
+
78
+	/**
79
+	 * Get the cryptography store for the specified session
80
+	 *
81
+	 * @param string $session_id
82
+	 * @return HybridSessionStore_Crypto
83
+	 */
84
+	protected function getCrypto($session_id)
85
+	{
86
+		$key = $this->getKey();
87
+
88
+		if (!$key) {
89
+			return null;
90
+		}
91
+
92
+		if (!$this->crypto || $this->crypto->getSalt() != $session_id) {
93
+			$this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id);
94
+		}
95
+
96
+		return $this->crypto;
97
+	}
98
+
99
+	public function read($session_id)
100
+	{
101
+		// Check ability to safely decrypt content
102
+		if (!$this->currentCookieData
103
+			|| !($crypto = $this->getCrypto($session_id))
104
+		) {
105
+			return;
106
+		}
107
+
108
+		// Decrypt and invalidate old data
109
+		$cookieData = $crypto->decrypt($this->currentCookieData);
110
+		$this->currentCookieData = null;
111
+
112
+		// Verify expiration
113
+		if ($cookieData) {
114
+			$expiry = (int)substr($cookieData, 0, 10);
115
+			$data = substr($cookieData, 10);
116
+
117
+			if ($expiry > $this->getNow()) {
118
+				return $data;
119
+			}
120
+		}
121
+	}
122
+
123
+	/**
124
+	 * Determine if the session could be verifably written to cookie storage
125
+	 *
126
+	 * @return bool
127
+	 */
128
+	protected function canWrite()
129
+	{
130
+		return !headers_sent();
131
+	}
132
+
133
+	public function write($session_id, $session_data)
134
+	{
135
+		// Check ability to safely encrypt and write content
136
+		if (!$this->canWrite()
137
+			|| (strlen($session_data) > static::config()->get('max_length'))
138
+			|| !($crypto = $this->getCrypto($session_id))
139
+		) {
140
+			return false;
141
+		}
142
+
143
+		// Prepare content for write
144
+		$params = session_get_cookie_params();
145
+		// Total max lifetime, stored internally
146
+		$lifetime = $this->getLifetime();
147
+		$expiry = $this->getNow() + $lifetime;
148
+
149
+		// Restore the known good cookie value
150
+		$this->currentCookieData = $this->crypto->encrypt(
151
+			sprintf('%010u', $expiry) . $session_data
152
+		);
153
+
154
+		// Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero)
155
+		$cookieLifetime = min((int)$params['lifetime'], $lifetime);
156
+
157
+		Cookie::set(
158
+			$this->cookie,
159
+			$this->currentCookieData,
160
+			$cookieLifetime / 86400,
161
+			$params['path'],
162
+			$params['domain'],
163
+			$params['secure'],
164
+			$params['httponly']
165
+		);
166
+
167
+		return true;
168
+	}
169
+
170
+	public function destroy($session_id)
171
+	{
172
+		$this->currentCookieData = null;
173
+
174
+		$params = session_get_cookie_params();
175
+
176
+		Cookie::force_expiry(
177
+			$this->cookie,
178
+			$params['path'],
179
+			$params['domain'],
180
+			$params['secure'],
181
+			$params['httponly']
182
+		);
183
+	}
184
+
185
+	public function gc($maxlifetime)
186
+	{
187
+		// NOP
188
+	}
189 189
 }
Please login to merge, or discard this patch.
src/Store/BaseStore.php 1 patch
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -8,56 +8,56 @@
 block discarded – undo
8 8
 
9 9
 abstract class BaseStore implements SessionHandlerInterface
10 10
 {
11
-    use Configurable;
12
-
13
-    /**
14
-     * Session secret key
15
-     *
16
-     * @var string
17
-     */
18
-    protected $key = null;
19
-
20
-    /**
21
-     * Assign a new session secret key
22
-     *
23
-     * @param string $key
24
-     */
25
-    public function setKey($key)
26
-    {
27
-        $this->key = $key;
28
-    }
29
-
30
-    /**
31
-     * Get the session secret key
32
-     *
33
-     * @return string
34
-     */
35
-    protected function getKey()
36
-    {
37
-        return $this->key;
38
-    }
39
-
40
-    /**
41
-     * Get lifetime in number of seconds
42
-     *
43
-     * @return int
44
-     */
45
-    protected function getLifetime()
46
-    {
47
-        $params = session_get_cookie_params();
48
-        $cookieLifetime = (int)$params['lifetime'];
49
-        $gcLifetime = (int)ini_get('session.gc_maxlifetime');
50
-
51
-        return $cookieLifetime ? min($cookieLifetime, $gcLifetime) : $gcLifetime;
52
-    }
53
-
54
-    /**
55
-     * Gets the current unix timestamp
56
-     *
57
-     * @return int
58
-     */
59
-    protected function getNow()
60
-    {
61
-        return (int) DBDatetime::now()->getTimestamp();
62
-    }
11
+	use Configurable;
12
+
13
+	/**
14
+	 * Session secret key
15
+	 *
16
+	 * @var string
17
+	 */
18
+	protected $key = null;
19
+
20
+	/**
21
+	 * Assign a new session secret key
22
+	 *
23
+	 * @param string $key
24
+	 */
25
+	public function setKey($key)
26
+	{
27
+		$this->key = $key;
28
+	}
29
+
30
+	/**
31
+	 * Get the session secret key
32
+	 *
33
+	 * @return string
34
+	 */
35
+	protected function getKey()
36
+	{
37
+		return $this->key;
38
+	}
39
+
40
+	/**
41
+	 * Get lifetime in number of seconds
42
+	 *
43
+	 * @return int
44
+	 */
45
+	protected function getLifetime()
46
+	{
47
+		$params = session_get_cookie_params();
48
+		$cookieLifetime = (int)$params['lifetime'];
49
+		$gcLifetime = (int)ini_get('session.gc_maxlifetime');
50
+
51
+		return $cookieLifetime ? min($cookieLifetime, $gcLifetime) : $gcLifetime;
52
+	}
53
+
54
+	/**
55
+	 * Gets the current unix timestamp
56
+	 *
57
+	 * @return int
58
+	 */
59
+	protected function getNow()
60
+	{
61
+		return (int) DBDatetime::now()->getTimestamp();
62
+	}
63 63
 }
Please login to merge, or discard this patch.