Просмотр исходного кода

Ticket #4574 - Update email confirmation to use code instead of link.

AntonLV 2 лет назад
Родитель
Сommit
0904b216bd

+ 8 - 7
inc/classes/BxDolAccount.php

@@ -346,18 +346,19 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
             return false;
 
         $oKey = BxDolKey::getInstance();
-        $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
-        $sConfirmationLink = bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email'), array('code' => $sConfirmationCode));
+        $sConfirmationCode = $oKey->getNewKeyNumeric(['account_id' => $iAccountId]);
+        $sConfirmationLink = bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email'), ['code' => $sConfirmationCode]);
 
-        $aReplaceVars = array(
+        $sEmailTemplate = 't_Confirmation';
+        $aEmailReplaceVars = [
             'name' => $this->getDisplayName($iAccountId),
             'email' => $aAccountInfo['email'],
             'conf_code' => $sConfirmationCode,
             'conf_link' => $sConfirmationLink,
             'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
-        );
+        ];
 
-        $bResult = sendMailTemplate('t_Confirmation', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
+        $bResult = sendMailTemplate($sEmailTemplate, $iAccountId, (int)$aAccountInfo['profile_id'], $aEmailReplaceVars, BX_EMAIL_SYSTEM);
         if($bResult)
             $this->doAudit($iAccountId, '_sys_audit_action_account_resend_confirmation_email');
 
@@ -670,9 +671,9 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
     {
         if (!$this->isConfirmed()) {
             if (!$this->isConfirmedEmail()) {
-                $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
+                $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email');
                 $aAccountInfo = $this->getInfo();
-                $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
+                $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl . '&resend=1', $aAccountInfo['email'], $sUrl), BX_INFORMER_ALERT);
             }
             if (!$this->isConfirmedPhone()) {
                 $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone') . '';

+ 21 - 0
inc/classes/BxDolKey.php

@@ -80,6 +80,27 @@ class BxDolKey extends BxDolFactory implements iBxDolSingleton
         return false;
     }
 
+    /**
+     * Get new key (numeric).
+     * @param $aData - some data to associate with the key
+     * @param $iExpire - number of seconds to generated key after, by default - 1 week
+     * @return newly generated key string
+     */
+    public function getNewKeyNumeric ($aData = false, $iExpire = 604800)
+    {
+        $sKey = '';
+        while(true) {
+            $sKey = mt_rand(100000, 999999);
+            if(!$this->isKeyExists($sKey))
+                break;
+        }
+
+        if($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
+            return $sKey;
+
+        return false;
+    }
+
     /**
      * Check if provided key exists.
      * @param $sKey - key string

+ 4 - 1
modules/boonex/english/data/langs/system/en.xml

@@ -2282,6 +2282,9 @@ If it is not enabled then please consider implement this optimization, since it
 <p> </p>
 <p><a href="{conf_link}">{conf_link}</a></p>
 <p> </p>
+<p>or use the code:</p>
+<p><span class="h3">{conf_code}</span></p>
+<p> </p>
 {email_footer}]]></string>
     <string name="_sys_et_txt_body_delayed_module_uninstall"><![CDATA[{email_header}
 <p>Module: <b>{Module}</b></p>
@@ -3653,7 +3656,7 @@ If it is not enabled then please consider implement this optimization, since it
     <string name="_sys_txt_account_profile_context_change_to_another_suggestion"><![CDATA[You can <a href="{0}">change your current profile</a> to this profile]]></string>
     <string name="_sys_txt_account_profile_context_changed_success"><![CDATA[Current profile has been successfully changed to: <b>{0}</b>]]></string>
     <string name="_sys_txt_account_suspended"><![CDATA[Your account is suspended.]]></string>
-    <string name="_sys_txt_account_unconfirmed_email"><![CDATA[Your email address is unconfirmed. Please, check your email ({1}) for a confirmation letter and click email confirmation link. Can't find the letter? Check your SPAM folder or <a href="{0}">send the verification letter again</a>.]]></string>
+    <string name="_sys_txt_account_unconfirmed_email"><![CDATA[Your email address is unconfirmed. Please, check your email ({1}) for a confirmation letter and click email confirmation link or use confirmation code (6 digit) to <a href="{2}">confirm your email</a>. Can't find the letter? Check your SPAM folder or <a href="{0}">send the verification letter again</a>.]]></string>
     <string name="_sys_txt_account_unconfirmed_phone"><![CDATA[Your phone is unconfirmed. Please, confirm it on <a href="{0}">verification page</a>.]]></string>
     <string name="_sys_txt_account_profile_system"><![CDATA[Please <a href="{0}">create a profile</a> to start using the site.]]></string>
     <string name="_sys_txt_account_need_to_change_password"><![CDATA[Your password has been expired. Please <a href="{0}">change your password</a>.]]></string>

+ 4 - 1
modules/boonex/russian/data/langs/system/ru.xml

@@ -2259,6 +2259,9 @@
 <p> </p>
 <p><a href="{conf_link}">{conf_link}</a></p>
 <p> </p>
+<p>или используя код:</p>
+<p><span class="h3">{conf_code}</span></p>
+<p> </p>
 {email_footer}]]></string>
     <string name="_sys_et_txt_body_delayed_module_uninstall"><![CDATA[{email_header}
 <p>Модуль: <b>{Module}</b></p>
@@ -3583,7 +3586,7 @@
     <string name="_sys_txt_account_profile_context_change_to_another_suggestion"><![CDATA[Вы можете <a href="{0}">изменить свой текущий профиль</a> на этот]]></string>
     <string name="_sys_txt_account_profile_context_changed_success"><![CDATA[Текущий профиль удачно изменён на: <b>{0}</b>]]></string>
     <string name="_sys_txt_account_suspended"><![CDATA[Ваш аккаунт заблокирован.]]></string>
-    <string name="_sys_txt_account_unconfirmed_email"><![CDATA[Ваш email не подтверждён. Пожалуйста, проверьте свой эл. ящик ({1}) на наличие письма подтверждения и перейдите по ссылке в нём. <br />Не можете найти письмо? Проверьте папку "спам" или <a href="{0}">снова запросите письмо верификации</a>.]]></string>
+    <string name="_sys_txt_account_unconfirmed_email"><![CDATA[Ваш email не подтверждён. Пожалуйста, проверьте свой эл. ящик ({1}) на наличие письма подтверждения и перейдите по ссылке в нём или используйте код подтверждения (6 цифр), чтобы <a href="{2}">подтвердить свой адрес эл. почты</a>. <br />Не можете найти письмо? Проверьте папку "спам" или <a href="{0}">снова запросите письмо верификации</a>.]]></string>
     <string name="_sys_txt_account_unconfirmed_phone"><![CDATA[Ваш телефон не подтверждён. Пожалуйста, подтвердите его на  <a href="{0}">странице верификации</a>.]]></string>
     <string name="_sys_txt_account_profile_system"><![CDATA[Пожалуйста, <a href="{0}">создайте профиль</a>, чтобы начать пользоваться сайтом.]]></string>
     <string name="_sys_txt_account_need_to_change_password"><![CDATA[Ваш пароль устарел. Пожалуйста <a href="{0}">измените Ваш пароль</a>.]]></string>

+ 39 - 25
template/scripts/BxBaseServiceAccount.php

@@ -390,34 +390,13 @@ class BxBaseServiceAccount extends BxDol
      */
     public function confirmEmail($sKey)
     {
-        // check if key exists
-        $oKey = BxDolKey::getInstance();
-        if (!$oKey || !$oKey->isKeyExists($sKey))
-            return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
-
-        // check if key data exists
-        $aData = $oKey->getKeyData($sKey);
-        if (!isset($aData['account_id']))
-            return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
-
-        // check if account exists
-        $oAccount = BxDolAccount::getInstance($aData['account_id']);
-        if (!$oAccount)
-            return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
-
-        // remove key
-        $oKey->removeKey($sKey);
-
-        // confirm email
-        if (!$oAccount->updateEmailConfirmed(true))
-            return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
-
-        // login to user's account automatically
-        bx_login($aData['account_id']);
+        $mixedAccountId = $this->_confirmEmail($sKey);
+        if(is_string($mixedAccountId) && !is_numeric($mixedAccountId))
+            return MsgBox($mixedAccountId);
 
         // redirect with success message
         $sUrl = getParam('sys_redirect_after_email_confirmation');
-        bx_alert('account', 'after_email_confirmation', $aData['account_id'], false, ['override_result' => &$sUrl]);
+        bx_alert('account', 'after_email_confirmation', $mixedAccountId, false, ['override_result' => &$sUrl]);
 
         $oTemplate = BxDolTemplate::getInstance();
         $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
@@ -429,6 +408,12 @@ class BxBaseServiceAccount extends BxDol
         exit;
     }
 
+    public function serviceConfirmEmail($sKey)
+    {
+        $mixedAccountId = $this->_confirmEmail($sKey);
+        return is_int($mixedAccountId) || is_numeric($mixedAccountId);
+    }
+
     public function serviceGetOptionsResetPasswordRedirect()
     {
         $aResult = array();
@@ -709,6 +694,35 @@ class BxBaseServiceAccount extends BxDol
         return $sPwd;
     }
 
+    protected function _confirmEmail($sKey)
+    {
+        // check if key exists
+        $oKey = BxDolKey::getInstance();
+        if (!$oKey || !$oKey->isKeyExists($sKey))
+            return _t("_sys_txt_confirm_email_error_occured");
+
+        // check if key data exists
+        $aData = $oKey->getKeyData($sKey);
+        if (!isset($aData['account_id']))
+            return _t("_sys_txt_confirm_email_error_occured");
+
+        // check if account exists
+        $oAccount = BxDolAccount::getInstance($aData['account_id']);
+        if (!$oAccount)
+            return _t("_sys_txt_confirm_email_error_occured");
+
+        // remove key
+        $oKey->removeKey($sKey);
+
+        // confirm email
+        if (!$oAccount->updateEmailConfirmed(true))
+            return _t("_sys_txt_confirm_email_error_occured");
+
+        // login to user's account automatically
+        bx_login($aData['account_id']);
+
+        return (int)$aData['account_id'];
+    }
 }
 
 /** @} */

+ 1 - 0
template/scripts/BxBaseServices.php

@@ -56,6 +56,7 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'SwitchProfile' => 'BxBaseServiceAccount',
             'AccountProfileSwitcher' => 'BxBaseServiceAccount',
             'EmailConfirmation' => 'BxBaseServiceAccount',
+            'ConfirmEmail' => 'BxBaseServiceAccount',
 
             'CategoriesList' => 'BxBaseServiceCategory',