diff --git a/BaseReCaptcha.php b/BaseReCaptcha.php new file mode 100644 index 0000000..1f0da14 --- /dev/null +++ b/BaseReCaptcha.php @@ -0,0 +1,68 @@ +_lastResponse; + } + + public function getLastErrors() : array + { + $return = $this->getLastErrorCodes(); + + foreach($return as $key => $value) + { + $return[$key] = lang('ReCaptcha.' . $value); + } + + return $return; + } + + public function getLastErrorCodes() : array + { + return $this->_lastResponse ? $this->_lastResponse->getErrorCodes() : []; + } + + public function verify($response, $remoteIp = null) + { + if (!$remoteIp) + { + $ip = service('request')->getIPAddress(); + + if ($ip && ($ip != '0.0.0.0')) + { + $remoteIp = $ip; + } + } + + $this->_lastResponse = parent::verify($response, $remoteIp); + + return $this->_lastResponse; + } + + public function render_v3(string $action, array $options = []) + { + $config = config(ReCaptchaConfig::class); + + if (!$config->key) + { + throw new Exception('The key parameter is missing.'); + } + + return view('Denis303\ReCaptcha\Views\recaptcha3', [ + 'key' => $config->key, + 'action' => $action, + 'options' => $options + ]); + } + +} \ No newline at end of file diff --git a/BaseReCaptchaValidator.php b/BaseReCaptchaValidator.php new file mode 100644 index 0000000..c57c196 --- /dev/null +++ b/BaseReCaptchaValidator.php @@ -0,0 +1,33 @@ +setExpectedAction(trim($params[0])); + + if (count($params) > 1) + { + $reCaptcha->setScoreThreshold(trim($params[1])); + } + + $response = $reCaptcha->verify($token); + + if (!$response->isSuccess()) + { + return false; + } + + return true; + } + +} \ No newline at end of file diff --git a/Config/BaseReCaptcha.php b/Config/BaseReCaptcha.php new file mode 100644 index 0000000..ffc7719 --- /dev/null +++ b/Config/BaseReCaptcha.php @@ -0,0 +1,18 @@ +secret) + { + throw new Exception('The secret parameter is missing.'); + } + + $return = new ReCaptcha($config->secret); + + if ($config->scoreThreshold !== null) + { + $return->setScoreThreshold($config->scoreThreshold); + } + + if ($config->expectedHostname !== null) + { + $return->setExpectedHostname($config->expectedHostname); + } + + if ($config->challengeTimeout !== null) + { + $return->setChallengeTimeout($config->challengeTimeout); + } + + return $return; + } + +} \ No newline at end of file diff --git a/Config/ReCaptcha.php b/Config/ReCaptcha.php new file mode 100644 index 0000000..7da27ab --- /dev/null +++ b/Config/ReCaptcha.php @@ -0,0 +1,7 @@ +render_v3($action, $options); + } +} \ No newline at end of file diff --git a/Language/en/ReCaptcha.php b/Language/en/ReCaptcha.php new file mode 100644 index 0000000..3bc0b8d --- /dev/null +++ b/Language/en/ReCaptcha.php @@ -0,0 +1,10 @@ + 'The secret parameter is missing.', + 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', + 'missing-input-response' => 'The response parameter is missing.', + 'invalid-input-response' => 'The response parameter is invalid or malformed.', + 'bad-request The request' => 'is invalid or malformed.', + 'timeout-or-duplicate' => 'The response is no longer valid: either is too old or has been used previously.' +]; \ No newline at end of file diff --git a/Language/en/Validation.php b/Language/en/Validation.php new file mode 100644 index 0000000..28eae3f --- /dev/null +++ b/Language/en/Validation.php @@ -0,0 +1,5 @@ + 'ReCaptcha is not valid.' +]; \ No newline at end of file diff --git a/ReCaptcha.php b/ReCaptcha.php new file mode 100644 index 0000000..0638f4d --- /dev/null +++ b/ReCaptcha.php @@ -0,0 +1,7 @@ +'; +} + +?> + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..5eba8ea --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "denis303/codeigniter4-recaptcha", + "license": "MIT", + "autoload": { + "psr-4": { + "Denis303\\ReCaptcha\\" : "" + } + } +}