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