1
0
Fork 0

Move Session "exists" and "expire" to new class

This commit is contained in:
Philipp Holzer 2022-10-20 20:26:54 +02:00
parent 940619325d
commit 0b66b6e0d5
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
4 changed files with 36 additions and 14 deletions

View file

@ -31,9 +31,6 @@ use Friendica\Util\Strings;
*/
class Session
{
public static $exists = false;
public static $expire = 180000;
/**
* Returns the user id of locally logged in user or false.
*

View file

@ -0,0 +1,28 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Session\Handler;
abstract class AbstractSessionHandler implements \SessionHandlerInterface
{
/** @var int Duration of the Session */
public const EXPIRE = 180000;
}

View file

@ -23,14 +23,12 @@ namespace Friendica\Core\Session\Handler;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Core\Session;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface;
/**
* SessionHandler using Friendica Cache
*/
class Cache implements SessionHandlerInterface
class Cache extends AbstractSessionHandler
{
/** @var ICanCache */
private $cache;
@ -57,7 +55,6 @@ class Cache implements SessionHandlerInterface
try {
$data = $this->cache->get('session:' . $id);
if (!empty($data)) {
Session::$exists = true;
return $data;
}
} catch (CachePersistenceException $exception) {
@ -91,7 +88,7 @@ class Cache implements SessionHandlerInterface
}
try {
return $this->cache->set('session:' . $id, $data, Session::$expire);
return $this->cache->set('session:' . $id, $data, static::EXPIRE);
} catch (CachePersistenceException $exception) {
$this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
return false;

View file

@ -21,15 +21,13 @@
namespace Friendica\Core\Session\Handler;
use Friendica\Core\Session;
use Friendica\Database\Database as DBA;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface;
/**
* SessionHandler using database
*/
class Database implements SessionHandlerInterface
class Database extends AbstractSessionHandler
{
/** @var DBA */
private $dba;
@ -37,6 +35,8 @@ class Database implements SessionHandlerInterface
private $logger;
/** @var array The $_SERVER variable */
private $server;
/** @var bool global check, if the current Session exists */
private $sessionExists = false;
/**
* DatabaseSessionHandler constructor.
@ -66,7 +66,7 @@ class Database implements SessionHandlerInterface
try {
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]);
if ($this->dba->isResult($session)) {
Session::$exists = true;
$this->sessionExists = true;
return $session['data'];
}
} catch (\Exception $exception) {
@ -101,11 +101,11 @@ class Database implements SessionHandlerInterface
return $this->destroy($id);
}
$expire = time() + Session::$expire;
$expire = time() + static::EXPIRE;
$default_expire = time() + 300;
try {
if (Session::$exists) {
if ($this->sessionExists) {
$fields = ['data' => $data, 'expire' => $expire];
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
$this->dba->update('session', $fields, $condition);