]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Trusted.php
Make $_REQUEST processing independent of sub-calls
[friendica.git] / src / Module / Settings / TwoFactor / Trusted.php
1 <?php
2
3 namespace Friendica\Module\Settings\TwoFactor;
4
5 use Friendica\App;
6 use Friendica\Core\L10n;
7 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
8 use Friendica\Core\Renderer;
9 use Friendica\Module\BaseSettings;
10 use Friendica\Module\Response;
11 use Friendica\Security\TwoFactor;
12 use Friendica\Util\DateTimeFormat;
13 use Friendica\Util\Profiler;
14 use Friendica\Util\Temporal;
15 use Psr\Log\LoggerInterface;
16 use UAParser\Parser;
17
18 /**
19  * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
20  */
21 class Trusted extends BaseSettings
22 {
23         /** @var IManagePersonalConfigValues */
24         protected $pConfig;
25         /** @var TwoFactor\Repository\TrustedBrowser */
26         protected $trustedBrowserRepo;
27
28         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 = [])
29         {
30                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
31
32                 $this->pConfig            = $pConfig;
33                 $this->trustedBrowserRepo = $trustedBrowserRepo;
34
35                 if (!local_user()) {
36                         return;
37                 }
38
39                 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
40
41                 if (!$verified) {
42                         $this->baseUrl->redirect('settings/2fa');
43                 }
44
45                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
46                         notice($this->t('Please enter your password to access this page.'));
47                         $this->baseUrl->redirect('settings/2fa');
48                 }
49         }
50
51         protected function post(array $request = [])
52         {
53                 if (!local_user()) {
54                         return;
55                 }
56
57                 if (!empty($_POST['action'])) {
58                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
59
60                         switch ($_POST['action']) {
61                                 case 'remove_all' :
62                                         $this->trustedBrowserRepo->removeAllForUser(local_user());
63                                         info($this->t('Trusted browsers successfully removed.'));
64                                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
65                                         break;
66                         }
67                 }
68
69                 if (!empty($_POST['remove_id'])) {
70                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
71
72                         if ($this->trustedBrowserRepo->removeForUser(local_user(), $_POST['remove_id'])) {
73                                 info($this->t('Trusted browser successfully removed.'));
74                         }
75
76                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
77                 }
78         }
79
80
81         protected function content(array $request = []): string
82         {
83                 parent::content();
84
85                 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(local_user());
86
87                 $parser = Parser::create();
88
89                 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
90                         $dates = [
91                                 'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
92                                 'created_utc' => DateTimeFormat::utc($trustedBrowser->created, 'c'),
93                                 'created_local' => DateTimeFormat::local($trustedBrowser->created, 'r'),
94                                 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
95                                 'last_used_utc' => DateTimeFormat::utc($trustedBrowser->last_used, 'c'),
96                                 'last_used_local' => DateTimeFormat::local($trustedBrowser->last_used, 'r'),
97                         ];
98
99                         $result = $parser->parse($trustedBrowser->user_agent);
100
101                         $uaData = [
102                                 'os' => $result->os->family,
103                                 'device' => $result->device->family,
104                                 'browser' => $result->ua->family,
105                         ];
106
107                         return $trustedBrowser->toArray() + $dates + $uaData;
108                 }, $trustedBrowsers->getArrayCopy());
109
110                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
111                         '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
112                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
113
114                         '$title'               => $this->t('Two-factor Trusted Browsers'),
115                         '$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.'),
116                         '$device_label'        => $this->t('Device'),
117                         '$os_label'            => $this->t('OS'),
118                         '$browser_label'       => $this->t('Browser'),
119                         '$created_label'       => $this->t('Trusted'),
120                         '$last_used_label'     => $this->t('Last Use'),
121                         '$remove_label'        => $this->t('Remove'),
122                         '$remove_all_label'    => $this->t('Remove All'),
123
124                         '$trusted_browsers'    => $trustedBrowserDisplay,
125                 ]);
126         }
127 }