This commit is contained in:
Joey Kimsey
2025-02-25 14:03:12 -05:00
parent c40b29e812
commit 46390c2447
21 changed files with 197 additions and 60 deletions

View File

@ -0,0 +1,85 @@
<?php
/**
* app/plugins/turnstile/plugin.php
*
* This houses all of the main plugin info and functionality.
*
* @package TP Turnstile
* @version 5.0.1
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Plugins;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\Models\Notification;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Issues;
class Turnstile extends Plugin {
private static $loaded = false;
public $pluginName = 'TP Turnstile';
public $pluginAuthor = 'JoeyK';
public $pluginWebsite = 'https://TheTempusProject.com';
public $modelVersion = '1.0';
public $pluginVersion = '3.0';
public $pluginDescription = 'A simple plugin which adds a site wide cloudflare turnstile integration.';
public $configName = 'turnstile';
public $configMatrix = [
'secretKey' => [
'type' => 'text',
'pretty' => 'Turnstile Secret Key',
'default' => 'xxxxxxxxxxxxx',
],
'apiKey' => [
'type' => 'text',
'pretty' => 'Turnstile API Key',
'default' => 'xxxxxxxxxxxxxx',
],
];
public function __construct( $load = false ) {
parent::__construct( $load );
if ( ! self::$loaded ) {
if ( $this->checkEnabled() ) {
Components::set( 'TURNSTILE_API_KEY', Config::getValue( 'turnstile/apiKey' ) );
Components::set( 'TURNSTILE_WIDGET', Views::simpleView( 'turnstile.widget') );
Components::append( 'TEMPLATE_JS_INCLUDES', '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>' );
}
self::$loaded = true;
}
}
public function verify() {
if ( ! Input::exists('cf-turnstile-response') ) {
Issues::add( 'notice', 'Turnstile verification failed. Please try again.' );
return false;
}
$verify_url = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
$data = [
"secret" => Config::getValue( 'turnstile/secretKey' ),
"response" => Input::post('cf-turnstile-response'),
"remoteip" => $_SERVER["REMOTE_ADDR"] // Optional, helps detect abuse
];
$options = [
"http" => [
"header" => "Content-Type: application/x-www-form-urlencoded",
"method" => "POST",
"content" => http_build_query($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents($verify_url, false, $context);
$result = json_decode($response, true);
if ( ! $result["success"]) {
Issues::add( 'notice', 'Turnstile verification failed. Please try again. If the issue persists, please contact the site administrator.' );
return false;
}
return true;
}
}

View File

@ -0,0 +1 @@
<div class="cf-turnstile" data-sitekey="{TURNSTILE_API_KEY}"></div>