Created
August 25, 2021 02:28
-
-
Save atsu666/42bfba2efbcd7cf475108012a7e7a630 to your computer and use it in GitHub Desktop.
php/ACMS/Session.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ACMS_Session | |
{ | |
protected static $instance = array(); | |
protected static $sessionStart = false; | |
protected $sess_name = 'acms_ssid'; | |
protected $sess_storage = 'acms_storage'; | |
protected $storage; | |
protected $lifetime = 7200; | |
protected $path = ''; | |
protected $domain = ''; | |
protected $secure = false; | |
protected $httponly = true; | |
public static function singleton($config = array()) | |
{ | |
$id = isset($config['sess_storage']) ? $config['sess_storage'] : 'sess_storage'; | |
if (!isset(self::$instance[$id])) { | |
$obj = new self($config); | |
self::$instance[$id] = $obj; | |
} | |
return self::$instance[$id]; | |
} | |
/** | |
* ACMS_Session constructor. | |
* | |
* @param array $config | |
*/ | |
private function __construct($config) | |
{ | |
$host = COOKIE_HOST; | |
$this->path = '/'; | |
$this->domain = ($host !== 'localhost') ? $host : false; | |
$this->secure = (COOKIE_SECURE === 1); | |
foreach ($config as $key => $val) { | |
if (!property_exists($this, $key)) { | |
continue; | |
} | |
$this->$key = $val; | |
} | |
//------------------------ | |
// セッションの開始(1度だけ) | |
if (!self::$instance) { | |
if (defined('PHP_SESSION_USE_DB') && PHP_SESSION_USE_DB) { | |
$handler = new ACMS_SessionHandler(); | |
if (session_set_save_handler( | |
array($handler, 'open'), | |
array($handler, 'close'), | |
array($handler, 'read'), | |
array($handler, 'write'), | |
array($handler, 'destroy'), | |
array($handler, 'gc') | |
)) { | |
register_shutdown_function('session_write_close'); | |
} | |
} | |
$this->start(); | |
} | |
//--------------------------------- | |
// セッション変数から,前回の状態を取得 | |
$this->storage = isset($_SESSION[$this->sess_storage]) ? acmsUnserialize($_SESSION[$this->sess_storage]) | |
: array(); | |
} | |
/** | |
* Session start. | |
* | |
* @return void | |
*/ | |
public function start() | |
{ | |
session_name($this->sess_name); | |
@session_set_cookie_params($this->lifetime, $this->path, $this->domain, $this->secure, $this->httponly); | |
session_cache_limiter(''); | |
session_start(); | |
$expiresKey = 'acms-session-expires'; | |
if (isset($_SESSION[$expiresKey])) { | |
if ($_SESSION[$expiresKey] + 300 < time()) { | |
$_SESSION[$expiresKey] = time(); | |
session_regenerate_id(true); | |
} | |
} else { | |
$_SESSION[$expiresKey] = time(); | |
} | |
} | |
/** | |
* Save | |
* | |
* @return void | |
*/ | |
public function save() | |
{ | |
$_SESSION[$this->sess_storage] = acmsSerialize($this->storage); | |
} | |
/** | |
* Get | |
* | |
* @param string $key | |
* | |
* @return bool | |
*/ | |
public function get($key) | |
{ | |
return isset($this->storage[$key]) ? $this->storage[$key] : false; | |
} | |
/** | |
* Set | |
* | |
* @param string $key | |
* @param mixed $val | |
* | |
* @return void | |
*/ | |
public function set($key, $val) | |
{ | |
$this->storage[$key] = $val; | |
} | |
/** | |
* Delete | |
* | |
* @param string $key | |
* | |
* @return void | |
*/ | |
public function delete($key) | |
{ | |
unset($this->storage[$key]); | |
} | |
/** | |
* Clear | |
* | |
* @return void | |
*/ | |
public function clear() | |
{ | |
$this->storage = null; | |
$this->save(); | |
} | |
} | |
/** | |
* Class ACMS_SessionHandler | |
*/ | |
class ACMS_SessionHandler | |
{ | |
/** | |
* @var string | |
*/ | |
private $savePath; | |
/** | |
* @param $savePath | |
* @param $sessionName | |
* | |
* @return bool | |
*/ | |
function open($savePath, $sessionName) | |
{ | |
$this->savePath = $savePath; | |
return true; | |
} | |
/** | |
* @return bool | |
*/ | |
function close() | |
{ | |
return true; | |
} | |
/** | |
* @param $id | |
* | |
* @return mixed | |
*/ | |
function read($id) | |
{ | |
$DB = DB::singleton(dsn()); | |
$SQL = SQL::newSelect('session_php'); | |
$SQL->addSelect('session_data'); | |
$SQL->addWhereOpr('session_id', $id); | |
$data = $DB->query($SQL->get(dsn()), 'one'); | |
return $data ? $data : ''; | |
} | |
/** | |
* @param $id | |
* @param $data | |
* | |
* @return bool | |
*/ | |
function write($id, $data) | |
{ | |
$DB = DB::singleton(dsn()); | |
$SQL = SQL::newSelect('session_php'); | |
$SQL->addSelect('session_id'); | |
$SQL->addWhereOpr('session_id', $id); | |
if ($DB->query($SQL->get(dsn()), 'one')) { | |
$SQL = SQL::newUpdate('session_php'); | |
$SQL->addUpdate('session_expire', REQUEST_TIME); | |
$SQL->addUpdate('session_data', $data); | |
$SQL->addWhereOpr('session_id', $id); | |
} else { | |
$SQL = SQL::newInsert('session_php'); | |
$SQL->addInsert('session_id', $id); | |
$SQL->addInsert('session_expire', REQUEST_TIME); | |
$SQL->addInsert('session_data', $data); | |
} | |
$DB->query($SQL->get(dsn()), 'exec'); | |
return ($DB->affected_rows() > 0); | |
} | |
/** | |
* @param $id | |
* | |
* @return bool | |
*/ | |
function destroy($id) | |
{ | |
$DB = DB::singleton(dsn()); | |
$SQL = SQL::newDelete('session_php'); | |
$SQL->addWhereOpr('session_id', $id); | |
$DB->query($SQL->get(dsn()), 'exec'); | |
return true; | |
} | |
/** | |
* @param $maxlifetime | |
* | |
* @return bool | |
*/ | |
function gc($maxlifetime) | |
{ | |
$DB = DB::singleton(dsn()); | |
$SQL = SQL::newDelete('session_php'); | |
$SQL->addWhereOpr('session_expire', REQUEST_TIME - $maxlifetime, '<'); | |
$DB->query($SQL->get(dsn()), 'exec'); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment