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