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 * Helper Class Cookie
*
* @package Icinga\Web
*/ */
class Cookie class Cookie
{ {
@ -15,64 +13,67 @@ class Cookie
*/ */
const CHECK_COOKIE = '_chc'; 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 * Check whether cookies are supported or not
* *
* @return bool * @return bool
*/ */
public static function isSupported() public function isSupported()
{ {
if (! empty($_COOKIE)) { if (! empty($_COOKIE)) {
self::cleanupCheck(); $this->cleanupCheck();
return true; return true;
} }
if (isset($_REQUEST['_checkCookie']) && empty($_COOKIE)) { $url = $this->request->getUrl();
if ($url->hasParam('_checkCookie') && empty($_COOKIE)) {
return false; return false;
} }
if (! isset($_REQUEST['_checkCookie'])) { if (! $url->hasParam('_checkCookie')) {
self::provideCheck(); $this->provideCheck();
} }
return false; 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 * Prepare check to detect cookie support
*/ */
public static function provideCheck() public function provideCheck()
{ {
setcookie(self::CHECK_COOKIE, '1'); setcookie(self::CHECK_COOKIE, '1');
if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY)) { $requestUri = $this->request->getUrl()->addParams(array('_checkCookie' => 1));
$requestUri = $_SERVER['REQUEST_URI'] . '&_checkCookie=1'; $this->request->getResponse()->redirectAndExit($requestUri);
} else {
$requestUri = $_SERVER['REQUEST_URI'] . '?_checkCookie=1';
}
self::redirect($requestUri);
} }
/** /**
* Cleanup the cookie support check * Cleanup the cookie support check
*/ */
public static function cleanupCheck() public function cleanupCheck()
{ {
if (isset($_REQUEST['_checkCookie']) && isset($_COOKIE[self::CHECK_COOKIE])) { if ($this->request->getUrl()->hasParam('_checkCookie') && isset($_COOKIE[self::CHECK_COOKIE])) {
$requestUri = preg_replace('/([&|\?]_checkCookie=1)/', '', $_SERVER['REQUEST_URI']); $requestUri =$this->request->getUrl()->without('_checkCookie');
self::redirect($requestUri); $this->request->getResponse()->redirectAndExit($requestUri);
} }
} }
} }