├── Cells └── ReCaptcha.php ├── Config ├── Events.php ├── ReCaptcha2.php ├── ReCaptcha3.php └── Services.php ├── Helpers └── reCaptcha_helper.php ├── Language └── en │ └── ReCaptcha.php ├── Libraries └── ReCaptcha.php ├── README.md ├── Validation └── ReCaptchaRules.php ├── Views ├── init_v2.php └── init_v3.php └── composer.json /Cells/ReCaptcha.php: -------------------------------------------------------------------------------- 1 | key; 23 | 24 | return view('Denis303\ReCaptcha\Views\init_v2', $params); 25 | } 26 | 27 | public function init_v3(array $params) 28 | { 29 | static $inited = []; 30 | 31 | if (array_search($params['key'], $inited) === false) 32 | { 33 | $params['init'] = true; 34 | 35 | $inited[] = $params['key']; 36 | } 37 | else 38 | { 39 | $params['init'] = false; 40 | } 41 | 42 | return view('Denis303\ReCaptcha\Views\init_v3', $params); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Config/Events.php: -------------------------------------------------------------------------------- 1 | secret) 26 | { 27 | throw new Exception('The secret parameter is missing.'); 28 | } 29 | 30 | $return = new ReCaptcha($config->secret); 31 | 32 | if ($config->expectedHostname !== null) 33 | { 34 | $return->setExpectedHostname($config->expectedHostname); 35 | } 36 | 37 | if ($config->challengeTimeout !== null) 38 | { 39 | $return->setChallengeTimeout($config->challengeTimeout); 40 | } 41 | 42 | return $return; 43 | } 44 | 45 | public static function reCaptcha3($getShared = true) 46 | { 47 | if ($getShared) 48 | { 49 | return static::getSharedInstance(__FUNCTION__); 50 | } 51 | 52 | $config = config(ReCaptcha3::class); 53 | 54 | if (!$config) 55 | { 56 | throw new Exception(ReCaptcha3::class . ' not found.'); 57 | } 58 | 59 | if (!$config->secret) 60 | { 61 | throw new Exception('The secret parameter is missing.'); 62 | } 63 | 64 | $return = new ReCaptcha($config->secret); 65 | 66 | if ($config->scoreThreshold !== null) 67 | { 68 | $return->setScoreThreshold($config->scoreThreshold); 69 | } 70 | 71 | if ($config->expectedHostname !== null) 72 | { 73 | $return->setExpectedHostname($config->expectedHostname); 74 | } 75 | 76 | if ($config->challengeTimeout !== null) 77 | { 78 | $return->setChallengeTimeout($config->challengeTimeout); 79 | } 80 | 81 | return $return; 82 | } 83 | 84 | } -------------------------------------------------------------------------------- /Helpers/reCaptcha_helper.php: -------------------------------------------------------------------------------- 1 | $config->key, 34 | 'id' => $attributes['id'], 35 | 'options' => $options 36 | ]); 37 | } 38 | 39 | return $return; 40 | } 41 | } 42 | 43 | if (!function_exists('reCaptcha3')) 44 | { 45 | function reCaptcha3(string $name, array $attributes = [], array $options = [], bool $init = true) 46 | { 47 | $config = config(ReCaptcha3::class); 48 | 49 | if (!$config) 50 | { 51 | throw new Exception(ReCaptcha3::class . ' not found.'); 52 | } 53 | 54 | helper('form'); 55 | 56 | if (empty($attributes['id'])) 57 | { 58 | $attributes['id'] = $name; 59 | } 60 | 61 | $attributes['type'] = 'hidden'; 62 | 63 | $attributes['name'] = $name; 64 | 65 | $return = form_input($attributes) . PHP_EOL; 66 | 67 | if ($init) 68 | { 69 | $return .= view_cell('Denis303\ReCaptcha\Cells\ReCaptcha::init_v3', [ 70 | 'key' => $config->key, 71 | 'id' => $attributes['id'], 72 | 'options' => $options 73 | ]); 74 | } 75 | 76 | return $return; 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /Language/en/ReCaptcha.php: -------------------------------------------------------------------------------- 1 | 'The secret parameter is missing.', 5 | 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', 6 | 'missing-input-response' => 'The response parameter is missing.', 7 | 'invalid-input-response' => 'The response parameter is invalid or malformed.', 8 | 'bad-request The request' => 'is invalid or malformed.', 9 | 'timeout-or-duplicate' => 'The response is no longer valid: either is too old or has been used previously.' 10 | ]; -------------------------------------------------------------------------------- /Libraries/ReCaptcha.php: -------------------------------------------------------------------------------- 1 | getIPAddress(); 13 | 14 | if ($ip && ($ip != '0.0.0.0')) 15 | { 16 | $remoteIp = $ip; 17 | } 18 | } 19 | 20 | return parent::verify($response, $remoteIp); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Google reCAPTCHA CodeIgniter 4 Library 2 | ====================================== 3 | 4 | ## Installation 5 | 6 | ```composer require denis303/codeigniter4-recaptcha:dev-master``` 7 | 8 | ## Configuration 9 | 10 | In the .env file you need to add your personal ReCaptcha keys. 11 | 12 | ``` 13 | # -------------------------------------------------------------------- 14 | # ReCaptcha 2 15 | # -------------------------------------------------------------------- 16 | recaptcha2.key = 'XXXXXXXX-XXXXXXXX' 17 | recaptcha2.secret = 'XXXXXXXX-XXXXXXXX' 18 | 19 | # -------------------------------------------------------------------- 20 | # ReCaptcha 3 21 | # -------------------------------------------------------------------- 22 | recaptcha3.key = 'XXXXXXXX-XXXXXXXX' 23 | recaptcha3.secret = 'XXXXXXXX-XXXXXXXX' 24 | recaptcha3.scoreThreshold = 0.5 25 | ``` 26 | 27 | In the /app/Config/Validation.php file you need to add settings for validator: 28 | 29 | ``` 30 | public $ruleSets = [ 31 | ... 32 | \Denis303\ReCaptcha\Validation\ReCaptchaRules::class 33 | ]; 34 | ``` 35 | 36 | ### Rendering ReCaptcha v2 37 | 38 | ``` 39 | helper(['form', 'reCaptcha']); 40 | 41 | echo form_open(); 42 | 43 | echo reCaptcha2('reCaptcha2', ['id' => 'recaptcha_v2'], ['theme' => 'dark']); 44 | 45 | echo form_submit('submit', 'Submit'); 46 | 47 | echo form_close(); 48 | ``` 49 | 50 | ### Rendering ReCaptcha v3 51 | 52 | ``` 53 | helper(['form', 'reCaptcha']); 54 | 55 | echo form_open(); 56 | 57 | echo reCaptcha3('reCaptcha3', ['id' => 'recaptcha_v3'], ['action' => 'contactForm']); 58 | 59 | echo form_submit('submit', 'Submit'); 60 | 61 | echo form_close(); 62 | ``` 63 | 64 | ### Checking ReCaptcha in a model: 65 | 66 | ``` 67 | public $validationRules = [ 68 | 'reCaptcha2' => 'required|reCaptcha2[]' 69 | 'reCaptcha3' => 'required|reCaptcha3[contactForm,0.9]' 70 | .... 71 | ]; 72 | ``` 73 | 74 | In the settings of the reCaptcha3 validator, the first parameter you specify is expectedAction, this parameter is not required. 75 | 76 | You can override a global scoreThreshold parameter in the second reCaptcha3 rule parameter. -------------------------------------------------------------------------------- /Validation/ReCaptchaRules.php: -------------------------------------------------------------------------------- 1 | verify($token); 17 | 18 | if ($response->isSuccess()) 19 | { 20 | return true; 21 | } 22 | 23 | foreach($response->getErrorCodes() as $key => $value) 24 | { 25 | $error = lang('ReCaptcha.' . $value); 26 | 27 | break; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public function reCaptcha3(string $token, string $params, array $data, &$error = null) 34 | { 35 | $service = service('reCaptcha3'); 36 | 37 | $params = explode(',', $params); 38 | 39 | if (count($params) > 0) 40 | { 41 | $service->setExpectedAction($params[0]); 42 | } 43 | 44 | if (count($params) > 1) 45 | { 46 | $service->setScoreThreshold($params[1]); 47 | } 48 | 49 | $response = $service->verify($token); 50 | 51 | if ($response->isSuccess()) 52 | { 53 | return true; 54 | } 55 | 56 | foreach($response->getErrorCodes() as $key => $value) 57 | { 58 | $error = lang('ReCaptcha.' . $value); 59 | 60 | break; 61 | } 62 | 63 | return false; 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /Views/init_v2.php: -------------------------------------------------------------------------------- 1 | 7 | 8 |
9 | 10 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Views/init_v3.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "denis303/codeigniter4-recaptcha", 3 | "license": "MIT", 4 | "autoload": { 5 | "psr-4": { 6 | "Denis303\\ReCaptcha\\" : "" 7 | } 8 | }, 9 | "require": { 10 | "google/recaptcha": "^1.2" 11 | } 12 | } 13 | --------------------------------------------------------------------------------