Cookie.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class LtCookie
  3. {
  4. public $configHandle;
  5. private $secretKey;
  6. public function __construct()
  7. {
  8. if (! $this->configHandle instanceof LtConfig)
  9. {
  10. if (class_exists("LtObjectUtil", false))
  11. {
  12. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  13. }
  14. else
  15. {
  16. $this->configHandle = new LtConfig;
  17. }
  18. }
  19. }
  20. public function init()
  21. {
  22. $this->secretKey = $this->configHandle->get("cookie.secret_key");
  23. if(empty($this->secretKey))
  24. {
  25. trigger_error("cookie.secret_key empty");
  26. }
  27. }
  28. /**
  29. * Decrypt the encrypted cookie
  30. *
  31. * @param string $encryptedText
  32. * @return string
  33. */
  34. protected function decrypt($encryptedText)
  35. {
  36. $key = $this->secretKey;
  37. $cryptText = base64_decode($encryptedText);
  38. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  39. $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
  40. $decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cryptText, MCRYPT_MODE_ECB, $iv);
  41. return trim($decryptText);
  42. }
  43. /**
  44. * Encrypt the cookie
  45. *
  46. * @param string $plainText
  47. * @return string
  48. */
  49. protected function encrypt($plainText)
  50. {
  51. $key = $this->secretKey;
  52. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  53. $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
  54. $encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_ECB, $iv);
  55. return trim(base64_encode($encryptText));
  56. }
  57. /**
  58. * Set cookie value to deleted with $name
  59. *
  60. * @param array $args
  61. * @return boolean
  62. */
  63. public function delCookie($name, $path = '/', $domain = null)
  64. {
  65. if (isset($_COOKIE[$name]))
  66. {
  67. if (is_array($_COOKIE[$name]))
  68. {
  69. foreach($_COOKIE[$name] as $k => $v)
  70. {
  71. setcookie($name . '[' . $k . ']', '', time() - 86400, $path, $domain);
  72. }
  73. }
  74. else
  75. {
  76. setcookie($name, '', time() - 86400, $path, $domain);
  77. }
  78. }
  79. }
  80. /**
  81. * Get cookie value with $name
  82. *
  83. * @param string $name
  84. * @return mixed
  85. */
  86. public function getCookie($name)
  87. {
  88. $ret = null;
  89. if (isset($_COOKIE[$name]))
  90. {
  91. if (is_array($_COOKIE[$name]))
  92. {
  93. $ret = array();
  94. foreach($_COOKIE[$name] as $k => $v)
  95. {
  96. $v = $this->decrypt($v);
  97. $ret[$k] = $v;
  98. }
  99. }
  100. else
  101. {
  102. $ret = $this->decrypt($_COOKIE[$name]);
  103. }
  104. }
  105. return $ret;
  106. }
  107. /**
  108. * Set cookie
  109. *
  110. * @param array $args
  111. * @return boolean
  112. */
  113. public function setCookie($name, $value = '', $expire = null, $path = '/', $domain = null, $secure = 0)
  114. {
  115. if (is_array($value))
  116. {
  117. foreach($value as $k => $v)
  118. {
  119. $v = $this->encrypt($v);
  120. setcookie($name . '[' . $k . ']', $v, $expire, $path, $domain, $secure);
  121. }
  122. }
  123. else
  124. {
  125. $value = $this->encrypt($value);
  126. setcookie($name, $value, $expire, $path, $domain, $secure);
  127. }
  128. }
  129. }