friendica/src/Core/Session/Handler/Cache.php

121 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @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;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Core\Session;
2021-10-26 21:44:29 +02:00
use Psr\Log\LoggerInterface;
use SessionHandlerInterface;
/**
* SessionHandler using Friendica Cache
*/
2020-02-09 15:45:36 +01:00
class Cache implements SessionHandlerInterface
{
2021-10-26 21:44:29 +02:00
/** @var ICanCache */
private $cache;
2021-10-26 21:44:29 +02:00
/** @var LoggerInterface */
private $logger;
2021-10-26 21:44:29 +02:00
public function __construct(ICanCache $cache, LoggerInterface $logger)
{
2021-10-26 21:44:29 +02:00
$this->cache = $cache;
$this->logger = $logger;
}
2021-10-26 21:44:29 +02:00
public function open($path, $name): bool
{
return true;
}
2021-10-26 21:44:29 +02:00
public function read($id)
{
2021-10-26 21:44:29 +02:00
if (empty($id)) {
return '';
}
2021-10-26 21:44:29 +02:00
try {
$data = $this->cache->get('session:' . $id);
if (!empty($data)) {
Session::$exists = true;
return $data;
}
} catch (CachePersistenceException $exception) {
$this->logger->warning('Cannot read session.'. ['id' => $id, 'exception' => $exception]);
return '';
}
return '';
}
/**
2020-01-19 07:05:23 +01:00
* Standard PHP session write callback
*
* This callback updates the stored session data and/or the expiration depending
* on the case. Uses the Session::expire for existing session, 5 minutes
* for newly created session.
*
2021-10-26 21:44:29 +02:00
* @param string $id Session ID with format: [a-z0-9]{26}
* @param string $data Serialized session data
*
2021-10-26 21:44:29 +02:00
* @return bool Returns false if parameters are missing, true otherwise
*/
2021-10-26 21:44:29 +02:00
public function write($id, $data): bool
{
2021-10-26 21:44:29 +02:00
if (!$id) {
return false;
}
2021-10-26 21:44:29 +02:00
if (!$data) {
return $this->destroy($id);
}
2021-10-26 21:44:29 +02:00
try {
return $this->cache->set('session:' . $id, $data, Session::$expire);
} catch (CachePersistenceException $exception) {
$this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
return false;
}
}
2021-10-26 21:44:29 +02:00
public function close(): bool
{
return true;
}
2021-10-26 21:44:29 +02:00
public function destroy($id): bool
{
2021-10-26 21:44:29 +02:00
try {
return $this->cache->delete('session:' . $id);
} catch (CachePersistenceException $exception) {
$this->logger->warning('Cannot destroy session', ['id' => $id, 'exception' => $exception]);
return false;
}
}
2021-10-26 21:44:29 +02:00
public function gc($max_lifetime): bool
{
return true;
}
}