From 01f94c574d67d3bb2eb65efb7c0d3858a0892adb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 21 Mar 2014 13:27:44 +0000 Subject: [PATCH] Handle window-id requests, allowing us to identify distinct windows --- .../Web/Controller/ActionController.php | 26 +++++++++++++++++++ public/js/icinga/ui.js | 12 ++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index a84196a1b..e5f4bdb82 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -68,6 +68,8 @@ class ActionController extends Zend_Controller_Action private $noXhrBody = false; + private $windowId; + // TODO: This would look better if we had a ModuleActionController public function Config($file = null) { @@ -111,9 +113,14 @@ class ActionController extends Zend_Controller_Action // when noInit is set (e.g. for testing), authentication and init is skipped if (isset($invokeArgs['noInit'])) { + // TODO: Find out whether this still makes sense? return; } + if ($this->_request->isXmlHttpRequest()) { + $this->windowId = $this->_request->getHeader('X-Icinga-WindowId', null); + } + if ($this->requiresLogin() === false) { $this->view->tabs = new Tabs(); $this->init(); @@ -127,6 +134,21 @@ class ActionController extends Zend_Controller_Action } } + protected function getWindowId() + { + if ($this->windowId === null) { + return 'undefined'; + } + return $this->windowId; + } + + protected function generateWindowId() + { + $letters = 'abcefghijklmnopqrstuvwxyz'; + $this->windowId = substr(str_shuffle($letters), 0, 12); + return $this->windowId; + } + /** * Return restriction information for an eventually authenticated user * @@ -328,6 +350,10 @@ class ActionController extends Zend_Controller_Action return; } + if ($isXhr && $this->getWindowId() === 'undefined') { + header('X-Icinga-WindowId: ' . $this->generateWindowId()); + } + if ($this->view->title) { if (preg_match('~[\r\n]~', $this->view->title)) { // TODO: Innocent exception and error log for hack attempts diff --git a/public/js/icinga/ui.js b/public/js/icinga/ui.js index cb6472d50..28fe19033 100644 --- a/public/js/icinga/ui.js +++ b/public/js/icinga/ui.js @@ -416,19 +416,19 @@ }, getWindowId: function () { - var res = window.name.match(/^Icinga_([a-zA-Z0-9])$/); - if (res) { - return res[1]; + if (! this.hasWindowId()) { + return undefined; } - return null; + return window.name.match(/^Icinga_([a-zA-Z0-9]+)$/)[1]; }, hasWindowId: function () { - var res = window.name.match(/^Icinga_([a-zA-Z0-9])$/); - return typeof res === 'object'; + var res = window.name.match(/^Icinga_([a-zA-Z0-9]+)$/); + return typeof res === 'object' && null !== res; }, setWindowId: function (id) { + this.icinga.logger.debug('Setting new window id', id); window.name = 'Icinga_' + id; },