mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/17414] Start work on new captcha classes
PHPBB-17414
This commit is contained in:
parent
ae8a5e791e
commit
b828c56dc9
5 changed files with 196 additions and 0 deletions
42
phpBB/phpbb/captcha/plugins/plugin_interface.php
Normal file
42
phpBB/phpbb/captcha/plugins/plugin_interface.php
Normal 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;
|
||||
}
|
93
phpBB/phpbb/captcha/plugins/turnstile.php
Normal file
93
phpBB/phpbb/captcha/plugins/turnstile.php
Normal 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.');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace foo\test_captcha\captcha;
|
||||
|
||||
class test_captcha extends \phpbb\captcha\plugins\captcha_abstract
|
||||
{
|
||||
|
||||
function get_generator_class()
|
||||
{
|
||||
}
|
||||
|
||||
public function init($type)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute_demo()
|
||||
{
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
}
|
||||
|
||||
public function validate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function is_solved()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
23
tests/functional/fixtures/ext/foo/test_captcha/composer.json
Normal file
23
tests/functional/fixtures/ext/foo/test_captcha/composer.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "foo/test_captcha",
|
||||
"type": "phpbb-extension",
|
||||
"description": "Testing extension for having a working captcha plugin",
|
||||
"homepage": "",
|
||||
"version": "1.0.0",
|
||||
"time": "2024-03-24 01:01:01",
|
||||
"license": "GPL-2.0",
|
||||
"authors": [{
|
||||
"name": "phpBB Limited",
|
||||
"homepage": "https://www.phpbb.com",
|
||||
"role": "Developer"
|
||||
}],
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"extra": {
|
||||
"display-name": "phpBB 4.0 Test Captcha",
|
||||
"soft-require": {
|
||||
"phpbb/phpbb": "4.0.*@dev"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
services:
|
||||
foo_test_captcha.captcha.test_captcha:
|
||||
class: foo\test_captcha\captcha\test_captcha
|
||||
tags:
|
||||
- { name: captcha.plugins }
|
Loading…
Add table
Reference in a new issue