Skip to content
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

Make request method configurable #396

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions config/config.devel.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
$conf['settings']['recaptcha']['enabled'] = 'false';
$conf['settings']['recaptcha']['public.key'] = '';
$conf['settings']['recaptcha']['private.key'] = '';
$conf['settings']['recaptcha']['request.method'] = 'curl'; // options are curl, post or socket. default: post
/**
* Email
*/
Expand Down
1 change: 1 addition & 0 deletions config/config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
$conf['settings']['recaptcha']['enabled'] = 'false';
$conf['settings']['recaptcha']['public.key'] = '';
$conf['settings']['recaptcha']['private.key'] = '';
$conf['settings']['recaptcha']['request.method'] = 'curl'; // options are curl, post or socket. default: post
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically makes the default curl. Why not make it post?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post uses file_get_contents which requires the option allow_url_fopen to be enabled. Because this could cause security issues and therefore it might not be enabled by default. On the other side I can hardly imagine a PHP installation without CURL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made post the default anyway so, that existing installations have the same behavior as before, when the setting is not added.

/**
* Email
*/
Expand Down
24 changes: 21 additions & 3 deletions lib/Application/Authentication/CaptchaService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

if (file_exists(ROOT_DIR . 'vendor/autoload.php')) {
if (file_exists(ROOT_DIR . 'vendor/autoload.php')) {
require_once ROOT_DIR . 'vendor/autoload.php';
}

Expand Down Expand Up @@ -106,8 +106,26 @@ public function IsCorrect($captchaValue)
$server = ServiceLocator::GetServer();

$privatekey = Configuration::Instance()->GetSectionKey(ConfigSection::RECAPTCHA, ConfigKeys::RECAPTCHA_PRIVATE_KEY);

$recap = new \ReCaptcha\ReCaptcha($privatekey);

$configuredMethod = Configuration::Instance()->GetSectionKey(ConfigSection::RECAPTCHA, ConfigKeys::RECAPTCHA_REQUEST_METHOD);
$method = new \ReCaptcha\RequestMethod\Post();
switch ($configuredMethod)
{
case null:
case '':
case 'post':
break;
case 'socket':
$method = new \ReCaptcha\RequestMethod\SocketPost();
break;
case 'curl':
$method = new \ReCaptcha\RequestMethod\CurlPost();
break;
default:
Log::Error('Invalid ReCaptcha request method: %s. Fallback to', $configuredMethod);
}

$recap = new \ReCaptcha\ReCaptcha($privatekey, $method);
$resp = $recap->verify($server->GetForm('g-recaptcha-response'),$server->GetRemoteAddress());

$success = $resp->isSuccess();
Expand Down
1 change: 1 addition & 0 deletions lib/Config/ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ConfigKeys
public const RECAPTCHA_ENABLED = 'enabled';
public const RECAPTCHA_PUBLIC_KEY = 'public.key';
public const RECAPTCHA_PRIVATE_KEY = 'private.key';
public const RECAPTCHA_REQUEST_METHOD = 'request.method';

public const DEFAULT_FROM_ADDRESS = 'default.from.address';
public const DEFAULT_FROM_NAME = 'default.from.name';
Expand Down
Loading