diff --git a/BaseReCaptcha.php b/BaseReCaptcha.php deleted file mode 100644 index 1f0da14..0000000 --- a/BaseReCaptcha.php +++ /dev/null @@ -1,68 +0,0 @@ -_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 deleted file mode 100644 index c57c196..0000000 --- a/BaseReCaptchaValidator.php +++ /dev/null @@ -1,33 +0,0 @@ -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/Cells/ReCaptcha.php b/Cells/ReCaptcha.php new file mode 100644 index 0000000..3c20011 --- /dev/null +++ b/Cells/ReCaptcha.php @@ -0,0 +1,45 @@ +key; + + return view('Denis303\ReCaptcha\Views\init_v2', $params); + } + + public function init_v3(array $params) + { + static $inited = []; + + if (array_search($params['key'], $inited) === false) + { + $params['init'] = true; + + $inited[] = $params['key']; + } + else + { + $params['init'] = false; + } + + return view('Denis303\ReCaptcha\Views\init_v3', $params); + } + +} \ No newline at end of file diff --git a/Config/BaseReCaptcha.php b/Config/BaseReCaptcha.php deleted file mode 100644 index ffc7719..0000000 --- a/Config/BaseReCaptcha.php +++ /dev/null @@ -1,18 +0,0 @@ -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/Events.php b/Config/Events.php new file mode 100644 index 0000000..5fa9953 --- /dev/null +++ b/Config/Events.php @@ -0,0 +1,7 @@ +secret) + { + throw new Exception('The secret parameter is missing.'); + } + + $return = new ReCaptcha($config->secret); + + if ($config->expectedHostname !== null) + { + $return->setExpectedHostname($config->expectedHostname); + } + + if ($config->challengeTimeout !== null) + { + $return->setChallengeTimeout($config->challengeTimeout); + } + + return $return; + } + + public static function reCaptcha3($getShared = true) + { + if ($getShared) + { + return static::getSharedInstance(__FUNCTION__); + } + + $config = config(ReCaptcha3::class); + + if (!$config) + { + throw new Exception(ReCaptcha3::class . ' not found.'); + } + + if (!$config->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/Helpers/form_recaptcha_helper.php b/Helpers/form_recaptcha_helper.php deleted file mode 100644 index 294f20a..0000000 --- a/Helpers/form_recaptcha_helper.php +++ /dev/null @@ -1,9 +0,0 @@ -render_v3($action, $options); - } -} \ No newline at end of file diff --git a/Helpers/reCaptcha_helper.php b/Helpers/reCaptcha_helper.php new file mode 100644 index 0000000..fb58c89 --- /dev/null +++ b/Helpers/reCaptcha_helper.php @@ -0,0 +1,79 @@ + $config->key, + 'id' => $attributes['id'], + 'options' => $options + ]); + } + + return $return; + } +} + +if (!function_exists('recaptcha3')) +{ + function recaptcha3(string $name, array $attributes = [], array $options = [], bool $init = true) + { + $config = config(ReCaptcha3::class); + + if (!$config) + { + throw new Exception(ReCaptcha3::class . ' not found.'); + } + + helper('form'); + + if (empty($attributes['id'])) + { + $attributes['id'] = $name; + } + + $attributes['type'] = 'hidden'; + + $attributes['name'] = $name; + + $return = form_input($attributes) . PHP_EOL; + + if ($init) + { + $return .= view_cell('Denis303\ReCaptcha\Cells\ReCaptcha::init_v3', [ + 'key' => $config->key, + 'id' => $attributes['id'], + 'options' => $options + ]); + } + + return $return; + } + +} \ No newline at end of file diff --git a/Language/en/Validation.php b/Language/en/Validation.php deleted file mode 100644 index 28eae3f..0000000 --- a/Language/en/Validation.php +++ /dev/null @@ -1,5 +0,0 @@ - 'ReCaptcha is not valid.' -]; \ No newline at end of file diff --git a/Libraries/ReCaptcha.php b/Libraries/ReCaptcha.php new file mode 100644 index 0000000..7e447e4 --- /dev/null +++ b/Libraries/ReCaptcha.php @@ -0,0 +1,23 @@ +getIPAddress(); + + if ($ip && ($ip != '0.0.0.0')) + { + $remoteIp = $ip; + } + } + + return parent::verify($response, $remoteIp); + } + +} \ No newline at end of file diff --git a/README.md b/README.md index 7c6d259..3e73d61 100644 --- a/README.md +++ b/README.md @@ -1 +1,83 @@ -# codeigniter4-recaptcha \ No newline at end of file +Google reCAPTCHA CodeIgniter 4 Library + +##Installation + +```composer require denis303/codeigniter4-recaptcha:dev-master``` + +##Configuration + +In the .env file you need to add your personal ReCaptcha keys. + +``` +# -------------------------------------------------------------------- +# ReCaptcha 2 +# -------------------------------------------------------------------- +recaptcha2.key = 'XXXXXXXX-XXXXXXXX' +recaptcha2.secret = 'XXXXXXXX-XXXXXXXX' + +# -------------------------------------------------------------------- +# ReCaptcha 3 +# -------------------------------------------------------------------- +recaptcha3.key = 'XXXXXXXX-XXXXXXXX' +recaptcha3.secret = 'XXXXXXXX-XXXXXXXX' +recaptcha3.scoreThreshold = 0.5 +``` + +In the /app/Config/Validation.php file you need to add settings for validator: + +public $ruleSets = [ + ... + \Denis303\ReCaptcha\Validation\ReCaptchaRules::class +]; + +## Rendering ReCaptcha 2 + +Rendering of a hidden field ReCaptcha v2 in the form: + +``` +helper(['form', 'reCaptcha']); + +echo form_open(); + +echo reCaptcha2('reCaptcha2', ['id' => 'recaptcha_1'], ['theme' => 'dark']); + +echo form_submit('submit', 'Submit'); + +echo form_close(); +``` + +## Rendering ReCaptcha 3 + +Rendering of a hidden field ReCaptcha v3 in the form: + +``` +helper(['form', 'reCaptcha']); + +echo form_open(); + +echo reCaptcha3('reCaptcha3', ['id' => 'recaptcha_1'], ['action' => 'contactForm']); + +echo form_submit('submit', 'Submit'); + +echo form_close(); +``` + +## Checking ReCaptcha in a model: + +public $validationRules = [ + 'reCaptcha2' => 'required|reCaptcha2[]' + 'reCaptcha3' => 'required|reCaptcha3[contactForm,0.9]' + .... +]; + +In the settings of the reCaptcha3 validator, the first parameter you specify is expectedAction, this parameter is not required. + +You can override global scoreThreshold parameter in the second rule parameter. + +## Setting custom ReCaptcha validation error in a model: + +protected $validationMessages = [ + 'reCaptcha' => [ + 'reCaptcha3' => 'Captcha is not valid.' + ] +]; \ No newline at end of file diff --git a/ReCaptcha.php b/ReCaptcha.php deleted file mode 100644 index 0638f4d..0000000 --- a/ReCaptcha.php +++ /dev/null @@ -1,7 +0,0 @@ -setExpectedAction($params[0]); + } + + $response = $service->verify($token); + + if ($response->isSuccess()) + { + return true; + } + + foreach($response->getErrorCodes() as $key => $value) + { + $error = lang('ReCaptcha.' . $value); + + break; + } + + return false; + } + + public function reCaptcha3(string $token, string $params, array $data, &$error = null) + { + $service = service('reCaptcha3'); + + $params = explode(',', $params); + + if (count($params) > 0) + { + $service->setExpectedAction($params[0]); + } + + if (count($params) > 1) + { + $service->setScoreThreshold($params[1]); + } + + $response = $service->verify($token); + + if ($response->isSuccess()) + { + return true; + } + + foreach($response->getErrorCodes() as $key => $value) + { + $error = lang('ReCaptcha.' . $value); + + break; + } + + return false; + } + +} \ No newline at end of file diff --git a/Views/init_v2.php b/Views/init_v2.php new file mode 100644 index 0000000..7b1acf3 --- /dev/null +++ b/Views/init_v2.php @@ -0,0 +1,21 @@ + + +
+ + + + diff --git a/Views/init_v3.php b/Views/init_v3.php new file mode 100644 index 0000000..edee83a --- /dev/null +++ b/Views/init_v3.php @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/Views/recaptcha3.php b/Views/recaptcha3.php deleted file mode 100644 index 4e46748..0000000 --- a/Views/recaptcha3.php +++ /dev/null @@ -1,43 +0,0 @@ -'; -} - -?> - \ No newline at end of file