@@ -25,6 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | /** |
27 | 27 | * @param SessionHandlerInterface[] |
28 | + * @param BaseStore[] $handlers |
|
28 | 29 | * |
29 | 30 | * @return $this |
30 | 31 | */ |
@@ -38,6 +39,7 @@ discard block |
||
38 | 39 | |
39 | 40 | /** |
40 | 41 | * @param string |
42 | + * @param string $key |
|
41 | 43 | * |
42 | 44 | * @return $this |
43 | 45 | */ |
@@ -9,152 +9,152 @@ |
||
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->getHandlers() 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 | - foreach ($this->getHandlers() as $handler) { |
|
72 | - $handler->open($save_path, $name); |
|
73 | - } |
|
74 | - |
|
75 | - return true; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - public function close() |
|
82 | - { |
|
83 | - foreach ($this->getHandlers() as $handler) { |
|
84 | - $handler->close(); |
|
85 | - } |
|
86 | - |
|
87 | - return true; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $session_id |
|
92 | - * |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function read($session_id) |
|
96 | - { |
|
97 | - foreach ($this->getHandlers() as $handler) { |
|
98 | - if ($data = $handler->read($session_id)) { |
|
99 | - return $data; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - return ''; |
|
104 | - } |
|
105 | - |
|
106 | - public function write($session_id, $session_data) |
|
107 | - { |
|
108 | - foreach ($this->getHandlers() as $handler) { |
|
109 | - if ($handler->write($session_id, $session_data)) { |
|
110 | - return true; |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return false; |
|
115 | - } |
|
116 | - |
|
117 | - public function destroy($session_id) |
|
118 | - { |
|
119 | - foreach ($this->getHandlers() as $handler) { |
|
120 | - $handler->destroy($session_id); |
|
121 | - } |
|
122 | - |
|
123 | - return true; |
|
124 | - } |
|
125 | - |
|
126 | - public function gc($maxlifetime) |
|
127 | - { |
|
128 | - foreach ($this->getHandlers() as $handler) { |
|
129 | - $handler->gc($maxlifetime); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Register the session handler as the default |
|
135 | - * |
|
136 | - * @param string $key Desired session key |
|
137 | - */ |
|
138 | - public static function init($key = null) |
|
139 | - { |
|
140 | - $instance = Injector::inst()->get(__CLASS__); |
|
141 | - |
|
142 | - if (empty($key)) { |
|
143 | - user_error( |
|
144 | - 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
145 | - E_USER_WARNING |
|
146 | - ); |
|
147 | - } else { |
|
148 | - $instance->setKey($key); |
|
149 | - } |
|
150 | - |
|
151 | - session_set_save_handler($instance, true); |
|
152 | - |
|
153 | - self::$enabled = true; |
|
154 | - } |
|
155 | - |
|
156 | - public static function is_enabled() |
|
157 | - { |
|
158 | - return self::$enabled; |
|
159 | - } |
|
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->getHandlers() 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 | + foreach ($this->getHandlers() as $handler) { |
|
72 | + $handler->open($save_path, $name); |
|
73 | + } |
|
74 | + |
|
75 | + return true; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + public function close() |
|
82 | + { |
|
83 | + foreach ($this->getHandlers() as $handler) { |
|
84 | + $handler->close(); |
|
85 | + } |
|
86 | + |
|
87 | + return true; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $session_id |
|
92 | + * |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function read($session_id) |
|
96 | + { |
|
97 | + foreach ($this->getHandlers() as $handler) { |
|
98 | + if ($data = $handler->read($session_id)) { |
|
99 | + return $data; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + return ''; |
|
104 | + } |
|
105 | + |
|
106 | + public function write($session_id, $session_data) |
|
107 | + { |
|
108 | + foreach ($this->getHandlers() as $handler) { |
|
109 | + if ($handler->write($session_id, $session_data)) { |
|
110 | + return true; |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return false; |
|
115 | + } |
|
116 | + |
|
117 | + public function destroy($session_id) |
|
118 | + { |
|
119 | + foreach ($this->getHandlers() as $handler) { |
|
120 | + $handler->destroy($session_id); |
|
121 | + } |
|
122 | + |
|
123 | + return true; |
|
124 | + } |
|
125 | + |
|
126 | + public function gc($maxlifetime) |
|
127 | + { |
|
128 | + foreach ($this->getHandlers() as $handler) { |
|
129 | + $handler->gc($maxlifetime); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Register the session handler as the default |
|
135 | + * |
|
136 | + * @param string $key Desired session key |
|
137 | + */ |
|
138 | + public static function init($key = null) |
|
139 | + { |
|
140 | + $instance = Injector::inst()->get(__CLASS__); |
|
141 | + |
|
142 | + if (empty($key)) { |
|
143 | + user_error( |
|
144 | + 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
145 | + E_USER_WARNING |
|
146 | + ); |
|
147 | + } else { |
|
148 | + $instance->setKey($key); |
|
149 | + } |
|
150 | + |
|
151 | + session_set_save_handler($instance, true); |
|
152 | + |
|
153 | + self::$enabled = true; |
|
154 | + } |
|
155 | + |
|
156 | + public static function is_enabled() |
|
157 | + { |
|
158 | + return self::$enabled; |
|
159 | + } |
|
160 | 160 | } |
@@ -9,83 +9,83 @@ |
||
9 | 9 | |
10 | 10 | class HybridSessionTest extends SapphireTest |
11 | 11 | { |
12 | - /** |
|
13 | - * @var BaseStore |
|
14 | - */ |
|
15 | - protected $handler; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var HybridSession |
|
19 | - */ |
|
20 | - protected $instance; |
|
21 | - |
|
22 | - protected function setUp() |
|
23 | - { |
|
24 | - parent::setUp(); |
|
25 | - |
|
26 | - $this->handler = $this->createMock(TestCookieStore::class); |
|
27 | - |
|
28 | - $this->instance = new HybridSession(); |
|
29 | - } |
|
30 | - |
|
31 | - public function testSetHandlersAlsoSetsKeyToEachHandler() |
|
32 | - { |
|
33 | - $this->instance->setKey('foobar'); |
|
34 | - $this->handler->expects($this->once())->method('setKey')->with('foobar'); |
|
35 | - $this->instance->setHandlers([$this->handler]); |
|
36 | - } |
|
37 | - |
|
38 | - public function testOpenDelegatesToAllHandlers() |
|
39 | - { |
|
40 | - $this->handler->expects($this->once())->method('open')->with('foo', 'bar'); |
|
41 | - $this->instance->setHandlers([$this->handler]); |
|
42 | - $this->assertTrue($this->instance->open('foo', 'bar'), 'Method returns true after delegation'); |
|
43 | - } |
|
44 | - |
|
45 | - public function testCloseDelegatesToAllHandlers() |
|
46 | - { |
|
47 | - $this->handler->expects($this->once())->method('close'); |
|
48 | - $this->instance->setHandlers([$this->handler]); |
|
49 | - $this->assertTrue($this->instance->close(), 'Method returns true after delegation'); |
|
50 | - } |
|
51 | - |
|
52 | - public function testReadReturnsEmptyStringWithNoHandlers() |
|
53 | - { |
|
54 | - $this->handler->expects($this->once())->method('read')->with('foosession')->willReturn(false); |
|
55 | - $this->instance->setHandlers([$this->handler]); |
|
56 | - $this->assertSame('', $this->instance->read('foosession')); |
|
57 | - } |
|
58 | - |
|
59 | - public function testReadReturnsHandlerDelegateResult() |
|
60 | - { |
|
61 | - $this->handler->expects($this->once())->method('read')->with('foo.session')->willReturn('success!'); |
|
62 | - $this->instance->setHandlers([$this->handler]); |
|
63 | - $this->assertSame('success!', $this->instance->read('foo.session')); |
|
64 | - } |
|
65 | - |
|
66 | - public function testWriteDelegatesToHandlerAndReturnsTrue() |
|
67 | - { |
|
68 | - $this->handler->expects($this->once())->method('write')->with('foo', 'bar')->willReturn(true); |
|
69 | - $this->instance->setHandlers([$this->handler]); |
|
70 | - $this->assertTrue($this->instance->write('foo', 'bar')); |
|
71 | - } |
|
72 | - |
|
73 | - public function testWriteReturnsFalseWithNoHandlers() |
|
74 | - { |
|
75 | - $this->assertFalse($this->instance->write('no', 'handlers')); |
|
76 | - } |
|
77 | - |
|
78 | - public function testDestroyDelegatesToHandler() |
|
79 | - { |
|
80 | - $this->handler->expects($this->once())->method('destroy')->with('sessid1234'); |
|
81 | - $this->instance->setHandlers([$this->handler]); |
|
82 | - $this->assertTrue($this->instance->destroy('sessid1234'), 'Method returns true after delegation'); |
|
83 | - } |
|
84 | - |
|
85 | - public function testGcDelegatesToHandlers() |
|
86 | - { |
|
87 | - $this->handler->expects($this->once())->method('gc')->with(12345); |
|
88 | - $this->instance->setHandlers([$this->handler]); |
|
89 | - $this->instance->gc(12345); |
|
90 | - } |
|
12 | + /** |
|
13 | + * @var BaseStore |
|
14 | + */ |
|
15 | + protected $handler; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var HybridSession |
|
19 | + */ |
|
20 | + protected $instance; |
|
21 | + |
|
22 | + protected function setUp() |
|
23 | + { |
|
24 | + parent::setUp(); |
|
25 | + |
|
26 | + $this->handler = $this->createMock(TestCookieStore::class); |
|
27 | + |
|
28 | + $this->instance = new HybridSession(); |
|
29 | + } |
|
30 | + |
|
31 | + public function testSetHandlersAlsoSetsKeyToEachHandler() |
|
32 | + { |
|
33 | + $this->instance->setKey('foobar'); |
|
34 | + $this->handler->expects($this->once())->method('setKey')->with('foobar'); |
|
35 | + $this->instance->setHandlers([$this->handler]); |
|
36 | + } |
|
37 | + |
|
38 | + public function testOpenDelegatesToAllHandlers() |
|
39 | + { |
|
40 | + $this->handler->expects($this->once())->method('open')->with('foo', 'bar'); |
|
41 | + $this->instance->setHandlers([$this->handler]); |
|
42 | + $this->assertTrue($this->instance->open('foo', 'bar'), 'Method returns true after delegation'); |
|
43 | + } |
|
44 | + |
|
45 | + public function testCloseDelegatesToAllHandlers() |
|
46 | + { |
|
47 | + $this->handler->expects($this->once())->method('close'); |
|
48 | + $this->instance->setHandlers([$this->handler]); |
|
49 | + $this->assertTrue($this->instance->close(), 'Method returns true after delegation'); |
|
50 | + } |
|
51 | + |
|
52 | + public function testReadReturnsEmptyStringWithNoHandlers() |
|
53 | + { |
|
54 | + $this->handler->expects($this->once())->method('read')->with('foosession')->willReturn(false); |
|
55 | + $this->instance->setHandlers([$this->handler]); |
|
56 | + $this->assertSame('', $this->instance->read('foosession')); |
|
57 | + } |
|
58 | + |
|
59 | + public function testReadReturnsHandlerDelegateResult() |
|
60 | + { |
|
61 | + $this->handler->expects($this->once())->method('read')->with('foo.session')->willReturn('success!'); |
|
62 | + $this->instance->setHandlers([$this->handler]); |
|
63 | + $this->assertSame('success!', $this->instance->read('foo.session')); |
|
64 | + } |
|
65 | + |
|
66 | + public function testWriteDelegatesToHandlerAndReturnsTrue() |
|
67 | + { |
|
68 | + $this->handler->expects($this->once())->method('write')->with('foo', 'bar')->willReturn(true); |
|
69 | + $this->instance->setHandlers([$this->handler]); |
|
70 | + $this->assertTrue($this->instance->write('foo', 'bar')); |
|
71 | + } |
|
72 | + |
|
73 | + public function testWriteReturnsFalseWithNoHandlers() |
|
74 | + { |
|
75 | + $this->assertFalse($this->instance->write('no', 'handlers')); |
|
76 | + } |
|
77 | + |
|
78 | + public function testDestroyDelegatesToHandler() |
|
79 | + { |
|
80 | + $this->handler->expects($this->once())->method('destroy')->with('sessid1234'); |
|
81 | + $this->instance->setHandlers([$this->handler]); |
|
82 | + $this->assertTrue($this->instance->destroy('sessid1234'), 'Method returns true after delegation'); |
|
83 | + } |
|
84 | + |
|
85 | + public function testGcDelegatesToHandlers() |
|
86 | + { |
|
87 | + $this->handler->expects($this->once())->method('gc')->with(12345); |
|
88 | + $this->instance->setHandlers([$this->handler]); |
|
89 | + $this->instance->gc(12345); |
|
90 | + } |
|
91 | 91 | } |
@@ -7,21 +7,21 @@ |
||
7 | 7 | |
8 | 8 | class TestCookieStore extends CookieStore implements TestOnly |
9 | 9 | { |
10 | - /** |
|
11 | - * Override value of 'headers_sent' but only if running tests. |
|
12 | - * |
|
13 | - * Set to true or false, or null to not override |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - public static $override_headers_sent = null; |
|
10 | + /** |
|
11 | + * Override value of 'headers_sent' but only if running tests. |
|
12 | + * |
|
13 | + * Set to true or false, or null to not override |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + public static $override_headers_sent = null; |
|
18 | 18 | |
19 | - protected function canWrite() |
|
20 | - { |
|
21 | - if (self::$override_headers_sent !== null) { |
|
22 | - return !self::$override_headers_sent; |
|
23 | - } |
|
19 | + protected function canWrite() |
|
20 | + { |
|
21 | + if (self::$override_headers_sent !== null) { |
|
22 | + return !self::$override_headers_sent; |
|
23 | + } |
|
24 | 24 | |
25 | - parent::canWrite(); |
|
26 | - } |
|
25 | + parent::canWrite(); |
|
26 | + } |
|
27 | 27 | } |
@@ -12,137 +12,137 @@ |
||
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 | - $decoded = $this->binaryDataJsonDecode($data['Data']); |
|
65 | - return is_null($decoded) ? $data['Data'] : $decoded; |
|
66 | - } |
|
67 | - } |
|
62 | + if ($result && $result->numRecords()) { |
|
63 | + $data = $result->first(); |
|
64 | + $decoded = $this->binaryDataJsonDecode($data['Data']); |
|
65 | + return is_null($decoded) ? $data['Data'] : $decoded; |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - public function write($session_id, $session_data) |
|
70 | - { |
|
71 | - if (!$this->isDatabaseReady()) { |
|
72 | - return false; |
|
73 | - } |
|
69 | + public function write($session_id, $session_data) |
|
70 | + { |
|
71 | + if (!$this->isDatabaseReady()) { |
|
72 | + return false; |
|
73 | + } |
|
74 | 74 | |
75 | - $expiry = $this->getNow() + $this->getLifetime(); |
|
75 | + $expiry = $this->getNow() + $this->getLifetime(); |
|
76 | 76 | |
77 | - DB::query($str = sprintf( |
|
78 | - 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
77 | + DB::query($str = sprintf( |
|
78 | + 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
79 | 79 | VALUES (\'%1$s\', %2$u, \'%3$s\') |
80 | 80 | ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'', |
81 | - Convert::raw2sql($session_id), |
|
82 | - $expiry, |
|
83 | - Convert::raw2sql($this->binaryDataJsonEncode($session_data)) |
|
84 | - )); |
|
85 | - |
|
86 | - return true; |
|
87 | - } |
|
88 | - |
|
89 | - public function destroy($session_id) |
|
90 | - { |
|
91 | - // NOP |
|
92 | - } |
|
93 | - |
|
94 | - public function gc($maxlifetime) |
|
95 | - { |
|
96 | - if (!$this->isDatabaseReady()) { |
|
97 | - return; |
|
98 | - } |
|
99 | - |
|
100 | - DB::query(sprintf( |
|
101 | - 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
102 | - $this->getNow() |
|
103 | - )); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Encode binary data into ASCII string (a subset of UTF-8) |
|
108 | - * |
|
109 | - * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
110 | - * binary data as text |
|
111 | - * |
|
112 | - * @param string $data This is a binary blob |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - private function binaryDataJsonEncode($data) |
|
117 | - { |
|
118 | - return json_encode([ |
|
119 | - self::class, |
|
120 | - base64_encode($data) |
|
121 | - ]); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Decode ASCII string into original binary data (a php string) |
|
126 | - * |
|
127 | - * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
128 | - * binary data as text |
|
129 | - * |
|
130 | - * @param string $text |
|
131 | - * |
|
132 | - * @param null|string |
|
133 | - */ |
|
134 | - private function binaryDataJsonDecode($text) |
|
135 | - { |
|
136 | - $struct = json_decode($text, true, 2); |
|
137 | - |
|
138 | - if (!is_array($struct) || count($struct) !== 2) { |
|
139 | - return null; |
|
140 | - } |
|
141 | - |
|
142 | - if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
143 | - return null; |
|
144 | - } |
|
145 | - |
|
146 | - return base64_decode($struct[1]); |
|
147 | - } |
|
81 | + Convert::raw2sql($session_id), |
|
82 | + $expiry, |
|
83 | + Convert::raw2sql($this->binaryDataJsonEncode($session_data)) |
|
84 | + )); |
|
85 | + |
|
86 | + return true; |
|
87 | + } |
|
88 | + |
|
89 | + public function destroy($session_id) |
|
90 | + { |
|
91 | + // NOP |
|
92 | + } |
|
93 | + |
|
94 | + public function gc($maxlifetime) |
|
95 | + { |
|
96 | + if (!$this->isDatabaseReady()) { |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + DB::query(sprintf( |
|
101 | + 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
102 | + $this->getNow() |
|
103 | + )); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Encode binary data into ASCII string (a subset of UTF-8) |
|
108 | + * |
|
109 | + * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
110 | + * binary data as text |
|
111 | + * |
|
112 | + * @param string $data This is a binary blob |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + private function binaryDataJsonEncode($data) |
|
117 | + { |
|
118 | + return json_encode([ |
|
119 | + self::class, |
|
120 | + base64_encode($data) |
|
121 | + ]); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Decode ASCII string into original binary data (a php string) |
|
126 | + * |
|
127 | + * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
128 | + * binary data as text |
|
129 | + * |
|
130 | + * @param string $text |
|
131 | + * |
|
132 | + * @param null|string |
|
133 | + */ |
|
134 | + private function binaryDataJsonDecode($text) |
|
135 | + { |
|
136 | + $struct = json_decode($text, true, 2); |
|
137 | + |
|
138 | + if (!is_array($struct) || count($struct) !== 2) { |
|
139 | + return null; |
|
140 | + } |
|
141 | + |
|
142 | + if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
143 | + return null; |
|
144 | + } |
|
145 | + |
|
146 | + return base64_decode($struct[1]); |
|
147 | + } |
|
148 | 148 | } |
@@ -9,69 +9,69 @@ |
||
9 | 9 | |
10 | 10 | class CookieStoreTest extends AbstractTest |
11 | 11 | { |
12 | - protected function getStore() |
|
13 | - { |
|
14 | - $store = Injector::inst()->get(CookieStore::class); |
|
15 | - $store->setKey(uniqid()); |
|
16 | - $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
12 | + protected function getStore() |
|
13 | + { |
|
14 | + $store = Injector::inst()->get(CookieStore::class); |
|
15 | + $store->setKey(uniqid()); |
|
16 | + $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
17 | 17 | |
18 | - return $store; |
|
19 | - } |
|
18 | + return $store; |
|
19 | + } |
|
20 | 20 | |
21 | - public function testStoreLargeData() |
|
22 | - { |
|
23 | - $session = uniqid(); |
|
24 | - $store = $this->getStore(); |
|
21 | + public function testStoreLargeData() |
|
22 | + { |
|
23 | + $session = uniqid(); |
|
24 | + $store = $this->getStore(); |
|
25 | 25 | |
26 | - // Test new session is blank |
|
27 | - $result = $store->read($session); |
|
28 | - $this->assertEmpty($result); |
|
26 | + // Test new session is blank |
|
27 | + $result = $store->read($session); |
|
28 | + $this->assertEmpty($result); |
|
29 | 29 | |
30 | - // Save data against session |
|
31 | - $data1 = array( |
|
32 | - 'Large' => str_repeat('A', 600), |
|
33 | - 'Content' => str_repeat('B', 600) |
|
34 | - ); |
|
35 | - $store->write($session, serialize($data1)); |
|
36 | - $result = $store->read($session); |
|
30 | + // Save data against session |
|
31 | + $data1 = array( |
|
32 | + 'Large' => str_repeat('A', 600), |
|
33 | + 'Content' => str_repeat('B', 600) |
|
34 | + ); |
|
35 | + $store->write($session, serialize($data1)); |
|
36 | + $result = $store->read($session); |
|
37 | 37 | |
38 | - // Cookies should not try to store data that large |
|
39 | - $this->assertEmpty($result); |
|
40 | - } |
|
38 | + // Cookies should not try to store data that large |
|
39 | + $this->assertEmpty($result); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Ensure that subsequent reads without the necessary write do not report data |
|
44 | - */ |
|
45 | - public function testReadInvalidatesData() |
|
46 | - { |
|
47 | - $session = uniqid(); |
|
48 | - $store = $this->getStore(); |
|
42 | + /** |
|
43 | + * Ensure that subsequent reads without the necessary write do not report data |
|
44 | + */ |
|
45 | + public function testReadInvalidatesData() |
|
46 | + { |
|
47 | + $session = uniqid(); |
|
48 | + $store = $this->getStore(); |
|
49 | 49 | |
50 | - // Test new session is blank |
|
51 | - $result = $store->read($session); |
|
52 | - $this->assertEmpty($result); |
|
50 | + // Test new session is blank |
|
51 | + $result = $store->read($session); |
|
52 | + $this->assertEmpty($result); |
|
53 | 53 | |
54 | - // Save data against session |
|
55 | - $data1 = array( |
|
56 | - 'Color' => 'red', |
|
57 | - 'Animal' => 'elephant' |
|
58 | - ); |
|
59 | - $store->write($session, serialize($data1)); |
|
60 | - $result = $store->read($session); |
|
61 | - $this->assertEquals($data1, unserialize($result)); |
|
54 | + // Save data against session |
|
55 | + $data1 = array( |
|
56 | + 'Color' => 'red', |
|
57 | + 'Animal' => 'elephant' |
|
58 | + ); |
|
59 | + $store->write($session, serialize($data1)); |
|
60 | + $result = $store->read($session); |
|
61 | + $this->assertEquals($data1, unserialize($result)); |
|
62 | 62 | |
63 | - // Since we have read the data into the result, the application could modify this content |
|
64 | - // and be unable to write it back due to headers being sent. We should thus assume |
|
65 | - // that subsequent reads without a successful write do not purport to have valid data |
|
66 | - $data1['Color'] = 'blue'; |
|
67 | - $result = $store->read($session); |
|
68 | - $this->assertEmpty($result); |
|
63 | + // Since we have read the data into the result, the application could modify this content |
|
64 | + // and be unable to write it back due to headers being sent. We should thus assume |
|
65 | + // that subsequent reads without a successful write do not purport to have valid data |
|
66 | + $data1['Color'] = 'blue'; |
|
67 | + $result = $store->read($session); |
|
68 | + $this->assertEmpty($result); |
|
69 | 69 | |
70 | - // Check that writing to cookie fails after headers are sent and these results remain |
|
71 | - // invalidated |
|
72 | - TestCookieStore::$override_headers_sent = true; |
|
73 | - $store->write($session, serialize($data1)); |
|
74 | - $result = $store->read($session); |
|
75 | - $this->assertEmpty($result); |
|
76 | - } |
|
70 | + // Check that writing to cookie fails after headers are sent and these results remain |
|
71 | + // invalidated |
|
72 | + TestCookieStore::$override_headers_sent = true; |
|
73 | + $store->write($session, serialize($data1)); |
|
74 | + $result = $store->read($session); |
|
75 | + $this->assertEmpty($result); |
|
76 | + } |
|
77 | 77 | } |
@@ -13,7 +13,7 @@ |
||
13 | 13 | { |
14 | 14 | $store = Injector::inst()->get(CookieStore::class); |
15 | 15 | $store->setKey(uniqid()); |
16 | - $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
16 | + $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE'); |
|
17 | 17 | |
18 | 18 | return $store; |
19 | 19 | } |