| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
use AcyMailing\Core\AcymObject; |
| 6 |
|
| 7 |
class CaptchaHelper extends AcymObject |
| 8 |
{ |
| 9 |
public function display(string $formName = '', bool $loadJsModule = false): string |
| 10 |
{ |
| 11 |
if (!acym_level(ACYM_ESSENTIAL)) { |
| 12 |
return ''; |
| 13 |
} |
| 14 |
|
| 15 |
$captchaPluginName = $this->config->get('captcha', 'none'); |
| 16 |
if ($captchaPluginName === 'none') { |
| 17 |
return ''; |
| 18 |
} |
| 19 |
|
| 20 |
$id = empty($formName) ? 'acym-captcha' : $formName.'-captcha'; |
| 21 |
|
| 22 |
if (in_array($captchaPluginName, ['acym_ireCaptcha', 'acym_reCaptcha_v3'])) { |
| 23 |
$pubkey = $this->config->get('recaptcha_sitekey', ''); |
| 24 |
if (empty($pubkey)) return ''; |
| 25 |
|
| 26 |
$return = ''; |
| 27 |
if ($captchaPluginName === 'acym_ireCaptcha') { |
| 28 |
$jsScript = 'https://www.google.com/recaptcha/api.js?render=explicit&hl='.acym_getLanguageTag(true); |
| 29 |
} else { |
| 30 |
$jsScript = 'https://www.google.com/recaptcha/api.js?render='.acym_escape($pubkey); |
| 31 |
} |
| 32 |
if ($loadJsModule) { |
| 33 |
$return .= '<script src="'.$jsScript.'" type="text/javascript" defer async></script>'; |
| 34 |
} else { |
| 35 |
acym_addScript(false, $jsScript, ['defer' => true, 'async' => true]); |
| 36 |
} |
| 37 |
|
| 38 |
return $return.'<div id="'.acym_escape($id).'" data-size="invisible" class="acyg-recaptcha" data-sitekey="'.acym_escape($pubkey).'"data-captchaname="'.acym_escape( |
| 39 |
$captchaPluginName |
| 40 |
).'"></div>'; |
| 41 |
} elseif ($captchaPluginName === 'acym_hcaptcha') { |
| 42 |
$siteKey = $this->config->get('hcaptcha_sitekey'); |
| 43 |
$return = '<div class="h-captcha" data-sitekey="'.$siteKey.'"></div>'; |
| 44 |
$jsScript = 'https://js.hcaptcha.com/1/api.js'; |
| 45 |
|
| 46 |
if ($loadJsModule) { |
| 47 |
$return .= '<script src="'.$jsScript.'" type="text/javascript" defer async></script>'; |
| 48 |
} else { |
| 49 |
acym_addScript(false, $jsScript, ['defer' => true, 'async' => true]); |
| 50 |
} |
| 51 |
|
| 52 |
return $return; |
| 53 |
} else { |
| 54 |
return acym_loadCaptcha($captchaPluginName, $id); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public function check(): bool |
| 59 |
{ |
| 60 |
if (!acym_level(ACYM_ESSENTIAL)) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
$captchaPluginName = $this->config->get('captcha', 'none'); |
| 65 |
if ($captchaPluginName === 'none') { |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
$secKey = acym_getVar('string', 'seckey', 'none'); |
| 70 |
if ($secKey === $this->config->get('security_key')) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
if (in_array($captchaPluginName, ['acym_ireCaptcha', 'acym_reCaptcha_v3'])) { |
| 75 |
$privatekey = $this->config->get('recaptcha_secretkey', ''); |
| 76 |
$response = acym_getVar('string', 'g-recaptcha-response', ''); |
| 77 |
$remoteip = acym_getVar('string', 'REMOTE_ADDR', '', 'SERVER'); |
| 78 |
if (empty($privatekey) || $response === '' || empty($remoteip)) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.urlencode(stripslashes($privatekey)); |
| 83 |
$url .= '&remoteip='.urlencode(stripslashes($remoteip)); |
| 84 |
$url .= '&response='.urlencode(stripslashes($response)); |
| 85 |
$getResponse = acym_fileGetContent($url); |
| 86 |
|
| 87 |
$answers = json_decode($getResponse, true); |
| 88 |
|
| 89 |
if (!is_array($answers) || empty($answers['success'])) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if ($captchaPluginName === 'acym_ireCaptcha') { |
| 94 |
return trim($answers['success']) !== ''; |
| 95 |
} else { |
| 96 |
$score = $this->config->get('recaptcha_score', 0.5); |
| 97 |
|
| 98 |
return trim($answers['success']) == true && $answers['score'] >= $score; |
| 99 |
} |
| 100 |
} elseif ($captchaPluginName === 'acym_hcaptcha') { |
| 101 |
$data = [ |
| 102 |
'secret' => $this->config->get('hcaptcha_secretkey'), |
| 103 |
'response' => $_REQUEST['h-captcha-response'], |
| 104 |
]; |
| 105 |
|
| 106 |
$responseData = acym_makeCurlCall( |
| 107 |
'https://hcaptcha.com/siteverify', |
| 108 |
['data' => $data, 'method' => 'POST'] |
| 109 |
); |
| 110 |
|
| 111 |
return !empty($responseData['success']); |
| 112 |
} else { |
| 113 |
$response = acym_getVar('string', 'acym-captcha', ''); |
| 114 |
|
| 115 |
return acym_checkCaptcha($captchaPluginName, $response); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|