From 575c8508f99deeb018cca44bce636b4dfb9ca136 Mon Sep 17 00:00:00 2001 From: Dom Stubbs Date: Thu, 16 Dec 2021 13:08:10 +0000 Subject: [PATCH 1/2] Include 'remoteip' with verify requests where possible --- src/services/CraftRecaptchaService.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/services/CraftRecaptchaService.php b/src/services/CraftRecaptchaService.php index 57e6ea7..1a7df9e 100644 --- a/src/services/CraftRecaptchaService.php +++ b/src/services/CraftRecaptchaService.php @@ -70,6 +70,12 @@ public function verify($data) 'response' => $data ); + $ip = Craft::$app->getRequest()->userIP; + + if ($ip) { + $params['remoteip'] = $ip; + } + $client = new GuzzleHttp\Client(); $response = $client->request('POST', $base, ['form_params' => $params]); From 1b618c87e24c3c5aa74d2a2c71cb967a1d61b1f7 Mon Sep 17 00:00:00 2001 From: Dom Stubbs Date: Tue, 21 Dec 2021 15:30:51 +0000 Subject: [PATCH 2/2] Added 'shareUserIPs' plugin setting Defaults to false. --- src/config.php | 3 ++- src/models/Settings.php | 7 +++++++ src/services/CraftRecaptchaService.php | 8 +++++--- src/templates/settings.twig | 8 ++++++++ src/translations/en/recaptcha.php | 1 + 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/config.php b/src/config.php index ca673a6..b83bd9a 100644 --- a/src/config.php +++ b/src/config.php @@ -27,5 +27,6 @@ "siteKey" => "", "secretKey" => "", "validateContactForm" => true, - + "shareUserIPs" => false, + ]; diff --git a/src/models/Settings.php b/src/models/Settings.php index f44d4e9..b5beb69 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -56,6 +56,13 @@ class Settings extends Model */ public $validateContactForm = true; + /** + * Share user IP addresses with Google + * + * @var bool + */ + public $shareUserIPs = false; + // Public Methods diff --git a/src/services/CraftRecaptchaService.php b/src/services/CraftRecaptchaService.php index 1a7df9e..f46e363 100644 --- a/src/services/CraftRecaptchaService.php +++ b/src/services/CraftRecaptchaService.php @@ -70,10 +70,12 @@ public function verify($data) 'response' => $data ); - $ip = Craft::$app->getRequest()->userIP; + if ($settings->shareUserIPs) { + $ip = Craft::$app->getRequest()->userIP; - if ($ip) { - $params['remoteip'] = $ip; + if ($ip) { + $params['remoteip'] = $ip; + } } $client = new GuzzleHttp\Client(); diff --git a/src/templates/settings.twig b/src/templates/settings.twig index e171df7..bb6d0a8 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -45,3 +45,11 @@ name: 'validateContactForm', on: settings['validateContactForm']}) }} + +{{ forms.lightswitchField({ + label: 'Share user IPs with Google?'|t('recaptcha'), + instructions: 'Enable to include requesting user IP addresses when making [siteverify](https://developers.google.com/recaptcha/docs/verify#api_request) requests.'|t('recaptcha'), + id: 'shareUserIPs', + name: 'shareUserIPs', + on: settings['shareUserIPs']}) +}} diff --git a/src/translations/en/recaptcha.php b/src/translations/en/recaptcha.php index 2b92242..1468c88 100644 --- a/src/translations/en/recaptcha.php +++ b/src/translations/en/recaptcha.php @@ -27,4 +27,5 @@ 'Enter your reCAPTCHA site key.' => 'Enter your reCAPTCHA site key.', 'Enter your reCAPTCHA secret key.' => 'Enter your reCAPTCHA secret key.', 'Validate contact forms?' => 'Validate contact forms?', + 'Share user IPs with Google?' => 'Share user IPs with Google?', ];