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