class-form-access-control.php
2 months ago
class-form-captcha-handler.php
2 months ago
class-form-controller.php
2 months ago
class-form-email-config-check.php
2 months ago
class-form-email-handler.php
2 months ago
class-form-encryption.php
2 months ago
class-form-exporter.php
2 months ago
class-form-field-validator.php
2 months ago
class-form-file-handler.php
2 months ago
class-form-google-auth.php
2 months ago
class-form-integration-handler.php
2 months ago
class-form-math-parser.php
2 months ago
class-form-permissions.php
2 months ago
class-form-registry.php
2 months ago
class-form-settings.php
2 months ago
class-form-submission-cpt.php
2 months ago
class-form-submission-handler.php
2 months ago
class-form-captcha-handler.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\Form; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | class FormCaptchaHandler |
| 8 | { |
| 9 | /** |
| 10 | * Verify a captcha token based on type. |
| 11 | * |
| 12 | * @param string $type Captcha type |
| 13 | * @param string $token Captcha token from frontend |
| 14 | * @return bool|string True on success, error message on failure |
| 15 | */ |
| 16 | public static function Verify($type, $token) |
| 17 | { |
| 18 | if ($type === 'honeypot') { |
| 19 | return true; // Honeypot is checked in the controller |
| 20 | } |
| 21 | |
| 22 | if (empty($token)) { |
| 23 | return __('Captcha verification failed. Please try again.', 'superb-blocks'); |
| 24 | } |
| 25 | |
| 26 | switch ($type) { |
| 27 | case 'hcaptcha': |
| 28 | return self::VerifyHCaptcha($token); |
| 29 | case 'recaptcha_v2': |
| 30 | case 'recaptcha_v3': |
| 31 | return self::VerifyRecaptcha($token); |
| 32 | case 'turnstile': |
| 33 | return self::VerifyTurnstile($token); |
| 34 | default: |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private static function VerifyHCaptcha($token) |
| 40 | { |
| 41 | $secret = FormSettings::Get(FormSettings::OPTION_HCAPTCHA_SECRET_KEY); |
| 42 | if (empty($secret)) { |
| 43 | return __('hCaptcha is not configured.', 'superb-blocks'); |
| 44 | } |
| 45 | |
| 46 | $response = wp_remote_post('https://hcaptcha.com/siteverify', array( |
| 47 | 'body' => array( |
| 48 | 'secret' => $secret, |
| 49 | 'response' => $token, |
| 50 | ), |
| 51 | )); |
| 52 | |
| 53 | return self::ParseVerificationResponse($response); |
| 54 | } |
| 55 | |
| 56 | private static function VerifyRecaptcha($token) |
| 57 | { |
| 58 | $secret = FormSettings::Get(FormSettings::OPTION_RECAPTCHA_SECRET_KEY); |
| 59 | if (empty($secret)) { |
| 60 | return __('reCAPTCHA is not configured.', 'superb-blocks'); |
| 61 | } |
| 62 | |
| 63 | $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array( |
| 64 | 'body' => array( |
| 65 | 'secret' => $secret, |
| 66 | 'response' => $token, |
| 67 | ), |
| 68 | )); |
| 69 | |
| 70 | return self::ParseVerificationResponse($response); |
| 71 | } |
| 72 | |
| 73 | private static function VerifyTurnstile($token) |
| 74 | { |
| 75 | $secret = FormSettings::Get(FormSettings::OPTION_TURNSTILE_SECRET_KEY); |
| 76 | if (empty($secret)) { |
| 77 | return __('Turnstile is not configured.', 'superb-blocks'); |
| 78 | } |
| 79 | |
| 80 | $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', array( |
| 81 | 'body' => array( |
| 82 | 'secret' => $secret, |
| 83 | 'response' => $token, |
| 84 | ), |
| 85 | )); |
| 86 | |
| 87 | return self::ParseVerificationResponse($response); |
| 88 | } |
| 89 | |
| 90 | private static function ParseVerificationResponse($response) |
| 91 | { |
| 92 | if (is_wp_error($response)) { |
| 93 | return __('Captcha verification failed. Please try again.', 'superb-blocks'); |
| 94 | } |
| 95 | |
| 96 | $body = json_decode(wp_remote_retrieve_body($response), true); |
| 97 | if (isset($body['success']) && $body['success'] === true) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return __('Captcha verification failed. Please try again.', 'superb-blocks'); |
| 102 | } |
| 103 | } |
| 104 |