@@ -9,20 +9,20 @@ |
||
| 9 | 9 | |
| 10 | 10 | class DatabaseStoreTest extends AbstractTest |
| 11 | 11 | { |
| 12 | - protected function setUp() |
|
| 13 | - { |
|
| 14 | - parent::setUp(); |
|
| 12 | + protected function setUp() |
|
| 13 | + { |
|
| 14 | + parent::setUp(); |
|
| 15 | 15 | |
| 16 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 17 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 18 | - } |
|
| 19 | - } |
|
| 16 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 17 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 18 | + } |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - protected function getStore() |
|
| 22 | - { |
|
| 23 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
| 24 | - $store->setKey(uniqid()); |
|
| 21 | + protected function getStore() |
|
| 22 | + { |
|
| 23 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
| 24 | + $store->setKey(uniqid()); |
|
| 25 | 25 | |
| 26 | - return $store; |
|
| 27 | - } |
|
| 26 | + return $store; |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -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 | } |
@@ -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 | } |
@@ -22,172 +22,172 @@ |
||
| 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 | - if (strlen($session_data) > static::config()->get('max_length')) { |
|
| 141 | - $this->currentCookieData = null; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Prepare content for write |
|
| 148 | - $params = session_get_cookie_params(); |
|
| 149 | - // Total max lifetime, stored internally |
|
| 150 | - $lifetime = $this->getLifetime(); |
|
| 151 | - $expiry = $this->getNow() + $lifetime; |
|
| 152 | - |
|
| 153 | - // Restore the known good cookie value |
|
| 154 | - $this->currentCookieData = $this->crypto->encrypt( |
|
| 155 | - sprintf('%010u', $expiry) . $session_data |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 159 | - $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 160 | - |
|
| 161 | - Cookie::set( |
|
| 162 | - $this->cookie, |
|
| 163 | - $this->currentCookieData, |
|
| 164 | - $cookieLifetime / 86400, |
|
| 165 | - $params['path'], |
|
| 166 | - $params['domain'], |
|
| 167 | - $params['secure'], |
|
| 168 | - $params['httponly'] |
|
| 169 | - ); |
|
| 170 | - |
|
| 171 | - return true; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - public function destroy($session_id) |
|
| 175 | - { |
|
| 176 | - $this->currentCookieData = null; |
|
| 177 | - |
|
| 178 | - $params = session_get_cookie_params(); |
|
| 179 | - |
|
| 180 | - Cookie::force_expiry( |
|
| 181 | - $this->cookie, |
|
| 182 | - $params['path'], |
|
| 183 | - $params['domain'], |
|
| 184 | - $params['secure'], |
|
| 185 | - $params['httponly'] |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - public function gc($maxlifetime) |
|
| 190 | - { |
|
| 191 | - // NOP |
|
| 192 | - } |
|
| 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 | + if (strlen($session_data) > static::config()->get('max_length')) { |
|
| 141 | + $this->currentCookieData = null; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Prepare content for write |
|
| 148 | + $params = session_get_cookie_params(); |
|
| 149 | + // Total max lifetime, stored internally |
|
| 150 | + $lifetime = $this->getLifetime(); |
|
| 151 | + $expiry = $this->getNow() + $lifetime; |
|
| 152 | + |
|
| 153 | + // Restore the known good cookie value |
|
| 154 | + $this->currentCookieData = $this->crypto->encrypt( |
|
| 155 | + sprintf('%010u', $expiry) . $session_data |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 159 | + $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 160 | + |
|
| 161 | + Cookie::set( |
|
| 162 | + $this->cookie, |
|
| 163 | + $this->currentCookieData, |
|
| 164 | + $cookieLifetime / 86400, |
|
| 165 | + $params['path'], |
|
| 166 | + $params['domain'], |
|
| 167 | + $params['secure'], |
|
| 168 | + $params['httponly'] |
|
| 169 | + ); |
|
| 170 | + |
|
| 171 | + return true; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + public function destroy($session_id) |
|
| 175 | + { |
|
| 176 | + $this->currentCookieData = null; |
|
| 177 | + |
|
| 178 | + $params = session_get_cookie_params(); |
|
| 179 | + |
|
| 180 | + Cookie::force_expiry( |
|
| 181 | + $this->cookie, |
|
| 182 | + $params['path'], |
|
| 183 | + $params['domain'], |
|
| 184 | + $params['secure'], |
|
| 185 | + $params['httponly'] |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + public function gc($maxlifetime) |
|
| 190 | + { |
|
| 191 | + // NOP |
|
| 192 | + } |
|
| 193 | 193 | } |