.gitignore000064400000000060144761607230006542 0ustar00/vendor/ /tests/ composer.lock .vscode/ /src_1/ README.md000064400000000014144761607230006030 0ustar00# dhsession composer.json000064400000000736144761607230007306 0ustar00{ "name": "boru/dhsession", "type": "library", "autoload": { "psr-4": { "boru\\dhsession\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "require": { "boru/dhutils": "*", "boru/dhdb": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] } instructions-composer.txt000064400000000311144761607230011703 0ustar00{ "require": { "boru/dhout": "dev-master" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/Session.php000064400000010260144761607230007500 0ustar00refresh(); } public static function getId() { self::assertInit(); return self::$handler->getSessionId(); } public static function getName() { self::assertInit(); return self::$handler->getSessionName(); } public static function setMetaId($metId) { self::assertInit(); self::$handler->setMetaId($metId); } public static function getMetaId() { self::assertInit(); return self::$handler->getMetaId(); } //Management functions public static function getSession($sessionId) { self::assertInit(); $data = self::$handler->read($sessionId); return $data ? unserialize($data) : null; } public static function setSession($sessionId, $data) { self::assertInit(); self::$handler->write($sessionId, serialize($data)); return true; } public static function deleteSession($sessionId,$key=null) { self::assertInit(); if($key) { $data = self::getSession($sessionId); unset($data[$key]); self::setSession($sessionId,$data); return true; } else { self::$handler->destroy($sessionId); return true; } } public static function destroySession($sessionId) { self::assertInit(); self::$handler->destroy($sessionId); return true; } public static function gc() { if(!self::$handler) { throw new \Exception("Session Handler not initialized"); } self::$handler->gc(ini_get('session.gc_maxlifetime')); return true; } public static function listSessions($key=null,$value=null) { self::assertInit(); if($key == "ip") { return self::$handler->listByIp($value); } elseif($key == "metaId") { return self::$handler->listByMetaId($value); } elseif($key == "userAgent") { return self::$handler->listByUserAgent($value); } else { return self::$handler->listSessions(); } } //Private functions private static function assertInit() { if(!self::$handler) { throw new \Exception("Session Handler not initialized"); } if(!self::$hasInit) { self::$handler->init(); self::$hasInit = true; } } }src/handlers/AdodbHandler.php000064400000012057144761607230012172 0ustar00db = $options["db"]; unset($options["db"]); } else { throw new \Exception("db not set"); } parent::__construct($options); } public function init() { $sql = "CREATE TABLE IF NOT EXISTS ".$this->getTableName()." ( `sessionid` varchar(255) NOT NULL, `ip` varchar(100) NOT NULL, `useragent` varchar(255) NOT NULL, `metaid` varchar(100) NOT NULL, `data` longtext NOT NULL, `modified` int NOT NULL, PRIMARY KEY (`sessionid`), KEY `idx_ip` (`ip`), KEY `idx_metaid` (`metaid`), KEY `idx_agent` (`useragent`), KEY `idx_modified` (`modified`) )"; $this->db->query($sql); return parent::init(); } /** * @return bool */ public function refresh() { $currentId = session_id(); $newId = session_regenerate_id(); $sql = "update ".$this->getTableName()." set sessionid = ?, modified = ? where sessionid = ?"; $this->db->pquery($sql, [$newId, time(), $currentId]); $this->setSessionId($newId); return true; } /** * @return bool */ public function close() { return $this->gc(); } /** * @return bool */ public function destroy($id) { $sql = "delete from ".$this->getTableName()." where sessionid = ?"; $this->db->pquery($sql, [$id]); return true; } /** * @return bool */ public function gc($max_lifetime = null) { $sql = "delete from ".$this->getTableName()." where modified < ?"; $this->db->pquery($sql, [$this->getExpireTime($max_lifetime)]); return true; } /** * @return bool */ public function open($path, $name) { $sql = "delete from ".$this->getTableName()." where sessionid = ? AND modified < ?"; $this->db->pquery($sql, [$this->getSessionId(), $this->getExpireTime()]); $this->setSessionName($name); return true; } /** * @return string|false */ public function read($id) { $sql = "select data from ".$this->getTableName()." where sessionid = ?"; $result = $this->db->pquery($sql, [$id]); if ($result && $row = $this->db->fetchAssoc($result)) { return $row["data"]; } if(empty($this->getSessionId())) { $this->refresh(); } return ""; } /** * @return bool */ public function write($id, $data) { $sql = "insert into ".$this->getTableName()." (sessionid, ip, `useragent`, `metaid`, data, modified) values (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE data = ?, modified = ?, metaid=?"; $this->db->pquery($sql, [$id, $_SERVER["REMOTE_ADDR"], $_SERVER['HTTP_USER_AGENT'], $this->getMetaId(), $data, time(), $data, time(), $this->getMetaId()]); return true; } /** * @return false|array */ public function readRaw($id) { $sql = "select data from ".$this->getTableName()." where sessionid = ?"; $result = $this->db->pquery($sql, [$id]); if ($result && $row = $this->db->fetchAssoc($result)) { return $row; } return false; } public function setMetaId($metaId) { $sql = "update ".$this->getTableName()." set metaid = ? where sessionid = ?"; $this->db->pquery($sql, [$metaId, $this->getSessionId()]); parent::setMetaId($metaId); } public function listSessions() { $sql = "select * from ".$this->getTableName(); $result = $this->db->query($sql); $sessions = []; while($row = $this->db->fetchAssoc($result)) { $sessions[] = $row; } return $sessions; } public function listByIp($ip) { $sql = "select * from ".$this->getTableName()." where ip = ?"; $result = $this->db->pquery($sql, [$ip]); $sessions = []; while($row = $this->db->fetchAssoc($result)) { $sessions[] = $row; } return $sessions; } public function listByUserAgent($userAgent) { $sql = "select * from ".$this->getTableName()." where useragent = ?"; $result = $this->db->pquery($sql, [$userAgent]); $sessions = []; while($row = $this->db->fetchAssoc($result)) { $sessions[] = $row; } return $sessions; } public function listByMetaId($metId) { $sql = "select * from ".$this->getTableName()." where metaid = ?"; $result = $this->db->pquery($sql, [$metId]); $sessions = []; while($row = $this->db->fetchAssoc($result)) { $sessions[] = $row; } return $sessions; } }src/handlers/DefaultHandler.php000064400000002300144761607230012533 0ustar00setOptions($options); //session_set_save_handler($this, true); } public function init() { parent::init(); return true; } public function close() { session_write_close(); return true; } public function destroy($id) { session_destroy(); return true; } public function gc($max_lifetime) { return true; } public function open($path, $name) { return true; } public function read($id) { return $_SESSION; } public function write($id, $data) { $_SESSION = $data; return true; } public function refresh() { session_regenerate_id(); return true; } public function readRaw($id) { return $_SESSION; } public function listSessions() { } public function listByIp($ip) { } public function listByUserAgent($userAgent) { } public function listByMetaId($metId) { } }src/handlers/DhdbHandler.php000064400000012104144761607230012013 0ustar00db = $options["db"]; unset($options["db"]); } else { throw new \Exception("db not set"); } parent::__construct($options); } public function init() { $sql = "CREATE TABLE IF NOT EXISTS ".$this->getTableName()." ( `sessionid` varchar(255) NOT NULL, `ip` varchar(100) NOT NULL, `useragent` varchar(255) NOT NULL, `metaid` varchar(100) NOT NULL, `data` longtext NOT NULL, `modified` int NOT NULL, PRIMARY KEY (`sessionid`), KEY `idx_ip` (`ip`), KEY `idx_metaid` (`metaid`), KEY `idx_agent` (`useragent`), KEY `idx_modified` (`modified`) )"; $this->db->query($sql); return parent::init(); } /** * @return bool */ public function refresh() { $currentId = session_id(); session_regenerate_id(); $newId = session_id(); $sql = "update ".$this->getTableName()." set sessionid = ?, modified = ? where sessionid = ?"; $this->db->query($sql, [$newId, time(), $currentId]); $this->setSessionId($newId); return true; } /** * @return bool */ public function close() { return $this->gc(); } /** * @return bool */ public function destroy($id) { $sql = "delete from ".$this->getTableName()." where sessionid = ?"; $this->db->query($sql, [$id]); return true; } /** * @return bool */ public function gc($max_lifetime = null) { $sql = "delete from ".$this->getTableName()." where modified < ?"; $this->db->query($sql, [$this->getExpireTime($max_lifetime)]); return true; } /** * @return bool */ public function open($path, $name) { $this->setSessionName($name); return true; } /** * @return string|false */ public function read($id) { $sql = "select data from ".$this->getTableName()." where sessionid = ? AND modified > ?"; $result = $this->db->query($sql, [$id, $this->getExpireTime()]); if ($row = $this->db->next($result)) { return $row->get("data"); } if(empty($this->getSessionId())) { //$this->refresh(); session_regenerate_id(); $this->setSessionId(session_id()); } return ""; } /** * @return bool */ public function write($id, $data) { $sql = "insert into ".$this->getTableName()." (sessionid, ip, `useragent`, `metaid`, data, modified) values (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE data = ?, modified = ?, `metaid`=?"; $this->db->query($sql, [$id, $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"], $this->getMetaId(), $data, time(), $data, time(), $this->getMetaId()]); return true; } /** * @return false|array */ public function readRaw($id) { $sql = "select data from ".$this->getTableName()." where sessionid = ?"; $result = $this->db->query($sql, [$id]); if ($row = $this->db->next($result)) { return $row->asArray(); } return false; } public function setMetaId($metaId) { $sql = "update ".$this->getTableName()." set metaid = ? where sessionid = ?"; $this->db->query($sql, [$metaId, $this->getSessionId()]); parent::setMetaId($metaId); } public function listSessions() { $sql = "select * from ".$this->getTableName(); $result = $this->db->query($sql); $sessions = []; while($row = $this->db->next($result)) { $sessions[] = $row->asArray(); } return $sessions; } public function listByIp($ip) { $sql = "select * from ".$this->getTableName()." where ip = ?"; $result = $this->db->query($sql, [$ip]); $sessions = []; while($row = $this->db->next($result)) { $sessions[] = $row->asArray(); } return $sessions; } public function listByUserAgent($userAgent) { $sql = "select * from ".$this->getTableName()." where useragent = ?"; $result = $this->db->query($sql, [$userAgent]); $sessions = []; while($row = $this->db->next($result)) { $sessions[] = $row->asArray(); } return $sessions; } public function listByMetaId($metId) { $sql = "select * from ".$this->getTableName()." where metaid = ?"; $result = $this->db->query($sql, [$metId]); $sessions = []; while($row = $this->db->next($result)) { $sessions[] = $row->asArray(); } return $sessions; } }src/handlers/HandlerInterface.php000064400000006136144761607230013062 0ustar00setOptions($options); session_set_save_handler($this, true); } public function setOptions($options=[]) { if (isset($options["expiry"])) { $this->setExpiry($options["expiry"]); } if (isset($options["sessionName"])) { $this->setSessionName($options["sessionName"]); } if (isset($options["tableName"])) { $this->setTableName($options["tableName"]); } } public function init() { ini_set("session.gc_maxlifetime", $this->expiry); ini_set("session.gc_probability", "0"); session_start(); $this->setSessionId(session_id()); $this->setSessionName(session_name()); return true; } /** * @return bool */ //abstract public function close(); /** * @return bool */ //abstract public function destroy($id); /** * @return bool */ //abstract public function gc($max_lifetime); /** * @return bool */ //abstract public function open($path, $name); /** * @return string|false */ //abstract public function read($id); /** * @return bool */ //abstract public function write($id, $data); /** * @return bool */ //abstract public function refresh(); /** * @return false|array */ abstract public function readRaw($id); abstract public function listSessions(); abstract public function listByIp($ip); abstract public function listByUserAgent($userAgent); abstract public function listByMetaId($metaId); public function setExpiry($expiry) { $this->expiry = $expiry; } public function getExpiry() { return $this->expiry; } public function setSessionId($sessionId) { $this->sessionId = $sessionId; } public function getSessionId() { return $this->sessionId; } public function setSessionName($sessionName) { $this->sessionName = $sessionName; } public function getSessionName() { return $this->sessionName; } public function setTableName($tableName) { $this->tableName = $tableName; } public function getTableName() { return $this->tableName; } public function setMetaId($metaId) { $this->metaId = $metaId; } public function getMetaId() { return $this->metaId; } public function getExpireTime($max_lifetime = null) { return time() - ($max_lifetime ? $max_lifetime : $this->getExpiry()); } }src/handlers/MemcacheHandler.php000064400000001470144761607230012660 0ustar00