├── .github └── FUNDING.yml ├── Block ├── Captcha.php ├── CaptchaNewsletter.php └── Checkout │ └── LayoutProcessor.php ├── Command └── Captcha.php ├── Helper ├── AbstractConfig.php └── Config │ ├── Backend.php │ ├── Frontend.php │ └── General.php ├── LICENSE ├── Model ├── AbstractCheckEnabledVerify.php ├── Area.php ├── Area │ ├── ConfigInterface.php │ ├── ConfigList.php │ └── ConfigListInterface.php ├── Captcha.php ├── CaptchaInterface.php ├── CheckEnabledVerify.php ├── CheckEnabledVerifyInterface.php ├── Config │ └── Source │ │ └── ScoreThreshold.php ├── Debug.php ├── LayoutSettings.php ├── Provider │ ├── AbstractFailure.php │ ├── Failure │ │ ├── AjaxResponseFailure.php │ │ ├── AuthenticationExceptionFailure.php │ │ ├── ObserverRedirectFailure.php │ │ ├── RedirectUrl │ │ │ ├── BeforeAuthUrlProvider.php │ │ │ ├── RefererProvider.php │ │ │ └── SimpleUrlProvider.php │ │ └── RedirectUrlInterface.php │ ├── FailureInterface.php │ ├── FailureMessages.php │ ├── TokenResponse │ │ ├── Ajax.php │ │ └── General.php │ └── TokenResponseInterface.php ├── ReCaptcha │ ├── RequestMethod │ │ └── CurlPost.php │ ├── RequestMethodInterface.php │ ├── RequestParameters.php │ ├── Response.php │ ├── Validators │ │ ├── Action.php │ │ ├── Host.php │ │ ├── Threshold.php │ │ ├── TimeoutSeconds.php │ │ ├── ValidatorInterface.php │ │ └── ValidatorList.php │ └── VerifyReCaptcha.php ├── ScoreThreshold │ ├── AbstractScoreThreshold.php │ ├── Backend │ │ ├── ForgotPassword.php │ │ └── Login.php │ └── Frontend │ │ ├── Contact.php │ │ ├── CustomerCreate.php │ │ ├── CustomerForgotPassword.php │ │ ├── CustomerLogin.php │ │ ├── Newsletter.php │ │ ├── ProductReview.php │ │ └── SendFriend.php ├── ScoreThresholdInterface.php └── Verify │ ├── Adminhtml │ ├── ForgotPassword.php │ └── Login.php │ ├── Contact.php │ ├── CustomerCreate.php │ ├── CustomerForgotPassword.php │ ├── CustomerLogin.php │ ├── Newsletter.php │ ├── ProductReview.php │ └── SendFriend.php ├── Observer └── Captcha.php ├── Plugin ├── Block │ ├── Account │ │ └── AuthenticationPopupPlugin.php │ └── ContactForm │ │ └── AddFormAdditionalInfoIfMissing.php └── DisableSubmit.php ├── README.md ├── composer.json ├── etc ├── acl.xml ├── adminhtml │ ├── di.xml │ ├── events.xml │ └── system.xml ├── config.xml ├── csp_whitelist.xml ├── di.xml ├── frontend │ ├── di.xml │ └── events.xml └── module.xml ├── registration.php ├── screenshots ├── admin_configuration.png └── lazy_load.jpg └── view ├── adminhtml └── layout │ ├── adminhtml_auth_forgotpassword.xml │ └── adminhtml_auth_login.xml ├── base ├── templates │ └── captcha.phtml └── web │ ├── css │ └── source │ │ └── _module.less │ ├── js │ ├── invisible-captcha.js │ └── model │ │ └── invisible-captcha.js │ └── template │ └── invisible-captcha.html └── frontend ├── layout ├── catalog_product_view.xml ├── checkout_index_index.xml ├── contact_index_index.xml ├── customer_account_create.xml ├── customer_account_forgotpassword.xml ├── customer_account_login.xml ├── default.xml └── sendfriend_product_send.xml └── templates └── captcha_newsletter.phtml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: hryvinskyi 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /Block/Captcha.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Block; 11 | 12 | use Hryvinskyi\Base\Helper\Json; 13 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 14 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 15 | use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings; 16 | use Magento\Framework\View\Element\Template; 17 | use Magento\Framework\View\Element\Template\Context; 18 | 19 | class Captcha extends Template 20 | { 21 | /** 22 | * @var int 23 | */ 24 | private static $widgetId = 0; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $widgetIdClass = ''; 30 | private $widgetScope = ''; 31 | 32 | /** 33 | * @var General 34 | */ 35 | private $generalConfig; 36 | 37 | /** 38 | * @var Frontend 39 | */ 40 | private $frontendConfig; 41 | 42 | /** 43 | * @var LayoutSettings 44 | */ 45 | private $layoutSettings; 46 | 47 | /** 48 | * Captcha constructor. 49 | * 50 | * @param Context $context 51 | * @param General $generalConfig 52 | * @param Frontend $frontendConfig 53 | * @param LayoutSettings $layoutSettings 54 | * @param array $data 55 | */ 56 | public function __construct( 57 | Context $context, 58 | General $generalConfig, 59 | Frontend $frontendConfig, 60 | LayoutSettings $layoutSettings, 61 | array $data = [] 62 | ) { 63 | 64 | parent::__construct($context, $data); 65 | 66 | $this->generalConfig = $generalConfig; 67 | $this->frontendConfig = $frontendConfig; 68 | $this->layoutSettings = $layoutSettings; 69 | } 70 | 71 | /** 72 | * Constructor 73 | */ 74 | public function _construct() 75 | { 76 | parent::_construct(); 77 | 78 | $this->widgetIdClass = 'invisible-captcha-container-' . ++self::$widgetId; 79 | $this->widgetScope = 'invisible-captcha-scope-' . ++self::$widgetId; 80 | } 81 | 82 | /** 83 | * @return string 84 | */ 85 | public function getWidgetId(): string 86 | { 87 | return $this->widgetIdClass; 88 | } 89 | 90 | /** 91 | * @return string 92 | */ 93 | public function getScope(): string 94 | { 95 | return $this->widgetScope; 96 | } 97 | 98 | /** 99 | * @inheritdoc 100 | */ 101 | public function getJsLayout() 102 | { 103 | $layout = Json::decode(parent::getJsLayout()); 104 | $layout['components'][$this->getScope()] = $layout['components']['invisible-captcha']; 105 | unset($layout['components']['invisible-captcha']); 106 | 107 | if ($this->frontendConfig->hasEnabled() && $this->isModuleOn()) { 108 | $layout['components'][$this->getScope()]['config'] = $this->layoutSettings->getCaptchaSettings(); 109 | } 110 | 111 | if ( 112 | (!$this->frontendConfig->hasEnabled() || !$this->isModuleOn()) 113 | && isset($layout['components'][$this->getScope()]) 114 | ) { 115 | unset($layout['components'][$this->getScope()]); 116 | } 117 | 118 | return Json::encode($layout); 119 | } 120 | 121 | /** 122 | * @return bool 123 | */ 124 | public function isModuleOn(): bool 125 | { 126 | return $this->generalConfig->hasEnabled(); 127 | } 128 | 129 | /** 130 | * @return bool 131 | */ 132 | public function isLazyLoad(): bool 133 | { 134 | return $this->generalConfig->isLazyLoad(); 135 | } 136 | 137 | /** 138 | * @return bool 139 | */ 140 | public function isHideBadge(): bool 141 | { 142 | return $this->generalConfig->isHideBadge(); 143 | } 144 | 145 | /** 146 | * @return string 147 | */ 148 | public function getHideBadgeText(): string 149 | { 150 | return $this->generalConfig->getHideBadgeText(); 151 | } 152 | 153 | /** 154 | * @return string 155 | */ 156 | public function toHtml() 157 | { 158 | if (!$this->isModuleOn()) { 159 | return ''; 160 | } 161 | return parent::toHtml(); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /Block/CaptchaNewsletter.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Block; 11 | 12 | use Hryvinskyi\Base\Helper\Json; 13 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 14 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 15 | use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings; 16 | use Magento\Framework\View\Element\Template; 17 | use Magento\Framework\View\Element\Template\Context; 18 | 19 | class CaptchaNewsletter extends Captcha 20 | { 21 | /** 22 | * @var General 23 | */ 24 | private $generalConfig; 25 | 26 | /** 27 | * @var Frontend 28 | */ 29 | private $frontendConfig; 30 | 31 | /** 32 | * @var LayoutSettings 33 | */ 34 | private $layoutSettings; 35 | 36 | /** 37 | * Captcha constructor. 38 | * 39 | * @param Context $context 40 | * @param General $generalConfig 41 | * @param Frontend $frontendConfig 42 | * @param LayoutSettings $layoutSettings 43 | * @param array $data 44 | */ 45 | public function __construct( 46 | Context $context, 47 | General $generalConfig, 48 | Frontend $frontendConfig, 49 | LayoutSettings $layoutSettings, 50 | array $data = [] 51 | ) { 52 | parent::__construct($context, $generalConfig, $frontendConfig, $layoutSettings, $data); 53 | 54 | $this->generalConfig = $generalConfig; 55 | $this->frontendConfig = $frontendConfig; 56 | $this->layoutSettings = $layoutSettings; 57 | } 58 | 59 | /** 60 | * @inheritdoc 61 | */ 62 | public function getJsLayout() 63 | { 64 | $layout = $this->jsLayout; 65 | 66 | if ($this->frontendConfig->hasEnabled() && $this->isModuleOn()) { 67 | $layout['components']['invisible-captcha-newsletter']['config'] = $this->layoutSettings->getCaptchaSettings(); 68 | } 69 | 70 | if ( 71 | (!$this->frontendConfig->hasEnabled() || !$this->isModuleOn()) 72 | && isset($layout['components']['invisible-captcha-newsletter']) 73 | ) { 74 | unset($layout['components']['invisible-captcha-newsletter']); 75 | } 76 | 77 | return Json::encode($layout); 78 | } 79 | 80 | /** 81 | * @return bool 82 | */ 83 | public function isModuleOn(): bool 84 | { 85 | return $this->generalConfig->hasEnabled() || $this->frontendConfig->hasEnabledNewsletter(); 86 | } 87 | 88 | /** 89 | * @return string 90 | */ 91 | public function toHtml() 92 | { 93 | if (!$this->isModuleOn()) { 94 | return ''; 95 | } 96 | 97 | return parent::toHtml(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Block/Checkout/LayoutProcessor.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Block\Checkout; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 13 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 14 | use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings; 15 | use Magento\Checkout\Block\Checkout\LayoutProcessorInterface; 16 | 17 | /** 18 | * Class LayoutProcessor 19 | */ 20 | class LayoutProcessor implements LayoutProcessorInterface 21 | { 22 | /** 23 | * @var LayoutSettings 24 | */ 25 | private $layoutSettings; 26 | 27 | /** 28 | * @var General 29 | */ 30 | private $generalConfig; 31 | 32 | /** 33 | * @var Frontend 34 | */ 35 | private $frontendConfig; 36 | 37 | /** 38 | * AuthenticationPopupPlugin constructor. 39 | * 40 | * @param LayoutSettings $layoutSettings 41 | * @param General $generalConfig 42 | * @param Frontend $frontendConfig 43 | */ 44 | public function __construct( 45 | LayoutSettings $layoutSettings, 46 | General $generalConfig, 47 | Frontend $frontendConfig 48 | ) { 49 | $this->layoutSettings = $layoutSettings; 50 | $this->generalConfig = $generalConfig; 51 | $this->frontendConfig = $frontendConfig; 52 | } 53 | 54 | /** 55 | * @inheritDoc 56 | */ 57 | public function process($layout) 58 | { 59 | $layout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] 60 | ['shippingAddress']['children']['customer-email']['children']['invisible-captcha']['config'] 61 | = $this->layoutSettings->getCaptchaSettings(); 62 | 63 | $layout['components']['checkout']['children']['authentication']['children']['invisible-captcha']['config'] 64 | = $this->layoutSettings->getCaptchaSettings(); 65 | 66 | if (!$this->generalConfig->hasEnabled() 67 | || !$this->frontendConfig->hasEnabled() 68 | || !$this->frontendConfig->hasEnabledCustomerLogin() 69 | ) { 70 | if (isset($layout['components']['checkout']['children']['authentication']['children']['invisible-captcha'])) { 71 | unset($layout['components']['checkout']['children']['authentication']['children']['invisible-captcha']); 72 | } 73 | 74 | if (isset($layout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] 75 | ['shippingAddress']['children']['customer-email']['children']['invisible-captcha'])) { 76 | unset($layout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] 77 | ['shippingAddress']['children']['customer-email']['children']['invisible-captcha']); 78 | } 79 | } 80 | 81 | return $layout; 82 | } 83 | } -------------------------------------------------------------------------------- /Command/Captcha.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Command; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Area; 13 | use Magento\Framework\App\Cache\Manager; 14 | use Magento\Framework\Exception\LocalizedException; 15 | use Magento\Store\Model\StoreManagerInterface; 16 | use Symfony\Component\Console\Command\Command; 17 | use Symfony\Component\Console\Input\InputArgument; 18 | use Symfony\Component\Console\Input\InputInterface; 19 | use Symfony\Component\Console\Input\InputOption; 20 | use Symfony\Component\Console\Output\OutputInterface; 21 | 22 | /** 23 | * Class Captcha 24 | */ 25 | class Captcha extends Command 26 | { 27 | /** 28 | * @var Manager 29 | */ 30 | private $cacheManager; 31 | 32 | /** 33 | * @var StoreManagerInterface 34 | */ 35 | private $storeManager; 36 | 37 | /** 38 | * @var Area 39 | */ 40 | private $area; 41 | 42 | /** 43 | * @var array 44 | */ 45 | private $areaConfigList; 46 | 47 | /** 48 | * ReCaptcha constructor. 49 | * 50 | * @param Manager $cacheManager 51 | * @param StoreManagerInterface $storeManager 52 | * @param Area $area 53 | * @param Area\ConfigList $areaConfigList 54 | */ 55 | public function __construct( 56 | Manager $cacheManager, 57 | StoreManagerInterface $storeManager, 58 | Area $area, 59 | Area\ConfigList $areaConfigList 60 | ) { 61 | $this->cacheManager = $cacheManager; 62 | $this->storeManager = $storeManager; 63 | $this->area = $area; 64 | $this->areaConfigList = $areaConfigList; 65 | 66 | parent::__construct(); 67 | } 68 | 69 | /** 70 | * @inheritdoc 71 | */ 72 | protected function configure() 73 | { 74 | $this->setName('hryvinskyi:invisible-captcha:disable') 75 | ->setDescription('Disable invisible captcha') 76 | ->addArgument( 77 | 'area', 78 | InputArgument::OPTIONAL, 79 | 'Area (' . implode('/', $this->area->getAllowedList()) . ')', 80 | Area::GLOBAL 81 | ) 82 | ->addOption( 83 | 'website_id', 84 | 'website_id', 85 | InputOption::VALUE_OPTIONAL, 86 | 'Website ID', 87 | null 88 | ); 89 | 90 | parent::configure(); 91 | } 92 | 93 | /** 94 | * {@inheritdoc} 95 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") 96 | * @throws LocalizedException 97 | */ 98 | protected function execute(InputInterface $input, OutputInterface $output) 99 | { 100 | $area = $input->getArgument('area'); 101 | $website = $input->getOption('website_id'); 102 | 103 | if ($website !== null && !isset($this->allWebsites()[$website])) { 104 | throw new LocalizedException(__('Website not found')); 105 | } 106 | 107 | // disable captcha 108 | $this->areaConfigList->getConfig($area)->disableCaptcha($area === Area::BACKEND ? null : $website); 109 | 110 | // flush cache 111 | $this->cacheManager->flush(['config']); 112 | } 113 | 114 | /** 115 | * Return all websites 116 | * 117 | * @return array 118 | */ 119 | private function allWebsites(): array 120 | { 121 | return $this->storeManager->getWebsites(); 122 | } 123 | } -------------------------------------------------------------------------------- /Helper/AbstractConfig.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Helper; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Area\ConfigInterface as AreaConfigInterface; 13 | use Magento\Framework\App\Config\ConfigResource\ConfigInterface; 14 | use Magento\Framework\App\Config\ScopeConfigInterface; 15 | use Magento\Framework\App\Helper\AbstractHelper; 16 | use Magento\Framework\App\Helper\Context; 17 | use Magento\Framework\App\ObjectManager; 18 | use Magento\Store\Model\ScopeInterface; 19 | 20 | /** 21 | * Class AbstractConfig 22 | */ 23 | abstract class AbstractConfig extends AbstractHelper implements AreaConfigInterface 24 | { 25 | /** 26 | * @var ConfigInterface 27 | */ 28 | private $config; 29 | 30 | /** 31 | * General constructor. 32 | * 33 | * @param Context $context 34 | * @param ConfigInterface $config 35 | */ 36 | public function __construct( 37 | Context $context, 38 | ConfigInterface $config = null 39 | ) { 40 | $this->config = $config ?: ObjectManager::getInstance()->get(ConfigInterface::class); 41 | 42 | parent::__construct($context); 43 | } 44 | 45 | /** 46 | * Is captcha enable 47 | * 48 | * @param string $scopeType 49 | * @param mixed $scopeCode 50 | * 51 | * @return bool 52 | */ 53 | abstract public function hasEnabled( 54 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 55 | $scopeCode = null 56 | ): bool; 57 | 58 | /** 59 | * Disable captcha by area 60 | * 61 | * @param null|string $website 62 | */ 63 | public function disableCaptcha(string $website = null): void 64 | { 65 | if (!$this->hasEnabled(ScopeInterface::SCOPE_WEBSITES, $website)) { 66 | return; 67 | } 68 | 69 | $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT; 70 | $scopeId = 0; 71 | 72 | if ($website !== null) { 73 | $scope = ScopeInterface::SCOPE_WEBSITES; 74 | $scopeId = $website; 75 | } 76 | 77 | $this->config->saveConfig( 78 | $this->disableConfigPath(), 79 | 0, 80 | $scope, 81 | $scopeId 82 | ); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Helper/Config/Backend.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Helper\Config; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\AbstractConfig; 13 | use Magento\Store\Model\ScopeInterface; 14 | 15 | /** 16 | * Class Backend 17 | */ 18 | class Backend extends AbstractConfig 19 | { 20 | /** 21 | * Configuration path 22 | */ 23 | const CONFIG_PATH_BACKEND_ENABLED = 'hryvinskyi_invisible_captcha/backend/enabled'; 24 | const CONFIG_PATH_BACKEND_ENABLED_LOGIN = 'hryvinskyi_invisible_captcha/backend/enabledLogin'; 25 | const CONFIG_PATH_BACKEND_SCORE_THRESHOLD_LOGIN = 'hryvinskyi_invisible_captcha/backend/scoreThresholdLogin'; 26 | const CONFIG_PATH_BACKEND_ENABLED_FORGOT = 'hryvinskyi_invisible_captcha/backend/enabledForgot'; 27 | const CONFIG_PATH_BACKEND_SCORE_THRESHOLD_FORGOT = 'hryvinskyi_invisible_captcha/backend/scoreThresholdForgot'; 28 | 29 | /** 30 | * Is google recaptcha enable backend 31 | * 32 | * @param string $scopeType 33 | * @param mixed $scopeCode 34 | * 35 | * @return bool 36 | */ 37 | public function hasEnabled( 38 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 39 | $scopeCode = null 40 | ): bool { 41 | return $this->scopeConfig->isSetFlag( 42 | self::CONFIG_PATH_BACKEND_ENABLED, 43 | $scopeType, 44 | $scopeCode 45 | ); 46 | } 47 | 48 | /** 49 | * Is google recaptcha enable login 50 | * 51 | * @param string $scopeType 52 | * @param mixed $scopeCode 53 | * 54 | * @return bool 55 | */ 56 | public function hasEnabledLogin( 57 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 58 | $scopeCode = null 59 | ): bool { 60 | return $this->scopeConfig->isSetFlag( 61 | self::CONFIG_PATH_BACKEND_ENABLED_LOGIN, 62 | $scopeType, 63 | $scopeCode 64 | ); 65 | } 66 | 67 | /** 68 | * Get google recaptcha score threshold login 69 | * 70 | * @param string $scopeType 71 | * @param mixed $scopeCode 72 | * 73 | * @return float 74 | */ 75 | public function getScoreThresholdLogin( 76 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 77 | $scopeCode = null 78 | ): float { 79 | return (float)$this->scopeConfig->getValue( 80 | self::CONFIG_PATH_BACKEND_SCORE_THRESHOLD_LOGIN, 81 | $scopeType, 82 | $scopeCode 83 | ); 84 | } 85 | 86 | /** 87 | * Is google recaptcha enable forgot 88 | * 89 | * @param string $scopeType 90 | * @param mixed $scopeCode 91 | * 92 | * @return bool 93 | */ 94 | public function hasEnabledForgot( 95 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 96 | $scopeCode = null 97 | ): bool { 98 | return $this->scopeConfig->isSetFlag( 99 | self::CONFIG_PATH_BACKEND_ENABLED_FORGOT, 100 | $scopeType, 101 | $scopeCode 102 | ); 103 | } 104 | 105 | /** 106 | * Get google recaptcha score threshold forgot 107 | * 108 | * @param string $scopeType 109 | * @param mixed $scopeCode 110 | * 111 | * @return float 112 | */ 113 | public function getScoreThresholdForgot( 114 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 115 | $scopeCode = null 116 | ): float { 117 | return (float)$this->scopeConfig->getValue( 118 | self::CONFIG_PATH_BACKEND_SCORE_THRESHOLD_FORGOT, 119 | $scopeType, 120 | $scopeCode 121 | ); 122 | } 123 | 124 | /** 125 | * Disable config path 126 | * 127 | * @return string 128 | */ 129 | public function disableConfigPath(): string 130 | { 131 | return self::CONFIG_PATH_BACKEND_ENABLED; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /Helper/Config/Frontend.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Helper\Config; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\AbstractConfig; 13 | use Magento\Store\Model\ScopeInterface; 14 | 15 | /** 16 | * Class Frontend 17 | */ 18 | class Frontend extends AbstractConfig 19 | { 20 | /** 21 | * Configuration path 22 | */ 23 | const CONFIG_PATH_FRONTEND_ENABLED = 'hryvinskyi_invisible_captcha/frontend/enabled'; 24 | const CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_LOGIN = 'hryvinskyi_invisible_captcha/frontend/enabledCustomerLogin'; 25 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_LOGIN = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdCustomerLogin'; 26 | const CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_CREATE = 'hryvinskyi_invisible_captcha/frontend/enabledCustomerCreate'; 27 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_CREATE = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdCustomerCreate'; 28 | const CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_FORGOT = 'hryvinskyi_invisible_captcha/frontend/enabledCustomerForgot'; 29 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_FORGOT = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdCustomerForgot'; 30 | const CONFIG_PATH_FRONTEND_ENABLED_CONTACT = 'hryvinskyi_invisible_captcha/frontend/enabledContact'; 31 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CONTACT = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdContact'; 32 | const CONFIG_PATH_FRONTEND_ENABLED_NEWSLETTER = 'hryvinskyi_invisible_captcha/frontend/enabledNewsletter'; 33 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_NEWSLETTER = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdNewsletter'; 34 | const CONFIG_PATH_FRONTEND_ENABLED_SENDFRIEND = 'hryvinskyi_invisible_captcha/frontend/enabledSendFriend'; 35 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_SENDFRIEND = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdSendFriend'; 36 | const CONFIG_PATH_FRONTEND_ENABLED_PRODUCT_REVIEW = 'hryvinskyi_invisible_captcha/frontend/enabledProductReview'; 37 | const CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_PRODUCT_REVIEW = 'hryvinskyi_invisible_captcha/frontend/scoreThresholdProductReview'; 38 | 39 | /** 40 | * Is google recaptcha enable global 41 | * 42 | * @param string $scopeType 43 | * @param mixed $scopeCode 44 | * 45 | * @return bool 46 | */ 47 | public function hasEnabled( 48 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 49 | $scopeCode = null 50 | ): bool { 51 | return $this->scopeConfig->isSetFlag( 52 | self::CONFIG_PATH_FRONTEND_ENABLED, 53 | $scopeType, 54 | $scopeCode 55 | ); 56 | } 57 | 58 | /** 59 | * Is google recaptcha enable customer login 60 | * 61 | * @param string $scopeType 62 | * @param mixed $scopeCode 63 | * 64 | * @return bool 65 | */ 66 | public function hasEnabledCustomerLogin( 67 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 68 | $scopeCode = null 69 | ): bool { 70 | return $this->scopeConfig->isSetFlag( 71 | self::CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_LOGIN, 72 | $scopeType, 73 | $scopeCode 74 | ); 75 | } 76 | 77 | /** 78 | * Get google recaptcha score threshold login 79 | * 80 | * @param string $scopeType 81 | * @param mixed $scopeCode 82 | * 83 | * @return float 84 | */ 85 | public function getScoreThresholdCustomerLogin( 86 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 87 | $scopeCode = null 88 | ): float { 89 | return (float)$this->scopeConfig->getValue( 90 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_LOGIN, 91 | $scopeType, 92 | $scopeCode 93 | ); 94 | } 95 | 96 | /** 97 | * Is google recaptcha enable customer create 98 | * 99 | * @param string $scopeType 100 | * @param mixed $scopeCode 101 | * 102 | * @return bool 103 | */ 104 | public function hasEnabledCustomerCreate( 105 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 106 | $scopeCode = null 107 | ): bool { 108 | return $this->scopeConfig->isSetFlag( 109 | self::CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_CREATE, 110 | $scopeType, 111 | $scopeCode 112 | ); 113 | } 114 | 115 | /** 116 | * Get google recaptcha score threshold customer create 117 | * 118 | * @param string $scopeType 119 | * @param mixed $scopeCode 120 | * 121 | * @return float 122 | */ 123 | public function getScoreThresholdCustomerCreate( 124 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 125 | $scopeCode = null 126 | ): float { 127 | return (float)$this->scopeConfig->getValue( 128 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_CREATE, 129 | $scopeType, 130 | $scopeCode 131 | ); 132 | } 133 | 134 | /** 135 | * Is google recaptcha enable customer forgot 136 | * 137 | * @param string $scopeType 138 | * @param mixed $scopeCode 139 | * 140 | * @return bool 141 | */ 142 | public function hasEnabledCustomerForgot( 143 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 144 | $scopeCode = null 145 | ): bool { 146 | return $this->scopeConfig->isSetFlag( 147 | self::CONFIG_PATH_FRONTEND_ENABLED_CUSTOMER_FORGOT, 148 | $scopeType, 149 | $scopeCode 150 | ); 151 | } 152 | 153 | /** 154 | * Get google recaptcha score threshold customer forgot 155 | * 156 | * @param string $scopeType 157 | * @param mixed $scopeCode 158 | * 159 | * @return float 160 | */ 161 | public function getScoreThresholdCustomerForgot( 162 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 163 | $scopeCode = null 164 | ): float { 165 | return (float)$this->scopeConfig->getValue( 166 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CUSTOMER_FORGOT, 167 | $scopeType, 168 | $scopeCode 169 | ); 170 | } 171 | 172 | /** 173 | * Is google recaptcha enable contact 174 | * 175 | * @param string $scopeType 176 | * @param mixed $scopeCode 177 | * 178 | * @return bool 179 | */ 180 | public function hasEnabledContact( 181 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 182 | $scopeCode = null 183 | ): bool { 184 | return $this->scopeConfig->isSetFlag( 185 | self::CONFIG_PATH_FRONTEND_ENABLED_CONTACT, 186 | $scopeType, 187 | $scopeCode 188 | ); 189 | } 190 | 191 | /** 192 | * Get google recaptcha score threshold contact 193 | * 194 | * @param string $scopeType 195 | * @param mixed $scopeCode 196 | * 197 | * @return float 198 | */ 199 | public function getScoreThresholdContact( 200 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 201 | $scopeCode = null 202 | ): float { 203 | return (float)$this->scopeConfig->getValue( 204 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_CONTACT, 205 | $scopeType, 206 | $scopeCode 207 | ); 208 | } 209 | 210 | /** 211 | * Is google recaptcha enable newsletter 212 | * 213 | * @param string $scopeType 214 | * @param mixed $scopeCode 215 | * 216 | * @return bool 217 | */ 218 | public function hasEnabledNewsletter( 219 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 220 | $scopeCode = null 221 | ): bool { 222 | return $this->scopeConfig->isSetFlag( 223 | self::CONFIG_PATH_FRONTEND_ENABLED_NEWSLETTER, 224 | $scopeType, 225 | $scopeCode 226 | ); 227 | } 228 | 229 | /** 230 | * Get google recaptcha score threshold newsletter 231 | * 232 | * @param string $scopeType 233 | * @param mixed $scopeCode 234 | * 235 | * @return float 236 | */ 237 | public function getScoreThresholdNewsletter( 238 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 239 | $scopeCode = null 240 | ): float { 241 | return (float)$this->scopeConfig->getValue( 242 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_NEWSLETTER, 243 | $scopeType, 244 | $scopeCode 245 | ); 246 | } 247 | 248 | /** 249 | * Is google recaptcha enable send to friend 250 | * 251 | * @param string $scopeType 252 | * @param mixed $scopeCode 253 | * 254 | * @return bool 255 | */ 256 | public function hasEnabledSendFriend( 257 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 258 | $scopeCode = null 259 | ): bool { 260 | return $this->scopeConfig->isSetFlag( 261 | self::CONFIG_PATH_FRONTEND_ENABLED_SENDFRIEND, 262 | $scopeType, 263 | $scopeCode 264 | ); 265 | } 266 | 267 | /** 268 | * Get google recaptcha score threshold send to friend 269 | * 270 | * @param string $scopeType 271 | * @param mixed $scopeCode 272 | * 273 | * @return float 274 | */ 275 | public function getScoreThresholdSendFriend( 276 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 277 | $scopeCode = null 278 | ): float { 279 | return (float)$this->scopeConfig->getValue( 280 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_SENDFRIEND, 281 | $scopeType, 282 | $scopeCode 283 | ); 284 | } 285 | 286 | /** 287 | * Is google recaptcha enable product review 288 | * 289 | * @param string $scopeType 290 | * @param mixed $scopeCode 291 | * 292 | * @return bool 293 | */ 294 | public function hasEnabledProductReview( 295 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 296 | $scopeCode = null 297 | ): bool { 298 | return $this->scopeConfig->isSetFlag( 299 | self::CONFIG_PATH_FRONTEND_ENABLED_PRODUCT_REVIEW, 300 | $scopeType, 301 | $scopeCode 302 | ); 303 | } 304 | 305 | /** 306 | * Get google recaptcha score threshold product review 307 | * 308 | * @param string $scopeType 309 | * @param mixed $scopeCode 310 | * 311 | * @return float 312 | */ 313 | public function getScoreThresholdProductReview( 314 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 315 | $scopeCode = null 316 | ): float { 317 | return (float)$this->scopeConfig->getValue( 318 | self::CONFIG_PATH_FRONTEND_SCORE_THRESHOLD_PRODUCT_REVIEW, 319 | $scopeType, 320 | $scopeCode 321 | ); 322 | } 323 | 324 | /** 325 | * Disable config path 326 | * 327 | * @return string 328 | */ 329 | public function disableConfigPath(): string 330 | { 331 | return self::CONFIG_PATH_FRONTEND_ENABLED; 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /Helper/Config/General.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Helper\Config; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\AbstractConfig; 13 | use Magento\Store\Model\ScopeInterface; 14 | 15 | /** 16 | * Class General 17 | */ 18 | class General extends AbstractConfig 19 | { 20 | /** 21 | * Configuration path 22 | */ 23 | const CONFIG_PATH_GENERAL_ENABLE_MODULE = 'hryvinskyi_invisible_captcha/general/enabledCaptcha'; 24 | const CONFIG_PATH_GENERAL_SITE_KEY = 'hryvinskyi_invisible_captcha/general/captchaSiteKey'; 25 | const CONFIG_PATH_GENERAL_SECRET_KEY = 'hryvinskyi_invisible_captcha/general/captchaSecretKey'; 26 | const CONFIG_PATH_GENERAL_USE_LAZY_LOAD = 'hryvinskyi_invisible_captcha/general/useLazyLoad'; 27 | const CONFIG_PATH_GENERAL_DISABLE_SUBMIT_FORM = 'hryvinskyi_invisible_captcha/general/disableSubmitForm'; 28 | const CONFIG_PATH_GENERAL_HIDE_BADGE = 'hryvinskyi_invisible_captcha/general/hideBadge'; 29 | const CONFIG_PATH_GENERAL_HIDE_BADGE_TEXT = 'hryvinskyi_invisible_captcha/general/hideBadgeText'; 30 | const CONFIG_PATH_GENERAL_DEBUG = 'hryvinskyi_invisible_captcha/general/debug'; 31 | 32 | /** 33 | * Is google recaptcha enable global 34 | * 35 | * @param string $scopeType 36 | * @param mixed $scopeCode 37 | * 38 | * @return bool 39 | */ 40 | public function hasEnabled( 41 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 42 | $scopeCode = null 43 | ): bool { 44 | return $this->scopeConfig->isSetFlag( 45 | self::CONFIG_PATH_GENERAL_ENABLE_MODULE, 46 | $scopeType, 47 | $scopeCode 48 | ); 49 | } 50 | 51 | /** 52 | * Return Captcha Key 53 | * 54 | * @param string $scopeType 55 | * @param mixed $scopeCode 56 | * 57 | * @return string 58 | */ 59 | public function getSiteKey( 60 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 61 | $scopeCode = null 62 | ): string { 63 | return $this->scopeConfig->getValue( 64 | self::CONFIG_PATH_GENERAL_SITE_KEY, 65 | $scopeType, 66 | $scopeCode 67 | ); 68 | } 69 | 70 | /** 71 | * Return Secret Key 72 | * 73 | * @param string $scopeType 74 | * @param mixed $scopeCode 75 | * 76 | * @return string 77 | */ 78 | public function getSecretKey( 79 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 80 | $scopeCode = null 81 | ): string { 82 | return $this->scopeConfig->getValue( 83 | self::CONFIG_PATH_GENERAL_SECRET_KEY, 84 | $scopeType, 85 | $scopeCode 86 | ); 87 | } 88 | 89 | /** 90 | * Is google recaptcha load lazy 91 | * 92 | * @param string $scopeType 93 | * @param mixed $scopeCode 94 | * 95 | * @return bool 96 | */ 97 | public function isLazyLoad( 98 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 99 | $scopeCode = null 100 | ): bool { 101 | return $this->scopeConfig->isSetFlag( 102 | self::CONFIG_PATH_GENERAL_USE_LAZY_LOAD, 103 | $scopeType, 104 | $scopeCode 105 | ); 106 | } 107 | 108 | /** 109 | * Is google recaptcha load lazy 110 | * 111 | * @param string $scopeType 112 | * @param mixed $scopeCode 113 | * 114 | * @return bool 115 | */ 116 | public function isDisabledSubmitForm( 117 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 118 | $scopeCode = null 119 | ): bool { 120 | return $this->scopeConfig->isSetFlag( 121 | self::CONFIG_PATH_GENERAL_DISABLE_SUBMIT_FORM, 122 | $scopeType, 123 | $scopeCode 124 | ); 125 | } 126 | 127 | /** 128 | * Is hide badge 129 | * 130 | * @return bool 131 | */ 132 | public function isHideBadge(): bool 133 | { 134 | return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_GENERAL_HIDE_BADGE); 135 | } 136 | 137 | /** 138 | * Return hide badge text 139 | * 140 | * @param string $scopeType 141 | * @param mixed $scopeCode 142 | * 143 | * @return bool 144 | */ 145 | public function getHideBadgeText( 146 | string $scopeType = ScopeInterface::SCOPE_WEBSITES, 147 | $scopeCode = null 148 | ): string { 149 | return (string)$this->scopeConfig->getValue( 150 | self::CONFIG_PATH_GENERAL_HIDE_BADGE_TEXT, 151 | $scopeType, 152 | $scopeCode 153 | ); 154 | } 155 | 156 | /** 157 | * Is enabled debugging 158 | * 159 | * @return bool 160 | */ 161 | public function isDebug(): bool 162 | { 163 | return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_GENERAL_DEBUG); 164 | } 165 | 166 | /** 167 | * Get config by path 168 | * 169 | * @param string $path 170 | * @param mixed $storeId 171 | * @param string $scope 172 | * 173 | * @return mixed 174 | */ 175 | public function getConfigValueByPath( 176 | $path, 177 | $storeId = null, 178 | $scope = ScopeInterface::SCOPE_WEBSITES 179 | ) { 180 | return $this->scopeConfig->getValue($path, $scope, $storeId); 181 | } 182 | 183 | /** 184 | * Disable config path 185 | * 186 | * @return string 187 | */ 188 | public function disableConfigPath(): string 189 | { 190 | return self::CONFIG_PATH_GENERAL_ENABLE_MODULE; 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Model/AbstractCheckEnabledVerify.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model; 9 | 10 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend; 11 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use InvalidArgumentException; 14 | use Magento\Framework\App\Area; 15 | 16 | abstract class AbstractCheckEnabledVerify implements CheckEnabledVerifyInterface 17 | { 18 | /** 19 | * @var string 20 | */ 21 | private $area; 22 | 23 | /** 24 | * @var General 25 | */ 26 | private $generalConfig; 27 | 28 | /** 29 | * @var Frontend 30 | */ 31 | private $frontendConfig; 32 | 33 | /** 34 | * @var Backend 35 | */ 36 | private $backendConfig; 37 | 38 | /** 39 | * AbstractCheckEnableVerify constructor. 40 | * 41 | * @param General $generalConfig 42 | * @param Frontend $frontendConfig 43 | * @param Backend $backendConfig 44 | * @param string $area 45 | */ 46 | public function __construct( 47 | General $generalConfig, 48 | Frontend $frontendConfig, 49 | Backend $backendConfig, 50 | string $area 51 | ) { 52 | $this->generalConfig = $generalConfig; 53 | $this->frontendConfig = $frontendConfig; 54 | $this->backendConfig = $backendConfig; 55 | $this->area = $area; 56 | 57 | if (!in_array($area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML])) { 58 | throw new InvalidArgumentException('Area parameter must be one of frontend or adminhtml'); 59 | } 60 | } 61 | 62 | /** 63 | * @return General 64 | */ 65 | public function getGeneralConfig(): General 66 | { 67 | return $this->generalConfig; 68 | } 69 | 70 | /** 71 | * @return Frontend 72 | */ 73 | public function getFrontendConfig(): Frontend 74 | { 75 | return $this->frontendConfig; 76 | } 77 | 78 | /** 79 | * @return Backend 80 | */ 81 | public function getBackendConfig(): Backend 82 | { 83 | return $this->backendConfig; 84 | } 85 | 86 | /** 87 | * Return true if area is configured to be active 88 | * 89 | * @return bool 90 | */ 91 | public function checkArea(): bool 92 | { 93 | $return = false; 94 | 95 | if ($this->getGeneralConfig()->hasEnabled() === false) { 96 | return $return; 97 | } 98 | 99 | switch ($this->area) { 100 | case Area::AREA_FRONTEND: 101 | $return = $this->getFrontendConfig()->hasEnabled(); 102 | break; 103 | 104 | case Area::AREA_ADMINHTML: 105 | $return = $this->getBackendConfig()->hasEnabled(); 106 | break; 107 | } 108 | 109 | return $return; 110 | } 111 | 112 | /** 113 | * @inheritDoc 114 | */ 115 | public function verify(): bool 116 | { 117 | return $this->checkArea(); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /Model/Area.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | use Magento\Framework\App\Area as BaseArea; 13 | 14 | /** 15 | * Class Area 16 | */ 17 | class Area 18 | { 19 | const GLOBAL = BaseArea::AREA_GLOBAL; 20 | const FRONTEND = BaseArea::AREA_FRONTEND; 21 | const BACKEND = BaseArea::AREA_ADMINHTML; 22 | 23 | /** 24 | * @return array 25 | */ 26 | public function getAllowedList(): array 27 | { 28 | return [ 29 | self::GLOBAL, 30 | self::BACKEND, 31 | self::FRONTEND, 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Model/Area/ConfigInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Area; 11 | 12 | interface ConfigInterface 13 | { 14 | /** 15 | * Disable config path 16 | * 17 | * @return string 18 | */ 19 | public function disableConfigPath(): string; 20 | 21 | /** 22 | * Disable captcha by area 23 | * 24 | * @param null|string $website 25 | */ 26 | public function disableCaptcha(string $website = null): void; 27 | } -------------------------------------------------------------------------------- /Model/Area/ConfigList.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Area; 11 | 12 | use Magento\Framework\Exception\LocalizedException; 13 | 14 | /** 15 | * Class ConfigList 16 | */ 17 | class ConfigList implements ConfigListInterface 18 | { 19 | /** 20 | * @var ConfigInterface[] 21 | */ 22 | private $list = []; 23 | 24 | /** 25 | * ConfigList constructor. 26 | * 27 | * @param ConfigInterface[] $list 28 | */ 29 | public function __construct(array $list = []) 30 | { 31 | $this->list = $list; 32 | } 33 | 34 | /** 35 | * @return ConfigInterface[] 36 | */ 37 | public function getConfigList(): array 38 | { 39 | return $this->list; 40 | } 41 | 42 | /** 43 | * @param string $area 44 | * 45 | * @return ConfigInterface 46 | * @throws LocalizedException 47 | */ 48 | public function getConfig(string $area): ConfigInterface 49 | { 50 | if (!isset($this->list[$area])) { 51 | throw new LocalizedException(__('Area not found')); 52 | } 53 | 54 | return $this->list[$area]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Model/Area/ConfigListInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Area; 11 | 12 | interface ConfigListInterface 13 | { 14 | /** 15 | * @return array 16 | */ 17 | public function getConfigList(): array; 18 | 19 | /** 20 | * @param string $area 21 | * 22 | * @return ConfigInterface 23 | */ 24 | public function getConfig(string $area): ConfigInterface; 25 | } -------------------------------------------------------------------------------- /Model/Captcha.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\FailureInterface; 13 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponseInterface; 14 | 15 | /** 16 | * Class Captcha 17 | */ 18 | class Captcha implements CaptchaInterface 19 | { 20 | /** 21 | * @var string|null 22 | */ 23 | private $action; 24 | 25 | /** 26 | * @var TokenResponseInterface 27 | */ 28 | private $tokenResponse; 29 | 30 | /** 31 | * @var FailureInterface 32 | */ 33 | private $failureProvider; 34 | 35 | /** 36 | * @var CheckEnabledVerifyInterface|null 37 | */ 38 | private $checkEnabledVerify; 39 | 40 | /** 41 | * @var ScoreThresholdInterface|null 42 | */ 43 | private $scoreThreshold; 44 | 45 | /** 46 | * Captcha constructor. 47 | * 48 | * @param string $action 49 | * @param TokenResponseInterface $tokenResponse 50 | * @param FailureInterface $failureProvider 51 | * @param ScoreThresholdInterface|null $scoreThreshold 52 | * @param CheckEnabledVerifyInterface|null $checkEnabledVerify 53 | */ 54 | public function __construct( 55 | string $action, 56 | TokenResponseInterface $tokenResponse, 57 | FailureInterface $failureProvider, 58 | ?ScoreThresholdInterface $scoreThreshold, 59 | ?CheckEnabledVerifyInterface $checkEnabledVerify 60 | ) { 61 | $this->action = $action; 62 | $this->tokenResponse = $tokenResponse; 63 | $this->failureProvider = $failureProvider; 64 | $this->scoreThreshold = $scoreThreshold; 65 | $this->checkEnabledVerify = $checkEnabledVerify; 66 | } 67 | 68 | /** 69 | * @return string 70 | */ 71 | public function getAction(): string 72 | { 73 | return $this->action; 74 | } 75 | 76 | /** 77 | * @return string|null 78 | */ 79 | public function getToken(): ?string 80 | { 81 | return $this->tokenResponse->getToken(); 82 | } 83 | 84 | /** 85 | * @return float 86 | */ 87 | public function getScoreThreshold(): float 88 | { 89 | return $this->scoreThreshold ? $this->scoreThreshold->getValue() : 0.5; 90 | } 91 | 92 | /** 93 | * @return FailureInterface 94 | */ 95 | public function getFailure(): FailureInterface 96 | { 97 | return $this->failureProvider; 98 | } 99 | 100 | /** 101 | * @inheritdoc 102 | */ 103 | public function isEnabled(): bool 104 | { 105 | return !$this->checkEnabledVerify || $this->checkEnabledVerify->verify(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Model/CaptchaInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\FailureInterface; 13 | 14 | /** 15 | * Class AbstractCaptcha 16 | */ 17 | interface CaptchaInterface 18 | { 19 | /** 20 | * @return string 21 | */ 22 | public function getAction(): string; 23 | 24 | /** 25 | * @return string 26 | */ 27 | public function getToken(): ?string; 28 | 29 | /** 30 | * @return FailureInterface 31 | */ 32 | public function getFailure(): FailureInterface; 33 | 34 | /** 35 | * @return float 36 | */ 37 | public function getScoreThreshold(): float; 38 | 39 | /** 40 | * @return bool 41 | */ 42 | public function isEnabled(): bool; 43 | } 44 | -------------------------------------------------------------------------------- /Model/CheckEnabledVerify.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model; 9 | 10 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend; 11 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | 14 | class CheckEnabledVerify extends AbstractCheckEnabledVerify 15 | { 16 | /** 17 | * @var string|null 18 | */ 19 | private $configPath; 20 | 21 | /** 22 | * CheckEnabledVerify constructor. 23 | * 24 | * @param General $generalConfig 25 | * @param Frontend $frontendConfig 26 | * @param Backend $backendConfig 27 | * @param string $area 28 | * @param string|null $configPath 29 | */ 30 | public function __construct( 31 | General $generalConfig, 32 | Frontend $frontendConfig, 33 | Backend $backendConfig, 34 | string $area, 35 | ?string $configPath 36 | ) { 37 | parent::__construct( 38 | $generalConfig, 39 | $frontendConfig, 40 | $backendConfig, 41 | $area 42 | ); 43 | 44 | $this->configPath = $configPath; 45 | } 46 | 47 | /** 48 | * @inheritDoc 49 | */ 50 | public function verify(): bool 51 | { 52 | if ($this->configPath) { 53 | return parent::verify() && !!$this->getGeneralConfig()->getConfigValueByPath($this->configPath); 54 | } 55 | 56 | return parent::verify(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Model/CheckEnabledVerifyInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model; 9 | 10 | interface CheckEnabledVerifyInterface 11 | { 12 | /** 13 | * Return true if check enabled captcha 14 | * 15 | * @return bool 16 | */ 17 | public function verify(): bool; 18 | } 19 | -------------------------------------------------------------------------------- /Model/Config/Source/ScoreThreshold.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Config\Source; 11 | 12 | use Magento\Framework\Data\OptionSourceInterface; 13 | 14 | /** 15 | * Class ScoreThreshold 16 | */ 17 | class ScoreThreshold implements OptionSourceInterface 18 | { 19 | /** 20 | * Options getter 21 | * 22 | * @return array 23 | */ 24 | public function toOptionArray() 25 | { 26 | return [ 27 | [ 28 | 'value' => 0.1, 29 | 'label' => 0.1 30 | ], 31 | [ 32 | 'value' => 0.2, 33 | 'label' => 0.2 34 | ], 35 | [ 36 | 'value' => 0.3, 37 | 'label' => 0.3 38 | ], 39 | [ 40 | 'value' => 0.4, 41 | 'label' => 0.4 42 | ], 43 | [ 44 | 'value' => 0.5, 45 | 'label' => 0.5 46 | ], 47 | [ 48 | 'value' => 0.6, 49 | 'label' => 0.6 50 | ], 51 | [ 52 | 'value' => 0.7, 53 | 'label' => 0.7 54 | ], 55 | [ 56 | 'value' => 0.8, 57 | 'label' => 0.8 58 | ], 59 | [ 60 | 'value' => 0.9, 61 | 'label' => 0.9 62 | ] 63 | ]; 64 | } 65 | 66 | /** 67 | * Get options in "key-value" format 68 | * 69 | * @return array 70 | */ 71 | public function toArray() 72 | { 73 | return [ 74 | '0.1' => 0.1, 75 | '0.2' => 0.2, 76 | '0.3' => 0.3, 77 | '0.4' => 0.4, 78 | '0.5' => 0.5, 79 | '0.6' => 0.6, 80 | '0.7' => 0.7, 81 | '0.8' => 0.8, 82 | '0.9' => 0.9, 83 | ]; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Model/Debug.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use Magento\Framework\Filesystem\DriverInterface; 14 | use Magento\Framework\Logger\Handler\Base; 15 | 16 | /** 17 | * Class Debug 18 | */ 19 | class Debug extends Base 20 | { 21 | /** 22 | * @var General 23 | */ 24 | private $config; 25 | 26 | /** 27 | * Debug constructor. 28 | * 29 | * @param DriverInterface $filesystem 30 | * @param General $config 31 | * @param null $filePath 32 | * @param string $fileName 33 | * 34 | * @throws \Exception 35 | */ 36 | public function __construct( 37 | DriverInterface $filesystem, 38 | General $config, 39 | $filePath = null, 40 | $fileName = '/var/log/invisible_captcha.log' 41 | ) { 42 | parent::__construct($filesystem, $filePath, $fileName); 43 | 44 | $this->config = $config; 45 | } 46 | 47 | /** 48 | * @inheritDoc 49 | * @throws \Magento\Framework\Exception\FileSystemException 50 | */ 51 | public function write(array $record): void 52 | { 53 | if ($this->config->isDebug() === false) { 54 | return; 55 | } 56 | 57 | $logDir = $this->filesystem->getParentDirectory($this->url); 58 | if (!$this->filesystem->isDirectory($logDir)) { 59 | $this->filesystem->createDirectory($logDir); 60 | } 61 | 62 | parent::write($record); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Model/LayoutSettings.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | 14 | /** 15 | * Class LayoutSettings 16 | */ 17 | class LayoutSettings 18 | { 19 | /** 20 | * @var General 21 | */ 22 | private $config; 23 | 24 | /** 25 | * LayoutSettings constructor. 26 | * 27 | * @param General $config 28 | */ 29 | public function __construct( 30 | General $config 31 | ) { 32 | $this->config = $config; 33 | } 34 | 35 | /** 36 | * Return captcha config for frontend 37 | * @return array 38 | */ 39 | public function getCaptchaSettings() 40 | { 41 | if ($this->config->hasEnabled()) { 42 | return [ 43 | 'siteKey' => $this->config->getSiteKey(), 44 | 'lazyLoad' => $this->config->isLazyLoad(), 45 | 'isDisabledSubmitForm' => $this->config->isDisabledSubmitForm() 46 | ]; 47 | } 48 | 49 | return []; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Model/Provider/AbstractFailure.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | 14 | /** 15 | * Class AbstractFailure 16 | */ 17 | abstract class AbstractFailure implements FailureInterface 18 | { 19 | /** 20 | * @var FailureMessages 21 | */ 22 | private $failureMessages; 23 | 24 | /** 25 | * AbstractFailure constructor. 26 | * 27 | * @param FailureMessages $failureMessages 28 | */ 29 | public function __construct( 30 | FailureMessages $failureMessages 31 | ) { 32 | $this->failureMessages = $failureMessages; 33 | } 34 | 35 | /** 36 | * @param Response $verifyReCaptcha 37 | * 38 | * @return array 39 | */ 40 | public function getMessages(Response $verifyReCaptcha): array 41 | { 42 | $return = []; 43 | 44 | $errorMessages = $this->failureMessages->getErrorMessages(); 45 | $errorsCodes = array_keys($errorMessages); 46 | $errors = array_intersect($verifyReCaptcha->getErrorCodes(), $errorsCodes); 47 | 48 | foreach ($errors as $error) { 49 | if ($this->failureMessages->hasErrorMessage($error)) { 50 | $return[] = $this->failureMessages->getErrorMessage($error); 51 | } 52 | } 53 | 54 | return $return; 55 | } 56 | 57 | /** 58 | * @param Response $verifyReCaptcha 59 | * 60 | * @return string 61 | */ 62 | public function getMessagesString(Response $verifyReCaptcha): string 63 | { 64 | return implode('
', $this->getMessages($verifyReCaptcha)); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Model/Provider/Failure/AjaxResponseFailure.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\AbstractFailure; 14 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\FailureMessages; 15 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 16 | use Magento\Framework\App\Action\Action; 17 | use Magento\Framework\App\ActionFlag; 18 | use Magento\Framework\App\Response\Http; 19 | use Magento\Framework\App\ResponseInterface; 20 | use Magento\Framework\Json\EncoderInterface; 21 | 22 | class AjaxResponseFailure extends AbstractFailure 23 | { 24 | /** 25 | * @var ActionFlag 26 | */ 27 | private $actionFlag; 28 | 29 | /** 30 | * @var EncoderInterface 31 | */ 32 | private $encoder; 33 | 34 | /** 35 | * @var General 36 | */ 37 | private $config; 38 | 39 | /** 40 | * AjaxResponseFailure constructor. 41 | * 42 | * @param ActionFlag $actionFlag 43 | * @param EncoderInterface $encoder 44 | * @param General $config 45 | * @param FailureMessages $failureMessages 46 | */ 47 | public function __construct( 48 | ActionFlag $actionFlag, 49 | EncoderInterface $encoder, 50 | General $config, 51 | FailureMessages $failureMessages 52 | ) { 53 | $this->actionFlag = $actionFlag; 54 | $this->encoder = $encoder; 55 | $this->config = $config; 56 | 57 | parent::__construct($failureMessages); 58 | } 59 | 60 | /** 61 | * Handle captcha failure 62 | * 63 | * @param Response $verifyReCaptcha 64 | * @param ResponseInterface|null $response 65 | * 66 | * @return void 67 | */ 68 | public function execute(Response $verifyReCaptcha, ResponseInterface $response = null) 69 | { 70 | if ($response === null || !$response instanceof Http) { 71 | return; 72 | } 73 | 74 | $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, /** @scrutinizer ignore-type */ true); 75 | 76 | $jsonPayload = $this->encoder->encode([ 77 | 'errors' => true, 78 | 'message' => $this->getMessagesString($verifyReCaptcha), 79 | ]); 80 | 81 | $response->representJson($jsonPayload); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Model/Provider/Failure/AuthenticationExceptionFailure.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\AbstractFailure; 14 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\FailureMessages; 15 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 16 | use Magento\Framework\App\ResponseInterface; 17 | use Magento\Framework\Exception\Plugin\AuthenticationException; 18 | 19 | class AuthenticationExceptionFailure extends AbstractFailure 20 | { 21 | /** 22 | * @var General 23 | */ 24 | private $config; 25 | 26 | /** 27 | * AuthenticationExceptionFailure constructor. 28 | * 29 | * @param FailureMessages $failureMessages 30 | * @param General $config 31 | */ 32 | public function __construct( 33 | FailureMessages $failureMessages, 34 | General $config 35 | ) { 36 | parent::__construct($failureMessages); 37 | 38 | $this->config = $config; 39 | } 40 | 41 | /** 42 | * Handle captcha failure 43 | * 44 | * @param Response $verifyReCaptcha 45 | * @param ResponseInterface $response 46 | * 47 | * @return void 48 | * @throws AuthenticationException 49 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 50 | */ 51 | public function execute(Response $verifyReCaptcha, ResponseInterface $response = null) 52 | { 53 | throw new AuthenticationException(__($this->getMessagesString($verifyReCaptcha))); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Model/Provider/Failure/ObserverRedirectFailure.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure; 9 | 10 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 11 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\AbstractFailure; 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\FailureMessages; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 14 | use Magento\Framework\App\Action\Action; 15 | use Magento\Framework\App\ActionFlag; 16 | use Magento\Framework\App\Response\Http; 17 | use Magento\Framework\App\ResponseInterface; 18 | use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; 19 | use Magento\Framework\UrlInterface; 20 | 21 | class ObserverRedirectFailure extends AbstractFailure 22 | { 23 | /** 24 | * @var MessageManagerInterface 25 | */ 26 | private $messageManager; 27 | 28 | /** 29 | * @var ActionFlag 30 | */ 31 | private $actionFlag; 32 | 33 | /** 34 | * @var General 35 | */ 36 | private $config; 37 | 38 | /** 39 | * @var RedirectUrlInterface 40 | */ 41 | private $redirectUrlProvider; 42 | 43 | /** 44 | * @var UrlInterface 45 | */ 46 | private $url; 47 | 48 | /** 49 | * RedirectFailure constructor. 50 | * 51 | * @param MessageManagerInterface $messageManager 52 | * @param ActionFlag $actionFlag 53 | * @param General $config 54 | * @param UrlInterface $url 55 | * @param FailureMessages $failureMessages 56 | * @param RedirectUrlInterface|null $redirectUrlProvider 57 | */ 58 | public function __construct( 59 | MessageManagerInterface $messageManager, 60 | ActionFlag $actionFlag, 61 | General $config, 62 | UrlInterface $url, 63 | FailureMessages $failureMessages, 64 | RedirectUrlInterface $redirectUrlProvider = null 65 | ) { 66 | $this->messageManager = $messageManager; 67 | $this->actionFlag = $actionFlag; 68 | $this->config = $config; 69 | $this->redirectUrlProvider = $redirectUrlProvider; 70 | $this->url = $url; 71 | 72 | parent::__construct($failureMessages); 73 | } 74 | 75 | /** 76 | * Get redirect URL 77 | * 78 | * @return string 79 | */ 80 | private function getUrl() 81 | { 82 | return $this->redirectUrlProvider->getRedirectUrl(); 83 | } 84 | 85 | /** 86 | * Handle captcha failure 87 | * 88 | * @param Response $verifyReCaptcha 89 | * @param ResponseInterface $response 90 | * 91 | * @return void 92 | */ 93 | public function execute(Response $verifyReCaptcha, ResponseInterface $response = null) 94 | { 95 | if ($response === null || !$response instanceof Http) { 96 | return; 97 | } 98 | 99 | $this->messageManager->addErrorMessage($this->getMessagesString($verifyReCaptcha)); 100 | $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, /** @scrutinizer ignore-type */ true); 101 | 102 | $response->setRedirect($this->getUrl()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Model/Provider/Failure/RedirectUrl/BeforeAuthUrlProvider.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrl; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrlInterface; 13 | use Magento\Customer\Model\Url; 14 | use Magento\Framework\Session\SessionManagerInterface; 15 | 16 | class BeforeAuthUrlProvider implements RedirectUrlInterface 17 | { 18 | /** 19 | * @var SessionManagerInterface 20 | */ 21 | private $sessionManager; 22 | 23 | /** 24 | * @var Url 25 | */ 26 | private $url; 27 | 28 | /** 29 | * BeforeAuthUrlProvider constructor. 30 | * 31 | * @param SessionManagerInterface $sessionManager 32 | * @param Url $url 33 | */ 34 | public function __construct( 35 | SessionManagerInterface $sessionManager, 36 | Url $url 37 | ) { 38 | $this->sessionManager = $sessionManager; 39 | $this->url = $url; 40 | } 41 | 42 | /** 43 | * Get redirection URL 44 | * 45 | * @return string 46 | */ 47 | public function getRedirectUrl(): string 48 | { 49 | $beforeUrl = $this->sessionManager->getBeforeAuthUrl(); 50 | 51 | return $beforeUrl ?: $this->url->getLoginUrl(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Model/Provider/Failure/RedirectUrl/RefererProvider.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrl; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrlInterface; 13 | use Magento\Framework\App\Response\RedirectInterface; 14 | 15 | class RefererProvider implements RedirectUrlInterface 16 | { 17 | /** 18 | * @var RedirectInterface 19 | */ 20 | private $redirect; 21 | 22 | /** 23 | * RefererProvider constructor. 24 | * 25 | * @param RedirectInterface $redirect 26 | */ 27 | public function __construct( 28 | RedirectInterface $redirect 29 | ) { 30 | $this->redirect = $redirect; 31 | } 32 | 33 | /** 34 | * @inheritDoc 35 | */ 36 | public function getRedirectUrl(): string 37 | { 38 | return $this->redirect->getRedirectUrl(); 39 | } 40 | } -------------------------------------------------------------------------------- /Model/Provider/Failure/RedirectUrl/SimpleUrlProvider.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrl; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrlInterface; 13 | use Magento\Framework\UrlInterface; 14 | 15 | class SimpleUrlProvider implements RedirectUrlInterface 16 | { 17 | /** 18 | * @var string 19 | */ 20 | private $urlPath; 21 | 22 | /** 23 | * @var array 24 | */ 25 | private $urlParams; 26 | 27 | /** 28 | * @var UrlInterface 29 | */ 30 | private $url; 31 | 32 | /** 33 | * SimpleUrlProvider constructor. 34 | * 35 | * @param UrlInterface $url 36 | * @param string $urlPath 37 | * @param null|array $urlParams 38 | */ 39 | public function __construct( 40 | UrlInterface $url, 41 | string $urlPath, 42 | $urlParams = null 43 | ) { 44 | $this->urlPath = $urlPath; 45 | $this->urlParams = $urlParams; 46 | $this->url = $url; 47 | } 48 | 49 | /** 50 | * Get redirection URL 51 | * @return string 52 | */ 53 | public function getRedirectUrl(): string 54 | { 55 | return $this->url->getUrl($this->urlPath, $this->urlParams); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Model/Provider/Failure/RedirectUrlInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure; 11 | 12 | interface RedirectUrlInterface 13 | { 14 | /** 15 | * Get redirection URL 16 | * 17 | * @return string 18 | */ 19 | public function getRedirectUrl(): string; 20 | } 21 | -------------------------------------------------------------------------------- /Model/Provider/FailureInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Magento\Framework\App\ResponseInterface; 14 | 15 | interface FailureInterface 16 | { 17 | /** 18 | * Handle captcha failure 19 | * 20 | * @param Response $verifyReCaptcha 21 | * @param ResponseInterface $response 22 | * 23 | * @return void 24 | */ 25 | public function execute(Response $verifyReCaptcha, ResponseInterface $response = null); 26 | } 27 | -------------------------------------------------------------------------------- /Model/Provider/FailureMessages.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider; 11 | 12 | use Hryvinskyi\Base\Helper\ArrayHelper; 13 | 14 | /** 15 | * Class FailureMessages 16 | */ 17 | class FailureMessages 18 | { 19 | /** 20 | * @var array 21 | */ 22 | private $errorMessages; 23 | 24 | /** 25 | * AbstractFailure constructor. 26 | * 27 | * @param array $errorMessages 28 | */ 29 | public function __construct( 30 | array $errorMessages = [] 31 | ) { 32 | $this->errorMessages = $errorMessages; 33 | } 34 | 35 | /** 36 | * @return array 37 | */ 38 | public function getErrorMessages(): array 39 | { 40 | return $this->errorMessages; 41 | } 42 | 43 | /** 44 | * @param string $key 45 | * 46 | * @return string|null 47 | */ 48 | public function getErrorMessage(string $key): ?string 49 | { 50 | return ArrayHelper::getValue($this->getErrorMessages(), $key); 51 | } 52 | 53 | /** 54 | * @param string $key 55 | * 56 | * @return bool 57 | */ 58 | public function hasErrorMessage(string $key): bool 59 | { 60 | return ArrayHelper::keyExists($key, $this->getErrorMessages()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Model/Provider/TokenResponse/Ajax.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponse; 9 | 10 | use Exception; 11 | use Hryvinskyi\Base\Helper\Json; 12 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponseInterface; 13 | use Magento\Framework\App\Request\Http; 14 | use Magento\Framework\App\RequestInterface; 15 | use Magento\Framework\Message\ManagerInterface; 16 | 17 | class Ajax implements TokenResponseInterface 18 | { 19 | /** 20 | * @var RequestInterface 21 | */ 22 | private $request; 23 | 24 | /** 25 | * @var ManagerInterface 26 | */ 27 | private $messageManager; 28 | 29 | /** 30 | * Ajax constructor. 31 | * 32 | * @param RequestInterface $request 33 | * @param ManagerInterface $messageManager 34 | */ 35 | public function __construct( 36 | RequestInterface $request, 37 | ManagerInterface $messageManager 38 | ) { 39 | $this->request = $request; 40 | $this->messageManager = $messageManager; 41 | } 42 | 43 | /** 44 | * Return token 45 | * 46 | * @return string|null 47 | */ 48 | public function getToken(): ?string 49 | { 50 | $token = null; 51 | 52 | if ($this->request instanceof Http) { 53 | $content = $this->request->getContent(); 54 | 55 | if ($content) { 56 | try { 57 | $params = Json::decode($content); 58 | 59 | if (isset($params['hryvinskyi_invisible_token'])) { 60 | $token = $params['hryvinskyi_invisible_token']; 61 | } 62 | } catch (Exception $e) { 63 | $this->messageManager->addErrorMessage(__('Not found invisible captcha token')); 64 | } 65 | } 66 | } 67 | 68 | return $token; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Model/Provider/TokenResponse/General.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponse; 9 | 10 | use Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponseInterface; 11 | use Magento\Framework\App\RequestInterface; 12 | use Magento\Framework\Message\ManagerInterface; 13 | 14 | class General implements TokenResponseInterface 15 | { 16 | /** 17 | * @var RequestInterface 18 | */ 19 | private $request; 20 | 21 | /** 22 | * @var ManagerInterface 23 | */ 24 | private $messageManager; 25 | 26 | /** 27 | * DefaultResponseProvider constructor. 28 | * 29 | * @param RequestInterface $request 30 | * @param ManagerInterface $messageManager 31 | */ 32 | public function __construct( 33 | RequestInterface $request, 34 | ManagerInterface $messageManager 35 | ) { 36 | $this->request = $request; 37 | $this->messageManager = $messageManager; 38 | } 39 | 40 | /** 41 | * @inheritDoc 42 | */ 43 | public function getToken(): ?string 44 | { 45 | return $this->request->getParam('hryvinskyi_invisible_token'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Model/Provider/TokenResponseInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Provider; 11 | 12 | interface TokenResponseInterface 13 | { 14 | /** 15 | * Return token 16 | * 17 | * @return string|null 18 | */ 19 | public function getToken(): ?string; 20 | } 21 | -------------------------------------------------------------------------------- /Model/ReCaptcha/RequestMethod/CurlPost.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\RequestMethod; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\RequestMethodInterface; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\RequestParameters; 14 | use Magento\Framework\HTTP\Client\Curl; 15 | 16 | /** 17 | * Class CurlPost 18 | */ 19 | class CurlPost implements RequestMethodInterface 20 | { 21 | /** 22 | * @var Curl 23 | */ 24 | private $curl; 25 | 26 | /** 27 | * CurlPost constructor. 28 | * 29 | * @param Curl $curl 30 | */ 31 | public function __construct( 32 | Curl $curl 33 | ) { 34 | $this->curl = $curl; 35 | } 36 | 37 | /** 38 | * @inheritDoc 39 | */ 40 | public function submit(string $url, RequestParameters $params): string 41 | { 42 | $this->curl->post($url, $params->toQueryString()); 43 | 44 | return $this->curl->getBody(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Model/ReCaptcha/RequestMethodInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha; 11 | 12 | interface RequestMethodInterface 13 | { 14 | /** 15 | * Submit the request with the specified parameters. 16 | * 17 | * @param string $url 18 | * @param RequestParameters $params Request parameters 19 | * 20 | * @return string Body of the reCAPTCHA response 21 | */ 22 | public function submit(string $url, RequestParameters $params): string; 23 | } 24 | -------------------------------------------------------------------------------- /Model/ReCaptcha/RequestParameters.php: -------------------------------------------------------------------------------- 1 | secret; 42 | } 43 | 44 | /** 45 | * @param string $secret 46 | * 47 | * @return RequestParameters 48 | */ 49 | public function setSecret(string $secret): RequestParameters 50 | { 51 | $this->secret = $secret; 52 | 53 | return $this; 54 | } 55 | 56 | /** 57 | * @return string 58 | */ 59 | public function getResponse(): string 60 | { 61 | return $this->response; 62 | } 63 | 64 | /** 65 | * @param string $response 66 | * 67 | * @return RequestParameters 68 | */ 69 | public function setResponse(string $response): RequestParameters 70 | { 71 | $this->response = $response; 72 | 73 | return $this; 74 | } 75 | 76 | /** 77 | * @return string 78 | */ 79 | public function getRemoteIp(): ?string 80 | { 81 | return $this->remoteIp; 82 | } 83 | 84 | /** 85 | * @param string|bool $remoteIp 86 | * 87 | * @return RequestParameters 88 | */ 89 | public function setRemoteIp(?string $remoteIp): RequestParameters 90 | { 91 | $this->remoteIp = $remoteIp; 92 | 93 | return $this; 94 | } 95 | 96 | /** 97 | * @return string 98 | */ 99 | public function getVersion(): ?string 100 | { 101 | return $this->version; 102 | } 103 | 104 | /** 105 | * @param string $version 106 | * 107 | * @return RequestParameters 108 | */ 109 | public function setVersion(string $version): RequestParameters 110 | { 111 | $this->version = $version; 112 | 113 | return $this; 114 | } 115 | 116 | /** 117 | * Array representation. 118 | * 119 | * @return array Array formatted parameters. 120 | */ 121 | public function toArray(): array 122 | { 123 | $params = [ 124 | 'secret' => $this->getSecret(), 125 | 'response' => $this->getResponse(), 126 | 'remoteip' => $this->getRemoteIp(), 127 | 'version' => $this->getVersion() 128 | ]; 129 | 130 | return array_filter($params); 131 | } 132 | 133 | /** 134 | * Query string representation for HTTP request. 135 | * 136 | * @return string Query string formatted parameters. 137 | */ 138 | public function toQueryString(): string 139 | { 140 | return http_build_query($this->toArray(), '', '&'); 141 | } 142 | } -------------------------------------------------------------------------------- /Model/ReCaptcha/Response.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha; 9 | 10 | use Hryvinskyi\Base\Helper\ArrayHelper; 11 | use Hryvinskyi\Base\Helper\Json; 12 | 13 | /** 14 | * The response returned from the service. 15 | */ 16 | class Response 17 | { 18 | /** 19 | * Success or failure. 20 | * @var boolean 21 | */ 22 | private $success = false; 23 | 24 | /** 25 | * Error code strings. 26 | * 27 | * @var array 28 | */ 29 | private $errorCodes = []; 30 | 31 | /** 32 | * The hostname of the site where the reCAPTCHA was solved. 33 | * 34 | * @var string 35 | */ 36 | private $hostname; 37 | 38 | /** 39 | * Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) 40 | * 41 | * @var string 42 | */ 43 | private $challengeTs; 44 | 45 | /** 46 | * Score assigned to the request 47 | * 48 | * @var float 49 | */ 50 | private $score; 51 | 52 | /** 53 | * Action as specified by the page 54 | * 55 | * @var string 56 | */ 57 | private $action; 58 | 59 | /** 60 | * Build the response from the expected JSON returned by the service. 61 | * 62 | * @param string $json 63 | * 64 | * @return Response 65 | */ 66 | public static function fromJson($json): Response 67 | { 68 | $responseData = Json::decode($json); 69 | 70 | if (!$responseData) { 71 | return new Response(false, [VerifyReCaptcha::E_INVALID_JSON]); 72 | } 73 | 74 | $hostname = ArrayHelper::getValue($responseData, 'hostname'); 75 | $challengeTs = ArrayHelper::getValue($responseData, 'challenge_ts'); 76 | $score = floatval(ArrayHelper::getValue($responseData, 'score')); 77 | $action = ArrayHelper::getValue($responseData, 'action'); 78 | $success = ArrayHelper::getValue($responseData, 'success'); 79 | $errorCodes = ArrayHelper::getValue($responseData, 'error-codes'); 80 | 81 | if ($success == true) { 82 | return new Response(true, [], $hostname, $challengeTs, $score, $action); 83 | } 84 | 85 | if ($errorCodes && is_array($errorCodes)) { 86 | return new Response( 87 | false, 88 | $responseData['error-codes'], 89 | $hostname, 90 | $challengeTs, 91 | $score, 92 | $action 93 | ); 94 | } 95 | 96 | return new Response( 97 | false, 98 | [VerifyReCaptcha::E_UNKNOWN_ERROR], 99 | $hostname, 100 | $challengeTs, 101 | $score, 102 | $action 103 | ); 104 | } 105 | 106 | /** 107 | * Response constructor. 108 | * 109 | * @param bool $success 110 | * @param array $errorCodes 111 | * @param string|null $hostname 112 | * @param string|null $challengeTs 113 | * @param float|null $score 114 | * @param string|null $action 115 | */ 116 | public function __construct( 117 | bool $success, 118 | array $errorCodes = [], 119 | ?string $hostname = null, 120 | ?string $challengeTs = null, 121 | ?float $score = null, 122 | ?string $action = null 123 | ) { 124 | $this->success = $success; 125 | $this->hostname = $hostname; 126 | $this->challengeTs = $challengeTs; 127 | $this->score = $score; 128 | $this->action = $action; 129 | $this->errorCodes = $errorCodes; 130 | } 131 | 132 | /** 133 | * Is success? 134 | * 135 | * @return boolean 136 | */ 137 | public function isSuccess(): bool 138 | { 139 | return $this->success; 140 | } 141 | 142 | /** 143 | * Get error codes. 144 | * 145 | * @return array 146 | */ 147 | public function getErrorCodes(): array 148 | { 149 | return $this->errorCodes; 150 | } 151 | 152 | /** 153 | * Get hostname. 154 | * 155 | * @return string 156 | */ 157 | public function getHostname(): ?string 158 | { 159 | return $this->hostname; 160 | } 161 | 162 | /** 163 | * Get challenge timestamp 164 | * 165 | * @return string 166 | */ 167 | public function getChallengeTs(): ?string 168 | { 169 | return $this->challengeTs; 170 | } 171 | 172 | /** 173 | * Get score 174 | * 175 | * @return float 176 | */ 177 | public function getScore(): ?float 178 | { 179 | return $this->score; 180 | } 181 | 182 | /** 183 | * Get action 184 | * 185 | * @return string 186 | */ 187 | public function getAction(): ?string 188 | { 189 | return $this->action; 190 | } 191 | 192 | /** 193 | * @return array 194 | */ 195 | public function toArray(): array 196 | { 197 | return [ 198 | 'success' => $this->isSuccess(), 199 | 'hostname' => $this->getHostname(), 200 | 'challenge_ts' => $this->getChallengeTs(), 201 | 'score' => $this->getScore(), 202 | 'action' => $this->getAction(), 203 | 'error-codes' => $this->getErrorCodes(), 204 | ]; 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/Action.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 14 | 15 | /** 16 | * Class Action 17 | */ 18 | class Action implements ValidatorInterface 19 | { 20 | /** 21 | * Expected action did not match 22 | * 23 | * @const string 24 | */ 25 | const E_ACTION_MISMATCH = 'action-mismatch'; 26 | 27 | /** 28 | * @param VerifyReCaptcha $verify 29 | * @param Response $response 30 | * 31 | * @return string|null 32 | */ 33 | public function validate(VerifyReCaptcha $verify, Response $response): ?string 34 | { 35 | if ( 36 | $verify->getExpectedAction() 37 | && $response->getAction() 38 | && strcasecmp($verify->getExpectedAction(), $response->getAction()) !== 0 39 | ) { 40 | return self::E_ACTION_MISMATCH; 41 | } 42 | 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/Host.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 14 | 15 | /** 16 | * Class Host 17 | */ 18 | class Host implements ValidatorInterface 19 | { 20 | /** 21 | * Expected hostname did not match 22 | * 23 | * @const string 24 | */ 25 | const E_HOSTNAME_MISMATCH = 'hostname-mismatch'; 26 | 27 | /** 28 | * Verify hostname 29 | * 30 | * @param VerifyReCaptcha $verify 31 | * @param Response $response 32 | * 33 | * @return string 34 | */ 35 | public function validate(VerifyReCaptcha $verify, Response $response): ?string 36 | { 37 | if ( 38 | $verify->getExpectedHostname() 39 | && $response->getHostname() 40 | && strcasecmp($verify->getExpectedHostname(), $response->getHostname()) !== 0 41 | ) { 42 | return self::E_HOSTNAME_MISMATCH; 43 | } 44 | 45 | return null; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/Threshold.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 14 | 15 | /** 16 | * Class Threshold 17 | */ 18 | class Threshold implements ValidatorInterface 19 | { 20 | /** 21 | * Score threshold not met 22 | * 23 | * @const string 24 | */ 25 | const E_SCORE_THRESHOLD_NOT_MET = 'score-threshold-not-met'; 26 | 27 | /** 28 | * @param VerifyReCaptcha $verify 29 | * @param Response $response 30 | * 31 | * @return string|null 32 | */ 33 | public function validate(VerifyReCaptcha $verify, Response $response): ?string 34 | { 35 | if ( 36 | $verify->getScoreThreshold() 37 | && $response->getScore() 38 | && $verify->getScoreThreshold() > $response->getScore() 39 | ) { 40 | return self::E_SCORE_THRESHOLD_NOT_MET; 41 | } 42 | 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/TimeoutSeconds.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 14 | 15 | /** 16 | * Class TimeoutSeconds 17 | */ 18 | class TimeoutSeconds implements ValidatorInterface 19 | { 20 | /** 21 | * Challenge timeout 22 | * 23 | * @const string 24 | */ 25 | const E_CHALLENGE_TIMEOUT = 'challenge-timeout'; 26 | 27 | /** 28 | * @param VerifyReCaptcha $verify 29 | * @param Response $response 30 | * 31 | * @return string|null 32 | */ 33 | public function validate(VerifyReCaptcha $verify, Response $response): ?string 34 | { 35 | if ($verify->getChallengeTimeout() && $response->getChallengeTs()) { 36 | $challengeTs = strtotime($response->getChallengeTs()); 37 | 38 | if ($challengeTs > 0 && time() - $challengeTs > $verify->getChallengeTimeout()) { 39 | return self::E_CHALLENGE_TIMEOUT; 40 | } 41 | } 42 | 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/ValidatorInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 14 | 15 | /** 16 | * Class ValidatorInterface 17 | */ 18 | interface ValidatorInterface 19 | { 20 | public function validate(VerifyReCaptcha $verify, Response $response): ?string; 21 | } 22 | -------------------------------------------------------------------------------- /Model/ReCaptcha/Validators/ValidatorList.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators; 11 | 12 | /** 13 | * Class ValidatorList 14 | */ 15 | class ValidatorList 16 | { 17 | /** 18 | * @var ValidatorInterface[] 19 | */ 20 | private $entityTypes = []; 21 | 22 | /** 23 | * EntityList constructor. 24 | * 25 | * @param ValidatorInterface[] $entityTypes 26 | */ 27 | public function __construct( 28 | $entityTypes = [] 29 | ) { 30 | $this->entityTypes = $entityTypes; 31 | } 32 | 33 | /** 34 | * Retrieve list of entities 35 | * 36 | * @return ValidatorInterface[] 37 | */ 38 | public function getList(): array 39 | { 40 | return $this->entityTypes; 41 | } 42 | 43 | /** 44 | * @param string $code 45 | * 46 | * @return ValidatorInterface 47 | */ 48 | public function getEntityByCode(string $code): ValidatorInterface 49 | { 50 | return $this->entityTypes[$code]; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Model/ReCaptcha/VerifyReCaptcha.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\ValidatorInterface; 13 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\ValidatorList; 14 | 15 | /** 16 | * Class VerifyVerifyReCaptcha 17 | */ 18 | class VerifyReCaptcha 19 | { 20 | /** 21 | * URL for reCAPTCHA site verify API 22 | * 23 | * @const string 24 | */ 25 | const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; 26 | 27 | /** 28 | * Invalid JSON received 29 | * 30 | * @const string 31 | */ 32 | const E_INVALID_JSON = 'invalid-json'; 33 | 34 | /** 35 | * Not a success, but no error codes received! 36 | * 37 | * @const string 38 | */ 39 | const E_UNKNOWN_ERROR = 'unknown-error'; 40 | 41 | /** 42 | * Bad validator 43 | * 44 | * @const string 45 | */ 46 | const BAD_VALIDATOR = 'bad-validator'; 47 | 48 | /** 49 | * @var string 50 | */ 51 | private $hostname; 52 | 53 | /** 54 | * @var string 55 | */ 56 | private $action; 57 | 58 | /** 59 | * @var float 60 | */ 61 | private $threshold; 62 | 63 | /** 64 | * @var int 65 | */ 66 | private $timeoutSeconds; 67 | 68 | /** 69 | * Shared secret for the site. 70 | * @var string 71 | */ 72 | private $secret; 73 | 74 | /** 75 | * Method used to communicate with service. Defaults to POST request. 76 | * 77 | * @var RequestMethodInterface 78 | */ 79 | private $requestMethod; 80 | 81 | /** 82 | * @var RequestParameters 83 | */ 84 | private $requestParameters; 85 | 86 | /** 87 | * @var ValidatorList 88 | */ 89 | private $verifyValidatorList = []; 90 | 91 | /** 92 | * VerifyReCaptcha constructor. 93 | * 94 | * @param RequestMethodInterface $requestMethod 95 | * @param RequestParameters $requestParameters 96 | * @param ValidatorList $verifyValidatorList 97 | */ 98 | public function __construct( 99 | RequestMethodInterface $requestMethod, 100 | RequestParameters $requestParameters, 101 | ValidatorList $verifyValidatorList 102 | ) { 103 | $this->requestMethod = $requestMethod; 104 | $this->requestParameters = $requestParameters; 105 | $this->verifyValidatorList = $verifyValidatorList; 106 | } 107 | 108 | /** 109 | * Calls the reCAPTCHA siteverify API to verify whether the user passes 110 | * CAPTCHA test and additionally runs any specified additional checks 111 | * 112 | * @param string $response The user response token provided by reCAPTCHA, verifying the user on your site. 113 | * @param string $remoteIp The end user's IP address. 114 | * 115 | * @return Response 116 | */ 117 | public function verify($response, $remoteIp = null): Response 118 | { 119 | $params = $this->requestParameters 120 | ->setSecret($this->getSecret()) 121 | ->setResponse((string)$response) 122 | ->setRemoteIp($remoteIp); 123 | 124 | $answer = $this->requestMethod->submit(self::SITE_VERIFY_URL, $params); 125 | $initialResponse = Response::fromJson($answer); 126 | $validationErrors = []; 127 | 128 | foreach ($this->verifyValidatorList as $item) { 129 | if (!$item instanceof ValidatorInterface) { 130 | $validationErrors[] = self::BAD_VALIDATOR; 131 | continue; 132 | } 133 | 134 | if ($error = $item->validate($this, $initialResponse)) { 135 | $validationErrors[] = $error; 136 | } 137 | } 138 | 139 | if (empty($validationErrors)) { 140 | return $initialResponse; 141 | } 142 | 143 | return new Response( 144 | false, 145 | array_merge($initialResponse->getErrorCodes(), $validationErrors), 146 | $initialResponse->getHostname(), 147 | $initialResponse->getChallengeTs(), 148 | $initialResponse->getScore(), 149 | $initialResponse->getAction() 150 | ); 151 | } 152 | 153 | /** 154 | * @return string 155 | */ 156 | public function getSecret(): string 157 | { 158 | return $this->secret; 159 | } 160 | 161 | /** 162 | * @param string $secret 163 | * 164 | * @return VerifyReCaptcha 165 | */ 166 | public function setSecret(string $secret): VerifyReCaptcha 167 | { 168 | $this->secret = $secret; 169 | 170 | return $this; 171 | } 172 | 173 | /** 174 | * Provide a hostname to match against in verify() 175 | * This should be without a protocol or trailing slash, e.g. www.google.com 176 | * 177 | * @param string $hostname Expected hostname 178 | * 179 | * @return VerifyReCaptcha 180 | */ 181 | public function setExpectedHostname($hostname): VerifyReCaptcha 182 | { 183 | $this->hostname = $hostname; 184 | 185 | return $this; 186 | } 187 | 188 | /** 189 | * @return string 190 | */ 191 | public function getExpectedHostname(): ?string 192 | { 193 | return $this->hostname; 194 | } 195 | 196 | /** 197 | * Provide an action to match against in verify() 198 | * This should be set per page. 199 | * 200 | * @param string $action Expected action 201 | * 202 | * @return VerifyReCaptcha 203 | */ 204 | public function setExpectedAction($action): VerifyReCaptcha 205 | { 206 | $this->action = $action; 207 | 208 | return $this; 209 | } 210 | 211 | /** 212 | * @return string 213 | */ 214 | public function getExpectedAction(): ?string 215 | { 216 | return $this->action; 217 | } 218 | 219 | /** 220 | * Provide a threshold to meet or exceed in verify() 221 | * Threshold should be a float between 0 and 1 which will be tested as response >= threshold. 222 | * 223 | * @param float $threshold Expected threshold 224 | * 225 | * @return VerifyReCaptcha 226 | */ 227 | public function setScoreThreshold($threshold): VerifyReCaptcha 228 | { 229 | $this->threshold = floatval($threshold); 230 | 231 | return $this; 232 | } 233 | 234 | /** 235 | * @return float|null 236 | */ 237 | public function getScoreThreshold(): ?float 238 | { 239 | return $this->threshold; 240 | } 241 | 242 | /** 243 | * Provide a timeout in seconds to test against the challenge timestamp in verify() 244 | * 245 | * @param int $timeoutSeconds Expected hostname 246 | * 247 | * @return VerifyReCaptcha 248 | */ 249 | public function setChallengeTimeout(int $timeoutSeconds): VerifyReCaptcha 250 | { 251 | $this->timeoutSeconds = $timeoutSeconds; 252 | 253 | return $this; 254 | } 255 | 256 | /** 257 | * @return int 258 | */ 259 | public function getChallengeTimeout(): ?int 260 | { 261 | return $this->timeoutSeconds; 262 | } 263 | } 264 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/AbstractScoreThreshold.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend; 13 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 14 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 15 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThresholdInterface; 16 | 17 | /** 18 | * Class AbstractScoreThreshold 19 | */ 20 | abstract class AbstractScoreThreshold implements ScoreThresholdInterface 21 | { 22 | /** 23 | * @var General 24 | */ 25 | private $generalConfig; 26 | 27 | /** 28 | * @var Frontend 29 | */ 30 | private $frontendConfig; 31 | 32 | /** 33 | * @var Backend 34 | */ 35 | private $backendConfig; 36 | 37 | /** 38 | * AbstractScoreThreshold constructor. 39 | * 40 | * @param General $generalConfig 41 | * @param Frontend $frontendConfig 42 | * @param Backend $backendConfig 43 | */ 44 | public function __construct( 45 | General $generalConfig, 46 | Frontend $frontendConfig, 47 | Backend $backendConfig 48 | ) { 49 | $this->generalConfig = $generalConfig; 50 | $this->frontendConfig = $frontendConfig; 51 | $this->backendConfig = $backendConfig; 52 | } 53 | 54 | /** 55 | * @return General 56 | */ 57 | public function getGeneralConfig(): General 58 | { 59 | return $this->generalConfig; 60 | } 61 | 62 | /** 63 | * @return Frontend 64 | */ 65 | public function getFrontendConfig(): Frontend 66 | { 67 | return $this->frontendConfig; 68 | } 69 | 70 | /** 71 | * @return Backend 72 | */ 73 | public function getBackendConfig(): Backend 74 | { 75 | return $this->backendConfig; 76 | } 77 | } -------------------------------------------------------------------------------- /Model/ScoreThreshold/Backend/ForgotPassword.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Backend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class ForgotPassword 16 | */ 17 | class ForgotPassword extends AbstractScoreThreshold 18 | { 19 | public function getValue(): float 20 | { 21 | return $this->getBackendConfig()->getScoreThresholdForgot(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Backend/Login.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Backend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class Login 16 | */ 17 | class Login extends AbstractScoreThreshold 18 | { 19 | public function getValue(): float 20 | { 21 | return $this->getBackendConfig()->getScoreThresholdLogin(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/Contact.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class Contact 16 | */ 17 | class Contact extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdContact(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/CustomerCreate.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class CustomerCreate 16 | */ 17 | class CustomerCreate extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdCustomerCreate(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/CustomerForgotPassword.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class CustomerForgotPassword 16 | */ 17 | class CustomerForgotPassword extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdCustomerForgot(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/CustomerLogin.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class CustomerLogin 16 | */ 17 | class CustomerLogin extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdCustomerLogin(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/Newsletter.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class Newsletter 16 | */ 17 | class Newsletter extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdNewsletter(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/ProductReview.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class ProductReview 16 | */ 17 | class ProductReview extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdProductReview(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThreshold/Frontend/SendFriend.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Frontend; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\AbstractScoreThreshold; 13 | 14 | /** 15 | * Class SendFriend 16 | */ 17 | class SendFriend extends AbstractScoreThreshold 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function getValue(): float 23 | { 24 | return $this->getFrontendConfig()->getScoreThresholdSendFriend(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/ScoreThresholdInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model; 11 | 12 | /** 13 | * Class ScoreThresholdInterface 14 | */ 15 | interface ScoreThresholdInterface 16 | { 17 | public function getValue(): float; 18 | } -------------------------------------------------------------------------------- /Model/Verify/Adminhtml/ForgotPassword.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify\Adminhtml; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class ForgotPassword 16 | */ 17 | class ForgotPassword extends AbstractCheckEnabledVerify 18 | { 19 | public function verify(): bool 20 | { 21 | return parent::verify() && $this->getBackendConfig()->hasEnabledForgot(); 22 | } 23 | } -------------------------------------------------------------------------------- /Model/Verify/Adminhtml/Login.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify\Adminhtml; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class Login 16 | */ 17 | class Login extends AbstractCheckEnabledVerify 18 | { 19 | public function verify(): bool 20 | { 21 | return parent::verify() && $this->getBackendConfig()->hasEnabledLogin(); 22 | } 23 | } -------------------------------------------------------------------------------- /Model/Verify/Contact.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class Contact 16 | */ 17 | class Contact extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledContact(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/CustomerCreate.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class CustomerCreate 16 | */ 17 | class CustomerCreate extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledCustomerCreate(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/CustomerForgotPassword.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class CustomerForgotPassword 16 | */ 17 | class CustomerForgotPassword extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledCustomerForgot(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/CustomerLogin.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class CustomerLogin 16 | */ 17 | class CustomerLogin extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledCustomerLogin(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/Newsletter.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class Newsletter 16 | */ 17 | class Newsletter extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledNewsletter(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/ProductReview.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class ProductReview 16 | */ 17 | class ProductReview extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledProductReview(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Model/Verify/SendFriend.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Model\Verify; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\AbstractCheckEnabledVerify; 13 | 14 | /** 15 | * Class SendFriend 16 | */ 17 | class SendFriend extends AbstractCheckEnabledVerify 18 | { 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function verify(): bool 23 | { 24 | return parent::verify() && $this->getFrontendConfig()->hasEnabledSendFriend(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Observer/Captcha.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Observer; 9 | 10 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 11 | use Hryvinskyi\InvisibleCaptcha\Model\CaptchaInterface; 12 | use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha; 13 | use Magento\Framework\App\Action\Action; 14 | use Magento\Framework\Event\Observer; 15 | use Magento\Framework\Event\ObserverInterface; 16 | 17 | class Captcha implements ObserverInterface 18 | { 19 | /** 20 | * @var General 21 | */ 22 | private $config; 23 | 24 | /** 25 | * @var VerifyReCaptcha 26 | */ 27 | private $verifyReCaptcha; 28 | 29 | /** 30 | * @var CaptchaInterface 31 | */ 32 | private $provider; 33 | 34 | /** 35 | * Action constructor. 36 | * 37 | * @param General $config 38 | * @param VerifyReCaptcha $verifyReCaptcha 39 | * @param CaptchaInterface $provider 40 | */ 41 | public function __construct( 42 | General $config, 43 | VerifyReCaptcha $verifyReCaptcha, 44 | CaptchaInterface $provider 45 | ) { 46 | $this->config = $config; 47 | $this->verifyReCaptcha = $verifyReCaptcha; 48 | $this->provider = $provider; 49 | } 50 | 51 | /** 52 | * @param Observer $observer 53 | */ 54 | public function execute(Observer $observer) 55 | { 56 | if ($this->provider->isEnabled() && $_SERVER['REQUEST_METHOD'] !== 'GET') { 57 | $verifyReCaptcha = $this->verifyReCaptcha 58 | ->setSecret($this->config->getSecretKey()) 59 | ->setExpectedAction($this->provider->getAction()) 60 | ->setScoreThreshold($this->provider->getScoreThreshold()) 61 | ->verify($this->provider->getToken()); 62 | 63 | if ($verifyReCaptcha->isSuccess() === false) { 64 | /** @var Action|null $controller */ 65 | $controller = $observer->getData('controller_action'); 66 | $this->provider->getFailure()->execute($verifyReCaptcha, $controller ? $controller->getResponse() : null); 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Plugin/Block/Account/AuthenticationPopupPlugin.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | namespace Hryvinskyi\InvisibleCaptcha\Plugin\Block\Account; 9 | 10 | use Hryvinskyi\Base\Helper\Json; 11 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings; 14 | use Magento\Customer\Block\Account\AuthenticationPopup; 15 | 16 | class AuthenticationPopupPlugin 17 | { 18 | /** 19 | * @var LayoutSettings 20 | */ 21 | private $layoutSettings; 22 | 23 | /** 24 | * @var General 25 | */ 26 | private $generalConfig; 27 | 28 | /** 29 | * @var Frontend 30 | */ 31 | private $frontendConfig; 32 | 33 | /** 34 | * AuthenticationPopupPlugin constructor. 35 | * 36 | * @param LayoutSettings $layoutSettings 37 | * @param General $generalConfig 38 | * @param Frontend $frontendConfig 39 | */ 40 | public function __construct( 41 | LayoutSettings $layoutSettings, 42 | General $generalConfig, 43 | Frontend $frontendConfig 44 | ) { 45 | $this->layoutSettings = $layoutSettings; 46 | $this->generalConfig = $generalConfig; 47 | $this->frontendConfig = $frontendConfig; 48 | } 49 | 50 | /** 51 | * @param AuthenticationPopup $subject 52 | * @param string $result 53 | * 54 | * @return string 55 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 56 | */ 57 | public function afterGetJsLayout(AuthenticationPopup $subject, $result) 58 | { 59 | $layout = Json::decode($result); 60 | $layout['components']['authenticationPopup']['children']['invisible-captcha']['config'] 61 | = $this->layoutSettings->getCaptchaSettings(); 62 | 63 | if ( 64 | ( 65 | !$this->generalConfig->hasEnabled() 66 | || !$this->frontendConfig->hasEnabled() 67 | || !$this->frontendConfig->hasEnabledCustomerLogin() 68 | ) 69 | && isset($layout['components']['authenticationPopup']['children']['invisible-captcha']) 70 | ) { 71 | unset($layout['components']['authenticationPopup']['children']['invisible-captcha']); 72 | } 73 | 74 | return Json::encode($layout); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Plugin/Block/ContactForm/AddFormAdditionalInfoIfMissing.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Plugin\Block\ContactForm; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Model\Verify\Contact; 13 | use Magento\Framework\Pricing\Render; 14 | use Magento\Contact\Block\ContactForm as Subject; 15 | use Psr\Log\LoggerInterface; 16 | 17 | /** 18 | * Class AddFormAdditionalInfoIfMissing 19 | */ 20 | class AddFormAdditionalInfoIfMissing 21 | { 22 | /** 23 | * @var LoggerInterface 24 | */ 25 | private $logger; 26 | 27 | /** 28 | * @var Contact 29 | */ 30 | private $verifyContact; 31 | 32 | /** 33 | * AddFormAdditionalInfoIfMissing constructor. 34 | * 35 | * @param Contact $verifyContact 36 | * @param LoggerInterface $logger 37 | */ 38 | public function __construct( 39 | Contact $verifyContact, 40 | LoggerInterface $logger 41 | ) { 42 | $this->verifyContact = $verifyContact; 43 | $this->logger = $logger; 44 | } 45 | 46 | public function beforeToHtml( 47 | Subject $subject 48 | ) { 49 | try { 50 | $childrens = $subject->getChildNames(); 51 | 52 | if (in_array('form.additional.info', $childrens) === false && $this->verifyContact->verify() === true) { 53 | $subject->getLayout()->addContainer( 54 | 'form.additional.info', 55 | 'Form Additional Info', 56 | [], 57 | $subject->getNameInLayout(), 58 | 'form.additional.info' 59 | ); 60 | 61 | 62 | $block = $subject->getLayout()->createBlock( 63 | \Hryvinskyi\InvisibleCaptcha\Block\Captcha::class, 64 | $subject->getNameInLayout() . '.invisible.recaptcha', 65 | [ 66 | 'data' => [ 67 | 'template' => 'Hryvinskyi_InvisibleCaptcha::captcha.phtml', 68 | 'jsLayout' => [ 69 | 'components' => [ 70 | 'invisible-captcha' => [ 71 | 'component' =>'Hryvinskyi_InvisibleCaptcha/js/invisible-captcha', 72 | 'action' => 'contact', 73 | 'captchaId' => 'contact' 74 | ] 75 | ] 76 | ] 77 | ] 78 | ] 79 | ); 80 | $subject->setChild('form.additional.info', $block); 81 | } 82 | } catch (\Throwable $exception) { 83 | $this->logger->critical($exception->getMessage(), $exception->getTrace()); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Plugin/DisableSubmit.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | declare(strict_types=1); 9 | 10 | namespace Hryvinskyi\InvisibleCaptcha\Plugin; 11 | 12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; 13 | use Magento\Framework\App\Request\Http as RequestHttp; 14 | use Magento\Framework\App\ResponseInterface; 15 | use Magento\Framework\Controller\ResultInterface; 16 | use Psr\Log\LoggerInterface; 17 | use Symfony\Component\DomCrawler\Crawler; 18 | use voku\helper\HtmlDomParser; 19 | use voku\helper\SimpleHtmlDom; 20 | use voku\helper\SimpleHtmlDomInterface; 21 | 22 | /** 23 | * Class DisableSubmit 24 | */ 25 | class DisableSubmit 26 | { 27 | /** 28 | * @var RequestHttp 29 | */ 30 | private $request; 31 | 32 | /** 33 | * @var General 34 | */ 35 | private $generalConfig; 36 | 37 | /** 38 | * @var LoggerInterface 39 | */ 40 | private $logger; 41 | 42 | /** 43 | * MergeJson constructor. 44 | * 45 | * @param RequestHttp $request 46 | * @param General $generalConfig 47 | * @param LoggerInterface $logger 48 | */ 49 | public function __construct( 50 | RequestHttp $request, 51 | General $generalConfig, 52 | LoggerInterface $logger 53 | ) { 54 | $this->request = $request; 55 | $this->generalConfig = $generalConfig; 56 | $this->logger = $logger; 57 | } 58 | 59 | /** 60 | * @param ResultInterface $subject 61 | * @param \Closure $proceed 62 | * @param ResponseInterface $response 63 | * 64 | * @return string 65 | */ 66 | public function aroundRenderResult( 67 | ResultInterface $subject, 68 | \Closure $proceed, 69 | ResponseInterface $response 70 | ) { 71 | $result = $proceed($response); 72 | 73 | if ( 74 | PHP_SAPI === 'cli' || 75 | $this->request->isXmlHttpRequest() === true || 76 | $this->generalConfig->isDisabledSubmitForm() === false 77 | ) { 78 | return $result; 79 | } 80 | 81 | $html = $response->getBody(); 82 | $dom = new HtmlDomParser(); 83 | 84 | $dom->overwriteSpecialScriptTags([ 85 | 'text/html', 86 | 'text/x-magento-template', 87 | 'text/x-custom-template', 88 | 'text/x-handlebars-template', 89 | ]); 90 | 91 | $dom = $dom->loadHtml($html); 92 | 93 | try { 94 | $elements = $dom->findMultiOrFalse('[data-hryvinskyi-recaptcha="default"]'); 95 | 96 | if ($elements !== false) { 97 | foreach ($elements as $element) { 98 | $form = $this->closest($element, 'form'); 99 | 100 | $form->setAttribute('onsubmit', 'return false;' . 101 | $form->getAttribute('onsubmit')); 102 | $form->setAttribute('class', $form->getAttribute('class') . 103 | ' hryvinskyi-recaptcha-disabled-submit'); 104 | } 105 | } 106 | 107 | $elementsTarget = $dom->findMultiOrFalse('[data-hryvinskyi-recaptcha="target"]'); 108 | 109 | if ($elementsTarget !== false) { 110 | foreach ($elementsTarget as $element) { 111 | $target = $dom->findOneOrFalse($element->getAttribute('data-hryvinskyi-recaptcha-target')); 112 | if ($target !== false) { 113 | $target->setAttribute('onsubmit', 'return false;' . 114 | $target->getAttribute('onsubmit')); 115 | $target->setAttribute('class', $target->getAttribute('class') . 116 | ' hryvinskyi-recaptcha-disabled-submit'); 117 | } 118 | } 119 | } 120 | 121 | if ($elements !== false || $elementsTarget !== false) { 122 | $response->setBody((string)$dom); 123 | } 124 | } catch (\Throwable $e) { 125 | $response->setBody($html); 126 | $this->logger->critical($e->getMessage()); 127 | } 128 | 129 | return $result; 130 | } 131 | 132 | /** 133 | * Return first parents (heading toward the document root) of the Element that matches the provided selector. 134 | * 135 | * @param SimpleHtmlDomInterface $element 136 | * @param string $selector 137 | * @return \DOMNode|SimpleHtmlDom 138 | */ 139 | public function closest($element, string $selector) 140 | { 141 | $domNode = $element->getNode(); 142 | 143 | while (XML_ELEMENT_NODE === $domNode->nodeType) { 144 | $symfonyNode = new Crawler($domNode); 145 | $domNode = new SimpleHtmlDom($domNode); 146 | 147 | if ($symfonyNode->matches($selector)) { 148 | return $domNode; 149 | } 150 | 151 | $domNode = $symfonyNode->getNode(0)->parentNode; 152 | } 153 | 154 | return $domNode; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Invisible Captcha v3 for magento 2 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/scriptua/magento2-invisible-captcha/v/stable)](https://packagist.org/packages/scriptua/magento2-invisible-captcha) 4 | [![Total Downloads](https://poser.pugx.org/scriptua/magento2-invisible-captcha/downloads)](https://packagist.org/packages/scriptua/magento2-invisible-captcha) 5 | [![PayPal donate button](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=legionerblack%40yandex%2eru&lc=UA&item_name=Magento%202%20Invisible%20Captcha¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted "Donate once-off to this project using Paypal") 6 | [![Latest Unstable Version](https://poser.pugx.org/scriptua/magento2-invisible-captcha/v/unstable)](https://packagist.org/packages/scriptua/magento2-invisible-captcha) 7 | [![License](https://poser.pugx.org/scriptua/magento2-invisible-captcha/license)](https://packagist.org/packages/scriptua/magento2-invisible-captcha) 8 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/hryvinskyi/magento2-invisible-captcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/hryvinskyi/magento2-invisible-captcha/?branch=master) 9 | [![Build Status](https://scrutinizer-ci.com/g/hryvinskyi/magento2-invisible-captcha/badges/build.png?b=master)](https://scrutinizer-ci.com/g/hryvinskyi/magento2-invisible-captcha/build-status/master) 10 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fhryvinskyi%2Fmagento2-invisible-captcha.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhryvinskyi%2Fmagento2-invisible-captcha?ref=badge_shield) 11 | 12 | Module version 2.0.\*||2.1.\*||2.2.\* support Magento 2.3.* 13 | Module version 1.0.* support Magento 2.1.\*||2.2.\* 14 | 15 | ## Features 16 | 1. Lazy Load, google page speed improvements 17 | 2. Easy to add captcha to your custom form 18 | 3. AJAX forms supported 19 | 4. Knockout forms supported 20 | 5. Refreshing invalid token after a long period of inactivity on the site and after ajax form submitted 21 | 22 | ## Frontend Forms 23 | * Login 24 | * Register 25 | * Forgot password 26 | * Contact 27 | * Newsletter 28 | * Send to Friend 29 | 30 | 31 | ## Backend Forms 32 | * Login 33 | * Forgot password 34 | 35 | ## Installation Guide 36 | ### Install by composer 37 | ``` 38 | composer require hryvinskyi/magento2-invisible-captcha 39 | bin/magento module:enable Hryvinskyi_Base 40 | bin/magento module:enable Hryvinskyi_InvisibleCaptcha 41 | bin/magento setup:upgrade 42 | ``` 43 | 44 | ### Install download package 45 | 1. Download module https://github.com/hryvinskyi/magento2-base [Link](https://github.com/hryvinskyi/magento2-base/archive/v1.1.2.zip) 46 | 2. Download this module [Link](https://github.com/hryvinskyi/magento2-invisible-captcha/archive/2.0.4.zip) 47 | 3. Unzip two modules in the folder app\code\Hryvinskyi\Base and app\code\Hryvinskyi\InvisibleCaptcha 48 | 4. Run commands: 49 | 50 | ``` 51 | bin/magento module:enable Hryvinskyi_Base 52 | bin/magento module:enable Hryvinskyi_InvisibleCaptcha 53 | bin/magento setup:upgrade 54 | ``` 55 | 5. Configure module in admin panel 56 | 57 | ### Command-line: 58 | 59 | ``` 60 | php bin/magento hryvinskyi:invisible-captcha:disable --website_id= 61 | ``` 62 | 63 | This command will disable invisible captcha for the area and/or website_id. 64 | 65 | * area = [global, frontend, adminhtml] 66 | * website_id = ID Website 67 | 68 | # General Settings 69 | 70 | [![Configuration](https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/master/screenshots/admin_configuration.pngs)](https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/master/screenshots/admin_configuration.png) 71 | 72 | 73 | ## LazyLoad Speed Test 74 | [![LazyLoad Speed](https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/master/screenshots/lazy_load.jpg)](https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/master/screenshots/lazy_load.jpg) 75 | 76 | 77 | ## License 78 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fhryvinskyi%2Fmagento2-invisible-captcha.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhryvinskyi%2Fmagento2-invisible-captcha?ref=badge_large) 79 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hryvinskyi/magento2-invisible-captcha", 3 | "description": "Magento 2 Google Invisible Captcha (Recaptcha) module", 4 | "require": { 5 | "ext-dom": "*", 6 | "php": "~7.1.3||~7.2.0||~7.3.0||~7.4.0||~8.0||~8.1||~8.2||~8.3", 7 | "symfony/dom-crawler": ">=2.7 <5.0", 8 | "voku/simple_html_dom": "4.8.6", 9 | "magento/framework": "*", 10 | "magento/module-store": "*", 11 | "magento/module-customer": "*", 12 | "magento/module-checkout": "*", 13 | "hryvinskyi/magento2-base": "2.1.3" 14 | }, 15 | "require-dev": { 16 | "roave/security-advisories": "dev-latest" 17 | }, 18 | "type": "magento2-module", 19 | "version": "2.4.9", 20 | "license": "MIT", 21 | "authors": [ 22 | { 23 | "name": "Volodymyr Hryvinskyi", 24 | "email": "volodymyr@hryvinskyi.com" 25 | } 26 | ], 27 | "autoload": { 28 | "files": [ "registration.php" ], 29 | "psr-4": { 30 | "Hryvinskyi\\InvisibleCaptcha\\": "" 31 | } 32 | }, 33 | "config": { 34 | "secure-http": false 35 | }, 36 | "repositories": [ 37 | { 38 | "type": "composer", 39 | "url": "http://repo.magento.com/" 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /etc/acl.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /etc/adminhtml/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | adminhtml 14 | 15 | 16 | 17 | 18 | 19 | adminhtml 20 | 21 | 22 | 23 | 25 | 26 | Hryvinskyi\InvisibleCaptcha\Model\Provider\Adminhtml\LoginCaptcha 27 | 28 | 29 | 30 | 32 | 33 | Hryvinskyi\InvisibleCaptcha\Model\Provider\Adminhtml\ForgotPasswordCaptcha 34 | 35 | 36 | 37 | 39 | 40 | Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\RedirectUrl\RefererProvider 41 | 42 | 43 | 44 | 46 | 47 | admin_login 48 | Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Backend\Login 50 | Hryvinskyi\InvisibleCaptcha\Model\Verify\Adminhtml\Login 52 | Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\AuthenticationExceptionFailure 54 | Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponse\General 56 | 57 | 58 | 59 | 61 | 62 | admin_forgot_password 63 | Hryvinskyi\InvisibleCaptcha\Model\ScoreThreshold\Backend\ForgotPassword 65 | Hryvinskyi\InvisibleCaptcha\Model\Verify\Adminhtml\ForgotPassword 67 | Hryvinskyi\InvisibleCaptcha\Model\Provider\Failure\Backend\ForgotPassword 69 | Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponse\General 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /etc/adminhtml/events.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 12 | 13 | 14 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 |
13 | 14 | hryvinskyi 15 | Hryvinskyi_InvisibleCaptcha::invisible_captcha 16 | 17 | 18 | 20 | 21 | Magento\Config\Model\Config\Source\Yesno 22 | 23 | 25 | 26 | 27 | here 30 | ]]> 31 | 32 | 33 | 1 34 | 35 | 36 | 38 | 39 | 40 | here 43 | ]]> 44 | 45 | 46 | 1 47 | 48 | 49 | 51 | 52 | Magento\Config\Model\Config\Source\Yesno 53 | 54 | 57 | 58 | 59 | 61 | 62 | Magento\Config\Model\Config\Source\Yesno 63 | Disable form submit before loading recaptcha 64 | 65 | 67 | 68 | Magento\Config\Model\Config\Source\Yesno 69 | 70 | 71 | This site is protected by reCAPTCHA and the Google 72 | Privacy Policy and 73 | Terms of Service apply.
74 | For example:
75 | reCAPTCHA 76 | ]]> 77 |
78 |
79 | 81 | 82 | 83 | 1 84 | 85 | 86 | 87 | 88 | Magento\Config\Model\Config\Source\Yesno 89 | 90 |
91 | 93 | 94 | 96 | 97 | Magento\Config\Model\Config\Source\Yesno 98 | 99 | 101 | 102 | Magento\Config\Model\Config\Source\Yesno 103 | 104 | 1 105 | 106 | 107 | 109 | 110 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 111 | 112 | 1 113 | 114 | 115 | 117 | 118 | Magento\Config\Model\Config\Source\Yesno 119 | 120 | 1 121 | 122 | 123 | 125 | 126 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 127 | 128 | 1 129 | 130 | 131 | 133 | 134 | Magento\Config\Model\Config\Source\Yesno 135 | 136 | 1 137 | 138 | 139 | 141 | 142 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 143 | 144 | 1 145 | 146 | 147 | 149 | 150 | Magento\Config\Model\Config\Source\Yesno 151 | 152 | 1 153 | 154 | 155 | 157 | 158 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 159 | 160 | 1 161 | 162 | 163 | 165 | 166 | Magento\Config\Model\Config\Source\Yesno 167 | 168 | 1 169 | 170 | 171 | 173 | 174 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 175 | 176 | 1 177 | 178 | 179 | 181 | 182 | Magento\Config\Model\Config\Source\Yesno 183 | 184 | 1 185 | 186 | 187 | 189 | 190 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 191 | 192 | 1 193 | 194 | 195 | 197 | 198 | Magento\Config\Model\Config\Source\Yesno 199 | 200 | 1 201 | 202 | 203 | 205 | 206 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 207 | 208 | 1 209 | 210 | 211 | 212 | 214 | 215 | 217 | 218 | Magento\Config\Model\Config\Source\Yesno 219 | 220 | 222 | 223 | Magento\Config\Model\Config\Source\Yesno 224 | 225 | 227 | 228 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 229 | 230 | 1 231 | 232 | 233 | 235 | 236 | Magento\Config\Model\Config\Source\Yesno 237 | 238 | 240 | 241 | Hryvinskyi\InvisibleCaptcha\Model\Config\Source\ScoreThreshold 242 | 243 | 1 244 | 245 | 246 | 247 |
248 |
249 |
250 | -------------------------------------------------------------------------------- /etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 0 14 | 15 | 0 16 | 1 17 | 0 18 | Privacy Policy and 20 | Terms of Service apply.]]> 21 | 1 22 | 23 | 24 | 0 25 | 0 26 | 0.5 27 | 0 28 | 0.5 29 | 0 30 | 0.5 31 | 0 32 | 0.5 33 | 0 34 | 0.5 35 | 0 36 | 0.5 37 | 38 | 39 | 0 40 | 0.5 41 | 0 42 | 0.5 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /etc/csp_whitelist.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | www.google.com 13 | www.gstatic.com 14 | 15 | 16 | 17 | 18 | www.google.com 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /etc/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 13 | 15 | 17 | 18 | 19 | 20 | 21 | 1 22 | 1 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | Captcha Error: Invalid Json. 32 | Captcha Error: Connection failed. 34 | Captcha Error: Bad response. 36 | Captcha Error: Unknown error. 38 | Captcha Error: The response parameter is missing. 40 | Captcha Error: Hostname mismatch. 42 | Captcha Error: Action mismatch. 44 | Captcha Error: Score Threshold not met. 46 | Captcha Error: Challenge timeout. 48 | Captcha Error: The response is no longer valid: either is too old or has been used previously. 50 | Captcha Error: The response parameter is invalid or malformed. 52 | Captcha Error: The request is invalid or malformed. 54 | Captcha Error: The secret parameter is invalid or malformed. 56 | Captcha Error: The secret parameter is missing. 58 | Captcha Error: Validator must implements 60 | \Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\ValidatorInterface. 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\Host 70 | Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\Action 71 | Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\Threshold 72 | Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators\TimeoutSeconds 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | Hryvinskyi\InvisibleCaptcha\Helper\Config\General 81 | Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend 82 | Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend 83 | 84 | 85 | 86 | 87 | 88 | 89 | verifyValidatorList 90 | 91 | 92 | 93 | 94 | 95 | captchaAreaConfigList 96 | 97 | 98 | 99 | 100 | 101 | 102 | \Hryvinskyi\InvisibleCaptcha\Command\Captcha 103 | 104 | 105 | 106 | 107 | 108 | 110 | 111 | 112 | 113 | 114 | 115 | Hryvinskyi\InvisibleCaptcha\Model\Debug 116 | 117 | 118 | 119 | 120 | 121 | 122 | Hryvinskyi\InvisibleCaptcha\Model\Logger 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /etc/frontend/events.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 13 | 14 | 15 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 29 | 30 | 31 | 33 | 34 | 35 | 37 | 38 | 39 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /registration.php: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | \Magento\Framework\Component\ComponentRegistrar::register( 9 | \Magento\Framework\Component\ComponentRegistrar::MODULE, 10 | 'Hryvinskyi_InvisibleCaptcha', 11 | __DIR__ 12 | ); 13 | -------------------------------------------------------------------------------- /screenshots/admin_configuration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/bf37d1af8dd0b0aa474101c9bdfa01d175dc4a83/screenshots/admin_configuration.png -------------------------------------------------------------------------------- /screenshots/lazy_load.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hryvinskyi/magento2-invisible-captcha/bf37d1af8dd0b0aa474101c9bdfa01d175dc4a83/screenshots/lazy_load.jpg -------------------------------------------------------------------------------- /view/adminhtml/layout/adminhtml_auth_forgotpassword.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 20 | admin_forgot_password 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /view/adminhtml/layout/adminhtml_auth_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 20 | admin_login 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /view/base/templates/captcha.phtml: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | /** 9 | * @var \Hryvinskyi\InvisibleCaptcha\Block\Captcha $block 10 | * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer 11 | */ 12 | ?> 13 |
15 | 16 | isHideBadge()): ?> 17 | renderTag('style', [], '.grecaptcha-badge {display:none;}', false) ?> 18 |
getHideBadgeText() ?>
19 | 20 |
21 | isLazyLoad()): ?> 22 | { 25 | const field = document.getElementById('{$block->getWidgetId()}'); 26 | const form = field.closest('form'); 27 | 28 | if (form !== null) { 29 | form.classList.add('hryvinskyi-recaptcha-disabled-submit'); 30 | } 31 | })(); 32 | JS; 33 | 34 | echo $secureRenderer->renderTag( 35 | 'script', 36 | [ 37 | 'data-pagespeed-ignore-merge' => null, 38 | 'data-ignore-extreme-lazy-load' => null, 39 | ], 40 | $scriptContent, 41 | false 42 | ); 43 | ?> 44 | 45 | 52 | -------------------------------------------------------------------------------- /view/base/web/css/source/_module.less: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved. 3 | * @author: 4 | * @github: 5 | */ 6 | 7 | // 8 | // Variables 9 | // --------------------------------------------- 10 | 11 | // 12 | // Common 13 | // _____________________________________________ 14 | 15 | & when (@media-common = true) { 16 | .hryvinskyi-recaptcha-disabled-submit { 17 | [type="submit"], 18 | .form-actions .actions .action-primary { 19 | pointer-events: none; 20 | cursor: not-allowed; 21 | opacity: .7; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /view/base/web/js/invisible-captcha.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. 3 | * @author: 4 | * @github: 5 | */ 6 | 7 | define([ 8 | 'jquery', 9 | 'ko', 10 | 'uiComponent', 11 | './model/invisible-captcha' 12 | ], function ($, ko, Component, invisibleCaptcha) { 13 | 'use strict'; 14 | 15 | return Component.extend({ 16 | defaults: { 17 | template: 'Hryvinskyi_InvisibleCaptcha/invisible-captcha', 18 | action: '', 19 | siteKey: '', 20 | captchaId: '', 21 | lazyLoad: false 22 | }, 23 | _initializedForms: [], 24 | 25 | /** 26 | * Initialization 27 | */ 28 | initialize: function () { 29 | this._super(); 30 | this._loadGoogleApi(); 31 | }, 32 | 33 | /** 34 | * Initialize Google ReCaptca Script 35 | * 36 | * @private 37 | */ 38 | _loadGoogleApi: function () { 39 | var self = this; 40 | 41 | if (invisibleCaptcha.isApiLoad() === true) { 42 | $(window).trigger('recaptcha_api_ready_' + self.captchaId); 43 | 44 | return; 45 | } 46 | 47 | window.onloadCallbackGoogleRecapcha = function () { 48 | invisibleCaptcha.isApiLoaded(true); 49 | invisibleCaptcha.initializeForms.each(function (item) { 50 | self._initializeTokenField(item.element, item.self); 51 | }); 52 | 53 | $(window).trigger('recaptcha_api_ready_' + self.captchaId); 54 | }; 55 | 56 | if (self.lazyLoad === false) { 57 | self._loadRecaptchaScript(); 58 | } 59 | }, 60 | 61 | /** 62 | * Load google recaptcha main script 63 | * 64 | * @private 65 | */ 66 | _loadRecaptchaScript: function () { 67 | if (invisibleCaptcha.isApiLoaded() === false) { 68 | require([ 69 | 'https://www.google.com/recaptcha/api.js?onload=onloadCallbackGoogleRecapcha&render=' + this.siteKey 70 | ]); 71 | 72 | invisibleCaptcha.isApiLoad(true); 73 | } 74 | }, 75 | 76 | /** 77 | * Create form input token 78 | * 79 | * @private 80 | */ 81 | _createToken: function (token, element, action, captchaId) { 82 | $(element).find('[name="hryvinskyi_invisible_token"]').remove(); 83 | var tokenField = $(''); 84 | 85 | tokenField.val(token); 86 | tokenField.attr('data-action', action); 87 | $(element).append(tokenField); 88 | invisibleCaptcha.initializedForms.push(captchaId); 89 | }, 90 | 91 | /** 92 | * Loads google API and triggers event, when loaded 93 | * 94 | * @private 95 | */ 96 | _initializeTokenField: function (element, self) { 97 | if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) === -1) { 98 | var execute = function () { 99 | window.grecaptcha 100 | .execute(self.siteKey, {action: self.action}) 101 | .then(function (token) { 102 | $.proxy(self._createToken(token, element, self.action, self.captchaId), self); 103 | }); 104 | }; 105 | 106 | window.grecaptcha.ready(execute); 107 | setInterval(execute, 90 * 1000); 108 | $(document).on("ajaxComplete", execute); 109 | } 110 | }, 111 | 112 | /** 113 | * Check is recaptcha loaded 114 | * 115 | * @param captchaId 116 | * @returns {boolean} 117 | */ 118 | isRecaptchaLoaded: function (captchaId) { 119 | return invisibleCaptcha.isApiLoaded() && invisibleCaptcha.initializedForms().indexOf(captchaId) !== -1; 120 | }, 121 | 122 | /** 123 | * Initialize recaptcha 124 | * 125 | * @param {Dom} element 126 | * @param {Object} self 127 | */ 128 | initializeCaptcha: function (element, self) { 129 | var form = $(element).closest('form'); 130 | 131 | form.on('submit', function (e) { 132 | form.addClass('hryvinskyi-recaptcha-disabled-submit'); 133 | setTimeout(function () { 134 | if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) !== -1) { 135 | invisibleCaptcha.initializedForms.remove(self.captchaId); 136 | } 137 | 138 | self._initializeTokenField(element, self.siteKey, self.action, self.captchaId); 139 | form.removeClass('hryvinskyi-recaptcha-disabled-submit'); 140 | }, 50); 141 | 142 | return true; 143 | }); 144 | 145 | if (self.lazyLoad === true) { 146 | form.on('focus blur change', ':input', $.proxy(self._loadRecaptchaScript, self)); 147 | 148 | // Disable submit form 149 | form.on('click', ':submit', function (e) { 150 | if (self.isRecaptchaLoaded(self.captchaId) === false) { 151 | form.data('needClickOnSubmit', e.target); 152 | e.preventDefault(); 153 | return false; 154 | } 155 | }); 156 | 157 | form.submit(function (e) { 158 | if (self.isRecaptchaLoaded(self.captchaId) === false) { 159 | form.data('needSubmit', true); 160 | e.preventDefault(); 161 | return false; 162 | } 163 | }); 164 | 165 | // Submit form after recaptcha loaded 166 | invisibleCaptcha.initializedForms.subscribe(function (newValue) { 167 | let isLoaded = newValue.indexOf(self.captchaId) !== -1 && invisibleCaptcha.isApiLoaded() === true; 168 | 169 | if (isLoaded) { 170 | if (form.data('needSubmit')) { 171 | form.submit(); 172 | form.data('needSubmit', null); 173 | return; 174 | } 175 | 176 | if (form.data('needClickOnSubmit')) { 177 | $(form.data('needClickOnSubmit')).trigger('click'); 178 | form.data('needClickOnSubmit', null); 179 | return; 180 | } 181 | } 182 | }); 183 | 184 | if (form.attr('onsubmit') !== undefined) { 185 | form.attr('onsubmit', form.attr('onsubmit').replace(/^.{13}/, '')); 186 | } 187 | 188 | form.removeClass('hryvinskyi-recaptcha-disabled-submit'); 189 | } 190 | 191 | if ((invisibleCaptcha.isApiLoad() === true || self.lazyLoad === true) && invisibleCaptcha.isApiLoaded() !== true) { 192 | invisibleCaptcha.initializeForms.push({'element': element, self: self}); 193 | } else if (invisibleCaptcha.isApiLoaded() === true) { 194 | self._initializeTokenField(element, self.siteKey, self.action, self.captchaId); 195 | } else { 196 | $(window).on('recaptcha_api_ready_' + self.captchaId, function () { 197 | self._initializeTokenField(element, self.siteKey, self.action, self.captchaId); 198 | }); 199 | } 200 | } 201 | }); 202 | }); 203 | -------------------------------------------------------------------------------- /view/base/web/js/model/invisible-captcha.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. 3 | * @author: 4 | * @github: 5 | */ 6 | 7 | define(['ko'], function (ko) { 8 | 'use strict'; 9 | 10 | return { 11 | isApiLoad: ko.observable(false), 12 | isApiLoaded: ko.observable(false), 13 | initializeForms: ko.observableArray([]), 14 | initializedForms: ko.observableArray([]) 15 | }; 16 | }); -------------------------------------------------------------------------------- /view/base/web/template/invisible-captcha.html: -------------------------------------------------------------------------------- 1 | 8 |
-------------------------------------------------------------------------------- /view/frontend/layout/catalog_product_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 20 | product_review 21 | product_review 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /view/frontend/layout/checkout_index_index.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 26 | additional-login-form-fields 27 | checkoutConfig 28 | customer_login_ajax 29 | login_checkout_shipping 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 44 | additional-login-form-fields 45 | checkoutConfig 46 | customer_login_ajax 47 | login_checkout 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /view/frontend/layout/contact_index_index.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 21 | contact 22 | contact 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /view/frontend/layout/customer_account_create.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 21 | customer_create 22 | customer_create 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /view/frontend/layout/customer_account_forgotpassword.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 21 | customer_forgot_password 22 | customer_forgot_password 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /view/frontend/layout/customer_account_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 21 | customer_login 22 | customer_login 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /view/frontend/layout/default.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 20 | newsletter 21 | newsletter 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 36 | additional-login-form-fields 37 | checkoutConfig 38 | customer_login_ajax 39 | customer_login_popup 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /view/frontend/layout/sendfriend_product_send.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | Hryvinskyi_InvisibleCaptcha/js/invisible-captcha 22 | 23 | send_friend 24 | send_friend 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /view/frontend/templates/captcha_newsletter.phtml: -------------------------------------------------------------------------------- 1 | 5 | * @github: 6 | */ 7 | 8 | /** 9 | * @var \Hryvinskyi\InvisibleCaptcha\Block\Captcha $block 10 | * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer 11 | */ 12 | 13 | $scriptContent = <<getWidgetId()}').appendTo('#newsletter-validate-detail'); 16 | }); 17 | JS; 18 | echo $secureRenderer->renderTag( 19 | 'script', 20 | [], 21 | $scriptContent, 22 | false 23 | ); 24 | ?> 25 | 26 |
33 | 34 |
35 | 36 | 43 | --------------------------------------------------------------------------------