*/
class Session
{
- public static $exists = false;
- public static $expire = 180000;
-
/**
* Returns the user id of locally logged in user or false.
*
--- /dev/null
+<?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;
+}
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;
try {
$data = $this->cache->get('session:' . $id);
if (!empty($data)) {
- Session::$exists = true;
return $data;
}
} catch (CachePersistenceException $exception) {
}
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;
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;
private $logger;
/** @var array The $_SERVER variable */
private $server;
+ /** @var bool global check, if the current Session exists */
+ private $sessionExists = false;
/**
* DatabaseSessionHandler constructor.
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) {
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);