mirror of
https://github.com/typecho/typecho.git
synced 2025-04-21 02:01:52 +02:00
Enhancement of Typecho\Cookie (#1399)
This commit is contained in:
parent
997aa607ac
commit
3512fd41bf
@ -27,6 +27,24 @@ class Cookie
|
||||
*/
|
||||
private static $path = '/';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
private static $domain = '';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* @access private
|
||||
*/
|
||||
private static $secure = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* @access private
|
||||
*/
|
||||
private static $httponly = false;
|
||||
|
||||
/**
|
||||
* 获取前缀
|
||||
*
|
||||
@ -51,6 +69,7 @@ class Cookie
|
||||
self::$prefix = md5($url);
|
||||
$parsed = parse_url($url);
|
||||
|
||||
self::$domain = $parsed['host'];
|
||||
/** 在路径后面强制加上斜杠 */
|
||||
self::$path = empty($parsed['path']) ? '/' : Common::url(null, $parsed['path']);
|
||||
}
|
||||
@ -66,6 +85,19 @@ class Cookie
|
||||
return self::$path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置额外的选项
|
||||
*
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public static function setOptions(array $options)
|
||||
{
|
||||
self::$domain = $options['domain'] ?: self::$domain;
|
||||
self::$secure = $options['secure'] ? (bool) $options['secure'] : false;
|
||||
self::$httponly = $options['httponly'] ? (bool) $options['httponly'] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的COOKIE值
|
||||
*
|
||||
@ -91,7 +123,7 @@ class Cookie
|
||||
{
|
||||
$key = self::$prefix . $key;
|
||||
$_COOKIE[$key] = $value;
|
||||
Response::getInstance()->setCookie($key, $value, $expire, self::$path);
|
||||
Response::getInstance()->setCookie($key, $value, $expire, self::$path, self::$domain, self::$secure, self::$httponly);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,7 +138,7 @@ class Cookie
|
||||
return;
|
||||
}
|
||||
|
||||
Response::getInstance()->setCookie($key, '', -1, self::$path);
|
||||
Response::getInstance()->setCookie($key, '', -1, self::$path, self::$domain, self::$secure, self::$httponly);
|
||||
unset($_COOKIE[$key]);
|
||||
}
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ class Response
|
||||
|
||||
// set cookie
|
||||
foreach ($this->cookies as $cookie) {
|
||||
[$key, $value, $timeout, $path, $domain] = $cookie;
|
||||
[$key, $value, $timeout, $path, $domain, $secure, $httponly] = $cookie;
|
||||
|
||||
if ($timeout > 0) {
|
||||
$now = time();
|
||||
@ -209,7 +209,7 @@ class Response
|
||||
$timeout = 1;
|
||||
}
|
||||
|
||||
setrawcookie($key, rawurlencode($value), $timeout, $path, $domain ?? '');
|
||||
setrawcookie($key, rawurlencode($value), $timeout, $path, $domain, $secure, $httponly);
|
||||
}
|
||||
}
|
||||
|
||||
@ -275,6 +275,8 @@ class Response
|
||||
* @param integer $timeout 过期时间,默认为0,表示随会话时间结束
|
||||
* @param string $path 路径信息
|
||||
* @param string|null $domain 域名信息
|
||||
* @param bool $secure 是否仅可通过安全的 HTTPS 连接传给客户端
|
||||
* @param bool $httponly 是否仅可通过 HTTP 协议访问
|
||||
* @return $this
|
||||
*/
|
||||
public function setCookie(
|
||||
@ -282,10 +284,12 @@ class Response
|
||||
$value,
|
||||
int $timeout = 0,
|
||||
string $path = '/',
|
||||
string $domain = null
|
||||
string $domain = '',
|
||||
bool $secure = false,
|
||||
bool $httponly = false
|
||||
): Response {
|
||||
if (!$this->sandbox) {
|
||||
$this->cookies[] = [$key, $value, $timeout, $path, $domain];
|
||||
$this->cookies[] = [$key, $value, $timeout, $path, $domain, $secure, $httponly];
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@ -93,7 +93,10 @@ class Init extends Widget
|
||||
}
|
||||
|
||||
/** cookie初始化 */
|
||||
Cookie::setPrefix($options->rootUrl);
|
||||
Cookie::setPrefix($options->rootUrl);
|
||||
if (defined('__TYPECHO_COOKIE_OPTIONS__')) {
|
||||
Cookie::setOptions(__TYPECHO_COOKIE_OPTIONS__);
|
||||
}
|
||||
|
||||
/** 初始化路由器 */
|
||||
Router::setRoutes($options->routingTable);
|
||||
|
Loading…
x
Reference in New Issue
Block a user