1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

[ticket/17414] Start work on new captcha classes

PHPBB-17414
This commit is contained in:
Marc Alexander
2024-08-31 22:05:42 +02:00
parent ae8a5e791e
commit b828c56dc9
5 changed files with 196 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace phpbb\captcha\plugins;
interface plugin_interface
{
const CONFIRMATION_REGISTRATION = 1;
const CONFIRMATION_LOGIN = 2;
const CONFIRMATION_POST = 3;
const CONFIRMATION_REPORT = 4;
/**
* Check if the plugin is available
* @return bool True if the plugin is available, false if not
*/
public function is_available(): bool;
/**
* Check if the plugin has a configuration
*
* @return bool True if the plugin has a configuration, false if not
*/
public function has_config(): bool;
/**
* Get the name of the plugin, should be language variable
*
* @return string
*/
public static function get_name(): string;
/**
* Display the captcha for the specified type
*
* @param int $type Type of captcha, should be one of the CONFIRMATION_* constants
* @return void
*/
public function show(int $type): void;
}

View File

@@ -0,0 +1,93 @@
<?php
namespace phpbb\captcha\plugins;
class turnstile extends captcha_abstract
{
/** @var \phpbb\config\config */
protected $config;
public function __construct(\phpbb\config\config $config)
{
$this->config = $config;
}
public function is_available()
{
return ($this->config->offsetGet('captcha_turnstile_key') ?? false);
}
public function get_template()
{
return 'custom_captcha.html'; // Template file for displaying the CAPTCHA
}
public function execute()
{
// Perform any necessary initialization or setup
}
public function validate()
{
// Implement server-side validation logic here
// Example: Validate the submitted CAPTCHA value using Cloudflare API
// Your Cloudflare API credentials
$api_email = 'your_email@example.com';
$api_key = 'your_api_key';
// Cloudflare API endpoint for CAPTCHA verification
$endpoint = 'https://api.cloudflare.com/client/v4/captcha/validate';
// CAPTCHA data to be sent in the request
$data = [
'email' => $api_email,
'key' => $api_key,
'response' => $this->confirm_code
];
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt_array($ch, [
CURLOPT_URL => $endpoint,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json'
],
CURLOPT_RETURNTRANSFER => true
]);
// Execute the cURL request
$response = curl_exec($ch);
// Check for errors
if ($response === false) {
// Handle cURL error
curl_close($ch);
return false;
}
// Decode the JSON response
$result = json_decode($response, true);
// Check if the response indicates success
if (isset($result['success']) && $result['success'] === true) {
// CAPTCHA validation passed
curl_close($ch);
return true;
} else {
// CAPTCHA validation failed
curl_close($ch);
return false;
}
}
public function get_generator_class()
{
throw new \Exception('No generator class given.');
}
}