.gitignore000064400000000060144761607070006544 0ustar00/vendor/ /tests/ composer.lock .vscode/ /src_1/ README.md000064400000000014144761607070006032 0ustar00# dhsession composer.json000064400000000736144761607070007310 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.txt000064400000000311144761607070011705 0ustar00{ "require": { "boru/dhout": "dev-master" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/Session.php000064400000014321144761607070007504 0ustar00init(); self::$hasInit = true; } public static function getHandler() { return self::$handler; } public static function createHandler($type="default",$options=[]) { $class = "boru\\dhsession\\handlers\\".ucfirst($type)."Handler"; if(!class_exists($class)) { throw new \Exception("Handler $type not found"); } return new $class($options); } public static function start($handler=null) { if($handler) { self::setHandler($handler); } if(self::$handler->start()) { /*$data = self::$handler->read(session_id()); if($data && !is_array($data)) { $_SESSION = self::unserialize($data); }*/ } } public static function get($key,$force=false) { self::start(); if($force) { $_SESSION = self::getSession(self::getId()); } return isset($_SESSION[$key]) ? $_SESSION[$key] : null; } public static function set($key, $value,$force=false) { self::start(); if($force) { $_SESSION = self::getSession(self::getId()); } $_SESSION[$key] = $value; if($force) { self::setSession(self::getId(),$_SESSION); } } public static function setId($id) { self::start(); self::$handler->setSessionId($id); } public static function delete($key) { self::start(); unset($_SESSION[$key]); } public static function destroy() { self::start(); session_destroy(); } public static function refresh($newId=null) { self::start(); self::$handler->refresh($newId); } public static function getId() { self::start(); return self::$handler->getSessionId(); } public static function getName() { self::start(); return self::$handler->getSessionName(); } public static function setMetaId($metId) { self::start(); self::$handler->setMetaId($metId); } public static function getMetaId() { self::start(); return self::$handler->getMetaId(); } //Management functions public static function getSession($sessionId) { $data = self::$handler->read($sessionId); return $data ? unserialize($data) : null; } public static function setSession($sessionId, $data) { self::$handler->write($sessionId, serialize($data)); return true; } public static function deleteSession($sessionId,$key=null) { 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::$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) { 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(); } } public static function unserialize($session_data) { $method = ini_get("session.serialize_handler"); switch ($method) { case "php": return self::unserialize_php($session_data); break; case "php_binary": return self::unserialize_phpbinary($session_data); break; default: throw new \Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary"); } } private static function unserialize_php($session_data) { $return_data = array(); $offset = 0; while ($offset < strlen($session_data)) { if (!strstr(substr($session_data, $offset), "|")) { return $return_data; //throw new \Exception("invalid data, remaining: " . substr($session_data, $offset)); } $pos = strpos($session_data, "|", $offset); $num = $pos - $offset; $varname = substr($session_data, $offset, $num); $offset += $num + 1; $data = unserialize(substr($session_data, $offset)); $return_data[$varname] = $data; $offset += strlen(serialize($data)); } return $return_data; } private static function unserialize_phpbinary($session_data) { $return_data = array(); $offset = 0; while ($offset < strlen($session_data)) { $num = ord($session_data[$offset]); $offset += 1; $varname = substr($session_data, $offset, $num); $offset += $num; $data = unserialize(substr($session_data, $offset)); $return_data[$varname] = $data; $offset += strlen(serialize($data)); } return $return_data; } }src/handlers/AdodbHandler.php000064400000012753144761607070012177 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() { $this->closeCallback(); 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)) { $this->readCallback(Session::unserialize($row->get("data"))); return $row["data"]; } if(empty($this->getSessionId())) { $this->refresh(); } return ""; } /** * @return bool */ public function write($id, $data) { $row = $this->readRaw($id); if($row && !empty($row["data"])) { $prevData = Session::unserialize($row["data"]); $newData = Session::unserialize($data); $_SESSION = array_merge($prevData, $newData); $data = session_encode(); } $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.php000064400000002612144761607070012543 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($newId=null) { if($newId) { session_id($newId); } else { session_regenerate_id(); } $_SESSION = Session::unserialize($this->read(session_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.php000064400000013546144761607070012030 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); parent::init(); } /** * @return bool */ public function refresh($newId=null) { $this->start(); $currentId = session_id(); if(empty($newId)) { session_regenerate_id(); $newId = session_id(); } session_id($newId); $sql = "update ".$this->getTableName()." set sessionid = ?, modified = ? where sessionid = ?"; $this->db->query($sql, [$newId, time(), $currentId]); $this->setSessionId($newId); $data = $this->read($newId); if($data) { $_SESSION = Session::unserialize($data); } return true; } /** * @return bool */ public function close() { $this->closeCallback(); 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, $decode=true) { $sql = "select data from ".$this->getTableName()." where sessionid = ? AND modified > ?"; $result = $this->db->query($sql, [$id, $this->getExpireTime()]); if ($row = $this->db->next($result)) { /*if($decode) { $_SESSION = Session::unserialize($row->get("data")); }*/ $this->readCallback(Session::unserialize($row->get("data"))); 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) { $row = $this->readRaw($id); if($row && !empty($row["data"])) { $prevData = Session::unserialize($row["data"]); $newData = Session::unserialize($data); $_SESSION = array_merge($prevData, $newData); $data = session_encode(); } $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.php000064400000011521144761607070013056 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"]); } if (isset($options["startCallback"])) { $this->setStartCallback($options["startCallback"]); } if (isset($options["closeCallback"])) { $this->setCloseCallback($options["closeCallback"]); } if (isset($options["readCallback"])) { $this->setReadCallback($options["readCallback"]); } } public function init() { ini_set("session.gc_maxlifetime", $this->expiry); ini_set("session.gc_probability", "0"); } public function start() { if (session_status() === PHP_SESSION_NONE) { session_start(); $this->setSessionId(session_id()); $this->setSessionName(session_name()); $this->isStarted = true; return true; } if(!$this->isStarted) { $this->startCallback(); } $this->setSessionId(session_id()); $this->setSessionName(session_name()); $this->isStarted = true; return false; } public function startCallback() { if ($this->callbackOnStart) { $callback = $this->callbackOnStart; $callback($this); } } public function closeCallback() { if ($this->callbackOnClose) { $callback = $this->callbackOnClose; $callback($this); } } public function readCallback($data) { if ($this->callbackOnRead) { $callback = $this->callbackOnRead; $callback($data); } } /** * @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; session_id($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()); } public function setStartCallback($callback) { $this->callbackOnStart = $callback; } public function setCloseCallback($callback) { $this->callbackOnClose = $callback; } public function setReadCallback($callback) { $this->callbackOnRead = $callback; } }src/handlers/MemcacheHandler.php000064400000001470144761607070012662 0ustar00