Files
OMS/app/base/lib/session.php
2025-12-28 23:13:25 +08:00

268 lines
7.6 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Copyright © ShopeX http://www.shopex.cn. All rights reserved.
* See LICENSE file for license details.
*/
class base_session{
private $_sess_id;
private $_sess_key = 's';
private $_session_started = false;
private $_sess_expires = 60;
private $_cookie_expires = 0;
private $_session_destoryed = false;
function __construct()
{
if(defined('SESS_NAME') && constant('SESS_NAME')) $this->_sess_key = constant('SESS_NAME');
if(defined('SESS_CACHE_EXPIRE') && constant('SESS_CACHE_EXPIRE')) $this->_sess_expires = constant('SESS_CACHE_EXPIRE');
}//End Function
/**
* sess_id
* @return mixed 返回值
*/
public function sess_id(){
return $this->_sess_id;
}
/**
* 设置_sess_expires
* @param mixed $minute minute
* @return mixed 返回操作结果
*/
public function set_sess_expires($minute)
{
$this->_sess_expires = $minute;
}//End Function
/**
* 设置_cookie_expires
* @param mixed $minute minute
* @return mixed 返回操作结果
*/
public function set_cookie_expires($minute)
{
$this->_cookie_expires = ($minute > 0) ? $minute : 0;
if(isset($this->_sess_id)){
$cookie_path = kernel::base_url();
$cookie_path = $cookie_path ? $cookie_path : "/";
header(sprintf('Set-Cookie: %s=%s; path=%s; expires=%s; httpOnly; SameSite=Strict; %s;', $this->_sess_key, $this->_sess_id, $cookie_path, gmdate('D, d M Y H:i:s T', time()+$minute*60), constant('WITH_HTTPS_SUPPORT')=='on'?'Secure':''), true);
}
}//End Function
/**
* start
* @param mixed $sence sence
* @return mixed 返回值
*/
public function start($sence = ''){
if($this->_session_started !== true){
// 登陆后重置session
if ($sence == 'login') {
unset($_GET['sess_id'], $_COOKIE[$this->_sess_key], $this->_sess_id);
}
$cookie_path = kernel::base_url();
$cookie_path = $cookie_path ? $cookie_path : "/";
if($this->_cookie_expires > 0){
$cookie_expires = sprintf("expires=%s;", gmdate('D, d M Y H:i:s T', time()+$this->_cookie_expires*60));
}else{
$cookie_expires = '';
}
if(isset($_GET['sess_id'])){
$this->_sess_id = $_GET['sess_id'];
if($_COOKIE[$this->_sess_key] != $_GET['sess_id'])
header(sprintf('Set-Cookie: %s=%s; path=%s; %s httpOnly; SameSite=Strict; %s;', $this->_sess_key, $this->_sess_id, $cookie_path, $cookie_expires, constant('WITH_HTTPS_SUPPORT')=='on'?'Secure':''), true);
}elseif($_COOKIE[$this->_sess_key]){
$this->_sess_id = $_COOKIE[$this->_sess_key];
}elseif(!$this->_sess_id){
$this->_sess_id = md5(microtime().base_request::get_remote_addr().mt_rand(0,9999));
header(sprintf('Set-Cookie: %s=%s; path=%s; %s httpOnly; SameSite=Strict; %s;', $this->_sess_key, $this->_sess_id, $cookie_path, $cookie_expires, constant('WITH_HTTPS_SUPPORT')=='on'?'Secure':''), true);
}
if($this->getStore()->fetch($this->getKey($this->_sess_id), $_SESSION) === false){
$_SESSION = array();
}
$this->_session_started = true;
register_shutdown_function(array(&$this,'close'));
}
return true;
}
/**
* close
* @param mixed $writeBack writeBack
* @return mixed 返回值
*/
public function close($writeBack = true){
if(strlen($this->_sess_id) != 32){
return false;
}
if(!$this->_session_started){
return false;
}
$this->_session_started = false;
if(!$writeBack){
return false;
}
if($this->_session_destoryed){
return true;
}else{
return $this->getStore()->store($this->getKey($this->_sess_id), $_SESSION, ($this->_sess_expires * 60));
}
}
/**
* destory
* @return mixed 返回值
*/
public function destory(){
if(!$this->_session_started){
return false;
}
$this->_session_started = false;
$res = $this->getStore()->store($this->getKey($this->_sess_id), array(), 1);
if($res){
$_SESSION = array();
$this->_session_destoryed = true;
$cookie_path = kernel::base_url();
$cookie_path = $cookie_path ? $cookie_path : "/";
header(sprintf('Set-Cookie: %s=%s; path=%s; httpOnly; SameSite=Strict; %s;', $this->_sess_key, $this->_sess_id, $cookie_path, constant('WITH_HTTPS_SUPPORT')=='on'?'Secure':''), true);
unset($this->_sess_id);
return true;
}else{
return false;
}
}
/**
* 通过对常量 MEMCACHE 的识别返回不同的存储对像
*
* @author hzjsq
× @param void
* @return Object
*/
private function getStore() {
if ($this->useCache()) {
return cachecore::instance();
} else {
return base_kvstore::instance('sessions');
}
}
/**
* 通过对常量对缓存key进行适配
*
* @author hzjsq
× @param $key String
* @return String
*/
private function getKey($key) {
if ($this->useCache()) {
$key = 'S_' . $key;
}
return $key;
}
/**
* 确定是否使用 MEMCACHE 缓存SESSION
*
* @author hzjsq
* @praram void
* @return Boolean
*/
private function useCache() {
//判断是否具有缓存配置来判断能否使用Cache模块保存SESSION
if(defined('CACHE_STORAGE') && in_array(constant('CACHE_STORAGE'), array('base_cache_memcache','base_cache_memcached','base_cache_redis'))){
return true;
} else {
return false;
}
}
public function getMobileVerifyCode($telephone, &$errorMsg) {
$key = $this->getKey($this->_sess_id.'mobileverifycode');
$this->getStore()->fetch($key, $verifyCode);
if(!$verifyCode) {
$verifyCode = mt_rand(100000,999999);
}
if(!kernel::single('taoexlib_sms')->sendSms(array('event_type'=>'login','check_code'=>$verifyCode,'telephone'=>$telephone), $errorMsg)) {
return false;
}
$this->getStore()->store($key, $verifyCode, 600);
return true;
}
/**
* 检查MobileVerifyCode
* @return mixed 返回验证结果
*/
public function checkMobileVerifyCode() {
$key = $this->getKey($this->_sess_id.'mobileverifycode');
$this->getStore()->fetch($key, $verifyCode);
if($verifyCode && $verifyCode == $_POST['mobileverifycode']) {
return true;
}
return false;
}
/**
* 删除指定的session_id
* @param string $session_id
*
* @return boolean
*/
public function deleteSessionId($session_id)
{
if(empty($session_id)){
return true;
}
//kv缓存key键名
$kv_session_id = $this->getKey($session_id);
//删除指定kv缓存
$this->getStore()->store($kv_session_id, array(), 1);
return true;
}
/**
* 指定KEY缓存SESSION
*
* @return void
* @author
**/
public function appointStore($key, $ttl)
{
$res = $this->getStore()->store($this->getKey($key), $_SESSION, $ttl);
return $res;
}
/**
* 指定KEY获取SESSION
*
* @return void
* @author
**/
public function appointFetch($key)
{
$this->getStore()->fetch($this->getKey($key), $s);
return $s;
}
}