Skip to content

Commit 25d7f54

Browse files
committed
add ReCAPTCHA on the reset password form. Related to the issue pkp#6984
1 parent 784c6a7 commit 25d7f54

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

classes/template/PKPTemplateManager.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ public function initialize(PKPRequest $request)
244244
if (Config::getVar('captcha', 'captcha_on_login')) {
245245
array_push($contexts, 'frontend-login-index', 'frontend-login-signIn');
246246
}
247+
if (Config::getVar('captcha', 'captcha_on_lost_password')) {
248+
array_push($contexts, 'frontend-login-lostPassword', 'frontend-login-requestResetPassword');
249+
}
247250
if (count($contexts)) {
248251
// These are the supported locales: https://developers.google.com/recaptcha/docs/language
249252
// It seems Google has already mapping for locales missing in that list, so that we can provide locale es it is.
@@ -1030,7 +1033,7 @@ public function setupBackendPage()
10301033
];
10311034
$isNewSubmissionLinkPresent = true;
10321035
}
1033-
1036+
10341037
$menu['dashboards'] = [
10351038
'name' => __('navigation.dashboards'),
10361039
'icon' => 'Dashboard',
@@ -1380,15 +1383,15 @@ public function display($template = null, $cache_id = null, $compile_id = null,
13801383
];
13811384

13821385
if($context) {
1383-
$pageContext = array_merge($pageContext, [
1386+
$pageContext = array_merge($pageContext, [
13841387
'dateFormatShort' => PKPString::convertStrftimeFormat($context->getLocalizedDateFormatShort()),
13851388
'dateFormatLong' => PKPString::convertStrftimeFormat($context->getLocalizedDateFormatLong()),
13861389
'datetimeFormatShort' => PKPString::convertStrftimeFormat($context->getLocalizedDateTimeFormatShort()),
13871390
'datetimeFormatLong' => PKPString::convertStrftimeFormat($context->getLocalizedDateTimeFormatLong()),
13881391
'timeFormat' => PKPString::convertStrftimeFormat($context->getLocalizedTimeFormat()),
13891392
]);
13901393
} else {
1391-
$pageContext = array_merge($pageContext, [
1394+
$pageContext = array_merge($pageContext, [
13921395
'dateFormatShort' => PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_short')),
13931396
'dateFormatLong' => PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_long')),
13941397
'datetimeFormatShort' => PKPString::convertStrftimeFormat(Config::getVar('general', 'datetime_format_short')),

pages/login/LoginHandler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public function lostPassword($args, $request)
214214
$this->setupTemplate($request);
215215
$templateMgr = TemplateManager::getManager($request);
216216

217+
$isCaptchaEnabled = Config::getVar('captcha', 'recaptcha') && Config::getVar('captcha', 'captcha_on_lost_password');
218+
if ($isCaptchaEnabled) {
219+
$templateMgr->assign('recaptchaPublicKey', Config::getVar('captcha', 'recaptcha_public_key'));
220+
}
221+
217222
$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);
218223
$templateMgr->display('frontend/pages/userLostPassword.tpl');
219224
}
@@ -226,6 +231,17 @@ public function requestResetPassword($args, $request)
226231
$this->setupTemplate($request);
227232
$templateMgr = TemplateManager::getManager($request);
228233

234+
$isCaptchaEnabled = Config::getVar('captcha', 'recaptcha') && Config::getVar('captcha', 'captcha_on_lost_password');
235+
$recaptchaError = null;
236+
if ($isCaptchaEnabled) {
237+
$templateMgr->assign('recaptchaPublicKey', Config::getVar('captcha', 'recaptcha_public_key'));
238+
try {
239+
FormValidatorReCaptcha::validateResponse($request->getUserVar('g-recaptcha-response'), $request->getRemoteAddr(), $request->getServerHost());
240+
} catch (Exception $exception) {
241+
$recaptchaError = 'common.captcha.error.missing-input-response';
242+
}
243+
}
244+
229245
$altchaHasError = $this->_validateAltchasResponse($request, 'altcha_on_lost_password');
230246

231247
if ($altchaHasError) {

templates/frontend/pages/userLostPassword.tpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
</label>
4141
</div>
4242

43+
{if $recaptchaPublicKey}
44+
<fieldset class="recaptcha_wrapper">
45+
<div class="fields">
46+
<div class="recaptcha">
47+
<div class="g-recaptcha" data-sitekey="{$recaptchaPublicKey|escape}">
48+
</div><label for="g-recaptcha-response" style="display:none;" hidden>Recaptcha response</label>
49+
</div>
50+
</div>
51+
</fieldset>
52+
{/if}
53+
4354
{* altcha spam blocker *}
4455
{if $altchaEnabled}
4556
<fieldset class="altcha_wrapper">
@@ -48,7 +59,7 @@
4859
</div>
4960
</fieldset>
5061
{/if}
51-
62+
5263
<div class="buttons">
5364
<button class="submit" type="submit">
5465
{translate key="user.login.resetPassword"}

0 commit comments

Comments
 (0)