2019-12-09 23:44:56 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2022-01-02 07:27:47 +00:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 14:45:36 +00: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-12-09 23:44:56 +00:00
|
|
|
|
2021-10-23 09:39:29 +00:00
|
|
|
namespace Friendica\Core\Session\Factory;
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
use Friendica\App;
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Cache\Enum;
|
2021-12-13 18:40:38 +00:00
|
|
|
use Friendica\Core\Cache\Factory\Cache;
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
|
|
|
use Friendica\Core\Session\Capability\IHandleSessions;
|
|
|
|
use Friendica\Core\Session\Type;
|
|
|
|
use Friendica\Core\Session\Handler;
|
2019-12-09 23:44:56 +00:00
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-12-09 23:50:05 +00:00
|
|
|
/**
|
|
|
|
* Factory for creating a valid Session for this run
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
class Session
|
2019-12-09 23:44:56 +00:00
|
|
|
{
|
|
|
|
/** @var string The plain, PHP internal session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_NATIVE = 'native';
|
2019-12-09 23:44:56 +00:00
|
|
|
/** @var string Using the database for session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_DATABASE = 'database';
|
2019-12-09 23:44:56 +00:00
|
|
|
/** @var string Using the cache for session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_CACHE = 'cache';
|
|
|
|
|
|
|
|
const HANDLER_DEFAULT = self::HANDLER_DATABASE;
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-26 19:44:29 +00:00
|
|
|
* @param App\Mode $mode
|
|
|
|
* @param App\BaseURL $baseURL
|
|
|
|
* @param IManageConfigValues $config
|
|
|
|
* @param Database $dba
|
2021-12-13 18:40:38 +00:00
|
|
|
* @param Cache $cacheFactory
|
2021-10-26 19:44:29 +00:00
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param Profiler $profiler
|
|
|
|
* @param array $server
|
2019-12-09 23:44:56 +00:00
|
|
|
*/
|
2022-10-23 18:41:17 +00:00
|
|
|
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = []): IHandleSessions
|
2019-12-09 23:44:56 +00:00
|
|
|
{
|
2021-07-27 13:56:28 +00:00
|
|
|
$profiler->startRecording('session');
|
2019-12-09 23:44:56 +00:00
|
|
|
$session = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($mode->isInstall() || $mode->isBackend()) {
|
2021-10-26 19:44:29 +00:00
|
|
|
$session = new Type\Memory();
|
2019-12-09 23:44:56 +00:00
|
|
|
} else {
|
2019-12-11 19:30:31 +00:00
|
|
|
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
|
2021-10-26 19:44:29 +00:00
|
|
|
$handler = null;
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
switch ($session_handler) {
|
2019-12-11 19:30:31 +00:00
|
|
|
case self::HANDLER_DATABASE:
|
2021-10-26 19:44:29 +00:00
|
|
|
$handler = new Handler\Database($dba, $logger, $server);
|
2019-12-09 23:44:56 +00:00
|
|
|
break;
|
2019-12-11 19:30:31 +00:00
|
|
|
case self::HANDLER_CACHE:
|
2021-12-13 18:40:38 +00:00
|
|
|
$cache = $cacheFactory->createDistributed();
|
|
|
|
|
2019-12-09 23:44:56 +00:00
|
|
|
// In case we're using the db as cache driver, use the native db session, not the cache
|
2021-10-26 19:44:29 +00:00
|
|
|
if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
|
|
|
|
$handler = new Handler\Database($dba, $logger, $server);
|
2019-12-09 23:44:56 +00:00
|
|
|
} else {
|
2021-10-26 19:44:29 +00:00
|
|
|
$handler = new Handler\Cache($cache, $logger);
|
2019-12-09 23:44:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-11 19:30:31 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
$session = new Type\Native($baseURL, $handler);
|
2019-12-09 23:44:56 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2021-07-27 04:57:29 +00:00
|
|
|
$profiler->stopRecording();
|
2019-12-09 23:44:56 +00:00
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|