| @@ -1,57 +1,38 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * reCAPTCHA v2 Validation | |
| 4 | - * | |
| 5 | - * Extends AccuaForm_Validation_CaptchaSpam: a failed verification follows the | |
| 6 | - * per-form spam action (reject with a visible error, or accept silently and | |
| 7 | - * flag the submission as spam — see the base class for the contract). | |
| 8 | - * | |
| 9 | - * @package Contact Forms | |
| 10 | - */ | |
| 2 | +class AccuaForm_Validation_Captcha2b extends Validation { | |
| 3 | + protected $message = "Error: The reCATPCHA response provided was incorrect. Please re-try."; | |
| 4 | + protected $privateKey; | |
| 5 | + | |
| 6 | + public function isValid($value) { | |
| 7 | + if (!isset($_POST['g-recaptcha-response'])) { | |
| 8 | + return false; | |
| 9 | + } | |
| 11 | 10 | |
| 12 | -// phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Server-side CAPTCHA validation requires POST data | |
| 11 | + $response = stripslashes_deep($_POST['g-recaptcha-response']); | |
| 13 | 12 | |
| 14 | -class AccuaForm_Validation_Captcha2b extends AccuaForm_Validation_CaptchaSpam { | |
| 15 | - protected $message = 'Error: The reCAPTCHA response provided was incorrect. Please retry.'; | |
| 13 | + if ($response === '') { | |
| 14 | + return false; | |
| 15 | + } | |
| 16 | 16 | |
| 17 | - public function isValid( $value ) { | |
| 18 | - if ( ! isset( $_POST['g-recaptcha-response'] ) ) { | |
| 19 | - return $this->failed(); | |
| 20 | - } | |
| 17 | + $url = 'https://www.google.com/recaptcha/api/siteverify' | |
| 18 | + .'?secret='.urlencode($this->privateKey) | |
| 19 | + .'&response='.urlencode($response) | |
| 20 | + .'&remoteip='.urlencode($_SERVER["REMOTE_ADDR"]); | |
| 21 | 21 | |
| 22 | - $response = stripslashes_deep( $_POST['g-recaptcha-response'] ); | |
| 22 | + $ch = curl_init(); | |
| 23 | + curl_setopt($ch, CURLOPT_URL, $url); | |
| 24 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| 25 | + curl_setopt($ch, CURLOPT_TIMEOUT, 20); | |
| 26 | + $res = curl_exec($ch); | |
| 27 | + curl_close($ch); | |
| 23 | 28 | |
| 24 | - if ( '' === $response ) { | |
| 25 | - return $this->failed(); | |
| 26 | - } | |
| 29 | + if ($res !== false) { | |
| 30 | + @ $res = json_decode($res, true); | |
| 31 | + if (!empty($res['success'])) { | |
| 32 | + return true; | |
| 33 | + } | |
| 34 | + } | |
| 27 | 35 | |
| 28 | - $verify_url = 'https://www.google.com/recaptcha/api/siteverify'; | |
| 29 | - | |
| 30 | - $verify_data = array( | |
| 31 | - 'secret' => $this->privateKey, | |
| 32 | - 'response' => $response, | |
| 33 | - 'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', | |
| 34 | - ); | |
| 35 | - | |
| 36 | - $response_obj = wp_remote_post( | |
| 37 | - $verify_url, | |
| 38 | - array( | |
| 39 | - 'body' => $verify_data, | |
| 40 | - 'timeout' => 20, | |
| 41 | - ) | |
| 42 | - ); | |
| 43 | - | |
| 44 | - if ( is_wp_error( $response_obj ) ) { | |
| 45 | - return $this->failed(); | |
| 46 | - } | |
| 47 | - | |
| 48 | - $body = wp_remote_retrieve_body( $response_obj ); | |
| 49 | - $result = json_decode( $body, true ); | |
| 50 | - | |
| 51 | - if ( ! empty( $result['success'] ) ) { | |
| 52 | - return true; | |
| 53 | - } | |
| 54 | - | |
| 55 | - return $this->failed(); | |
| 36 | + return false; | |
| 56 | 37 | } |
| 57 | 38 | } |