Cookie: Make it no static and use the request

refs #7383
This commit is contained in:
Alexander Fuhr 2015-08-13 11:19:48 +02:00
parent 2332b94f0b
commit 813154f6ef
1 changed files with 31 additions and 30 deletions

View File

@ -5,8 +5,6 @@ namespace Icinga\Web;
/**
* Helper Class Cookie
*
* @package Icinga\Web
*/
class Cookie
{
@ -15,64 +13,67 @@ class 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 static function isSupported()
public function isSupported()
{
if (! empty($_COOKIE)) {
self::cleanupCheck();
$this->cleanupCheck();
return true;
}
if (isset($_REQUEST['_checkCookie']) && empty($_COOKIE)) {
$url = $this->request->getUrl();
if ($url->hasParam('_checkCookie') && empty($_COOKIE)) {
return false;
}
if (! isset($_REQUEST['_checkCookie'])) {
self::provideCheck();
if (! $url->hasParam('_checkCookie')) {
$this->provideCheck();
}
return false;
}
/**
* Redirect to a given uri
*
* @param string $uri
*/
public static function redirect($uri)
{
header('location: ' . $uri);
exit(0);
}
/**
* Prepare check to detect cookie support
*/
public static function provideCheck()
public function provideCheck()
{
setcookie(self::CHECK_COOKIE, '1');
if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY)) {
$requestUri = $_SERVER['REQUEST_URI'] . '&_checkCookie=1';
} else {
$requestUri = $_SERVER['REQUEST_URI'] . '?_checkCookie=1';
}
self::redirect($requestUri);
$requestUri = $this->request->getUrl()->addParams(array('_checkCookie' => 1));
$this->request->getResponse()->redirectAndExit($requestUri);
}
/**
* Cleanup the cookie support check
*/
public static function cleanupCheck()
public function cleanupCheck()
{
if (isset($_REQUEST['_checkCookie']) && isset($_COOKIE[self::CHECK_COOKIE])) {
$requestUri = preg_replace('/([&|\?]_checkCookie=1)/', '', $_SERVER['REQUEST_URI']);
self::redirect($requestUri);
if ($this->request->getUrl()->hasParam('_checkCookie') && isset($_COOKIE[self::CHECK_COOKIE])) {
$requestUri =$this->request->getUrl()->without('_checkCookie');
$this->request->getResponse()->redirectAndExit($requestUri);
}
}
}