]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Recovery.php
Merge pull request #8271 from MrPetovan/bug/8229-frio-mobile-back-to-top
[friendica.git] / src / Module / Settings / TwoFactor / Recovery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\TwoFactor;
23
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
26 use Friendica\Model\TwoFactor\RecoveryCode;
27 use Friendica\Module\BaseSettings;
28 use Friendica\Module\Security\Login;
29
30 /**
31  * // Page 3: 2FA enabled but not verified, show recovery codes
32  *
33  * @package Friendica\Module\TwoFactor
34  */
35 class Recovery extends BaseSettings
36 {
37         public static function init(array $parameters = [])
38         {
39                 if (!local_user()) {
40                         return;
41                 }
42
43                 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
44
45                 if (!$secret) {
46                         DI::baseUrl()->redirect('settings/2fa');
47                 }
48
49                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
50                         notice(DI::l10n()->t('Please enter your password to access this page.'));
51                         DI::baseUrl()->redirect('settings/2fa');
52                 }
53         }
54
55         public static function post(array $parameters = [])
56         {
57                 if (!local_user()) {
58                         return;
59                 }
60
61                 if (!empty($_POST['action'])) {
62                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
63
64                         if ($_POST['action'] == 'regenerate') {
65                                 RecoveryCode::regenerateForUser(local_user());
66                                 notice(DI::l10n()->t('New recovery codes successfully generated.'));
67                                 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
68                         }
69                 }
70         }
71
72         public static function content(array $parameters = [])
73         {
74                 if (!local_user()) {
75                         return Login::form('settings/2fa/recovery');
76                 }
77
78                 parent::content($parameters);
79
80                 if (!RecoveryCode::countValidForUser(local_user())) {
81                         RecoveryCode::generateForUser(local_user());
82                 }
83
84                 $recoveryCodes = RecoveryCode::getListForUser(local_user());
85
86                 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
87                 
88                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
89                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_recovery'),
90                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
91
92                         '$title'              => DI::l10n()->t('Two-factor recovery codes'),
93                         '$help_label'         => DI::l10n()->t('Help'),
94                         '$message'            => DI::l10n()->t('<p>Recovery codes can be used to access your account in the event you lose access to your device and cannot receive two-factor authentication codes.</p><p><strong>Put these in a safe spot!</strong> If you lose your device and don’t have the recovery codes you will lose access to your account.</p>'),
95                         '$recovery_codes'     => $recoveryCodes,
96                         '$regenerate_message' => DI::l10n()->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
97                         '$regenerate_label'   => DI::l10n()->t('Generate new recovery codes'),
98                         '$verified'           => $verified,
99                         '$verify_label'       => DI::l10n()->t('Next: Verification'),
100                 ]);
101         }
102 }