|
Server : nginx/1.18.0 System : Linux iZrj9edhd5u5pfsek09o1jZ 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 User : www ( 1000) PHP Version : 5.6.40 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /mnt/web/www.neatabattery.com/vendor/alipay/lotusphp_runtime/Cookie/ |
<?php
class LtCookie
{
public $configHandle;
private $secretKey;
public function __construct()
{
if (! $this->configHandle instanceof LtConfig)
{
if (class_exists("LtObjectUtil", false))
{
$this->configHandle = LtObjectUtil::singleton("LtConfig");
}
else
{
$this->configHandle = new LtConfig;
}
}
}
public function init()
{
$this->secretKey = $this->configHandle->get("cookie.secret_key");
if(empty($this->secretKey))
{
trigger_error("cookie.secret_key empty");
}
}
/**
* Decrypt the encrypted cookie
*
* @param string $encryptedText
* @return string
*/
protected function decrypt($encryptedText)
{
$key = $this->secretKey;
$cryptText = base64_decode($encryptedText);
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cryptText, MCRYPT_MODE_ECB, $iv);
return trim($decryptText);
}
/**
* Encrypt the cookie
*
* @param string $plainText
* @return string
*/
protected function encrypt($plainText)
{
$key = $this->secretKey;
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_ECB, $iv);
return trim(base64_encode($encryptText));
}
/**
* Set cookie value to deleted with $name
*
* @param array $args
* @return boolean
*/
public function delCookie($name, $path = '/', $domain = null)
{
if (isset($_COOKIE[$name]))
{
if (is_array($_COOKIE[$name]))
{
foreach($_COOKIE[$name] as $k => $v)
{
setcookie($name . '[' . $k . ']', '', time() - 86400, $path, $domain);
}
}
else
{
setcookie($name, '', time() - 86400, $path, $domain);
}
}
}
/**
* Get cookie value with $name
*
* @param string $name
* @return mixed
*/
public function getCookie($name)
{
$ret = null;
if (isset($_COOKIE[$name]))
{
if (is_array($_COOKIE[$name]))
{
$ret = array();
foreach($_COOKIE[$name] as $k => $v)
{
$v = $this->decrypt($v);
$ret[$k] = $v;
}
}
else
{
$ret = $this->decrypt($_COOKIE[$name]);
}
}
return $ret;
}
/**
* Set cookie
*
* @param array $args
* @return boolean
*/
public function setCookie($name, $value = '', $expire = null, $path = '/', $domain = null, $secure = 0)
{
if (is_array($value))
{
foreach($value as $k => $v)
{
$v = $this->encrypt($v);
setcookie($name . '[' . $k . ']', $v, $expire, $path, $domain, $secure);
}
}
else
{
$value = $this->encrypt($value);
setcookie($name, $value, $expire, $path, $domain, $secure);
}
}
}