123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace think;
- class Cookie
- {
-
- protected static $config = [
- 'prefix' => '',
- 'expire' => 0,
- 'path' => '/',
- 'domain' => '',
- 'secure' => false,
- 'httponly' => false,
- 'setcookie' => true,
- ];
-
- protected static $init;
-
- public static function init(array $config = [])
- {
- if (empty($config)) {
- $config = Config::get('cookie');
- }
- self::$config = array_merge(self::$config, array_change_key_case($config));
- if (!empty(self::$config['httponly'])) {
- ini_set('session.cookie_httponly', 1);
- }
- self::$init = true;
- }
-
- public static function prefix($prefix = '')
- {
- if (empty($prefix)) {
- return self::$config['prefix'];
- }
- return self::$config['prefix'] = $prefix;
- }
-
- public static function set($name, $value = '', $option = null)
- {
- !isset(self::$init) && self::init();
-
- if (!is_null($option)) {
- if (is_numeric($option)) {
- $option = ['expire' => $option];
- } elseif (is_string($option)) {
- parse_str($option, $option);
- }
- $config = array_merge(self::$config, array_change_key_case($option));
- } else {
- $config = self::$config;
- }
- $name = $config['prefix'] . $name;
-
- if (is_array($value)) {
- array_walk_recursive($value, 'self::jsonFormatProtect', 'encode');
- $value = 'think:' . json_encode($value);
- }
- $expire = !empty($config['expire']) ?
- $_SERVER['REQUEST_TIME'] + intval($config['expire']) :
- 0;
- if ($config['setcookie']) {
- setcookie(
- $name, $value, $expire, $config['path'], $config['domain'],
- $config['secure'], $config['httponly']
- );
- }
- $_COOKIE[$name] = $value;
- }
-
- public static function forever($name, $value = '', $option = null)
- {
- if (is_null($option) || is_numeric($option)) {
- $option = [];
- }
- $option['expire'] = 315360000;
- self::set($name, $value, $option);
- }
-
- public static function has($name, $prefix = null)
- {
- !isset(self::$init) && self::init();
- $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
- return isset($_COOKIE[$prefix . $name]);
- }
-
- public static function get($name = '', $prefix = null)
- {
- !isset(self::$init) && self::init();
- $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
- $key = $prefix . $name;
- if ('' == $name) {
-
- if ($prefix) {
- $value = [];
- foreach ($_COOKIE as $k => $val) {
- if (0 === strpos($k, $prefix)) {
- $value[$k] = $val;
- }
- }
- } else {
- $value = $_COOKIE;
- }
- } elseif (isset($_COOKIE[$key])) {
- $value = $_COOKIE[$key];
- if (0 === strpos($value, 'think:')) {
- $value = json_decode(substr($value, 6), true);
- array_walk_recursive($value, 'self::jsonFormatProtect', 'decode');
- }
- } else {
- $value = null;
- }
- return $value;
- }
-
- public static function delete($name, $prefix = null)
- {
- !isset(self::$init) && self::init();
- $config = self::$config;
- $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
- $name = $prefix . $name;
- if ($config['setcookie']) {
- setcookie(
- $name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'],
- $config['domain'], $config['secure'], $config['httponly']
- );
- }
-
- unset($_COOKIE[$name]);
- }
-
- public static function clear($prefix = null)
- {
- if (empty($_COOKIE)) {
- return;
- }
- !isset(self::$init) && self::init();
-
- $config = self::$config;
- $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
- if ($prefix) {
- foreach ($_COOKIE as $key => $val) {
- if (0 === strpos($key, $prefix)) {
- if ($config['setcookie']) {
- setcookie(
- $key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'],
- $config['domain'], $config['secure'], $config['httponly']
- );
- }
- unset($_COOKIE[$key]);
- }
- }
- }
- }
-
- protected static function jsonFormatProtect(&$val, $key, $type = 'encode')
- {
- if (!empty($val) && true !== $val) {
- $val = 'decode' == $type ? urldecode($val) : urlencode($val);
- }
- }
- }
|