Files
iblogs/src/Frontend/Cookie/Cookie.php
Sam Kintop bf3870ccca
Some checks failed
Publish Docker Image / build-and-push (push) Failing after 2m13s
all
2026-04-30 09:44:02 -05:00

139 lines
2.5 KiB
PHP

<?php
namespace Aternos\Mclogs\Frontend\Cookie;
use Aternos\Mclogs\Util\URL;
abstract class Cookie
{
protected ?string $value = null;
/**
* @return string
*/
abstract protected function getKey(): string;
/**
* @return string
*/
protected function getDomain(): string
{
return "";
}
/**
* @return int|null
*/
protected function getMaxAge(): ?int
{
return null;
}
/**
* @return string
*/
protected function getPath(): string
{
return "/";
}
/**
* @return bool
*/
protected function isSecure(): bool
{
return URL::getCurrent()->getScheme() === "https";
}
/**
* @return bool
*/
protected function isHttpOnly(): bool
{
return true;
}
/**
* @return string
*/
protected function getSameSite(): string
{
return "Lax";
}
public function __construct()
{
$this->value = $_COOKIE[$this->getKey()] ?? null;
}
/**
* @param string $value
* @return bool
*/
public function set(string $value): bool
{
$options = [
'expires' => $this->getMaxAge() !== null ? time() + $this->getMaxAge() : 0,
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httponly' => $this->isHttpOnly(),
'samesite' => $this->getSameSite()
];
$result = setcookie(
$this->getKey(),
$value,
$options
);
if ($result) {
$this->value = $value;
}
return $result;
}
/**
* @return bool
*/
public function delete(): bool
{
$options = [
'expires' => time() - 3600,
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httponly' => $this->isHttpOnly(),
'samesite' => $this->getSameSite()
];
$result = setcookie(
$this->getKey(),
'',
$options
);
if ($result) {
$this->value = null;
}
return $result;
}
/**
* @return string|null
*/
public function getValue(): ?string
{
return $this->value;
}
/**
* @return bool
*/
public function exists(): bool
{
return $this->getValue() !== null;
}
}