* @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', '' ); } 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; } }