Facades
1 month ago
Form
3 weeks ago
ActionHooks.php
1 month ago
Canvas.php
1 month ago
CollectionItem.php
1 month ago
ContentManager.php
1 month ago
DateTime.php
1 month ago
EditorPreview.php
1 month ago
FileHandler.php
7 hours ago
FilterHooks.php
1 month ago
PageUrl.php
1 month ago
Recaptcha.php
1 week ago
Role.php
1 month ago
Session.php
1 month ago
Template.php
1 month ago
Recaptcha.php
110 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Google reCAPTCHA support |
| 4 | * |
| 5 | * Verifies a reCAPTCHA token against Google's siteverify endpoint using the |
| 6 | * keys configured in the Kirki admin common data option. |
| 7 | * |
| 8 | * @package kirki |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\App\Supports; |
| 12 | |
| 13 | use Kirki\Framework\Http\Response; |
| 14 | use Kirki\Framework\Supports\Facades\Http; |
| 15 | use Kirki\App\Supports\Session; |
| 16 | use RuntimeException; |
| 17 | |
| 18 | if (!defined('ABSPATH')) { |
| 19 | exit; // Exit if accessed directly. |
| 20 | } |
| 21 | |
| 22 | class Recaptcha |
| 23 | { |
| 24 | /** |
| 25 | * Google reCAPTCHA verification endpoint. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
| 30 | |
| 31 | /** |
| 32 | * Verify a reCAPTCHA token. |
| 33 | * |
| 34 | * No-op when no token is supplied (matches the legacy behaviour where the |
| 35 | * check only runs if the front-end sent a token). Throws on misconfiguration |
| 36 | * or a failed verification so the caller can abort the request. |
| 37 | * |
| 38 | * @param array|null $params Get the reCAPTCHA token from the submission params. |
| 39 | * @param string|null $form_id The form ID. |
| 40 | * @return void |
| 41 | * |
| 42 | * @throws RuntimeException When configuration is missing or verification fails. |
| 43 | */ |
| 44 | public static function verify(?array $params = [], ?string $form_id = null) |
| 45 | { |
| 46 | $session_data = Session::get($form_id); |
| 47 | $recaptcha = $session_data['recaptcha'] ?? []; |
| 48 | |
| 49 | if (empty($recaptcha)) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if (!isset($recaptcha['GRC_version'])) { |
| 54 | throw new RuntimeException( |
| 55 | esc_html__('reCAPTCHA configuration not found', 'kirki'), |
| 56 | (int) Response::BAD_REQUEST |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | $token = $recaptcha['GRC_version'] === '2.0' ? ($params['g-recaptcha-response'] ?? '') : ($params['g-recaptcha-token'] ?? ''); |
| 61 | |
| 62 | if (empty($token)) { |
| 63 | throw new RuntimeException( |
| 64 | esc_html__('Recaptcha is required.', 'kirki'), |
| 65 | (int) Response::BAD_REQUEST |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $secret_key = $recaptcha['GRC_secret_key'] ?? ''; |
| 70 | |
| 71 | if (empty($secret_key)) { |
| 72 | throw new RuntimeException( |
| 73 | esc_html__('reCAPTCHA secret key not configured', 'kirki'), |
| 74 | (int) Response::BAD_REQUEST |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | if (!static::is_token_valid($secret_key, $token)) { |
| 79 | throw new RuntimeException( |
| 80 | esc_html__('Google reCAPTCHA verification failed', 'kirki'), |
| 81 | (int) Response::BAD_REQUEST |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Call Google's siteverify endpoint for the given token. |
| 88 | * |
| 89 | * @param string $secret_key The reCAPTCHA secret key. |
| 90 | * @param string $token The reCAPTCHA token. |
| 91 | * @return bool |
| 92 | */ |
| 93 | protected static function is_token_valid(string $secret_key, string $token) |
| 94 | { |
| 95 | $response = Http::as_form()->post( |
| 96 | static::VERIFY_URL, |
| 97 | [ |
| 98 | 'secret' => $secret_key, |
| 99 | 'response' => $token, |
| 100 | ] |
| 101 | ); |
| 102 | |
| 103 | if ($response->failed()) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return (bool) $response->json('success'); |
| 108 | } |
| 109 | } |
| 110 |