| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 |
if (!headers_sent()) { |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
die("Protected By WebTotem!"); |
| 8 |
} |
| 9 |
/** |
| 10 |
* WebTotem reCaptcha class for Wordpress. |
| 11 |
*/ |
| 12 |
class WebTotemCaptcha{ |
| 13 |
|
| 14 |
const WTOTEM_RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Returns whether or not the authentication CAPTCHA is enabled. |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
public static function isEnabled() { |
| 22 |
$site_key = self::_siteKey(); |
| 23 |
$secret = self::_secret(); |
| 24 |
$recaptcha = WebTotemOption::getPluginSettings('recaptcha'); |
| 25 |
|
| 26 |
return $recaptcha && !empty($site_key) && !empty($secret); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns CAPTCHA site key. |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public static function _siteKey() { |
| 35 |
return WebTotemOption::getPluginSettings('recaptcha_v3_site_key'); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Returns CAPTCHA secret code. |
| 40 |
* |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
public static function _secret() { |
| 44 |
return WebTotemOption::getPluginSettings('recaptcha_v3_secret'); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Queries the reCAPTCHA endpoint with the given token, verifies the action matches, and returns the corresponding |
| 49 |
* score. If validation fails, false is returned. Any other failure (e.g., mangled response or connection dropped) returns 0.0. |
| 50 |
* |
| 51 |
* @param string $token |
| 52 |
* @param string $secret |
| 53 |
* @param string $action |
| 54 |
* @param int $timeout |
| 55 |
* @return float|false |
| 56 |
*/ |
| 57 |
public static function score($token, $secret = false, $action = 'login', $timeout = 20) { |
| 58 |
try { |
| 59 |
$payload = array( |
| 60 |
'secret' => $secret, |
| 61 |
'response' => $token, |
| 62 |
'remoteip' => WebTotem::getUserIP(), |
| 63 |
); |
| 64 |
|
| 65 |
$response = wp_remote_post(self::WTOTEM_RECAPTCHA_ENDPOINT, |
| 66 |
array( |
| 67 |
'body' => $payload, |
| 68 |
'headers' => array( |
| 69 |
'Referer' => false, |
| 70 |
), |
| 71 |
'timeout' => $timeout, |
| 72 |
'blocking' => true, |
| 73 |
)); |
| 74 |
|
| 75 |
if (!is_wp_error($response)) { |
| 76 |
|
| 77 |
$jsonResponse = wp_remote_retrieve_body($response); |
| 78 |
$decoded = @json_decode($jsonResponse, true); |
| 79 |
|
| 80 |
if (is_array($decoded) && isset($decoded['success']) && isset($decoded['score']) && isset($decoded['action'])) { |
| 81 |
if ($decoded['success'] && $decoded['action'] == $action) { |
| 82 |
return (float) $decoded['score']; |
| 83 |
} |
| 84 |
return 0.0; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
catch (\Exception $e) { |
| 89 |
//Fall through |
| 90 |
} |
| 91 |
|
| 92 |
return 0.0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the captcha token provided with the current request |
| 97 |
* @param string $key if specified, override the default token parameter |
| 98 |
* @return string|null the captcha token, if present, null otherwise |
| 99 |
*/ |
| 100 |
public static function get_token($key = 'wtotem-recaptcha-token') { |
| 101 |
return (isset($_POST[$key]) && is_string($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : null); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|