Skip to content
Permalink
Browse files

Move cookie support detection to a helper class

Icinga\Web\Cookie will become a real cookie implementation.
  • Loading branch information
lippserd committed Nov 27, 2015
1 parent de10171 commit 271b5f9d5cd4d7e918ca53f3dfa8049d43792b6d
@@ -6,6 +6,7 @@
use Icinga\Application\Icinga;
use Icinga\Forms\Authentication\LoginForm;
use Icinga\Web\Controller;
use Icinga\Web\Helper\CookieHelper;
use Icinga\Web\Url;
/**
@@ -37,13 +38,14 @@ public function loginAction()
$this->redirectNow($form->getRedirectUrl());
}
if (! $requiresSetup) {
if (! $this->getRequest()->hasCookieSupport()) {
$cookies = new CookieHelper($this->getRequest());
if (! $cookies->isSupported()) {
$this
->getResponse()
->setBody("Cookies must be enabled to run this application.\n")
->setHttpResponseCode(403)
->sendResponse();
exit();
exit;
}
$form->handleRequest();
}
@@ -0,0 +1,81 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Helper;
use Icinga\Web\Request;
/**
* Helper Class Cookie
*/
class CookieHelper
{
/**
* The name of the control cookie
*/
const CHECK_COOKIE = '_chc';
/**
* The request
*
* @var Request
*/
protected $request;
/**
* Create a new cookie
*
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Check whether cookies are supported or not
*
* @return bool
*/
public function isSupported()
{
if (! empty($_COOKIE)) {
$this->cleanupCheck();
return true;
}
$url = $this->request->getUrl();
if ($url->hasParam('_checkCookie') && empty($_COOKIE)) {
return false;
}
if (! $url->hasParam('_checkCookie')) {
$this->provideCheck();
}
return false;
}
/**
* Prepare check to detect cookie support
*/
public function provideCheck()
{
setcookie(self::CHECK_COOKIE, '1');
$requestUri = $this->request->getUrl()->addParams(array('_checkCookie' => 1));
$this->request->getResponse()->redirectAndExit($requestUri);
}
/**
* Cleanup the cookie support check
*/
public function cleanupCheck()
{
if ($this->request->getUrl()->hasParam('_checkCookie') && isset($_COOKIE[self::CHECK_COOKIE])) {
$requestUri =$this->request->getUrl()->without('_checkCookie');
$this->request->getResponse()->redirectAndExit($requestUri);
}
}
}
@@ -118,15 +118,4 @@ public function protectId($id)
}
return $id . '-' . $this->uniqueId;
}
/**
* Detect whether cookies are enabled
*
* @return bool
*/
public function hasCookieSupport()
{
$cookie = new Cookie($this);
return $cookie->isSupported();
}
}

0 comments on commit 271b5f9

Please sign in to comment.
You can’t perform that action at this time.