Web\Window: move window handle to dedicated class
One more step in cleaning up our ActionController
This commit is contained in:
parent
d69a6d1640
commit
b350e3640d
|
@ -40,6 +40,7 @@ use Icinga\Application\Benchmark;
|
||||||
use Icinga\Application\Config;
|
use Icinga\Application\Config;
|
||||||
use Icinga\Util\Translator;
|
use Icinga\Util\Translator;
|
||||||
use Icinga\Web\Widget\Tabs;
|
use Icinga\Web\Widget\Tabs;
|
||||||
|
use Icinga\Web\Window;
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
use Icinga\Web\Notification;
|
use Icinga\Web\Notification;
|
||||||
use Icinga\File\Pdf;
|
use Icinga\File\Pdf;
|
||||||
|
@ -108,10 +109,7 @@ class ActionController extends Zend_Controller_Action
|
||||||
->_setInvokeArgs($invokeArgs);
|
->_setInvokeArgs($invokeArgs);
|
||||||
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
|
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
|
||||||
$this->_helper->addPath('../application/controllers/helpers');
|
$this->_helper->addPath('../application/controllers/helpers');
|
||||||
|
$this->handleWindowId();
|
||||||
if ($this->_request->isXmlHttpRequest()) {
|
|
||||||
$this->windowId = $this->_request->getHeader('X-Icinga-WindowId', null);
|
|
||||||
}
|
|
||||||
|
|
||||||
$module = $request->getModuleName();
|
$module = $request->getModuleName();
|
||||||
$this->view->translationDomain = $module === 'default' ? 'icinga' : $module;
|
$this->view->translationDomain = $module === 'default' ? 'icinga' : $module;
|
||||||
|
@ -153,61 +151,25 @@ class ActionController extends Zend_Controller_Action
|
||||||
return $this->auth;
|
return $this->auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function handleWindowId()
|
||||||
|
{
|
||||||
|
if ($this->_request->isXmlHttpRequest()) {
|
||||||
|
$windowId = $this->_request->getHeader('X-Icinga-WindowId', null);
|
||||||
|
if ($windowId === Window::UNDEFINED) {
|
||||||
|
$this->_response->setHeader('X-Icinga-WindowId', Window::generateId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function moduleInit()
|
protected function moduleInit()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function reloadCss()
|
protected function reloadCss()
|
||||||
{
|
{
|
||||||
$this->reloadCss = true;
|
$this->reloadCss = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a window-aware session by using the given prefix
|
|
||||||
*
|
|
||||||
* @param string $prefix The prefix to use
|
|
||||||
* @param bool $reset Whether to reset any existing session-data
|
|
||||||
*
|
|
||||||
* @return SessionNamespace
|
|
||||||
*/
|
|
||||||
public function getWindowSession($prefix, $reset = false)
|
|
||||||
{
|
|
||||||
$session = Session::getSession();
|
|
||||||
$windowId = $this->getWindowId();
|
|
||||||
|
|
||||||
$identifier = $prefix . '_' . $windowId;
|
|
||||||
if ($reset && $session->hasNamespace($identifier)) {
|
|
||||||
$session->removeNamespace($identifier);
|
|
||||||
}
|
|
||||||
$namespace = $session->getNamespace($identifier);
|
|
||||||
|
|
||||||
if (!$reset && $windowId !== 'undefined' && $session->hasNamespace($prefix . '_undefined')) {
|
|
||||||
// We do not have any window-id on the very first request. Now we add all values from the
|
|
||||||
// namespace, that has been created in this case, to the new one and remove it afterwards.
|
|
||||||
foreach ($session->getNamespace($prefix . '_undefined') as $name => $value) {
|
|
||||||
$namespace->set($name, $value);
|
|
||||||
}
|
|
||||||
$session->removeNamespace($prefix . '_undefined');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $namespace;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return restriction information for an eventually authenticated user
|
* Return restriction information for an eventually authenticated user
|
||||||
*
|
*
|
||||||
|
@ -373,10 +335,12 @@ class ActionController extends Zend_Controller_Action
|
||||||
{
|
{
|
||||||
$url = preg_replace('~&~', '&', $url);
|
$url = preg_replace('~&~', '&', $url);
|
||||||
if ($this->_request->isXmlHttpRequest()) {
|
if ($this->_request->isXmlHttpRequest()) {
|
||||||
header('X-Icinga-Redirect: ' . rawurlencode($url));
|
$this->getResponse()
|
||||||
// $this->getResponse()->sendHeaders() ??
|
->setHeader('X-Icinga-Redirect', rawurlencode($url))
|
||||||
// Session shutdown
|
->sendHeaders();
|
||||||
exit; // Really?
|
|
||||||
|
// TODO: Session shutdown?
|
||||||
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$this->_helper->Redirector->gotoUrlAndExit(Url::fromPath($url)->getRelativeUrl());
|
$this->_helper->Redirector->gotoUrlAndExit(Url::fromPath($url)->getRelativeUrl());
|
||||||
}
|
}
|
||||||
|
@ -438,10 +402,6 @@ class ActionController extends Zend_Controller_Action
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($isXhr && $this->getWindowId() === 'undefined') {
|
|
||||||
header('X-Icinga-WindowId: ' . $this->generateWindowId());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->view->title) {
|
if ($this->view->title) {
|
||||||
if (preg_match('~[\r\n]~', $this->view->title)) {
|
if (preg_match('~[\r\n]~', $this->view->title)) {
|
||||||
// TODO: Innocent exception and error log for hack attempts
|
// TODO: Innocent exception and error log for hack attempts
|
||||||
|
@ -459,6 +419,10 @@ class ActionController extends Zend_Controller_Action
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function Window()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
protected function sendAsPdf()
|
protected function sendAsPdf()
|
||||||
{
|
{
|
||||||
$pdf = new Pdf();
|
$pdf = new Pdf();
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Web;
|
||||||
|
|
||||||
|
use Icinga\Web\Session;
|
||||||
|
|
||||||
|
class Window
|
||||||
|
{
|
||||||
|
const UNDEFINED = 'undefined';
|
||||||
|
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
public function __construct($id)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isUndefined()
|
||||||
|
{
|
||||||
|
return $this->id === self::UNDEFINED;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a window-aware session by using the given prefix
|
||||||
|
*
|
||||||
|
* @param string $prefix The prefix to use
|
||||||
|
* @param bool $reset Whether to reset any existing session-data
|
||||||
|
*
|
||||||
|
* @return SessionNamespace
|
||||||
|
*/
|
||||||
|
public function getSessionNamespace($prefix, $reset = false)
|
||||||
|
{
|
||||||
|
$session = Session::getSession();
|
||||||
|
|
||||||
|
$identifier = $prefix . '_' . $this->id;
|
||||||
|
if ($reset && $session->hasNamespace($identifier)) {
|
||||||
|
$session->removeNamespace($identifier);
|
||||||
|
}
|
||||||
|
$namespace = $session->getNamespace($identifier);
|
||||||
|
$nsUndef = $prefix . '_' . self::UNDEFINED;
|
||||||
|
|
||||||
|
if (!$reset && $this->id !== self::UNDEFINED && $session->hasNamespace($nsUndef)) {
|
||||||
|
// We do not have any window-id on the very first request. Now we add
|
||||||
|
// all values from the namespace, that has been created in this case,
|
||||||
|
// to the new one and remove it afterwards.
|
||||||
|
foreach ($session->getNamespace($nsUndef) as $name => $value) {
|
||||||
|
$namespace->set($name, $value);
|
||||||
|
}
|
||||||
|
$session->removeNamespace($nsUndef);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function generateId()
|
||||||
|
{
|
||||||
|
$letters = 'abcefghijklmnopqrstuvwxyz';
|
||||||
|
return substr(str_shuffle($letters), 0, 12);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue