Permalink
Browse files

Sloppy start

  • Loading branch information...
0 parents commit 2cb9fc0f14af7adb5853d8803894b60c3f960319 @EvanDotPro EvanDotPro committed Nov 1, 2011
@@ -0,0 +1,47 @@
+<?php
+
+namespace EdpSession;
+
+use Zend\Config\Config,
+ Zend\Module\Manager,
+ Zend\EventManager\StaticEventManager,
+ Zend\Loader\AutoloaderFactory;
+
+class Module
+{
+ public function init(Manager $moduleManager)
+ {
+ $this->initAutoloader();
+ $moduleManager->events()->attach('init.post', array($this, 'prepareSession'));
+ }
+
+ public function initAutoloader()
+ {
+ AutoloaderFactory::factory(array(
+ 'Zend\Loader\ClassMapAutoloader' => array(
+ __DIR__ . '/autoload_classmap.php',
+ ),
+ 'Zend\Loader\StandardAutoloader' => array(
+ 'namespaces' => array(
+ __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
+ ),
+ ),
+ ));
+ }
+
+ public function getConfig($env = null)
+ {
+ return new Config(include __DIR__ . '/configs/module.config.php');
+ }
+
+ public function prepareSession($e)
+ {
+ $moduleManager = $e->getTarget();
+ $config = $moduleManager->getMergedConfig();
+ $sessionService = new Service\DomainSession($config->session);
+ $events = StaticEventManager::getInstance();
+ $events->attach('bootstrap', 'bootstrap', function($e) use ($sessionService) {
+ $sessionService->initializeSession($e);
+ });
+ }
+}
@@ -0,0 +1 @@
+ZF2 module for cross-domain sessions.
@@ -0,0 +1,2 @@
+<?php
+return array ();
@@ -0,0 +1,12 @@
+<?php
+return function ($class) {
+ static $map;
+ if (!$map) {
+ $map = include __DIR__ . '/autoload_classmap.php';
+ }
+
+ if (!isset($map[$class])) {
+ return false;
+ }
+ return include $map[$class];
+};
@@ -0,0 +1,2 @@
+<?php
+spl_autoload_register(include __DIR__ . '/autoload_function.php');
@@ -0,0 +1,33 @@
+<?php
+return array(
+ 'session' => array(
+ 'groups' => array(
+ 'skeleton' => array(
+ 'master' => 'zendskeletonapplication.dev',
+ ),
+ ),
+ 'hosts' => array(
+ 'zendskeletonapplication.dev' => 'skeleton',
+ 'test.zendskeletonapplication.dev' => 'skeleton',
+ ),
+ ),
+ 'di' => array(
+ 'instance' => array(
+ 'alias' => array(
+ 'session-manager' => 'Zend\Session\SessionManager',
+ 'session' => 'EdpSession\Controller\SessionController',
+ ),
+ 'session-manager' => array(
+ 'parameters' => array(
+ 'name' => 'test',
+ 'config' => 'Zend\Session\Configuration\SessionConfiguration',
+ ),
+ ),
+ 'Zend\Session\Configuration\SessionConfiguration' => array(
+ 'parameters' => array(
+ //'cookieDomain' => '.mydomain.tld',
+ ),
+ ),
+ ),
+ ),
+);
@@ -0,0 +1,14 @@
+<?php
+
+namespace EdpSession\Controller;
+
+use Zend\Mvc\Controller\ActionController;
+
+class SessionController extends ActionController
+{
+ public function indexAction()
+ {
+ $request = $this->getRequest();
+ var_dump($request->headers());die();
+ }
+}
@@ -0,0 +1,86 @@
+<?php
+
+namespace EdpSession\Service;
+
+use Zend\Session\Container as SessionContainer,
+ Zend\Http\PhpEnvironment\Response as HttpResponse,
+ Zend\EventManager\StaticEventManager;
+
+class DomainSession
+{
+ protected $options;
+ protected $hostname;
+ protected $app;
+
+ public function __construct($options = null)
+ {
+ $this->options = $options;
+ }
+
+ public function initializeSession($e)
+ {
+ $this->app = $e->getParam('application');
+ $request = $this->app->getRequest();
+ $this->hostname = $request->uri()->getHost();
+ $locator = $this->app->getLocator();
+ $sessionManager = $locator->get('session-manager');
+ SessionContainer::setDefaultManager($sessionManager);
+ if ($request->query()->session !== null) {
+ var_dump($request->query()->redirect);
+ die();
+ }
+ if ($request->query()->sid) {
+ if ($request->query()->sid !== $sessionManager->getId()) {
+ $this->newSession($request->query()->sid);
+ }
+ } else {
+ $this->newSession();
+ }
+ }
+
+ public function newSession($sid = null)
+ {
+ $sessionManager = SessionContainer::getDefaultManager();
+ if ($sid !== null) {
+ $sessionManager->setId($sid);
+ $container = new SessionContainer('EdpSession');
+ if ($container->valid !== true) {
+ $sessionManager->destroy();
+ die('Invalid session ID given');
+ // go fetch valid session id
+ }
+ } else {
+ $this->fetchMasterSession();
+ // go fetch valid session id
+ $sessionManager->regenerateId();
+ $container = new SessionContainer('EdpSession');
+ $container->valid = true;
+ }
+ }
+
+ public function fetchMasterSession()
+ {
+ if ($this->isMasterHost() === false) {
+ $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', 'http://' . $masterHost . '/?session&redirect='. (string) $uri);
+ $response->setStatusCode(302);
+ $response->send();die();
+ return $response;
+ }, 9999);
+ }
+ }
+
+ public function isMasterHost()
+ {
+ return ($this->hostname === $this->getMasterHost());
+ }
+
+ public function getMasterHost()
+ {
+ $groupName = $this->options->hosts->{$this->hostname};
+ return $this->options->groups->{$groupName}->master;
+ }
+}

0 comments on commit 2cb9fc0

Please sign in to comment.