Session read working
This commit is contained in:
		
							parent
							
								
									592752e18a
								
							
						
					
					
						commit
						33ed96eba0
					
				
							
								
								
									
										16
									
								
								src/Ratchet/Component/Session/Serialize/HandlerInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/Ratchet/Component/Session/Serialize/HandlerInterface.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | |||||||
|  | <?php | ||||||
|  | namespace Ratchet\Component\Session\Serialize; | ||||||
|  | 
 | ||||||
|  | interface HandlerInterface { | ||||||
|  |     /** | ||||||
|  |      * @param array | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     function serialize(array $data); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * @param string | ||||||
|  |      * @return array | ||||||
|  |      */ | ||||||
|  |     function unserialize($raw); | ||||||
|  | } | ||||||
							
								
								
									
										33
									
								
								src/Ratchet/Component/Session/Serialize/PhpBinaryHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/Ratchet/Component/Session/Serialize/PhpBinaryHandler.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | |||||||
|  | <?php | ||||||
|  | namespace Ratchet\Component\Session\Serialize; | ||||||
|  | 
 | ||||||
|  | class PhpBinaryHandler implements HandlerInterface { | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      */ | ||||||
|  |     function serialize(array $data) { | ||||||
|  |         throw new \RuntimeException("Serialize PhpHandler:serialize code not written yet, write me!"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      * @link http://ca2.php.net/manual/en/function.session-decode.php#108037 Code from this comment on php.net
 | ||||||
|  |      */ | ||||||
|  |     public function unserialize($raw) { | ||||||
|  |         $returnData = array(); | ||||||
|  |         $offset     = 0; | ||||||
|  | 
 | ||||||
|  |         while ($offset < strlen($raw)) { | ||||||
|  |             $num     = ord($raw[$offset]); | ||||||
|  |             $offset += 1; | ||||||
|  |             $varname = substr($raw, $offset, $num); | ||||||
|  |             $offset += $num; | ||||||
|  |             $data    = unserialize(substr($raw, $offset)); | ||||||
|  | 
 | ||||||
|  |             $returnData[$varname] = $data; | ||||||
|  |             $offset += strlen(serialize($data)); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return $returnData; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										37
									
								
								src/Ratchet/Component/Session/Serialize/PhpHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/Ratchet/Component/Session/Serialize/PhpHandler.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | |||||||
|  | <?php | ||||||
|  | namespace Ratchet\Component\Session\Serialize; | ||||||
|  | 
 | ||||||
|  | class PhpHandler implements HandlerInterface { | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      */ | ||||||
|  |     function serialize(array $data) { | ||||||
|  |         throw new \RuntimeException("Serialize PhpHandler:serialize code not written yet, write me!"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      * @link http://ca2.php.net/manual/en/function.session-decode.php#108037 Code from this comment on php.net
 | ||||||
|  |      */ | ||||||
|  |     public function unserialize($raw) { | ||||||
|  |         $returnData = array(); | ||||||
|  |         $offset     = 0; | ||||||
|  | 
 | ||||||
|  |         while ($offset < strlen($raw)) { | ||||||
|  |             if (!strstr(substr($raw, $offset), "|")) { | ||||||
|  |                 throw new Exception("invalid data, remaining: " . substr($raw, $offset)); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             $pos     = strpos($raw, "|", $offset); | ||||||
|  |             $num     = $pos - $offset; | ||||||
|  |             $varname = substr($raw, $offset, $num); | ||||||
|  |             $offset += $num + 1; | ||||||
|  |             $data    = unserialize(substr($raw, $offset)); | ||||||
|  | 
 | ||||||
|  |             $returnData[$varname] = $data; | ||||||
|  |             $offset += strlen(serialize($data)); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return $returnData; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -3,6 +3,7 @@ namespace Ratchet\Component\Session; | |||||||
| use Ratchet\Component\MessageComponentInterface; | use Ratchet\Component\MessageComponentInterface; | ||||||
| use Ratchet\Resource\ConnectionInterface; | use Ratchet\Resource\ConnectionInterface; | ||||||
| use Ratchet\Component\Session\Storage\VirtualSessionStorage; | use Ratchet\Component\Session\Storage\VirtualSessionStorage; | ||||||
|  | use Ratchet\Component\Session\Serialize\HandlerInterface; | ||||||
| use Symfony\Component\HttpFoundation\Session\Session; | use Symfony\Component\HttpFoundation\Session\Session; | ||||||
| use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler; | use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler; | ||||||
| 
 | 
 | ||||||
| @ -13,7 +14,9 @@ class SessionComponent implements MessageComponentInterface { | |||||||
|     protected $_handler; |     protected $_handler; | ||||||
|     protected $_null; |     protected $_null; | ||||||
| 
 | 
 | ||||||
|     public function __construct(MessageComponentInterface $app, \SessionHandlerInterface $handler, array $options = array()) { |     protected $_serializer; | ||||||
|  | 
 | ||||||
|  |     public function __construct(MessageComponentInterface $app, \SessionHandlerInterface $handler, array $options = array(), HandlerInterface $serializer = null) { | ||||||
|         $this->_app     = $app; |         $this->_app     = $app; | ||||||
|         $this->_handler = $handler; |         $this->_handler = $handler; | ||||||
|         $this->_options = array(); |         $this->_options = array(); | ||||||
| @ -24,6 +27,17 @@ class SessionComponent implements MessageComponentInterface { | |||||||
|         ini_set('session.use_cookies', 0); |         ini_set('session.use_cookies', 0); | ||||||
| 
 | 
 | ||||||
|         $options = $this->setOptions($options); |         $options = $this->setOptions($options); | ||||||
|  | 
 | ||||||
|  |         if (null === $serializer) { | ||||||
|  |             $serialClass = __NAMESPACE__ . '\\Serialize\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', ini_get('session.serialize_handler')))) . 'Handler'; // awesome/terrible hack, eh?
 | ||||||
|  |             if (!class_exists($serialClass)) { | ||||||
|  |                 throw new \RuntimeExcpetion('Unable to parse session serialize handler'); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             $serializer = new $serialClass; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         $this->_serializer = $serializer; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -31,13 +45,13 @@ class SessionComponent implements MessageComponentInterface { | |||||||
|      */ |      */ | ||||||
|     function onOpen(ConnectionInterface $conn) { |     function onOpen(ConnectionInterface $conn) { | ||||||
|         if (null === ($id = $conn->WebSocket->headers->getCookie($this->_options['name']))) { |         if (null === ($id = $conn->WebSocket->headers->getCookie($this->_options['name']))) { | ||||||
|             $saveHandler = new NullSessionHandler; |             $saveHandler = $this->_null; | ||||||
|             $id = ''; |             $id = ''; | ||||||
|         } else { |         } else { | ||||||
|             $saveHandler = $this->_handler; |             $saveHandler = $this->_handler; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id)); |         $conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id, $this->_serializer)); | ||||||
| 
 | 
 | ||||||
|         return $this->_app->onOpen($conn); |         return $this->_app->onOpen($conn); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -3,17 +3,23 @@ namespace Ratchet\Component\Session\Storage\Proxy; | |||||||
| use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; | use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; | ||||||
| 
 | 
 | ||||||
| class VirtualProxy extends SessionHandlerProxy { | class VirtualProxy extends SessionHandlerProxy { | ||||||
|  |     /** | ||||||
|  |      * @var string | ||||||
|  |      */ | ||||||
|     protected $_sessionId; |     protected $_sessionId; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * @var string | ||||||
|  |      */ | ||||||
|     protected $_sessionName; |     protected $_sessionName; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * {@inheritdoc} |      * {@inheritdoc} | ||||||
|      */ |      */ | ||||||
|     public function __construct(\SessionHandlerInterface $handler, $sessionId) { |     public function __construct(\SessionHandlerInterface $handler) { | ||||||
|         parent::__construct($handler); |         parent::__construct($handler); | ||||||
| 
 | 
 | ||||||
|         $this->saveHandlerName = 'user'; |         $this->saveHandlerName = 'user'; | ||||||
|         $this->_sessionId      = $sessionId; |  | ||||||
|         $this->_sessionName    = ini_get('session.name'); |         $this->_sessionName    = ini_get('session.name'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -24,8 +30,11 @@ class VirtualProxy extends SessionHandlerProxy { | |||||||
|         return $this->_sessionId; |         return $this->_sessionId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      */ | ||||||
|     public function setId($id) { |     public function setId($id) { | ||||||
|         throw new \RuntimeException("Can not change session id in VirtualProxy"); |         $this->_sessionId = $id; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -2,13 +2,21 @@ | |||||||
| namespace Ratchet\Component\Session\Storage; | namespace Ratchet\Component\Session\Storage; | ||||||
| use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; | use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; | ||||||
| use Ratchet\Component\Session\Storage\Proxy\VirtualProxy; | use Ratchet\Component\Session\Storage\Proxy\VirtualProxy; | ||||||
|  | use Ratchet\Component\Session\Serialize\HandlerInterface; | ||||||
| 
 | 
 | ||||||
| class VirtualSessionStorage extends NativeSessionStorage { | class VirtualSessionStorage extends NativeSessionStorage { | ||||||
|  |     /** | ||||||
|  |      * @var Ratchet\Component\Session\Serialize\HandlerInterface | ||||||
|  |      */ | ||||||
|  |     protected $_serializer; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * {@inheritdoc} |      * {@inheritdoc} | ||||||
|      */ |      */ | ||||||
|     public function __construct(\SessionHandlerInterface $handler, $id) { |     public function __construct(\SessionHandlerInterface $handler, $sessionId, HandlerInterface $serializer) { | ||||||
|         $this->setSaveHandler($handler, $id); |         $this->setSaveHandler($handler); | ||||||
|  |         $this->saveHandler->setId($sessionId); | ||||||
|  |         $this->_serializer = $serializer; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -19,8 +27,10 @@ class VirtualSessionStorage extends NativeSessionStorage { | |||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $ignore = array(); |         $rawData     = $this->saveHandler->read($this->saveHandler->getId()); | ||||||
|         $this->loadSession($ignore); |         $sessionData = $this->_serializer->unserialize($rawData); | ||||||
|  | 
 | ||||||
|  |         $this->loadSession($sessionData); | ||||||
| 
 | 
 | ||||||
|         if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) { |         if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) { | ||||||
|             $this->saveHandler->setActive(false); |             $this->saveHandler->setActive(false); | ||||||
| @ -40,6 +50,9 @@ class VirtualSessionStorage extends NativeSessionStorage { | |||||||
|      * {@inheritdoc} |      * {@inheritdoc} | ||||||
|      */ |      */ | ||||||
|     public function save() { |     public function save() { | ||||||
|  |         // get the data from the bags?
 | ||||||
|  |         // serialize the data
 | ||||||
|  |         // save the data using the saveHandler
 | ||||||
| //        $this->saveHandler->write($this->saveHandler->getId(), 
 | //        $this->saveHandler->write($this->saveHandler->getId(), 
 | ||||||
| 
 | 
 | ||||||
|         if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) { |         if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) { | ||||||
| @ -52,9 +65,13 @@ class VirtualSessionStorage extends NativeSessionStorage { | |||||||
|     /** |     /** | ||||||
|      * {@inheritdoc} |      * {@inheritdoc} | ||||||
|      */ |      */ | ||||||
|     public function setSaveHandler(\SessionHandlerInterface $saveHandler, $id) { |     public function setSaveHandler($saveHandler = null) { | ||||||
|  |         if (!($saveHandler instanceof \SessionHandlerInterface)) { | ||||||
|  |             throw new \InvalidArgumentException('Handler must be instance of SessionHandlerInterface'); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         if (!($saveHandler instanceof \VirtualProxy)) { |         if (!($saveHandler instanceof \VirtualProxy)) { | ||||||
|             $saveHandler = new VirtualProxy($saveHandler, $id); |             $saveHandler = new VirtualProxy($saveHandler); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $this->saveHandler = $saveHandler; |         $this->saveHandler = $saveHandler; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Chris Boden
						Chris Boden