]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/TwoFactor/Verify.php
Merge branch 'friendica:develop' into doc_faq_client-clean-up
[friendica.git] / src / Module / Settings / TwoFactor / Verify.php
index 57995cd75cc3091328c8233d6449f674db658f88..93fdde9207d5a709d85cd9d758827f0f038cb97b 100644 (file)
@@ -1,20 +1,37 @@
 <?php
-
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Settings\TwoFactor;
 
-
 use BaconQrCode\Renderer\Image\SvgImageBackEnd;
 use BaconQrCode\Renderer\ImageRenderer;
 use BaconQrCode\Renderer\RendererStyle\RendererStyle;
 use BaconQrCode\Writer;
-use Friendica\BaseModule;
+use Friendica\App\BaseURL;
 use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
+use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session;
-use Friendica\Module\BaseSettingsModule;
-use Friendica\Module\Login;
+use Friendica\Module\BaseSettings;
+use Friendica\Module\Security\Login;
 use PragmaRX\Google2FA\Google2FA;
 
 /**
@@ -22,54 +39,64 @@ use PragmaRX\Google2FA\Google2FA;
  *
  * @package Friendica\Module\TwoFactor\Settings
  */
-class Verify extends BaseSettingsModule
+class Verify extends BaseSettings
 {
-       public static function init()
+       /** @var IManagePersonalConfigValues */
+       protected $pConfig;
+       /** @var BaseURL */
+       protected $baseUrl;
+
+       public function __construct(IManagePersonalConfigValues $pConfig, BaseURL $baseUrl, L10n $l10n, array $parameters = [])
        {
+               parent::__construct($l10n, $parameters);
+
+               $this->pConfig = $pConfig;
+               $this->baseUrl = $baseUrl;
+
                if (!local_user()) {
                        return;
                }
 
-               $secret = PConfig::get(local_user(), '2fa', 'secret');
-               $verified = PConfig::get(local_user(), '2fa', 'verified');
+               $secret   = $this->pConfig->get(local_user(), '2fa', 'secret');
+               $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
 
                if ($secret && $verified) {
-                       self::getApp()->internalRedirect('settings/2fa');
+                       $this->baseUrl->redirect('settings/2fa');
                }
 
                if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
-                       notice(L10n::t('Please enter your password to access this page.'));
-                       self::getApp()->internalRedirect('settings/2fa');
+                       notice($this->t('Please enter your password to access this page.'));
+                       $this->baseUrl->redirect('settings/2fa');
                }
        }
 
-       public static function post()
+       public function post()
        {
                if (!local_user()) {
                        return;
                }
 
-               if (defaults($_POST, 'action', null) == 'verify') {
+               if (($_POST['action'] ?? '') == 'verify') {
                        self::checkFormSecurityTokenRedirectOnError('settings/2fa/verify', 'settings_2fa_verify');
 
                        $google2fa = new Google2FA();
 
-                       $valid = $google2fa->verifyKey(PConfig::get(local_user(), '2fa', 'secret'), defaults($_POST, 'verify_code', ''));
+                       $valid = $google2fa->verifyKey($this->pConfig->get(local_user(), '2fa', 'secret'), $_POST['verify_code'] ?? '');
 
                        if ($valid) {
-                               PConfig::set(local_user(), '2fa', 'verified', true);
+                               $this->pConfig->set(local_user(), '2fa', 'verified', true);
                                Session::set('2fa', true);
 
-                               notice(L10n::t('Two-factor authentication successfully activated.'));
+                               info($this->t('Two-factor authentication successfully activated.'));
 
-                               self::getApp()->internalRedirect('settings/2fa');
+                               $this->baseUrl->redirect('settings/2fa');
                        } else {
-                               notice(L10n::t('Invalid code, please retry.'));
+                               notice($this->t('Invalid code, please retry.'));
                        }
                }
        }
 
-       public static function content()
+       public function content(): string
        {
                if (!local_user()) {
                        return Login::form('settings/2fa/verify');
@@ -79,7 +106,7 @@ class Verify extends BaseSettingsModule
 
                $company = 'Friendica';
                $holder = Session::get('my_address');
-               $secret = PConfig::get(local_user(), '2fa', 'secret');
+               $secret = $this->pConfig->get(local_user(), '2fa', 'secret');
 
                $otpauthUrl = (new Google2FA())->getQRCodeUrl($company, $holder, $secret);
 
@@ -93,7 +120,7 @@ class Verify extends BaseSettingsModule
 
                $shortOtpauthUrl = explode('?', $otpauthUrl)[0];
 
-               $manual_message = L10n::t('<p>Or you can submit the authentication settings manually:</p>
+               $manual_message = $this->t('<p>Or you can submit the authentication settings manually:</p>
 <dl>
        <dt>Issuer</dt>
        <dd>%s</dd>
@@ -110,20 +137,21 @@ class Verify extends BaseSettingsModule
 </dl>', $company, $holder, $secret);
 
                return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/verify.tpl'), [
-                       '$form_security_token' => self::getFormSecurityToken('settings_2fa_verify'),
+                       '$form_security_token'     => self::getFormSecurityToken('settings_2fa_verify'),
                        '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
-                       '$title' => L10n::t('Two-factor code verification'),
-                       '$help_label' => L10n::t('Help'),
-                       '$message' => L10n::t('<p>Please scan this QR Code with your authenticator app and submit the provided code.</p>'),
-                       '$qrcode_image' => $qrcode_image,
-                       '$qrcode_url_message' => L10n::t('<p>Or you can open the following URL in your mobile devicde:</p><p><a href="%s">%s</a></p>', $otpauthUrl, $shortOtpauthUrl),
-                       '$manual_message' => $manual_message,
-                       '$company' => $company,
-                       '$holder' => $holder,
-                       '$secret' => $secret,
-
-                       '$verify_code' => ['verify_code', L10n::t('Please enter a code from your authentication app'), '', '', 'required', 'autofocus placeholder="000000"'],
-                       '$verify_label' => L10n::t('Verify code and enable two-factor authentication'),
+
+                       '$title'              => $this->t('Two-factor code verification'),
+                       '$help_label'         => $this->t('Help'),
+                       '$message'            => $this->t('<p>Please scan this QR Code with your authenticator app and submit the provided code.</p>'),
+                       '$qrcode_image'       => $qrcode_image,
+                       '$qrcode_url_message' => $this->t('<p>Or you can open the following URL in your mobile device:</p><p><a href="%s">%s</a></p>', $otpauthUrl, $shortOtpauthUrl),
+                       '$manual_message'     => $manual_message,
+                       '$company'            => $company,
+                       '$holder'             => $holder,
+                       '$secret'             => $secret,
+
+                       '$verify_code'  => ['verify_code', $this->t('Please enter a code from your authentication app'), '', '', $this->t('Required'), 'autofocus autocomplete="off" placeholder="000000"'],
+                       '$verify_label' => $this->t('Verify code and enable two-factor authentication'),
                ]);
        }
 }