| @@ -1,124 +1,0 @@ | ||
| 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): void | |
| 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)) { | |
| 25 | - return; | |
| 26 | - } | |
| 27 | - | |
| 28 | - if ($captchaPluginName === 'acym_ireCaptcha') { | |
| 29 | - $jsScript = 'https://www.google.com/recaptcha/api.js?render=explicit&hl='.acym_getLanguageTag(true); | |
| 30 | - } else { | |
| 31 | - $jsScript = 'https://www.google.com/recaptcha/api.js?render='.esc_attr($pubkey); | |
| 32 | - } | |
| 33 | - | |
| 34 | - if ($loadJsModule) { | |
| 35 | - // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Option to include the JS inside the module. | |
| 36 | - echo '<script src="'.esc_url($jsScript).'" type="text/javascript" defer async></script>'; | |
| 37 | - } else { | |
| 38 | - acym_addScript(false, $jsScript, ['defer' => true, 'async' => true]); | |
| 39 | - } | |
| 40 | - | |
| 41 | - echo '<div id="'.esc_attr($id).'" | |
| 42 | - data-size="invisible" | |
| 43 | - class="acyg-recaptcha" | |
| 44 | - data-sitekey="'.esc_attr($pubkey).'" | |
| 45 | - data-captchaname="'.esc_attr($captchaPluginName).'"></div>'; | |
| 46 | - } elseif ($captchaPluginName === 'acym_hcaptcha') { | |
| 47 | - $siteKey = $this->config->get('hcaptcha_sitekey'); | |
| 48 | - echo '<div class="h-captcha" data-sitekey="'.esc_attr($siteKey).'"></div>'; | |
| 49 | - $jsScript = 'https://js.hcaptcha.com/1/api.js'; | |
| 50 | - | |
| 51 | - if ($loadJsModule) { | |
| 52 | - // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Option to include the JS inside the module. | |
| 53 | - echo '<script src="'.esc_url($jsScript).'" type="text/javascript" defer async></script>'; | |
| 54 | - } else { | |
| 55 | - acym_addScript(false, $jsScript, ['defer' => true, 'async' => true]); | |
| 56 | - } | |
| 57 | - } else { | |
| 58 | - acym_loadCaptcha($captchaPluginName, $id); | |
| 59 | - } | |
| 60 | - } | |
| 61 | - | |
| 62 | - public function check(): bool | |
| 63 | - { | |
| 64 | - if (!acym_level(ACYM_ESSENTIAL)) { | |
| 65 | - return true; | |
| 66 | - } | |
| 67 | - | |
| 68 | - $captchaPluginName = $this->config->get('captcha', 'none'); | |
| 69 | - if ($captchaPluginName === 'none') { | |
| 70 | - return true; | |
| 71 | - } | |
| 72 | - | |
| 73 | - // The security key can be used for direct subscription links | |
| 74 | - $secKey = acym_getVar('string', 'seckey', 'none'); | |
| 75 | - $securityKey = $this->config->get('security_key'); | |
| 76 | - if (!empty($securityKey) && hash_equals((string)$securityKey, (string)$secKey)) { | |
| 77 | - return true; | |
| 78 | - } | |
| 79 | - | |
| 80 | - if (in_array($captchaPluginName, ['acym_ireCaptcha', 'acym_reCaptcha_v3'])) { | |
| 81 | - $privatekey = $this->config->get('recaptcha_secretkey', ''); | |
| 82 | - $response = acym_getVar('string', 'g-recaptcha-response', ''); | |
| 83 | - $remoteip = acym_getVar('string', 'REMOTE_ADDR', '', 'SERVER'); | |
| 84 | - if (empty($privatekey) || $response === '' || empty($remoteip)) { | |
| 85 | - return false; | |
| 86 | - } | |
| 87 | - | |
| 88 | - $url = 'https://www.google.com/recaptcha/api/siteverify?secret='.urlencode(stripslashes($privatekey)); | |
| 89 | - $url .= '&remoteip='.urlencode(stripslashes($remoteip)); | |
| 90 | - $url .= '&response='.urlencode(stripslashes($response)); | |
| 91 | - $getResponse = acym_fileGetContent($url); | |
| 92 | - | |
| 93 | - $answers = json_decode($getResponse, true); | |
| 94 | - | |
| 95 | - if (!is_array($answers) || empty($answers['success'])) { | |
| 96 | - return false; | |
| 97 | - } | |
| 98 | - | |
| 99 | - if ($captchaPluginName === 'acym_ireCaptcha') { | |
| 100 | - return trim($answers['success']) !== ''; | |
| 101 | - } else { | |
| 102 | - $score = $this->config->get('recaptcha_score', 0.5); | |
| 103 | - | |
| 104 | - return trim($answers['success']) == true && $answers['score'] >= $score; | |
| 105 | - } | |
| 106 | - } elseif ($captchaPluginName === 'acym_hcaptcha') { | |
| 107 | - $data = [ | |
| 108 | - 'secret' => $this->config->get('hcaptcha_secretkey'), | |
| 109 | - 'response' => acym_getVar('string', 'h-captcha-response'), | |
| 110 | - ]; | |
| 111 | - | |
| 112 | - $responseData = acym_makeCurlCall( | |
| 113 | - 'https://hcaptcha.com/siteverify', | |
| 114 | - ['data' => $data, 'method' => 'POST'] | |
| 115 | - ); | |
| 116 | - | |
| 117 | - return !empty($responseData['success']); | |
| 118 | - } else { | |
| 119 | - $response = acym_getVar('string', 'acym-captcha', ''); | |
| 120 | - | |
| 121 | - return acym_checkCaptcha($captchaPluginName, $response); | |
| 122 | - } | |
| 123 | - } | |
| 124 | -} | |