]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/TwoFactor/Trusted.php
Merge pull request #11141 from urbalazs/language-names
[friendica.git] / src / Module / Settings / TwoFactor / Trusted.php
index 753250941732f095885887ed58fb524767015311..12327a5918961af3875de186faa5d6b48acb23f7 100644 (file)
@@ -1,12 +1,37 @@
 <?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\Module\Settings\TwoFactor;
 
+use Friendica\App;
+use Friendica\Core\L10n;
+use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\Renderer;
-use Friendica\DI;
 use Friendica\Module\BaseSettings;
+use Friendica\Module\Response;
 use Friendica\Security\TwoFactor;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Profiler;
 use Friendica\Util\Temporal;
+use Psr\Log\LoggerInterface;
 use UAParser\Parser;
 
 /**
@@ -14,40 +39,48 @@ use UAParser\Parser;
  */
 class Trusted extends BaseSettings
 {
-       public static function init(array $parameters = [])
+       /** @var IManagePersonalConfigValues */
+       protected $pConfig;
+       /** @var TwoFactor\Repository\TrustedBrowser */
+       protected $trustedBrowserRepo;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, array $server, array $parameters = [])
        {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->pConfig            = $pConfig;
+               $this->trustedBrowserRepo = $trustedBrowserRepo;
+
                if (!local_user()) {
                        return;
                }
 
-               $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
+               $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
 
                if (!$verified) {
-                       DI::baseUrl()->redirect('settings/2fa');
+                       $this->baseUrl->redirect('settings/2fa');
                }
 
                if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
-                       notice(DI::l10n()->t('Please enter your password to access this page.'));
-                       DI::baseUrl()->redirect('settings/2fa');
+                       notice($this->t('Please enter your password to access this page.'));
+                       $this->baseUrl->redirect('settings/2fa');
                }
        }
 
-       public static function post(array $parameters = [])
+       protected function post(array $request = [])
        {
                if (!local_user()) {
                        return;
                }
 
-               $trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger());
-
                if (!empty($_POST['action'])) {
                        self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
 
                        switch ($_POST['action']) {
                                case 'remove_all' :
-                                       $trustedBrowserRepository->removeAllForUser(local_user());
-                                       info(DI::l10n()->t('Trusted browsers successfully removed.'));
-                                       DI::baseUrl()->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                                       $this->trustedBrowserRepo->removeAllForUser(local_user());
+                                       info($this->t('Trusted browsers successfully removed.'));
+                                       $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
                                        break;
                        }
                }
@@ -55,28 +88,31 @@ class Trusted extends BaseSettings
                if (!empty($_POST['remove_id'])) {
                        self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
 
-                       if ($trustedBrowserRepository->removeForUser(local_user(), $_POST['remove_id'])) {
-                               info(DI::l10n()->t('Trusted browser successfully removed.'));
+                       if ($this->trustedBrowserRepo->removeForUser(local_user(), $_POST['remove_id'])) {
+                               info($this->t('Trusted browser successfully removed.'));
                        }
 
-                       DI::baseUrl()->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                       $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
                }
        }
 
 
-       public static function content(array $parameters = []): string
+       protected function content(array $request = []): string
        {
-               parent::content($parameters);
+               parent::content();
 
-               $trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger());
-               $trustedBrowsers = $trustedBrowserRepository->selectAllByUid(local_user());
+               $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(local_user());
 
                $parser = Parser::create();
 
                $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
                        $dates = [
-                               'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
-                               'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
+                               'created_ago'     => Temporal::getRelativeDate($trustedBrowser->created),
+                               'created_utc'     => DateTimeFormat::utc($trustedBrowser->created, 'c'),
+                               'created_local'   => DateTimeFormat::local($trustedBrowser->created, 'r'),
+                               'last_used_ago'   => Temporal::getRelativeDate($trustedBrowser->last_used),
+                               'last_used_utc'   => $trustedBrowser->last_used ? DateTimeFormat::utc($trustedBrowser->last_used, 'c') : '',
+                               'last_used_local' => $trustedBrowser->last_used ? DateTimeFormat::local($trustedBrowser->last_used, 'r') : '',
                        ];
 
                        $result = $parser->parse($trustedBrowser->user_agent);
@@ -94,15 +130,15 @@ class Trusted extends BaseSettings
                        '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
                        '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
 
-                       '$title'               => DI::l10n()->t('Two-factor Trusted Browsers'),
-                       '$message'             => DI::l10n()->t('Trusted browsers are individual browsers you chose to skip two-factor authentication to access Friendica. Please use this feature sparingly, as it can negate the benefit of two-factor authentication.'),
-                       '$device_label'        => DI::l10n()->t('Device'),
-                       '$os_label'            => DI::l10n()->t('OS'),
-                       '$browser_label'       => DI::l10n()->t('Browser'),
-                       '$created_label'       => DI::l10n()->t('Trusted'),
-                       '$last_used_label'     => DI::l10n()->t('Last Use'),
-                       '$remove_label'        => DI::l10n()->t('Remove'),
-                       '$remove_all_label'    => DI::l10n()->t('Remove All'),
+                       '$title'               => $this->t('Two-factor Trusted Browsers'),
+                       '$message'             => $this->t('Trusted browsers are individual browsers you chose to skip two-factor authentication to access Friendica. Please use this feature sparingly, as it can negate the benefit of two-factor authentication.'),
+                       '$device_label'        => $this->t('Device'),
+                       '$os_label'            => $this->t('OS'),
+                       '$browser_label'       => $this->t('Browser'),
+                       '$created_label'       => $this->t('Trusted'),
+                       '$last_used_label'     => $this->t('Last Use'),
+                       '$remove_label'        => $this->t('Remove'),
+                       '$remove_all_label'    => $this->t('Remove All'),
 
                        '$trusted_browsers'    => $trustedBrowserDisplay,
                ]);