@@ -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,184 +22,184 @@ |
||
| 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 | - $canWrite = $this->canWrite(); |
|
| 136 | - $isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); |
|
| 137 | - $crypto = $this->getCrypto($session_id); |
|
| 138 | - |
|
| 139 | - // Check ability to safely encrypt and write content |
|
| 140 | - if (!$canWrite || $isExceedingCookieLimit || !$crypto) { |
|
| 141 | - if ($canWrite && $isExceedingCookieLimit) { |
|
| 142 | - $params = session_get_cookie_params(); |
|
| 143 | - // Clear stored cookie value and cookie when length exceeds the set limit |
|
| 144 | - $this->currentCookieData = null; |
|
| 145 | - Cookie::set( |
|
| 146 | - $this->cookie, |
|
| 147 | - '', |
|
| 148 | - 0, |
|
| 149 | - $params['path'], |
|
| 150 | - $params['domain'], |
|
| 151 | - $params['secure'], |
|
| 152 | - $params['httponly'] |
|
| 153 | - ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Prepare content for write |
|
| 160 | - $params = session_get_cookie_params(); |
|
| 161 | - // Total max lifetime, stored internally |
|
| 162 | - $lifetime = $this->getLifetime(); |
|
| 163 | - $expiry = $this->getNow() + $lifetime; |
|
| 164 | - |
|
| 165 | - // Restore the known good cookie value |
|
| 166 | - $this->currentCookieData = $this->crypto->encrypt( |
|
| 167 | - sprintf('%010u', $expiry) . $session_data |
|
| 168 | - ); |
|
| 169 | - |
|
| 170 | - // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 171 | - $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 172 | - |
|
| 173 | - Cookie::set( |
|
| 174 | - $this->cookie, |
|
| 175 | - $this->currentCookieData, |
|
| 176 | - $cookieLifetime / 86400, |
|
| 177 | - $params['path'], |
|
| 178 | - $params['domain'], |
|
| 179 | - $params['secure'], |
|
| 180 | - $params['httponly'] |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - return true; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - public function destroy($session_id) |
|
| 187 | - { |
|
| 188 | - $this->currentCookieData = null; |
|
| 189 | - |
|
| 190 | - $params = session_get_cookie_params(); |
|
| 191 | - |
|
| 192 | - Cookie::force_expiry( |
|
| 193 | - $this->cookie, |
|
| 194 | - $params['path'], |
|
| 195 | - $params['domain'], |
|
| 196 | - $params['secure'], |
|
| 197 | - $params['httponly'] |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function gc($maxlifetime) |
|
| 202 | - { |
|
| 203 | - // NOP |
|
| 204 | - } |
|
| 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 | + $canWrite = $this->canWrite(); |
|
| 136 | + $isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); |
|
| 137 | + $crypto = $this->getCrypto($session_id); |
|
| 138 | + |
|
| 139 | + // Check ability to safely encrypt and write content |
|
| 140 | + if (!$canWrite || $isExceedingCookieLimit || !$crypto) { |
|
| 141 | + if ($canWrite && $isExceedingCookieLimit) { |
|
| 142 | + $params = session_get_cookie_params(); |
|
| 143 | + // Clear stored cookie value and cookie when length exceeds the set limit |
|
| 144 | + $this->currentCookieData = null; |
|
| 145 | + Cookie::set( |
|
| 146 | + $this->cookie, |
|
| 147 | + '', |
|
| 148 | + 0, |
|
| 149 | + $params['path'], |
|
| 150 | + $params['domain'], |
|
| 151 | + $params['secure'], |
|
| 152 | + $params['httponly'] |
|
| 153 | + ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Prepare content for write |
|
| 160 | + $params = session_get_cookie_params(); |
|
| 161 | + // Total max lifetime, stored internally |
|
| 162 | + $lifetime = $this->getLifetime(); |
|
| 163 | + $expiry = $this->getNow() + $lifetime; |
|
| 164 | + |
|
| 165 | + // Restore the known good cookie value |
|
| 166 | + $this->currentCookieData = $this->crypto->encrypt( |
|
| 167 | + sprintf('%010u', $expiry) . $session_data |
|
| 168 | + ); |
|
| 169 | + |
|
| 170 | + // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 171 | + $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 172 | + |
|
| 173 | + Cookie::set( |
|
| 174 | + $this->cookie, |
|
| 175 | + $this->currentCookieData, |
|
| 176 | + $cookieLifetime / 86400, |
|
| 177 | + $params['path'], |
|
| 178 | + $params['domain'], |
|
| 179 | + $params['secure'], |
|
| 180 | + $params['httponly'] |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + return true; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + public function destroy($session_id) |
|
| 187 | + { |
|
| 188 | + $this->currentCookieData = null; |
|
| 189 | + |
|
| 190 | + $params = session_get_cookie_params(); |
|
| 191 | + |
|
| 192 | + Cookie::force_expiry( |
|
| 193 | + $this->cookie, |
|
| 194 | + $params['path'], |
|
| 195 | + $params['domain'], |
|
| 196 | + $params['secure'], |
|
| 197 | + $params['httponly'] |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function gc($maxlifetime) |
|
| 202 | + { |
|
| 203 | + // NOP |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -8,20 +8,20 @@ |
||
| 8 | 8 | |
| 9 | 9 | class OpenSSLCryptoTest extends SapphireTest |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @requires extension openssl |
|
| 13 | - */ |
|
| 14 | - public function testIntegrity() |
|
| 15 | - { |
|
| 16 | - $key = random_bytes(8); |
|
| 17 | - $salt = random_bytes(16); |
|
| 11 | + /** |
|
| 12 | + * @requires extension openssl |
|
| 13 | + */ |
|
| 14 | + public function testIntegrity() |
|
| 15 | + { |
|
| 16 | + $key = random_bytes(8); |
|
| 17 | + $salt = random_bytes(16); |
|
| 18 | 18 | |
| 19 | - $handler = new OpenSSLCrypto($key, $salt); |
|
| 19 | + $handler = new OpenSSLCrypto($key, $salt); |
|
| 20 | 20 | |
| 21 | - for ($i=0; $i<1000; ++$i) { |
|
| 22 | - $data = random_bytes(1024 * 4); |
|
| 21 | + for ($i=0; $i<1000; ++$i) { |
|
| 22 | + $data = random_bytes(1024 * 4); |
|
| 23 | 23 | |
| 24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 25 | - } |
|
| 26 | - } |
|
| 24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -10,29 +10,29 @@ |
||
| 10 | 10 | |
| 11 | 11 | class DatabaseStoreTest extends AbstractTest |
| 12 | 12 | { |
| 13 | - protected function setUp() |
|
| 14 | - { |
|
| 15 | - parent::setUp(); |
|
| 16 | - |
|
| 17 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 18 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 19 | - } |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - protected function getStore() |
|
| 23 | - { |
|
| 24 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
| 25 | - $store->setKey(uniqid()); |
|
| 26 | - |
|
| 27 | - return $store; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function testDataCodecIntegrity() |
|
| 31 | - { |
|
| 32 | - for ($i=0; $i<1000; ++$i) { |
|
| 33 | - $data = random_bytes(1024 * 4); |
|
| 34 | - |
|
| 35 | - $this->assertEquals($data, DataCodec::decode(DataCodec::encode($data))); |
|
| 36 | - } |
|
| 37 | - } |
|
| 13 | + protected function setUp() |
|
| 14 | + { |
|
| 15 | + parent::setUp(); |
|
| 16 | + |
|
| 17 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 18 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 19 | + } |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + protected function getStore() |
|
| 23 | + { |
|
| 24 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
| 25 | + $store->setKey(uniqid()); |
|
| 26 | + |
|
| 27 | + return $store; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function testDataCodecIntegrity() |
|
| 31 | + { |
|
| 32 | + for ($i=0; $i<1000; ++$i) { |
|
| 33 | + $data = random_bytes(1024 * 4); |
|
| 34 | + |
|
| 35 | + $this->assertEquals($data, DataCodec::decode(DataCodec::encode($data))); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -8,20 +8,20 @@ |
||
| 8 | 8 | |
| 9 | 9 | class McryptCryptoTest extends SapphireTest |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @requires extension mcrypt |
|
| 13 | - */ |
|
| 14 | - public function testIntegrity() |
|
| 15 | - { |
|
| 16 | - $key = random_bytes(8); |
|
| 17 | - $salt = random_bytes(16); |
|
| 11 | + /** |
|
| 12 | + * @requires extension mcrypt |
|
| 13 | + */ |
|
| 14 | + public function testIntegrity() |
|
| 15 | + { |
|
| 16 | + $key = random_bytes(8); |
|
| 17 | + $salt = random_bytes(16); |
|
| 18 | 18 | |
| 19 | - $handler = new McryptCrypto($key, $salt); |
|
| 19 | + $handler = new McryptCrypto($key, $salt); |
|
| 20 | 20 | |
| 21 | - for ($i=0; $i<1000; ++$i) { |
|
| 22 | - $data = random_bytes(1024 * 4); |
|
| 21 | + for ($i=0; $i<1000; ++$i) { |
|
| 22 | + $data = random_bytes(1024 * 4); |
|
| 23 | 23 | |
| 24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 25 | - } |
|
| 26 | - } |
|
| 24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -13,38 +13,38 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class DataCodec |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Encode binary data into ASCII string (a subset of UTF-8) |
|
| 18 | - * |
|
| 19 | - * @param string $data This is a binary blob |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public static function encode($data) { |
|
| 24 | - return json_encode([ |
|
| 25 | - self::class, |
|
| 26 | - base64_encode($data) |
|
| 27 | - ]); |
|
| 28 | - } |
|
| 16 | + /** |
|
| 17 | + * Encode binary data into ASCII string (a subset of UTF-8) |
|
| 18 | + * |
|
| 19 | + * @param string $data This is a binary blob |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public static function encode($data) { |
|
| 24 | + return json_encode([ |
|
| 25 | + self::class, |
|
| 26 | + base64_encode($data) |
|
| 27 | + ]); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Decode ASCII string into original binary data (a php string) |
|
| 32 | - * |
|
| 33 | - * @param string $text |
|
| 34 | - * |
|
| 35 | - * @param null|string |
|
| 36 | - */ |
|
| 37 | - public static function decode($text) { |
|
| 38 | - $struct = json_decode($text, true, 2); |
|
| 30 | + /** |
|
| 31 | + * Decode ASCII string into original binary data (a php string) |
|
| 32 | + * |
|
| 33 | + * @param string $text |
|
| 34 | + * |
|
| 35 | + * @param null|string |
|
| 36 | + */ |
|
| 37 | + public static function decode($text) { |
|
| 38 | + $struct = json_decode($text, true, 2); |
|
| 39 | 39 | |
| 40 | - if (!is_array($struct) || count($struct) !== 2) { |
|
| 41 | - return null; |
|
| 42 | - } |
|
| 40 | + if (!is_array($struct) || count($struct) !== 2) { |
|
| 41 | + return null; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
| 45 | - return null; |
|
| 46 | - } |
|
| 44 | + if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
| 45 | + return null; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return base64_decode($struct[1]); |
|
| 49 | - } |
|
| 48 | + return base64_decode($struct[1]); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -13,94 +13,94 @@ |
||
| 13 | 13 | class DatabaseStore extends BaseStore |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Determine if the DB is ready to use. |
|
| 18 | - * |
|
| 19 | - * @return bool |
|
| 20 | - * @throws Exception |
|
| 21 | - */ |
|
| 22 | - protected function isDatabaseReady() |
|
| 23 | - { |
|
| 24 | - // Such as during setup of testsession prior to DB connection. |
|
| 25 | - if (!DB::is_active()) { |
|
| 26 | - return false; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - // If we have a DB of the wrong type then complain |
|
| 30 | - if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
| 31 | - throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - // Prevent freakout during dev/build |
|
| 35 | - return ClassInfo::hasTable('HybridSessionDataObject'); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function open($save_path, $name) |
|
| 39 | - { |
|
| 40 | - // no-op |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function close() |
|
| 44 | - { |
|
| 45 | - // no-op |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function read($session_id) |
|
| 49 | - { |
|
| 50 | - if (!$this->isDatabaseReady()) { |
|
| 51 | - return null; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $query = sprintf( |
|
| 55 | - 'SELECT "Data" FROM "HybridSessionDataObject" |
|
| 16 | + /** |
|
| 17 | + * Determine if the DB is ready to use. |
|
| 18 | + * |
|
| 19 | + * @return bool |
|
| 20 | + * @throws Exception |
|
| 21 | + */ |
|
| 22 | + protected function isDatabaseReady() |
|
| 23 | + { |
|
| 24 | + // Such as during setup of testsession prior to DB connection. |
|
| 25 | + if (!DB::is_active()) { |
|
| 26 | + return false; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + // If we have a DB of the wrong type then complain |
|
| 30 | + if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
| 31 | + throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + // Prevent freakout during dev/build |
|
| 35 | + return ClassInfo::hasTable('HybridSessionDataObject'); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function open($save_path, $name) |
|
| 39 | + { |
|
| 40 | + // no-op |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function close() |
|
| 44 | + { |
|
| 45 | + // no-op |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function read($session_id) |
|
| 49 | + { |
|
| 50 | + if (!$this->isDatabaseReady()) { |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $query = sprintf( |
|
| 55 | + 'SELECT "Data" FROM "HybridSessionDataObject" |
|
| 56 | 56 | WHERE "SessionID" = \'%s\' AND "Expiry" >= %s', |
| 57 | - Convert::raw2sql($session_id), |
|
| 58 | - $this->getNow() |
|
| 59 | - ); |
|
| 57 | + Convert::raw2sql($session_id), |
|
| 58 | + $this->getNow() |
|
| 59 | + ); |
|
| 60 | 60 | |
| 61 | - $result = DB::query($query); |
|
| 61 | + $result = DB::query($query); |
|
| 62 | 62 | |
| 63 | - if ($result && $result->numRecords()) { |
|
| 64 | - $data = $result->first(); |
|
| 65 | - $decoded = DataCodec::decode($data['Data']); |
|
| 66 | - return is_null($decoded) ? $data['Data'] : $decoded; |
|
| 67 | - } |
|
| 68 | - } |
|
| 63 | + if ($result && $result->numRecords()) { |
|
| 64 | + $data = $result->first(); |
|
| 65 | + $decoded = DataCodec::decode($data['Data']); |
|
| 66 | + return is_null($decoded) ? $data['Data'] : $decoded; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public function write($session_id, $session_data) |
|
| 71 | - { |
|
| 72 | - if (!$this->isDatabaseReady()) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 70 | + public function write($session_id, $session_data) |
|
| 71 | + { |
|
| 72 | + if (!$this->isDatabaseReady()) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - $expiry = $this->getNow() + $this->getLifetime(); |
|
| 76 | + $expiry = $this->getNow() + $this->getLifetime(); |
|
| 77 | 77 | |
| 78 | - DB::query($str = sprintf( |
|
| 79 | - 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
| 78 | + DB::query($str = sprintf( |
|
| 79 | + 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
| 80 | 80 | VALUES (\'%1$s\', %2$u, \'%3$s\') |
| 81 | 81 | ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'', |
| 82 | - Convert::raw2sql($session_id), |
|
| 83 | - $expiry, |
|
| 84 | - Convert::raw2sql(DataCodec::encode($session_data)) |
|
| 85 | - )); |
|
| 86 | - |
|
| 87 | - return true; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function destroy($session_id) |
|
| 91 | - { |
|
| 92 | - // NOP |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function gc($maxlifetime) |
|
| 96 | - { |
|
| 97 | - if (!$this->isDatabaseReady()) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - DB::query(sprintf( |
|
| 102 | - 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
| 103 | - $this->getNow() |
|
| 104 | - )); |
|
| 105 | - } |
|
| 82 | + Convert::raw2sql($session_id), |
|
| 83 | + $expiry, |
|
| 84 | + Convert::raw2sql(DataCodec::encode($session_data)) |
|
| 85 | + )); |
|
| 86 | + |
|
| 87 | + return true; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function destroy($session_id) |
|
| 91 | + { |
|
| 92 | + // NOP |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function gc($maxlifetime) |
|
| 96 | + { |
|
| 97 | + if (!$this->isDatabaseReady()) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + DB::query(sprintf( |
|
| 102 | + 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
| 103 | + $this->getNow() |
|
| 104 | + )); |
|
| 105 | + } |
|
| 106 | 106 | } |