]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Recovery.php
Merge pull request #8163 from MrPetovan/task/7817-custom-fields-part-3
[friendica.git] / src / Module / Settings / TwoFactor / Recovery.php
1 <?php
2
3 namespace Friendica\Module\Settings\TwoFactor;
4
5 use Friendica\Core\Renderer;
6 use Friendica\DI;
7 use Friendica\Model\TwoFactor\RecoveryCode;
8 use Friendica\Module\BaseSettings;
9 use Friendica\Module\Security\Login;
10
11 /**
12  * // Page 3: 2FA enabled but not verified, show recovery codes
13  *
14  * @package Friendica\Module\TwoFactor
15  */
16 class Recovery extends BaseSettings
17 {
18         public static function init(array $parameters = [])
19         {
20                 if (!local_user()) {
21                         return;
22                 }
23
24                 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
25
26                 if (!$secret) {
27                         DI::baseUrl()->redirect('settings/2fa');
28                 }
29
30                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
31                         notice(DI::l10n()->t('Please enter your password to access this page.'));
32                         DI::baseUrl()->redirect('settings/2fa');
33                 }
34         }
35
36         public static function post(array $parameters = [])
37         {
38                 if (!local_user()) {
39                         return;
40                 }
41
42                 if (!empty($_POST['action'])) {
43                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
44
45                         if ($_POST['action'] == 'regenerate') {
46                                 RecoveryCode::regenerateForUser(local_user());
47                                 notice(DI::l10n()->t('New recovery codes successfully generated.'));
48                                 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
49                         }
50                 }
51         }
52
53         public static function content(array $parameters = [])
54         {
55                 if (!local_user()) {
56                         return Login::form('settings/2fa/recovery');
57                 }
58
59                 parent::content($parameters);
60
61                 if (!RecoveryCode::countValidForUser(local_user())) {
62                         RecoveryCode::generateForUser(local_user());
63                 }
64
65                 $recoveryCodes = RecoveryCode::getListForUser(local_user());
66
67                 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
68                 
69                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
70                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_recovery'),
71                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
72
73                         '$title'              => DI::l10n()->t('Two-factor recovery codes'),
74                         '$help_label'         => DI::l10n()->t('Help'),
75                         '$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>'),
76                         '$recovery_codes'     => $recoveryCodes,
77                         '$regenerate_message' => DI::l10n()->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
78                         '$regenerate_label'   => DI::l10n()->t('Generate new recovery codes'),
79                         '$verified'           => $verified,
80                         '$verify_label'       => DI::l10n()->t('Next: Verification'),
81                 ]);
82         }
83 }