From: Philipp Date: Sun, 23 Oct 2022 18:41:17 +0000 (+0200) Subject: Extend `IHandleUserSessions` from `IHandleSessions` and adapt classes X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b5bc1b0844c0cde24e006fb9dab9edc326933afd;p=friendica.git Extend `IHandleUserSessions` from `IHandleSessions` and adapt classes --- diff --git a/src/App.php b/src/App.php index 1933b359e7..c91ca4c4f2 100644 --- a/src/App.php +++ b/src/App.php @@ -26,7 +26,6 @@ use Friendica\App\Arguments; use Friendica\App\BaseURL; use Friendica\Capabilities\ICanCreateResponses; use Friendica\Core\Config\Factory\Config; -use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Module\Maintenance; use Friendica\Security\Authentication; @@ -129,15 +128,10 @@ class App */ private $pConfig; - /** - * @var IHandleSessions - */ - private $session; - /** * @var IHandleUserSessions */ - private $userSession; + private $session; /** * Set the user ID @@ -163,7 +157,7 @@ class App public function isLoggedIn(): bool { - return $this->userSession->getLocalUserId() && $this->user_id && ($this->user_id == $this->userSession->getLocalUserId()); + return $this->session->getLocalUserId() && $this->user_id && ($this->user_id == $this->session->getLocalUserId()); } /** @@ -177,7 +171,7 @@ class App $adminlist = explode(',', str_replace(' ', '', $admin_email)); - return $this->userSession->getLocalUserId() && $admin_email && $this->database->exists('user', ['uid' => $this->getLoggedInUserId(), 'email' => $adminlist]); + return $this->session->getLocalUserId() && $admin_email && $this->database->exists('user', ['uid' => $this->getLoggedInUserId(), 'email' => $adminlist]); } /** @@ -340,21 +334,20 @@ class App * @param L10n $l10n The translator instance * @param App\Arguments $args The Friendica Arguments of the call * @param IManagePersonalConfigValues $pConfig Personal configuration - * @param IHandleSessions $session The Session handler - */ - public function __construct(Database $database, IManageConfigValues $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, IManagePersonalConfigValues $pConfig, IHandleSessions $session, IHandleUserSessions $userSession) - { - $this->database = $database; - $this->config = $config; - $this->mode = $mode; - $this->baseURL = $baseURL; - $this->profiler = $profiler; - $this->logger = $logger; - $this->l10n = $l10n; - $this->args = $args; - $this->pConfig = $pConfig; - $this->session = $session; - $this->userSession = $userSession; + * @param IHandleUserSessions $sessions The (User)Session handler + */ + public function __construct(Database $database, IManageConfigValues $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, IManagePersonalConfigValues $pConfig, IHandleUserSessions $sessions) + { + $this->database = $database; + $this->config = $config; + $this->mode = $mode; + $this->baseURL = $baseURL; + $this->profiler = $profiler; + $this->logger = $logger; + $this->l10n = $l10n; + $this->args = $args; + $this->pConfig = $pConfig; + $this->session = $sessions; $this->load(); } @@ -502,11 +495,11 @@ class App $page_theme = null; // Find the theme that belongs to the user whose stuff we are looking at - if (!empty($this->profile_owner) && ($this->profile_owner != $this->userSession->getLocalUserId())) { + if (!empty($this->profile_owner) && ($this->profile_owner != $this->session->getLocalUserId())) { // Allow folks to override user themes and always use their own on their own site. // This works only if the user is on the same server $user = $this->database->selectFirst('user', ['theme'], ['uid' => $this->profile_owner]); - if ($this->database->isResult($user) && !$this->userSession->getLocalUserId()) { + if ($this->database->isResult($user) && !$this->session->getLocalUserId()) { $page_theme = $user['theme']; } } @@ -535,10 +528,10 @@ class App $page_mobile_theme = null; // Find the theme that belongs to the user whose stuff we are looking at - if (!empty($this->profile_owner) && ($this->profile_owner != $this->userSession->getLocalUserId())) { + if (!empty($this->profile_owner) && ($this->profile_owner != $this->session->getLocalUserId())) { // Allow folks to override user themes and always use their own on their own site. // This works only if the user is on the same server - if (!$this->userSession->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { $page_mobile_theme = $this->pConfig->get($this->profile_owner, 'system', 'mobile-theme'); } } @@ -635,7 +628,7 @@ class App } // ZRL - if (!empty($_GET['zrl']) && $this->mode->isNormal() && !$this->mode->isBackend() && !$this->userSession->getLocalUserId()) { + if (!empty($_GET['zrl']) && $this->mode->isNormal() && !$this->mode->isBackend() && !$this->session->getLocalUserId()) { // Only continue when the given profile link seems valid // Valid profile links contain a path with "/profile/" and no query parameters if ((parse_url($_GET['zrl'], PHP_URL_QUERY) == '') && @@ -743,7 +736,7 @@ class App $response = $module->run($input); $this->profiler->set(microtime(true) - $timestamp, 'content'); if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) { - $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig, $this->userSession->getLocalUserId()); + $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig, $this->session->getLocalUserId()); } else { $page->exit($response); } diff --git a/src/Content/Conversation.php b/src/Content/Conversation.php index aa8a05f7ab..a3bdecc3f9 100644 --- a/src/Content/Conversation.php +++ b/src/Content/Conversation.php @@ -32,7 +32,6 @@ use Friendica\Core\L10n; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Protocol; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Theme; use Friendica\Database\DBA; @@ -78,27 +77,24 @@ class Conversation private $page; /** @var App\Mode */ private $mode; - /** @var IHandleSessions */ - private $session; /** @var IHandleUserSessions */ - private $userSession; + private $session; - public function __construct(LoggerInterface $logger, Profiler $profiler, Activity $activity, L10n $l10n, Item $item, Arguments $args, BaseURL $baseURL, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, App\Page $page, App\Mode $mode, App $app, IHandleSessions $session, IHandleUserSessions $userSession) + public function __construct(LoggerInterface $logger, Profiler $profiler, Activity $activity, L10n $l10n, Item $item, Arguments $args, BaseURL $baseURL, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, App\Page $page, App\Mode $mode, App $app, IHandleUserSessions $session) { - $this->activity = $activity; - $this->item = $item; - $this->config = $config; - $this->mode = $mode; - $this->baseURL = $baseURL; - $this->profiler = $profiler; - $this->logger = $logger; - $this->l10n = $l10n; - $this->args = $args; - $this->pConfig = $pConfig; - $this->page = $page; - $this->app = $app; - $this->session = $session; - $this->userSession = $userSession; + $this->activity = $activity; + $this->item = $item; + $this->config = $config; + $this->mode = $mode; + $this->baseURL = $baseURL; + $this->profiler = $profiler; + $this->logger = $logger; + $this->l10n = $l10n; + $this->args = $args; + $this->pConfig = $pConfig; + $this->page = $page; + $this->app = $app; + $this->session = $session; } /** @@ -175,7 +171,7 @@ class Conversation continue; } - if ($this->userSession->getPublicContactId() == $activity['author-id']) { + if ($this->session->getPublicContactId() == $activity['author-id']) { $conv_responses[$mode][$activity['thr-parent-id']]['self'] = 1; } @@ -300,7 +296,7 @@ class Conversation $x['bang'] = $x['bang'] ?? ''; $x['visitor'] = $x['visitor'] ?? 'block'; $x['is_owner'] = $x['is_owner'] ?? true; - $x['profile_uid'] = $x['profile_uid'] ?? $this->userSession->getLocalUserId(); + $x['profile_uid'] = $x['profile_uid'] ?? $this->session->getLocalUserId(); $geotag = !empty($x['allow_location']) ? Renderer::replaceMacros(Renderer::getMarkupTemplate('jot_geotag.tpl'), []) : ''; @@ -363,7 +359,7 @@ class Conversation '$title' => $x['title'] ?? '', '$placeholdertitle' => $this->l10n->t('Set title'), '$category' => $x['category'] ?? '', - '$placeholdercategory' => Feature::isEnabled($this->userSession->getLocalUserId(), 'categories') ? $this->l10n->t("Categories \x28comma-separated list\x29") : '', + '$placeholdercategory' => Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->l10n->t("Categories \x28comma-separated list\x29") : '', '$scheduled_at' => Temporal::getDateTimeField( new \DateTime(), new \DateTime('now + 6 months'), @@ -401,7 +397,7 @@ class Conversation '$browser' => $this->l10n->t('Browser'), '$compose_link_title' => $this->l10n->t('Open Compose page'), - '$always_open_compose' => $this->pConfig->get($this->userSession->getLocalUserId(), 'frio', 'always_open_compose', false), + '$always_open_compose' => $this->pConfig->get($this->session->getLocalUserId(), 'frio', 'always_open_compose', false), ]); @@ -440,7 +436,7 @@ class Conversation $this->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css')); $this->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css')); - $ssl_state = (bool)$this->userSession->getLocalUserId(); + $ssl_state = (bool)$this->session->getLocalUserId(); $live_update_div = ''; @@ -492,11 +488,11 @@ class Conversation } } } elseif ($mode === 'notes') { - $items = $this->addChildren($items, false, $order, $this->userSession->getLocalUserId(), $mode); + $items = $this->addChildren($items, false, $order, $this->session->getLocalUserId(), $mode); if (!$update) { $live_update_div = '
' . "\r\n" - . "\r\n"; } } elseif ($mode === 'display') { @@ -504,7 +500,7 @@ class Conversation if (!$update) { $live_update_div = '
' . "\r\n" - . ""; } } elseif ($mode === 'community') { @@ -530,7 +526,7 @@ class Conversation $live_update_div = '' . "\r\n"; } - $page_dropping = $this->userSession->getLocalUserId() && $this->userSession->getLocalUserId() == $uid; + $page_dropping = $this->session->getLocalUserId() && $this->session->getLocalUserId() == $uid; if (!$update) { $_SESSION['return_path'] = $this->args->getQueryString(); @@ -550,7 +546,7 @@ class Conversation 'announce' => [], ]; - if ($this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'hide_dislike')) { + if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'hide_dislike')) { unset($conv_responses['dislike']); } @@ -568,7 +564,7 @@ class Conversation $writable = $items[0]['writable'] || ($items[0]['uid'] == 0) && in_array($items[0]['network'], Protocol::FEDERATED); } - if (!$this->userSession->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { $writable = false; } @@ -601,7 +597,7 @@ class Conversation $threadsid++; // prevent private email from leaking. - if ($item['network'] === Protocol::MAIL && $this->userSession->getLocalUserId() != $item['uid']) { + if ($item['network'] === Protocol::MAIL && $this->session->getLocalUserId() != $item['uid']) { continue; } @@ -645,17 +641,17 @@ class Conversation 'announce' => null, ]; - if ($this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'hide_dislike')) { + if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'hide_dislike')) { unset($likebuttons['dislike']); } $body_html = ItemModel::prepareBody($item, true, $preview); - [$categories, $folders] = $this->item->determineCategoriesTerms($item, $this->userSession->getLocalUserId()); + [$categories, $folders] = $this->item->determineCategoriesTerms($item, $this->session->getLocalUserId()); if (!empty($item['title'])) { $title = $item['title']; - } elseif (!empty($item['content-warning']) && $this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'disable_cw', false)) { + } elseif (!empty($item['content-warning']) && $this->pConfig->get($this->session->getLocalUserId(), 'system', 'disable_cw', false)) { $title = ucfirst($item['content-warning']); } else { $title = ''; @@ -749,7 +745,7 @@ class Conversation $this->builtinActivityPuller($item, $conv_responses); // Only add what is visible - if ($item['network'] === Protocol::MAIL && $this->userSession->getLocalUserId() != $item['uid']) { + if ($item['network'] === Protocol::MAIL && $this->session->getLocalUserId() != $item['uid']) { continue; } @@ -794,11 +790,11 @@ class Conversation private function getBlocklist(): array { - if (!$this->userSession->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { return []; } - $str_blocked = str_replace(["\n", "\r"], ",", $this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'blocked')); + $str_blocked = str_replace(["\n", "\r"], ",", $this->pConfig->get($this->session->getLocalUserId(), 'system', 'blocked')); if (empty($str_blocked)) { return []; } @@ -868,7 +864,7 @@ class Conversation $row['direction'] = ['direction' => 4, 'title' => $this->l10n->t('You subscribed to one or more tags in this post.')]; break; case ItemModel::PR_ANNOUNCEMENT: - if (!empty($row['causer-id']) && $this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'display_resharer')) { + if (!empty($row['causer-id']) && $this->pConfig->get($this->session->getLocalUserId(), 'system', 'display_resharer')) { $row['owner-id'] = $row['causer-id']; $row['owner-link'] = $row['causer-link']; $row['owner-avatar'] = $row['causer-avatar']; @@ -1220,7 +1216,7 @@ class Conversation $parents[$i]['children'] = $this->sortItemChildren($parents[$i]['children']); } - if (!$this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'no_smart_threading', 0)) { + if (!$this->pConfig->get($this->session->getLocalUserId(), 'system', 'no_smart_threading', 0)) { foreach ($parents as $i => $parent) { $parents[$i] = $this->smartFlattenConversation($parent); } diff --git a/src/Core/Session/Capability/IHandleUserSessions.php b/src/Core/Session/Capability/IHandleUserSessions.php index e5d1b4230d..307b694923 100644 --- a/src/Core/Session/Capability/IHandleUserSessions.php +++ b/src/Core/Session/Capability/IHandleUserSessions.php @@ -22,9 +22,9 @@ namespace Friendica\Core\Session\Capability; /** - * Handles user infos based on session infos + * This interface handles UserSessions, which is directly extended from the global Session interface */ -interface IHandleUserSessions +interface IHandleUserSessions extends IHandleSessions { /** * Returns the user id of locally logged-in user or false. @@ -88,8 +88,6 @@ interface IHandleUserSessions /** * Set the session variable that contains the contact IDs for the visitor's contact URL - * - * @param string $url Contact URL */ public function setVisitorsContacts(); } diff --git a/src/Core/Session/Factory/Session.php b/src/Core/Session/Factory/Session.php index 5b366d6763..bf5dffb1f0 100644 --- a/src/Core/Session/Factory/Session.php +++ b/src/Core/Session/Factory/Session.php @@ -55,10 +55,8 @@ class Session * @param LoggerInterface $logger * @param Profiler $profiler * @param array $server - * - * @return IHandleSessions */ - public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = []) + public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = []): IHandleSessions { $profiler->startRecording('session'); $session = null; diff --git a/src/Core/Session/Model/UserSession.php b/src/Core/Session/Model/UserSession.php index 9d7d5c091e..79f5b2aaef 100644 --- a/src/Core/Session/Model/UserSession.php +++ b/src/Core/Session/Model/UserSession.php @@ -25,6 +25,9 @@ use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Model\Contact; +/** + * This class handles user sessions, which is directly extended from regular session + */ class UserSession implements IHandleUserSessions { /** @var IHandleSessions */ @@ -130,4 +133,52 @@ class UserSession implements IHandleUserSessions { $this->session->set('submanage', $managed_uid); } + + /** {@inheritDoc} */ + public function start(): IHandleSessions + { + return $this; + } + + /** {@inheritDoc} */ + public function exists(string $name): bool + { + return $this->session->exists($name); + } + + /** {@inheritDoc} */ + public function get(string $name, $defaults = null) + { + return $this->session->get($name, $defaults); + } + + /** {@inheritDoc} */ + public function pop(string $name, $defaults = null) + { + return $this->session->pop($name, $defaults); + } + + /** {@inheritDoc} */ + public function set(string $name, $value) + { + $this->session->set($name, $value); + } + + /** {@inheritDoc} */ + public function setMultiple(array $values) + { + $this->session->setMultiple($values); + } + + /** {@inheritDoc} */ + public function remove(string $name) + { + $this->session->remove($name); + } + + /** {@inheritDoc} */ + public function clear() + { + $this->session->clear(); + } } diff --git a/src/DI.php b/src/DI.php index b107e8c348..0b5c9c0558 100644 --- a/src/DI.php +++ b/src/DI.php @@ -22,6 +22,7 @@ namespace Friendica; use Dice\Dice; +use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Navigation\SystemMessages; use Psr\Log\LoggerInterface; @@ -212,10 +213,7 @@ abstract class DI return self::$dice->create(Core\Worker\Repository\Process::class); } - /** - * @return Core\Session\Capability\IHandleSessions - */ - public static function session() + public static function session(): IHandleSessions { return self::$dice->create(Core\Session\Capability\IHandleSessions::class); } diff --git a/src/Module/Security/Login.php b/src/Module/Security/Login.php index 19e1d8dca3..e41e6a702f 100644 --- a/src/Module/Security/Login.php +++ b/src/Module/Security/Login.php @@ -27,7 +27,7 @@ use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\DI; use Friendica\Module\Register; use Friendica\Module\Response; @@ -46,10 +46,10 @@ class Login extends BaseModule /** @var IManageConfigValues */ private $config; - /** @var IHandleSessions */ + /** @var IHandleUserSessions */ private $session; - public function __construct(Authentication $auth, IManageConfigValues $config, IHandleSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(Authentication $auth, IManageConfigValues $config, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -62,7 +62,7 @@ class Login extends BaseModule { $return_path = $request['return_path'] ?? $this->session->pop('return_path', '') ; - if (DI::userSession()->getLocalUserId()) { + if ($this->session->getLocalUserId()) { $this->baseUrl->redirect($return_path); } diff --git a/src/Module/Security/Logout.php b/src/Module/Security/Logout.php index d68a414e16..e50599407b 100644 --- a/src/Module/Security/Logout.php +++ b/src/Module/Security/Logout.php @@ -26,7 +26,7 @@ use Friendica\BaseModule; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Hook; use Friendica\Core\L10n; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\DI; use Friendica\Model\Profile; @@ -44,10 +44,11 @@ class Logout extends BaseModule protected $cache; /** @var Cookie */ protected $cookie; - /** @var IHandleSessions */ + /** @var IHandleUserSessions + */ protected $session; - public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, ICanCache $cache, Cookie $cookie, IHandleSessions $session, array $server, array $parameters = []) + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, ICanCache $cache, Cookie $cookie, IHandleUserSessions $session, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -63,7 +64,7 @@ class Logout extends BaseModule protected function rawContent(array $request = []) { $visitor_home = null; - if (DI::userSession()->getRemoteUserId()) { + if ($this->session->getRemoteUserId()) { $visitor_home = Profile::getMyURL(); $this->cache->delete('zrlInit:' . $visitor_home); } diff --git a/src/Module/Security/TwoFactor/Recovery.php b/src/Module/Security/TwoFactor/Recovery.php index 991a9b7787..d23b6b9306 100644 --- a/src/Module/Security/TwoFactor/Recovery.php +++ b/src/Module/Security/TwoFactor/Recovery.php @@ -25,7 +25,7 @@ use Friendica\App; use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\DI; use Friendica\Model\User; use Friendica\Module\Response; @@ -41,14 +41,14 @@ use Psr\Log\LoggerInterface; */ class Recovery extends BaseModule { - /** @var IHandleSessions */ + /** @var IHandleUserSessions */ protected $session; /** @var App */ protected $app; /** @var Authentication */ protected $auth; - public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Authentication $auth, IHandleSessions $session, array $server, array $parameters = []) + public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Authentication $auth, IHandleUserSessions $session, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -59,7 +59,7 @@ class Recovery extends BaseModule protected function post(array $request = []) { - if (!DI::userSession()->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { return; } @@ -68,10 +68,10 @@ class Recovery extends BaseModule $recovery_code = $_POST['recovery_code'] ?? ''; - if (RecoveryCode::existsForUser(DI::userSession()->getLocalUserId(), $recovery_code)) { - RecoveryCode::markUsedForUser(DI::userSession()->getLocalUserId(), $recovery_code); + if (RecoveryCode::existsForUser($this->session->getLocalUserId(), $recovery_code)) { + RecoveryCode::markUsedForUser($this->session->getLocalUserId(), $recovery_code); $this->session->set('2fa', true); - DI::sysmsg()->addInfo($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(DI::userSession()->getLocalUserId()))); + DI::sysmsg()->addInfo($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser($this->session->getLocalUserId()))); $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true); @@ -84,7 +84,7 @@ class Recovery extends BaseModule protected function content(array $request = []): string { - if (!DI::userSession()->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { $this->baseUrl->redirect(); } diff --git a/src/Module/Security/TwoFactor/SignOut.php b/src/Module/Security/TwoFactor/SignOut.php index 1c5263b59b..523d6d5650 100644 --- a/src/Module/Security/TwoFactor/SignOut.php +++ b/src/Module/Security/TwoFactor/SignOut.php @@ -25,11 +25,10 @@ use Friendica\App; use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\DI; use Friendica\Model\User\Cookie; use Friendica\Module\Response; -use Friendica\Network\HTTPException\NotFoundException; use Friendica\Util\Profiler; use Friendica\Security\TwoFactor; use Psr\Log\LoggerInterface; @@ -43,14 +42,14 @@ class SignOut extends BaseModule { protected $errors = []; - /** @var IHandleSessions */ + /** @var IHandleUserSessions */ protected $session; /** @var Cookie */ protected $cookie; /** @var TwoFactor\Repository\TrustedBrowser */ protected $trustedBrowserRepository; - public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, IHandleSessions $session, Cookie $cookie, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepository, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, IHandleUserSessions $session, Cookie $cookie, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepository, Profiler $profiler, Response $response, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -61,7 +60,7 @@ class SignOut extends BaseModule protected function post(array $request = []) { - if (!DI::userSession()->getLocalUserId() || !($this->cookie->get('2fa_cookie_hash'))) { + if (!$this->session->getLocalUserId() || !($this->cookie->get('2fa_cookie_hash'))) { return; } @@ -80,7 +79,7 @@ class SignOut extends BaseModule $this->baseUrl->redirect(); break; case 'sign_out': - $this->trustedBrowserRepository->removeForUser(DI::userSession()->getLocalUserId(), $this->cookie->get('2fa_cookie_hash')); + $this->trustedBrowserRepository->removeForUser($this->session->getLocalUserId(), $this->cookie->get('2fa_cookie_hash')); $this->cookie->clear(); $this->session->clear(); @@ -95,7 +94,7 @@ class SignOut extends BaseModule protected function content(array $request = []): string { - if (!DI::userSession()->getLocalUserId() || !($this->cookie->get('2fa_cookie_hash'))) { + if (!$this->session->getLocalUserId() || !($this->cookie->get('2fa_cookie_hash'))) { $this->baseUrl->redirect(); } diff --git a/src/Module/Security/TwoFactor/Trust.php b/src/Module/Security/TwoFactor/Trust.php index 83a19ce023..06ea01ca83 100644 --- a/src/Module/Security/TwoFactor/Trust.php +++ b/src/Module/Security/TwoFactor/Trust.php @@ -25,7 +25,7 @@ use Friendica\App; use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\DI; use Friendica\Model\User; use Friendica\Model\User\Cookie; @@ -51,7 +51,7 @@ class Trust extends BaseModule protected $app; /** @var Authentication */ protected $auth; - /** @var IHandleSessions */ + /** @var IHandleUserSessions */ protected $session; /** @var Cookie */ protected $cookie; @@ -60,7 +60,7 @@ class Trust extends BaseModule /** @var TwoFactor\Repository\TrustedBrowser */ protected $trustedBrowserRepository; - public function __construct(App $app, Authentication $auth, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IHandleSessions $session, Cookie $cookie, TwoFactor\Factory\TrustedBrowser $trustedBrowserFactory, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepositoy, Response $response, array $server, array $parameters = []) + public function __construct(App $app, Authentication $auth, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IHandleUserSessions $session, Cookie $cookie, TwoFactor\Factory\TrustedBrowser $trustedBrowserFactory, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepositoy, Response $response, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -74,7 +74,7 @@ class Trust extends BaseModule protected function post(array $request = []) { - if (!DI::userSession()->getLocalUserId() || !$this->session->get('2fa')) { + if (!$this->session->getLocalUserId() || !$this->session->get('2fa')) { $this->logger->info('Invalid call', ['request' => $request]); return; } @@ -87,7 +87,7 @@ class Trust extends BaseModule switch ($action) { case 'trust': case 'dont_trust': - $trustedBrowser = $this->trustedBrowserFactory->createForUserWithUserAgent(DI::userSession()->getLocalUserId(), $this->server['HTTP_USER_AGENT'], $action === 'trust'); + $trustedBrowser = $this->trustedBrowserFactory->createForUserWithUserAgent($this->session->getLocalUserId(), $this->server['HTTP_USER_AGENT'], $action === 'trust'); try { $this->trustedBrowserRepository->save($trustedBrowser); @@ -115,7 +115,7 @@ class Trust extends BaseModule protected function content(array $request = []): string { - if (!DI::userSession()->getLocalUserId() || !$this->session->get('2fa')) { + if (!$this->session->getLocalUserId() || !$this->session->get('2fa')) { $this->baseUrl->redirect(); } diff --git a/src/Module/Security/TwoFactor/Verify.php b/src/Module/Security/TwoFactor/Verify.php index efd7e2c73a..9225eba4f9 100644 --- a/src/Module/Security/TwoFactor/Verify.php +++ b/src/Module/Security/TwoFactor/Verify.php @@ -26,7 +26,6 @@ use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Renderer; -use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Module\Response; use Friendica\Util\Profiler; @@ -43,25 +42,22 @@ class Verify extends BaseModule { protected $errors = []; - /** @var IHandleSessions */ - protected $session; /** @var IManagePersonalConfigValues */ protected $pConfig; /** @var IHandleUserSessions */ - protected $userSession; + protected $session; - public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, IHandleSessions $session, IHandleUserSessions $userSession, $server, array $parameters = []) + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, IHandleUserSessions $session, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->session = $session; $this->pConfig = $pConfig; - $this->userSession = $userSession; } protected function post(array $request = []) { - if (!$this->userSession->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { return; } @@ -70,7 +66,7 @@ class Verify extends BaseModule $code = $request['verify_code'] ?? ''; - $valid = (new Google2FA())->verifyKey($this->pConfig->get($this->userSession->getLocalUserId(), '2fa', 'secret'), $code); + $valid = (new Google2FA())->verifyKey($this->pConfig->get($this->session->getLocalUserId(), '2fa', 'secret'), $code); // The same code can't be used twice even if it's valid if ($valid && $this->session->get('2fa') !== $code) { @@ -85,7 +81,7 @@ class Verify extends BaseModule protected function content(array $request = []): string { - if (!$this->userSession->getLocalUserId()) { + if (!$this->session->getLocalUserId()) { $this->baseUrl->redirect(); } diff --git a/src/Navigation/Notifications/Factory/Introduction.php b/src/Navigation/Notifications/Factory/Introduction.php index af4bddbd02..a182c69394 100644 --- a/src/Navigation/Notifications/Factory/Introduction.php +++ b/src/Navigation/Notifications/Factory/Introduction.php @@ -29,7 +29,6 @@ use Friendica\Content\Text\BBCode; use Friendica\Core\L10n; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Protocol; -use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; use Friendica\Model\Contact; @@ -54,24 +53,21 @@ class Introduction extends BaseFactory private $l10n; /** @var IManagePersonalConfigValues */ private $pConfig; - /** @var IHandleSessions */ - private $session; /** @var IHandleUserSessions */ - private $userSession; + private $session; /** @var string */ private $nick; - public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IManagePersonalConfigValues $pConfig, IHandleSessions $session, IHandleUserSessions $userSession) + public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IManagePersonalConfigValues $pConfig, IHandleUserSessions $session) { parent::__construct($logger); - $this->dba = $dba; - $this->baseUrl = $baseUrl; - $this->l10n = $l10n; - $this->pConfig = $pConfig; - $this->session = $session; - $this->userSession = $userSession; - $this->nick = $app->getLoggedInUserNickname() ?? ''; + $this->dba = $dba; + $this->baseUrl = $baseUrl; + $this->l10n = $l10n; + $this->pConfig = $pConfig; + $this->session = $session; + $this->nick = $app->getLoggedInUserNickname() ?? ''; } /** @@ -111,7 +107,7 @@ class Introduction extends BaseFactory LEFT JOIN `contact` AS `sugggest-contact` ON `intro`.`suggest-cid` = `sugggest-contact`.`id` WHERE `intro`.`uid` = ? $sql_extra LIMIT ?, ?", - $_SESSION['uid'], + $this->session->getLocalUserId(), $start, $limit ); @@ -146,7 +142,7 @@ class Introduction extends BaseFactory 'url' => $intro['furl'], 'zrl' => Contact::magicLink($intro['furl']), 'hidden' => $intro['hidden'] == 1, - 'post_newfriend' => (intval($this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'post_newfriend')) ? '1' : 0), + 'post_newfriend' => (intval($this->pConfig->get($this->session->getLocalUserId(), 'system', 'post_newfriend')) ? '1' : 0), 'note' => $intro['note'], 'request' => $intro['frequest'] . '?addr=' . $return_addr]); @@ -162,7 +158,7 @@ class Introduction extends BaseFactory 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'), 'str_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')), 'dfrn_id' => $intro['issued-id'], - 'uid' => $this->session->get('uid'), + 'uid' => $this->session->getLocalUserId(), 'intro_id' => $intro['intro_id'], 'contact_id' => $intro['contact-id'], 'photo' => Contact::getPhoto($intro), @@ -171,7 +167,7 @@ class Introduction extends BaseFactory 'about' => BBCode::convert($intro['about'], false), 'keywords' => $intro['keywords'], 'hidden' => $intro['hidden'] == 1, - 'post_newfriend' => (intval($this->pConfig->get($this->userSession->getLocalUserId(), 'system', 'post_newfriend')) ? '1' : 0), + 'post_newfriend' => (intval($this->pConfig->get($this->session->getLocalUserId(), 'system', 'post_newfriend')) ? '1' : 0), 'url' => $intro['url'], 'zrl' => Contact::magicLink($intro['url']), 'addr' => $intro['addr'], @@ -182,7 +178,7 @@ class Introduction extends BaseFactory } } } catch (Exception $e) { - $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]); + $this->logger->warning('Select failed.', ['uid' => $this->session->getLocalUserId(), 'exception' => $e]); } return $formattedIntroductions; diff --git a/src/Security/Authentication.php b/src/Security/Authentication.php index 7b9f0ff3ec..c38a2eb6dd 100644 --- a/src/Security/Authentication.php +++ b/src/Security/Authentication.php @@ -26,7 +26,7 @@ use Friendica\App; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Hook; -use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Database\DBA; @@ -59,7 +59,7 @@ class Authentication private $logger; /** @var User\Cookie */ private $cookie; - /** @var IHandleSessions */ + /** @var IHandleUserSessions */ private $session; /** @var IManagePersonalConfigValues */ private $pConfig; @@ -88,11 +88,11 @@ class Authentication * @param Database $dba * @param LoggerInterface $logger * @param User\Cookie $cookie - * @param IHandleSessions $session + * @param IHandleUserSessions $session * @param IManagePersonalConfigValues $pConfig * @param App\Request $request */ - public function __construct(IManageConfigValues $config, App\Mode $mode, App\BaseURL $baseUrl, L10n $l10n, Database $dba, LoggerInterface $logger, User\Cookie $cookie, IHandleSessions $session, IManagePersonalConfigValues $pConfig, App\Request $request) + public function __construct(IManageConfigValues $config, App\Mode $mode, App\BaseURL $baseUrl, L10n $l10n, Database $dba, LoggerInterface $logger, User\Cookie $cookie, IHandleUserSessions $session, IManagePersonalConfigValues $pConfig, App\Request $request) { $this->config = $config; $this->mode = $mode; @@ -330,9 +330,10 @@ class Authentication 'my_url' => $this->baseUrl->get() . '/profile/' . $user_record['nickname'], 'my_address' => $user_record['nickname'] . '@' . substr($this->baseUrl->get(), strpos($this->baseUrl->get(), '://') + 3), 'addr' => $this->remoteAddress, + 'nickname' => $user_record['nickname'], ]); - DI::userSession()->setVisitorsContacts(); + $this->session->setVisitorsContacts(); $member_since = strtotime($user_record['register_date']); $this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14)));