From 3bb8989d2b77e53ffd332ba0196858c8bfdda747 Mon Sep 17 00:00:00 2001 From: Tom Mayfield Date: Thu, 18 Jan 2024 16:44:26 -0800 Subject: [PATCH 1/2] Add support for reCaptcha validation --- lang/en/local_pages.php | 7 ++++++- renderer.php | 39 +++++++++++++++++++++++++++++++++++++++ settings.php | 17 +++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lang/en/local_pages.php b/lang/en/local_pages.php index 22e1363..0f6d7ed 100644 --- a/lang/en/local_pages.php +++ b/lang/en/local_pages.php @@ -67,6 +67,10 @@ $string['email_headers'] = 'Custom headers for PHP mail'; $string['user_copy'] = "Copy message to person"; $string['user_copy_description'] = "Select if the person filling in the form is to receive a message"; +$string['recaptcha_site_key'] = "Google reCaptcha site key"; +$string['recaptcha_site_key_description'] = "Included in the data sent to Google. When set, all forms will have a reCaptcha added and the user response will be validated."; +$string['recaptcha_secret_key'] = "Google reCaptcha secret key"; +$string['recaptcha_secret_key_description'] = "Used to validate the reCaptcha submission from the user"; $string['message_copy'] = "Message to go to user"; $string['message_copy_description'] = "Enter {field name} from the form to appear in the message. Use {table} to place the all form fields"; $string['enable_limit'] = "Limit emails to one per session"; @@ -99,4 +103,5 @@ $string['select_fullname'] = "fullname"; $string['view'] = "View"; $string['edit'] = "Edit"; -$string['delete'] = "Delete"; \ No newline at end of file +$string['delete'] = "Delete"; +$string['invalid_recaptcha'] = 'The reCaptcha response did not verify. Please try again (with JavaScript enabled).'; diff --git a/renderer.php b/renderer.php index 91de531..906d587 100644 --- a/renderer.php +++ b/renderer.php @@ -283,6 +283,25 @@ public function createform($data) { $str .= '' . $this->error_fields[$value->name] . ''; } + $recaptcha_site_key = get_config('local_pages', 'recaptcha_site_key'); + if ($recaptcha_site_key) + { + $recaptcha_error = ""; + if (isset($this->error_fields['recaptcha'])) { + $recaptcha_error = '' . $this->error_fields['recaptcha'] . ''; + } + $str .= << +
+
+ $recaptcha_error +
+
+
+HTML; + + } + $str .= '
' . ' ' . '' . @@ -322,6 +341,26 @@ public function valid($records) { } } } + $recaptcha_site_key = get_config('local_pages', 'recaptcha_site_key'); + if ($recaptcha_site_key) + { + $recaptcha_secret_key = get_config('local_pages', 'recaptcha_secret_key'); + $captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : ""; + if (!$captcha) + { + $this->error_fields['recaptcha'] = get_string('pleasefillin', 'local_pages', 'reCaptcha'); + return false; + } + + // Verify the reCaptcha response + $url = "https://www.google.com/recaptcha/api/siteverify?secret=$recaptcha_secret_key&response={$captcha}&remoteip={$_SERVER['REMOTE_ADDR']}"; + $response = json_decode(file_get_contents($url), true); + if (!$response['success']) + { + $this->error_fields['recaptcha'] = get_string('invalid_recaptcha', 'local_pages'); + return false; + } + } return $valid; } diff --git a/settings.php b/settings.php index 02c9acd..b73e48b 100644 --- a/settings.php +++ b/settings.php @@ -63,6 +63,23 @@ 0 ); + $settings[] = new \admin_setting_configtext( + 'local_pages/recaptcha_site_key', + get_string('recaptcha_site_key', 'local_pages'), + get_string('recaptcha_site_key_description', 'local_pages'), + '', + PARAM_TEXT, + 80 + ); + $settings[] = new \admin_setting_configtext( + 'local_pages/recaptcha_secret_key', + get_string('recaptcha_secret_key', 'local_pages'), + get_string('recaptcha_secret_key_description', 'local_pages'), + '', + PARAM_TEXT, + 80 + ); + // Setting to define a message to be sent to a user from a form. $settings[] = new \admin_setting_confightmleditor( 'local_pages/message_copy', From b5a28993749eacf1c3d11b82f0ca8129fee44f82 Mon Sep 17 00:00:00 2001 From: Tom Mayfield Date: Thu, 18 Jan 2024 16:45:18 -0800 Subject: [PATCH 2/2] Put the new language strings together --- lang/en/local_pages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/en/local_pages.php b/lang/en/local_pages.php index 0f6d7ed..e682868 100644 --- a/lang/en/local_pages.php +++ b/lang/en/local_pages.php @@ -71,6 +71,7 @@ $string['recaptcha_site_key_description'] = "Included in the data sent to Google. When set, all forms will have a reCaptcha added and the user response will be validated."; $string['recaptcha_secret_key'] = "Google reCaptcha secret key"; $string['recaptcha_secret_key_description'] = "Used to validate the reCaptcha submission from the user"; +$string['invalid_recaptcha'] = 'The reCaptcha response did not verify. Please try again (with JavaScript enabled).'; $string['message_copy'] = "Message to go to user"; $string['message_copy_description'] = "Enter {field name} from the form to appear in the message. Use {table} to place the all form fields"; $string['enable_limit'] = "Limit emails to one per session"; @@ -103,5 +104,4 @@ $string['select_fullname'] = "fullname"; $string['view'] = "View"; $string['edit'] = "Edit"; -$string['delete'] = "Delete"; -$string['invalid_recaptcha'] = 'The reCaptcha response did not verify. Please try again (with JavaScript enabled).'; +$string['delete'] = "Delete"; \ No newline at end of file