From 391d1cbc1a853383a71f649b29fc9a88d4feb28d Mon Sep 17 00:00:00 2001 From: ikkez Date: Sat, 28 Dec 2024 23:41:34 +0100 Subject: [PATCH] fix: php 8.4 session handler compatibility --- db/jig/session.php | 55 +++++++++++++++++++++++++---------------- db/mongo/session.php | 49 +++++++++++++++++++++++-------------- db/sql/session.php | 51 +++++++++++++++++++++++--------------- session.php | 58 ++++++++++++++++++++++++-------------------- sessionadapter.php | 46 +++++++++++++++++++++++++++++++++++ web/pingback.php | 2 +- 6 files changed, 176 insertions(+), 85 deletions(-) create mode 100644 sessionadapter.php diff --git a/db/jig/session.php b/db/jig/session.php index eee13395..20f509b8 100644 --- a/db/jig/session.php +++ b/db/jig/session.php @@ -22,6 +22,9 @@ namespace DB\Jig; +use ReturnTypeWillChange; +use SessionAdapter; + //! Jig-managed session handler class Session extends Mapper { @@ -43,15 +46,17 @@ class Session extends Mapper { * @param $path string * @param $name string **/ - function open($path,$name) { - return TRUE; - } + function open(string $path, string $name): bool + { + return TRUE; + } /** * Close session * @return TRUE **/ - function close() { + function close(): bool + { $this->reset(); $this->sid=NULL; return TRUE; @@ -59,10 +64,12 @@ function close() { /** * Return session data in serialized format - * @return string + * @return string|false * @param $id string **/ - function read($id) { + #[ReturnTypeWillChange] + function read($id) + { $this->load(['@session_id=?',$this->sid=$id]); if ($this->dry()) return ''; @@ -87,7 +94,8 @@ function read($id) { * @param $id string * @param $data string **/ - function write($id,$data) { + function write(string $id, string $data): bool + { $this->set('session_id',$id); $this->set('data',$data); $this->set('ip',$this->_ip); @@ -102,19 +110,19 @@ function write($id,$data) { * @return TRUE * @param $id string **/ - function destroy($id) { + function destroy($id): bool + { $this->erase(['@session_id=?',$id]); return TRUE; } /** * Garbage collector - * @return TRUE - * @param $max int **/ - function cleanup($max) { - $this->erase(['@stamp+?erase(['@stamp+?onsuspect=$onsuspect; - session_set_save_handler( - [$this,'open'], - [$this,'close'], - [$this,'read'], - [$this,'write'], - [$this,'destroy'], - [$this,'cleanup'] - ); + if (version_compare(PHP_VERSION, '8.4.0')>=0) { + // TODO: remove this when php7 support is dropped + session_set_save_handler(new SessionAdapter($this)); + } else { + session_set_save_handler( + [$this,'open'], + [$this,'close'], + [$this,'read'], + [$this,'write'], + [$this,'destroy'], + [$this,'gc'] + ); + } register_shutdown_function('session_commit'); $fw=\Base::instance(); $headers=$fw->HEADERS; diff --git a/db/mongo/session.php b/db/mongo/session.php index 013f1717..f41f171f 100644 --- a/db/mongo/session.php +++ b/db/mongo/session.php @@ -22,6 +22,9 @@ namespace DB\Mongo; +use ReturnTypeWillChange; +use SessionAdapter; + //! MongoDB-managed session handler class Session extends Mapper { @@ -43,7 +46,8 @@ class Session extends Mapper { * @param $path string * @param $name string **/ - function open($path,$name) { + function open(string $path, string $name): bool + { return TRUE; } @@ -51,7 +55,8 @@ function open($path,$name) { * Close session * @return TRUE **/ - function close() { + function close(): bool + { $this->reset(); $this->sid=NULL; return TRUE; @@ -62,7 +67,9 @@ function close() { * @return string * @param $id string **/ - function read($id) { + #[ReturnTypeWillChange] + function read(string $id) + { $this->load(['session_id'=>$this->sid=$id]); if ($this->dry()) return ''; @@ -87,7 +94,8 @@ function read($id) { * @param $id string * @param $data string **/ - function write($id,$data) { + function write(string $id, string $data): bool + { $this->set('session_id',$id); $this->set('data',$data); $this->set('ip',$this->_ip); @@ -102,19 +110,19 @@ function write($id,$data) { * @return TRUE * @param $id string **/ - function destroy($id) { + function destroy($id): bool + { $this->erase(['session_id'=>$id]); return TRUE; } /** * Garbage collector - * @return TRUE - * @param $max int **/ - function cleanup($max) { - $this->erase(['$where'=>'this.stamp+'.$max.'<'.time()]); - return TRUE; + #[ReturnTypeWillChange] + function gc(int $max_lifetime): int + { + return (int) $this->erase(['$where'=>'this.stamp+'.$max_lifetime.'<'.time()]); } /** @@ -169,14 +177,19 @@ function agent() { function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) { parent::__construct($db,$table); $this->onsuspect=$onsuspect; - session_set_save_handler( - [$this,'open'], - [$this,'close'], - [$this,'read'], - [$this,'write'], - [$this,'destroy'], - [$this,'cleanup'] - ); + if (version_compare(PHP_VERSION, '8.4.0')>=0) { + // TODO: remove this when php7 support is dropped + session_set_save_handler(new SessionAdapter($this)); + } else { + session_set_save_handler( + [$this,'open'], + [$this,'close'], + [$this,'read'], + [$this,'write'], + [$this,'destroy'], + [$this,'gc'] + ); + } register_shutdown_function('session_commit'); $fw=\Base::instance(); $headers=$fw->HEADERS; diff --git a/db/sql/session.php b/db/sql/session.php index 8defbf40..45c7137c 100644 --- a/db/sql/session.php +++ b/db/sql/session.php @@ -22,6 +22,9 @@ namespace DB\SQL; +use ReturnTypeWillChange; +use SessionAdapter; + //! SQL-managed session handler class Session extends Mapper { @@ -43,7 +46,8 @@ class Session extends Mapper { * @param $path string * @param $name string **/ - function open($path,$name) { + function open(string $path, string $name): bool + { return TRUE; } @@ -51,7 +55,8 @@ function open($path,$name) { * Close session * @return TRUE **/ - function close() { + function close(): bool + { $this->reset(); $this->sid=NULL; return TRUE; @@ -59,10 +64,12 @@ function close() { /** * Return session data in serialized format - * @return string + * @return string|false * @param $id string **/ - function read($id) { + #[ReturnTypeWillChange] + function read(string $id) + { $this->load(['session_id=?',$this->sid=$id]); if ($this->dry()) return ''; @@ -86,7 +93,8 @@ function read($id) { * @param $id string * @param $data string **/ - function write($id,$data) { + function write(string $id, string $data): bool + { $this->set('session_id',$id); $this->set('data',$data); $this->set('ip',$this->_ip); @@ -101,19 +109,19 @@ function write($id,$data) { * @return TRUE * @param $id string **/ - function destroy($id) { + function destroy($id): bool + { $this->erase(['session_id=?',$id]); return TRUE; } /** * Garbage collector - * @return TRUE - * @param $max int **/ - function cleanup($max) { - $this->erase(['stamp+?erase(['stamp+?onsuspect=$onsuspect; - session_set_save_handler( - [$this,'open'], - [$this,'close'], - [$this,'read'], - [$this,'write'], - [$this,'destroy'], - [$this,'cleanup'] - ); + if (version_compare(PHP_VERSION, '8.4.0')>=0) { + // TODO: remove this when php7 support is dropped + session_set_save_handler(new SessionAdapter($this)); + } else { + session_set_save_handler( + [$this,'open'], + [$this,'close'], + [$this,'read'], + [$this,'write'], + [$this,'destroy'], + [$this,'gc'] + ); + } register_shutdown_function('session_commit'); $fw=\Base::instance(); $headers=$fw->HEADERS; diff --git a/session.php b/session.php index 9e8d6009..a87d602f 100644 --- a/session.php +++ b/session.php @@ -45,7 +45,8 @@ class Session extends Magic { * @param $path string * @param $name string **/ - function open($path,$name) { + function open(string $path, string $name): bool + { return TRUE; } @@ -53,7 +54,8 @@ function open($path,$name) { * Close session * @return TRUE **/ - function close() { + function close(): bool + { $this->sid=NULL; $this->_data=[]; return TRUE; @@ -61,13 +63,15 @@ function close() { /** * Return session data in serialized format - * @return string + * @return false|string * @param $id string **/ - function read($id) { + #[ReturnTypeWillChange] + function read(string $id) + { $this->sid=$id; if (!$data=$this->_cache->get($id.'.@')) - return ''; + return false; $this->_data = $data; if ($data['ip']!=$this->_ip || $data['agent']!=$this->_agent) { $fw=Base::instance(); @@ -85,11 +89,9 @@ function read($id) { /** * Write session data - * @return TRUE - * @param $id string - * @param $data string - **/ - function write($id,$data) { + */ + function write(string $id, string $data): bool + { $fw=Base::instance(); $jar=$fw->JAR; $this->_cache->set($id.'.@', @@ -106,21 +108,20 @@ function write($id,$data) { /** * Destroy session - * @return TRUE - * @param $id string - **/ - function destroy($id) { + */ + function destroy(string $id): bool + { $this->_cache->clear($id.'.@'); return TRUE; } /** * Garbage collector - * @return TRUE - * @param $max int **/ - function cleanup($max) { - $this->_cache->reset('.@',$max); + #[ReturnTypeWillChange] + function gc(int $max_lifetime) + { + $this->_cache->reset('.@',$max_lifetime); return TRUE; } @@ -175,14 +176,19 @@ function agent() { function __construct($onsuspect=NULL,$key=NULL,$cache=null) { $this->onsuspect=$onsuspect; $this->_cache=$cache?:Cache::instance(); - session_set_save_handler( - [$this,'open'], - [$this,'close'], - [$this,'read'], - [$this,'write'], - [$this,'destroy'], - [$this,'cleanup'] - ); + if (version_compare(PHP_VERSION, '8.4.0')>=0) { + // TODO: remove this when php7 support is dropped + session_set_save_handler(new SessionAdapter($this)); + } else { + session_set_save_handler( + [$this,'open'], + [$this,'close'], + [$this,'read'], + [$this,'write'], + [$this,'destroy'], + [$this,'gc'] + ); + } register_shutdown_function('session_commit'); $fw=\Base::instance(); $headers=$fw->HEADERS; diff --git a/sessionadapter.php b/sessionadapter.php new file mode 100644 index 00000000..87131185 --- /dev/null +++ b/sessionadapter.php @@ -0,0 +1,46 @@ +_handler = $handler; + } + + public function close(): bool + { + return $this->_handler->close(); + } + + public function destroy(string $id): bool + { + return $this->_handler->destroy($id); + } + + #[ReturnTypeWillChange] + public function gc(int $max_lifetime): int + { + return $this->_handler->gc($max_lifetime); + } + + public function open(string $path, string $name): bool + { + return $this->_handler->open($path, $name); + } + + #[ReturnTypeWillChange] + public function read(string $id): string + { + return $this->_handler->read($id); + } + + public function write(string $id, string $data): bool + { + return $this->_handler->write($id, $data); + } +} \ No newline at end of file diff --git a/web/pingback.php b/web/pingback.php index 28c51a5a..ef055820 100644 --- a/web/pingback.php +++ b/web/pingback.php @@ -69,7 +69,7 @@ function inspect($source) { $parts['host']==$fw->HOST) { $req=$web->request($source); $doc=new \DOMDocument('1.0',$fw->ENCODING); - $doc->stricterrorchecking=FALSE; + $doc->strictErrorChecking=FALSE; $doc->recover=TRUE; if (@$doc->loadhtml($req['body'])) { // Parse anchor tags