Skip to content

Instantly share code, notes, and snippets.

@basz
Created June 6, 2012 14:50
Show Gist options
  • Select an option

  • Save basz/2882338 to your computer and use it in GitHub Desktop.

Select an option

Save basz/2882338 to your computer and use it in GitHub Desktop.
<?php
namespace MultiSess\Service;
use Zend\Session\Container as SessionContainer,
Zend\Http\PhpEnvironment\Response as HttpResponse,
Zend\EventManager\StaticEventManager;
class DomainSession
{
protected $options;
protected $hostname;
protected $app;
protected $container;
public function log($mes) {
static $logger;
if ($logger === null) {
$logger = new \Zend\Log\Logger();
$logger->addWriter(new \Zend\Log\Writer\Stream('data/logs/session.log'));
}
$logger->debug($mes);
}
public function __construct($options = null)
{
$this->options = $options;
}
public function initializeSession(\Zend\Mvc\MvcEvent $e)
{
$this->log('initializeSession');
$this->app = $e->getApplication();
$request = $this->app->getRequest();
$this->hostname = $request->uri()->getHost();
$sessionManager = $e->getApplication()->getServiceManager()->get('MultiSess.SessionManager');
SessionContainer::setDefaultManager($sessionManager);
// $this->startSession();
// if ($request->query()->sid && $request->query()->sid !== $sessionManager->getId()) {
// $this->restartExistingSession($request->query()->sid);
// }
if ($this->isMasterHost()) {
$masterSid = $sessionManager->getId();
}
// only on non master
if (!$this->isMasterHost() && $request->query()->doDiscoveryResponse !== null && $request->query()->masterSid !== null) {
$this->container->masterSid = $request->query()->masterSid;
$this->doDiscoveryResponseDone();
return;
}
// only on master
if ($this->isMasterHost() && $request->query()->doDiscovery !== null && $request->query()->redirect !== null) {
$this->doDiscoveryResponse($request->query()->redirect, $masterSid);
return;
}
// only on non master
if (!$this->isMasterHost() && $this->container->masterSid !== null) {
$this->startWithMasterSession($this->container->masterSid);
return;
}
// only on non master
if (!$this->isMasterHost() && $this->container->masterSid === null) {
$this->doDiscovery();
return;
}
}
public function startSession() {
$sessionManager = SessionContainer::getDefaultManager();
$this->container = new SessionContainer('EdpSession');
$this->log(sprintf("%s : %s : %s", __FUNCTION__, $sessionManager->getId(), $this->container->masterSid));
}
public function restartExistingSession($sid) {
$this->log(sprintf("%s : %s", __FUNCTION__, $sid));
$sessionManager = SessionContainer::getDefaultManager();
$sessionManager->setId($sid);
$this->container = new SessionContainer('EdpSession');
}
public function startWithMasterSession($masterSid) {
$this->log(sprintf("%s : %s", __FUNCTION__, $masterSid));
$sessionManager = SessionContainer::getDefaultManager();
$sessionManager->setId($masterSid);
$this->container = new SessionContainer('EdpSession');
}
public function doDiscovery()
{
$masterHost = $this->getMasterHost();
$uri = $this->app->getRequest()->uri();
$that = $this;
$this->app->events()->attach('dispatch', function($e) use ($masterHost, $uri, $that) {
$that->log(sprintf("%s : %s", 'doDiscovery', 'http://' . $masterHost . '/?doDiscovery&redirect=' . (string) $uri));
$response = new HttpResponse();
$response->headers()->addHeaderLine('Location', 'http://' . $masterHost . '/?doDiscovery&redirect=' . (string) $uri);
$response->setStatusCode(302);
$response->send();
return $response;
}, 9999);
}
public function doDiscoveryResponse($redirect, $masterSid) {
$that = $this;
$this->app->events()->attach('dispatch', function($e) use ($redirect, $masterSid, $that) {
$that->log(sprintf("%s : %s", 'doDiscoveryResponse', $redirect . '?doDiscoveryResponse&masterSid=' . (string) $masterSid));
$response = new HttpResponse();
$response->headers()->addHeaderLine('Location', $redirect . '?doDiscoveryResponse&masterSid=' . (string) $masterSid);
$response->setStatusCode(302);
$response->send();
return $response;
}, 9999);
}
public function doDiscoveryResponseDone() {
$uri = $this->app->getRequest()->uri();
$that = $this;
$this->app->events()->attach('dispatch', function($e) use ($uri, $that) {
$uri = new \Zend\Uri\Uri($uri);
$q = $uri->getQueryAsArray();
unset($q['doDiscoveryResponse']);
unset($q['masterSid']);
$uri->setQuery($q);
$response = new HttpResponse();
$that->log(sprintf("%s : %s", 'doDiscoveryResponseDone', $uri->toString()));
$response->headers()->addHeaderLine('Location', $uri->toString());
$response->setStatusCode(302);
$response->send();
return $response;
}, 9999);
}
// public function newSession($sid = null)
// {
// $sessionManager = SessionContainer::getDefaultManager();
// if ($sid !== null) {
// $sessionManager->setId($sid);
// $this->container = new SessionContainer('EdpSession');
// if ($this->container->valid !== true) {
// $sessionManager->destroy();
// die('Invalid session ID given');
// // go fetch valid session id
// }
// } else {
// $this->fetchMasterSession();die();
// // go fetch valid session id
// $sessionManager->regenerateId();
// $this->container = new SessionContainer('EdpSession');
// $this->container->valid = true;
// }
// }
// public function fetchMasterSession()
// {
// // $container = new SessionContainer('EdpSession');
// if ($this->isMasterHost() === false && !$this->container->masterSid) {
// $masterHost = $this->getMasterHost();
// $uri = $this->app->getRequest()->uri();
// $this->app->events()->attach('dispatch', function($e) use ($masterHost, $uri) {
// $response = new HttpResponse();
//// $response->headers()->addHeaderLine('Location', $uri . '?session&redirect=http://'. (string) $masterHost);
// $response->headers()->addHeaderLine('Location', 'http://' . $masterHost . '/?getMasterSessId&redirect='. (string) $uri);
// $response->setStatusCode(302);
// $response->send();
// return $response;
// }, 9999);
// }
// }
public function isMasterHost()
{
return ($this->hostname === $this->getMasterHost());
}
public function getMasterHost()
{
$groupName = $this->options->hosts->get($this->hostname);
return $this->options->groups->get($groupName)->master;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment