Skip to content

Feature/recaptcha #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lang/en/local_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
$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['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";
Expand Down
39 changes: 39 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ public function createform($data) {
$str .= '<span class="help-block">' . $this->error_fields[$value->name] . '</span>';
}

$recaptcha_site_key = get_config('local_pages', 'recaptcha_site_key');
if ($recaptcha_site_key)
{
$recaptcha_error = "";
if (isset($this->error_fields['recaptcha'])) {
$recaptcha_error = '<span class="help-block error-item">' . $this->error_fields['recaptcha'] . '</span>';
}
$str .= <<<HTML
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="fitem fitem_actionbuttons fitem_fgroup">
<div class="felement fgroup">
$recaptcha_error
<div class="g-recaptcha" data-sitekey="$recaptcha_site_key"></div>
</div>
</div>
HTML;

}

$str .= '<div class="fitem fitem_actionbuttons fitem_fgroup"><div class="felement fgroup">' .
'<input type="text" name="hp" value="" style="position:absolute;left:-99999px" /> ' .
'<button type="submit" name="formsubmit" value="1" class="btn btn-primary">Submit</button>' .
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 17 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down