]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/AppSpecific.php
Move L10n::t() calls to DI::l10n()->t() calls
[friendica.git] / src / Module / Settings / TwoFactor / AppSpecific.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\AppSpecificPassword;
9 use Friendica\Module\BaseSettingsModule;
10 use Friendica\Module\Security\Login;
11
12 /**
13  * // Page 5: 2FA enabled, app-specific password generation
14  *
15  * @package Friendica\Module\TwoFactor
16  */
17 class AppSpecific extends BaseSettingsModule
18 {
19         private static $appSpecificPassword = null;
20
21         public static function init(array $parameters = [])
22         {
23                 if (!local_user()) {
24                         return;
25                 }
26
27                 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
28
29                 if (!$verified) {
30                         DI::baseUrl()->redirect('settings/2fa');
31                 }
32
33                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
34                         notice(DI::l10n()->t('Please enter your password to access this page.'));
35                         DI::baseUrl()->redirect('settings/2fa');
36                 }
37         }
38
39         public static function post(array $parameters = [])
40         {
41                 if (!local_user()) {
42                         return;
43                 }
44
45                 if (!empty($_POST['action'])) {
46                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
47
48                         switch ($_POST['action']) {
49                                 case 'generate':
50                                         $description = $_POST['description'] ?? '';
51                                         if (empty($description)) {
52                                                 notice(DI::l10n()->t('App-specific password generation failed: The description is empty.'));
53                                                 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
54                                         } elseif (AppSpecificPassword::checkDuplicateForUser(local_user(), $description)) {
55                                                 notice(DI::l10n()->t('App-specific password generation failed: This description already exists.'));
56                                                 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
57                                         } else {
58                                                 self::$appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
59                                                 notice(DI::l10n()->t('New app-specific password generated.'));
60                                         }
61
62                                         break;
63                                 case 'revoke_all' :
64                                         AppSpecificPassword::deleteAllForUser(local_user());
65                                         notice(DI::l10n()->t('App-specific passwords successfully revoked.'));
66                                         DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
67                                         break;
68                         }
69                 }
70
71                 if (!empty($_POST['revoke_id'])) {
72                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
73
74                         if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
75                                 notice(DI::l10n()->t('App-specific password successfully revoked.'));
76                         }
77
78                         DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
79                 }
80         }
81
82         public static function content(array $parameters = [])
83         {
84                 if (!local_user()) {
85                         return Login::form('settings/2fa/app_specific');
86                 }
87
88                 parent::content($parameters);
89
90                 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
91
92                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
93                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_app_specific'),
94                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
95
96                         '$title'                  => DI::l10n()->t('Two-factor app-specific passwords'),
97                         '$help_label'             => DI::l10n()->t('Help'),
98                         '$message'                => DI::l10n()->t('<p>App-specific passwords are randomly generated passwords used instead your regular password to authenticate your account on third-party applications that don\'t support two-factor authentication.</p>'),
99                         '$generated_message'      => DI::l10n()->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
100                         '$generated_app_specific_password' => self::$appSpecificPassword,
101
102                         '$description_label'      => DI::l10n()->t('Description'),
103                         '$last_used_label'        => DI::l10n()->t('Last Used'),
104                         '$revoke_label'           => DI::l10n()->t('Revoke'),
105                         '$revoke_all_label'       => DI::l10n()->t('Revoke All'),
106
107                         '$app_specific_passwords' => $appSpecificPasswords,
108                         '$generate_message'       => DI::l10n()->t('When you generate a new app-specific password, you must use it right away, it will be shown to you once after you generate it.'),
109                         '$generate_title'         => DI::l10n()->t('Generate new app-specific password'),
110                         '$description_placeholder_label' => DI::l10n()->t('Friendiqa on my Fairphone 2...'),
111                         '$generate_label' => DI::l10n()->t('Generate'),
112                 ]);
113         }
114 }