Skip to content

Commit 40177ad

Browse files
committed
Use a separate function for recaptcha and turnstile lang-key check
1 parent 8449fc3 commit 40177ad

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Phpbb/TranslationValidator/Validator/FileValidator.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ public function validate($sourceFile, $originFile)
304304
else if ($originFile === $this->originLanguagePath . 'composer.json')
305305
{
306306
$this->validateJsonFile($originFile);
307+
$this->validateCaptchaValues($originFile);
307308
}
308309
else if (substr($originFile, -4) === '.css')
309310
{
@@ -547,8 +548,7 @@ public function validateIndexFile($originFile)
547548
* name, description, type, version, homepage, license
548549
* Authors: name (optional: email and homepage)
549550
* Extra: language-iso, english-name, local-name,
550-
* phpbb-version, direction, user-lang, plural-rule,
551-
* recaptcha-lang, turnstile-lang
551+
* phpbb-version, direction, user-lang, plural-rule
552552
* Optional:
553553
* Support: urls to: forum, wiki, issues etc
554554
*
@@ -652,15 +652,27 @@ public function validateJsonFile($originFile)
652652
{
653653
$this->output->addMessage(Output::FATAL, 'Plural rules does not have a valid value.', $originFile);
654654
}
655+
}
656+
657+
/**
658+
* Check that the reCaptcha and Turnstile key provided is allowed
659+
* @param $originFile
660+
*/
661+
public function validateCaptchaValues($originFile)
662+
{
663+
$fileContents = (string) file_get_contents($this->originPath . '/' . $originFile);
664+
$jsonContent = json_decode($fileContents, true);
665+
// The key 'RECAPTCHA_LANG' must match the list provided by Google, or be left empty
655666
// Check for valid recaptcha-lang: en-GB
656667
if (!in_array($jsonContent['extra']['recaptcha-lang'], $this->reCaptchaLanguages))
657668
{
658-
$this->output->addMessage(Output::ERROR, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty', $originFile);
669+
$this->output->addMessage(Output::ERROR, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en".', $originFile);
659670
}
660-
// Check for valid turnstile-lang: en (should be in: https://developers.cloudflare.com/turnstile/reference/supported-languages/ )
671+
// Check for valid turnstile-lang: en
672+
// (should be in: https://developers.cloudflare.com/turnstile/reference/supported-languages/ )
661673
if (!in_array($jsonContent['extra']['turnstile-lang'], $this->reTurnstilesLanguages))
662674
{
663-
$this->output->addMessage(Output::ERROR, 'Turnstile must match a 2-digit-language code from https://developers.cloudflare.com/turnstile/reference/supported-languages/ ', $originFile);
675+
$this->output->addMessage(Output::ERROR, 'Turnstile must match a 2-digit-language code from https://developers.cloudflare.com/turnstile/reference/supported-languages/ - if no code exists for your language you can use "en".', $originFile);
664676
}
665677
}
666678

tests/FileValidator/ValidateLangTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testValidateLangReCaptcha()
4545
{
4646
// Failure - as we supply a key that isn't valid
4747
$reCaptchaLanguage = ['RECAPTCHA_LANG' => 'incorrect'];
48-
$this->validator->validateReCaptchaValue('', $reCaptchaLanguage);
48+
$this->validator->validateCaptchaValues('', $reCaptchaLanguage);
4949

5050
$output = $this->output->getMessages();
5151
$expected = Output::ERROR . '-reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty--RECAPTCHA_LANG';
@@ -55,7 +55,7 @@ public function testValidateLangReCaptcha()
5555

5656
// Pass - as 'en' is valid
5757
$reCaptchaLanguage['RECAPTCHA_LANG'] = 'en';
58-
$this->validator->validateReCaptchaValue('', $reCaptchaLanguage);
58+
$this->validator->validateCaptchaValues('', $reCaptchaLanguage);
5959

6060
$this->assertEquals($this->output->getMessageCount(Output::ERROR), 1); // Shouldn't change in size as no error added
6161
}

0 commit comments

Comments
 (0)