mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Changes for #451
This commit is contained in:
parent
f69a4c3997
commit
d8b2253351
1 changed files with 43 additions and 20 deletions
|
|
@ -99,43 +99,66 @@ class Utils
|
||||||
*/
|
*/
|
||||||
public static function GetCookie(string $sName, $mDefault = null)
|
public static function GetCookie(string $sName, $mDefault = null)
|
||||||
{
|
{
|
||||||
return isset($_COOKIE[$sName]) ? $_COOKIE[$sName] : $mDefault;
|
if (isset($_COOKIE[$sName])) {
|
||||||
|
$aParts = [];
|
||||||
|
foreach (\array_keys($_COOKIE) as $sCookieName) {
|
||||||
|
if (\strtok($sCookieName, '~') === $sName) {
|
||||||
|
$aParts[$sCookieName] = $_COOKIE[$sCookieName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\ksort($aParts);
|
||||||
|
return \implode('', $aParts);
|
||||||
|
}
|
||||||
|
return $mDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function GetSecureCookie(string $sName)
|
public static function GetSecureCookie(string $sName)
|
||||||
{
|
{
|
||||||
return isset($_COOKIE[$sName]) && 1024 > \strlen($_COOKIE[$sName])
|
return isset($_COOKIE[$sName])
|
||||||
? \SnappyMail\Crypt::DecryptFromJSON(\MailSo\Base\Utils::UrlSafeBase64Decode($_COOKIE[$sName]))
|
? \SnappyMail\Crypt::DecryptFromJSON(\MailSo\Base\Utils::UrlSafeBase64Decode(static::GetCookie($sName)))
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function SetCookie(string $sName, string $sValue = '', int $iExpire = 0, bool $bHttpOnly = true)
|
public static function SetCookie(string $sName, string $sValue = '', int $iExpire = 0, bool $bHttpOnly = true)
|
||||||
{
|
{
|
||||||
$sPath = static::$CookieDefaultPath;
|
$sPath = static::$CookieDefaultPath;
|
||||||
|
$sPath = $sPath && \strlen($sPath) ? $sPath : '/';
|
||||||
$_COOKIE[$sName] = $sValue;
|
$_COOKIE[$sName] = $sValue;
|
||||||
\setcookie($sName, $sValue, array(
|
// https://github.com/the-djmaze/snappymail/issues/451
|
||||||
'expires' => $iExpire,
|
// The 4K browser limit is for the entire cookie, including name, value, expiry date etc.
|
||||||
'path' => $sPath && \strlen($sPath) ? $sPath : '/',
|
$iMaxSize = 4000 - \strlen($sPath . $sName);
|
||||||
// 'domain' => $sDomain,
|
if ($iMaxSize < \strlen($sValue)) {
|
||||||
'secure' => isset($_SERVER['HTTPS']) || static::$CookieDefaultSecure,
|
throw new \Exception("Cookie '{$sName}' value too long");
|
||||||
'httponly' => $bHttpOnly,
|
}
|
||||||
'samesite' => 'Strict'
|
foreach (\str_split($sValue, $iMaxSize) as $i => $sPart) {
|
||||||
));
|
\setcookie($i ? "{$sName}~{$i}" : $sName, $sPart, array(
|
||||||
|
'expires' => $iExpire,
|
||||||
|
'path' => $sPath,
|
||||||
|
// 'domain' => $sDomain,
|
||||||
|
'secure' => isset($_SERVER['HTTPS']) || static::$CookieDefaultSecure,
|
||||||
|
'httponly' => $bHttpOnly,
|
||||||
|
'samesite' => 'Strict'
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function ClearCookie(string $sName)
|
public static function ClearCookie(string $sName)
|
||||||
{
|
{
|
||||||
if (isset($_COOKIE[$sName])) {
|
if (isset($_COOKIE[$sName])) {
|
||||||
$sPath = static::$CookieDefaultPath;
|
$sPath = static::$CookieDefaultPath;
|
||||||
unset($_COOKIE[$sName]);
|
foreach (\array_keys($_COOKIE) as $sCookieName) {
|
||||||
\setcookie($sName, '', array(
|
if (\strtok($sCookieName, '~') === $sName) {
|
||||||
'expires' => \time() - 3600 * 24 * 30,
|
unset($_COOKIE[$sCookieName]);
|
||||||
'path' => $sPath && \strlen($sPath) ? $sPath : '/',
|
\setcookie($sCookieName, '', array(
|
||||||
// 'domain' => null,
|
'expires' => \time() - 3600 * 24 * 30,
|
||||||
'secure' => isset($_SERVER['HTTPS']) || static::$CookieDefaultSecure,
|
'path' => $sPath && \strlen($sPath) ? $sPath : '/',
|
||||||
'httponly' => true,
|
// 'domain' => null,
|
||||||
'samesite' => 'Strict'
|
'secure' => isset($_SERVER['HTTPS']) || static::$CookieDefaultSecure,
|
||||||
));
|
'httponly' => true,
|
||||||
|
'samesite' => 'Strict'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue