|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Haffner\JhCaptcha\Validation\Validator; |
| 4 | + |
| 5 | +class ReCaptchaValidator extends \Haffner\JhCaptcha\Validation\Validator\AbstractCaptchaValidator |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Check if $value is valid. If it is not valid, needs to add an error |
| 9 | + * to Result. |
| 10 | + * |
| 11 | + * @param mixed $value |
| 12 | + */ |
| 13 | + protected function isValid($value) |
| 14 | + { |
| 15 | + $extensionName = 'jh_captcha'; |
| 16 | + $secret = $this->settings['reCaptcha']['secretKey']; |
| 17 | + $url = 'https://www.google.com/recaptcha/api/siteverify'; |
| 18 | + $apiResponse = json_decode(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($url.'?secret='.$secret.'&response='.$value), true); |
| 19 | + if ($apiResponse['success'] == false) { |
| 20 | + if (is_array($apiResponse['error-codes'])) { |
| 21 | + foreach ($apiResponse['error-codes'] as $errorCode) { |
| 22 | + switch ($errorCode) { |
| 23 | + case 'missing-input-secret': |
| 24 | + $this->addError($this->translateErrorMessage('missingInputSecret', $extensionName), 1426877004); |
| 25 | + break; |
| 26 | + case 'invalid-input-secret': |
| 27 | + $this->addError($this->translateErrorMessage('invalidInputSecret', $extensionName), 1426877455); |
| 28 | + break; |
| 29 | + case 'missing-input-response': |
| 30 | + $this->addError($this->translateErrorMessage('missingInputResponse', $extensionName), 1426877525); |
| 31 | + break; |
| 32 | + case 'invalid-input-response': |
| 33 | + $this->addError($this->translateErrorMessage('invalidInputResponse', $extensionName), 1426877590); |
| 34 | + break; |
| 35 | + default: |
| 36 | + $this->addError($this->translateErrorMessage('defaultError', $extensionName), 1427031929); |
| 37 | + } |
| 38 | + } |
| 39 | + } else { |
| 40 | + $this->addError($this->translateErrorMessage('defaultError', $extensionName), 1427031929); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Validate function for the powermail extension. |
| 47 | + * |
| 48 | + * @param \In2code\Powermail\Domain\Model\Mail $mail the form field |
| 49 | + * @param \In2code\Powermail\Domain\Validator\CustomValidator $customValidator the validator from powermail |
| 50 | + */ |
| 51 | + public function validateCaptchaInPowermail($mail, $customValidator) |
| 52 | + { |
| 53 | + foreach ($mail->getAnswers() as $answer) { |
| 54 | + if ($answer->getField()->getType() == 'jhcaptcharecaptcha') { |
| 55 | + // validate the captcha |
| 56 | + $result = $this->validate($answer->getValue()); |
| 57 | + // set error messages if necessary |
| 58 | + $errors = $result->getErrors(); |
| 59 | + if (!empty($errors)) { |
| 60 | + foreach ($errors as $error) { |
| 61 | + $customValidator->setErrorAndMessage($answer->getField(), $error->getMessage()); |
| 62 | + } |
| 63 | + } |
| 64 | + // remove captcha field from the mail |
| 65 | + $mail->removeAnswer($answer); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments