Cookie.class.php 15.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
<?php

/**
 * Cookie.class.php
 * Cookie 操作
 * $Author$
 * $Id$
 */
namespace Com;

class Cookie
{

    /**
     * Cookie 秘钥
     *
     * @type string
     */
    protected $_cookie_secret = null;

    /**
     * Cookie 域名
     *
     * @type string
     */
    protected $_domain = null;

    /**
     * Cookie 过期时长
     *
     * @type int
     */
    protected $_expire = null;

    /**
     * Cookie 目录
     *
     * @type string
     */
    protected $_path = '/';

    /**
     * Cookie 数据
     *
     * @type array
     */
    protected $_cookie_data = array();

    /**
     * 原生 Cookie 数据
     *
     * @type array
     */
    protected $_datax = array();

    /**
     * 待输出的 Cookie 数据
     *
     * @type array
     */
    protected $_writecookie_data = array();

    /**
     * 校验字串
     *
     * @type string
     */
    private $__auth = null;

    /**
     * Cookie 数据
     *
     * @type array
     */
    private $__data = array();

    /**
     * 实例
     *
     * @type Cookie
     */
    private static $__instance = '';

    /**
     * Cookie 前缀
     *
     * @type string
     */
    private $cookiePrefix = '';

    /**
     * 获取当前实例
     *
     * @param string $domain        域名
     * @param int    $expire        有效时长
     * @param string $cookie_secret 加密秘钥
     *
     * @return Cookie
     */
    public static function &instance($domain = '', $expire = 0, $cookie_secret = '')
    {

        if (! Cookie::$__instance) {
            Cookie::$__instance = new Cookie($domain, $expire, $cookie_secret);
        }

        return Cookie::$__instance;
    }

    /**
     * 重置Cookie密钥
     *
     * @param $secret
     * @return bool
     */
    public function setCookieSecret($secret)
    {

        $this->_cookie_secret = $secret;

        return true;
    }

    /**
     * 写入到COOKIE中的最终数据
     *
     * @param string $domain        域名
     * @param int    $expire        有效时长
     * @param string $cookie_secret 加密秘钥
     */
    private function __construct($domain = '', $expire = 0, $cookie_secret = '')
    {
        // cookie前缀
        $this->cookiePrefix = cfg('COOKIE_PREFIX');

        // 域名为空时, 取当前域名
        if (! $domain) {
            $domain = cfg('COOKIE_DOMAIN');
            if (empty($domain)) {
                $domain = I('server.HTTP_HOST');
                if (- 1 < ($pos = stripos($domain, ':'))) {
                    $domain = substr($domain, 0, $pos);
                }
            }
        }
        // 有效时长
        if (! $expire) {
            $expire = cfg('COOKIE_EXPIRE');
        }
        // 加密秘钥
        if (! $cookie_secret) {
            $cookie_secret = cfg('COOKIE_SECRET');
        }
        $this->_domain = $domain;
        $this->_expire = $expire;
        $this->_cookie_secret = $cookie_secret;
        $this->_datax = array();
        // 加密数据
        $this->_cookie_data = array(
            $this->cookiePrefix . '_AUTH' => isset($_COOKIE[$this->cookiePrefix . '_AUTH']) ? $_COOKIE[$this->cookiePrefix . '_AUTH'] : '',
            $this->cookiePrefix . '_DATA' => isset($_COOKIE[$this->cookiePrefix . '_DATA']) ? $_COOKIE[$this->cookiePrefix . '_DATA'] : '',
            $this->cookiePrefix . '_SESSIONID' => isset($_COOKIE[$this->cookiePrefix . '_SESSIONID']) ? $_COOKIE[$this->cookiePrefix . '_SESSIONID'] : ''
        );
        $this->__auth = $this->_cookie_data[$this->cookiePrefix . '_AUTH'];
        $content = $this->_cookie_data[$this->cookiePrefix . '_DATA'];
        $session_id = $this->_cookie_data[$this->cookiePrefix . '_SESSIONID'];
        // 如果存在校验字串和 Cookie 数据
        if ($this->__auth && $content) {
            // 解密
            $data = $this->__cipher($content, 'DECODE', $this->_cookie_secret);
            // 验证数据正确性
            if ($this->__validate_auth($data, $this->_cookie_secret . $session_id, $this->__auth)) {
                parse_str($data, $this->__data);
            }
        }
        // 清理旧cookie;
        setcookie($this->cookiePrefix . '_AUTH', '', - 1, '/', $_SERVER['HTTP_HOST']);
        setcookie($this->cookiePrefix . '_DATA', '', - 1, '/', $_SERVER['HTTP_HOST']);
        setcookie($this->cookiePrefix . '_SESSIONID', '', - 1, '/', $_SERVER['HTTP_HOST']);
        // 新用户
        if (! $this->_cookie_data || ! array_key_exists($this->cookiePrefix . '_SESSIONID', $_COOKIE)) {
            $token = md5(uniqid());
            $this->_cookie_data[$this->cookiePrefix . '_SESSIONID'] = $token;
        }
        // 写 Cookie 头信息
        $this->_write();
    }

    /**
     * 获取 Cookie
     *
     * @param string $key Cookie 名称
     *
     * @return Ambigous <>|NULL
     */
    public function get($key)
    {

        $data = $this->__data;
        // 如果键值存在, 则返回对应的数据
        if (array_key_exists($key, $data)) {
            return $data[$key];
        }

        return null;
    }

    /**
     * 设置 Cookie
     *
     * @param string $key    键名
     * @param string $value  值
     * @param string $expire 有效时长
     * @param string $path   Cookie 路径
     * @param string $domain 域名
     *
     * @return boolean
     */
    public function set($key, $value, $expire = null, $path = null, $domain = null)
    {

        // 如果没有 SESSIONID 和键名, 则返回 false
        if (! is_string($key) || ! array_key_exists($this->cookiePrefix . '_SESSIONID', $this->_cookie_data)) {
            return false;
        }
        // 有效时长
        if ($expire !== null) {
            $this->_expire = $expire;
        }
        // 路径
        if ($path !== null) {
            $this->_path = $path;
        }
        // 域名
        if ($domain !== null) {
            $this->_domain = $domain;
        }
        $this->__data[$key] = $value;

        return $this->_write($this->__data);
    }

    /**
     * 取原生 Cookie
     *
     * @param string $key     键值
     * @param string $default 默认值
     *
     * @return string
     */
    public function getx($key, $default = null)
    {

        // 如果键值存在
        if ($key && array_key_exists($key, $_COOKIE)) {
            return $_COOKIE[$key];
        }

        return $default;
    }

    /**
     * 设置原生 Cookie
     *
     * @param string $key    键值
     * @param string $value  值
     * @param int    $expire 时长
     * @param string $path   路径
     * @param string $domain 域名
     *
     * @return boolean
     */
    public function setx($key, $value = null, $expire = 0, $path = '', $domain = '')
    {

        // 键值不存在
        if (! $key) {
            return false;
        }
        // 域名
        if (! $domain) {
            $domain = $this->_domain;
        }
        // 路径
        if (! $path) {
            $path = $this->_path;
        }
        // 时长
        if (! $expire) {
            $expire = $this->_expire;
        }
        // 存入数据
        $this->_datax[$key] = array(
            'value' => $value,
            'expire' => $expire,
            'path' => $path,
            'domain' => $domain
        );
        $this->_write();

        return true;
    }

    /**
     * 移除键值
     *
     * @param string $key 键值
     *
     * @return boolean
     */
    public function remove($key)
    {

        // 键值为空
        if (! is_string($key)) {
            return false;
        }

        $data = $this->__data;
        if (array_key_exists($key, $this->__data)) { // 如果自定义键值存在
            unset($this->__data[$key]);
            $this->_write();
        } elseif (array_key_exists($key, $_COOKIE)) { // 普通 Cookie 的键值存在
            $this->setx($key, null, - 3600, $this->_path, $this->_domain);
        }

        return true;
    }

    // 清除 Cookie
    public function destroy()
    {
        $expired = strtotime('-100 day');
        $write_data[$this->cookiePrefix . '_SESSIONID'] = array(
            'value' => '',
            'expired' => $expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );
        $write_data[$this->cookiePrefix . '_AUTH'] = array(
            'value' => '',
            'expired' => $expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );
        $write_data[$this->cookiePrefix . '_DATA'] = array(
            'value' => '',
            'expired' => $expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );
        $write_data['qyDomain'] = array(
            'value' => '',
            'expired' => $expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );
        // 外部人员 标识
        $write_data['wx_openid'] = array(
            'value' => '',
            'expired' => $expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );

        $this->_cookie_data[$this->cookiePrefix . '_AUTH'] = '';
        $this->_cookie_data[$this->cookiePrefix . '_SESSIONID'] = '';
        $this->_cookie_data[$this->cookiePrefix . '_DATA'] = '';
        $this->_cookie_data['qyDomain'] = '';
        $this->__data = array();
        $this->_writecookie_data = $write_data;
    }

    /**
     * 写 Cookie
     *
     * @param string $data Cookie 数据
     *
     * @return boolean
     */
    protected function _write($data = null)
    {

        $write_data = array();
        // 读取 SESSIONID
        $session_id = $this->_cookie_data[$this->cookiePrefix . '_SESSIONID'];
        // 合并数据
        if ($data && is_array($data)) {
            $this->__data = array_merge($this->__data, $data);
        }
        $content = http_build_query($this->__data);
        // 验证字串
        $this->_cookie_data[$this->cookiePrefix . '_AUTH'] = $this->__generate_auth($content, $this->_cookie_secret . $session_id);
        // 加密
        $this->_cookie_data[$this->cookiePrefix . '_DATA'] = $this->__cipher($content, 'ENCODE', $this->_cookie_secret);
        $cookie_expired = time() + $this->_expire;
        // SESSIONID
        $write_data[$this->cookiePrefix . '_SESSIONID'] = array(
            'value' => $session_id,
            'expired' => $cookie_expired,
            'path' => $this->_path,
            'domain' => $this->_domain
        );
        // 校验字串
        if ($this->_cookie_data[$this->cookiePrefix . '_AUTH']) {
            $write_data[$this->cookiePrefix . '_AUTH'] = array(
                'value' => $this->_cookie_data[$this->cookiePrefix . '_AUTH'],
                'expired' => $cookie_expired,
                'path' => $this->_path,
                'domain' => $this->_domain
            );
        }
        // Cookie 数据
        if ($this->_cookie_data[$this->cookiePrefix . '_DATA']) {
            $write_data[$this->cookiePrefix . '_DATA'] = array(
                'value' => $this->_cookie_data[$this->cookiePrefix . '_DATA'],
                'expired' => $cookie_expired,
                'path' => $this->_path,
                'domain' => $this->_domain
            );
        }
        // 处理原始数据
        if ($this->_datax) {
            // 遍历原始数据数组
            foreach ($this->_datax as $key => $value) {
                $write_data[$key] = array(
                    'value' => $value['value'],
                    'expired' => time() + $value['expire'],
                    'path' => $value['path'],
                    'domain' => $value['domain']
                );
            }
        }
        $this->_writecookie_data = $write_data;

        return true;
    }

    // 获取 cookie 数据
    public function get_cookie_data()
    {

        return $this->_writecookie_data;
    }

    /**
     * 清除/刷新缓存时调用
     *
     * @param string $buffer 缓存字串
     *
     * @return string
     */
    public function send($buffer = null)
    {

        // Cookie 数据
        $write_data = $this->_writecookie_data;
        if ($write_data && is_array($write_data)) {
            $i = 1;
            // 遍历待写的 Cookie
            foreach ($write_data as $name => $item) {
                if ($i == 1) {
                    $this->__set_cookie($name, $item['value'], $item['expired'], $item['path'], $item['domain'], true);
                } else {
                    $this->__set_cookie($name, $item['value'], $item['expired'], $item['path'], $item['domain'], false);
                }
                $i ++;
            }
        }

        return $buffer;
    }

    /**
     * 设置 Cookie
     *
     * @param string $name    Cookie 名称
     * @param string $value   Cookie 值
     * @param number $expired 有效时长
     * @param string $path    Cookie 路径
     * @param string $domain  Cookie 域名
     * @param string $replace 是否替换类似 Cookie
     *
     * @return boolean
     */
    private function __set_cookie($name, $value = '', $expired = 0, $path = '/', $domain = '', $replace = false)
    {
        // 判断有效时间
        if (empty($this->_expire)) {
            $cook_str = 'Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value) . '; path=' . $path . '; domain=' . $domain;
        } else {
            $cook_str = 'Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value) . '; expires=' . gmdate('D, d-M-Y H:i:s \G\M\T', $expired) . '; path=' . $path . '; domain=' . $domain;
        }
        header($cook_str, $replace);

        return true;
    }

    /**
     * 生成校验字串
     *
     * @param array  $data          数据
     * @param string $cookie_secret 干扰字串
     *
     * @return string
     */
    private function __generate_auth($data, $cookie_secret)
    {

        return md5($data . $cookie_secret);
    }

    /**
     * 验证校验字串的正确性
     *
     * @param array  $data          数据
     * @param string $cookie_secret 干扰字串
     * @param string $auth_key      校验字串
     *
     * @return boolean
     */
    private function __validate_auth($data, $cookie_secret, $auth_key)
    {

        return $auth_key == $this->__generate_auth($data, $cookie_secret);
    }

    /**
     * 加/解密
     *
     * @param string $string    待加/解密字串
     * @param string $operation 操作(ENCODE/DECODE)
     * @param string $key       秘钥
     *
     * @return string
     */
    private function __cipher($string, $operation, $key = '')
    {

        $key = md5($key);
        $key_length = strlen($key);
        $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
        $string_length = strlen($string);
        $rndkey = $box = array();
        $result = '';
        // 生成随机串
        for ($i = 0; $i <= 255; $i ++) {
            $rndkey[$i] = ord($key[$i % $key_length]);
            $box[$i] = $i;
        }
        // 根据随机串重新生成密码对照表
        for ($j = $i = 0; $i < 256; $i ++) {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        // 加/解密
        for ($a = $j = $i = 0; $i < $string_length; $i ++) {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }
        // 如果是解密操作
        if ($operation == 'DECODE') {
            // 剔除干扰字串
            if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
                return substr($result, 8);
            } else {
                return '';
            }
        } else {
            return str_replace('=', '', base64_encode($result));
        }
    }
}