94 parent::__construct();
107 private function getFormIdKey($key)
109 return $this->formId .
'_' . $key;
119 return \Magento\Captcha\Block\Captcha\DefaultCaptcha::class;
130 if (($this->isUserAuth()
132 || !$this->isEnabled()
135 $this->getTargetForms()
141 return $this->isShowAlways()
142 || $this->isOverLimitAttempts($login)
143 || $this->session->getData($this->getFormIdKey(
'show_captcha'));
153 $forms = (array)$this->captchaData->getConfig(
'shown_to_logged_in_user');
154 foreach ($forms as
$formId => $isShownToLoggedIn) {
155 if ($isShownToLoggedIn && $this->formId ==
$formId) {
168 private function isOverLimitAttempts($login)
170 return $this->isOverLimitIpAttempt() || $this->isOverLimitLoginAttempts($login);
178 private function getAllowedAttemptsForSameLogin()
180 return (
int)$this->captchaData->getConfig(
'failed_attempts_login');
188 private function getAllowedAttemptsFromSameIp()
190 return (
int)$this->captchaData->getConfig(
'failed_attempts_ip');
198 private function isOverLimitIpAttempt()
200 $countAttemptsByIp = $this->getResourceModel()->countAttemptsByRemoteAddress();
201 return $countAttemptsByIp >= $this->getAllowedAttemptsFromSameIp();
210 private function isOverLimitLoginAttempts($login)
212 if ($login !=
false) {
213 $countAttemptsByLogin = $this->getResourceModel()->countAttemptsByUserLogin($login);
214 return $countAttemptsByLogin >= $this->getAllowedAttemptsForSameLogin();
224 private function isUserAuth()
226 return $this->session->isLoggedIn();
236 return (
string)$this->captchaData->getConfig(
'case_sensitive');
246 $font = (string)$this->captchaData->getConfig(
'font');
247 $fonts = $this->captchaData->getFonts();
249 if (isset($fonts[$font])) {
250 $fontPath = $fonts[$font][
'path'];
252 $fontData = array_shift($fonts);
253 $fontPath = $fontData[
'path'];
266 if (!$this->expiration) {
271 $this->expiration = (int)$this->captchaData->getConfig(
'timeout') * 60;
293 return $this->captchaData->getImgDir();
303 return $this->captchaData->getImgUrl();
314 $storedWord = $this->
getWord();
317 if (!$word || !$storedWord) {
322 $storedWord = strtolower($storedWord);
323 $word = strtolower($word);
325 return $word === $storedWord;
335 return $this->
getImgUrl() . $this->getId() . $this->getSuffix();
346 if ($this->isEnabled() && in_array($this->formId, $this->getTargetForms())) {
347 $this->getResourceModel()->logAttempt($login);
348 if ($this->isOverLimitLoginAttempts($login)) {
368 $this->session->setData($this->getFormIdKey(
'show_captcha'),
$value);
381 $symbols = $this->getSymbols();
383 for (
$i = 0;
$i < $wordLen;
$i++) {
384 $word .= $symbols[array_rand($symbols)];
394 private function getSymbols()
396 return str_split((
string)$this->captchaData->getConfig(
'symbols'));
410 $length = (string)$this->captchaData->getConfig(
'length');
411 if (!is_numeric($length)) {
412 if (preg_match(
'/(\d+)-(\d+)/', $length, $matches)) {
413 $from = (int)$matches[1];
414 $to = (int)$matches[2];
417 $from = (int)$length;
421 if ($to < $from || $from < 1 || $to < 1) {
426 return \Magento\Framework\Math\Random::getRandomNumber($from, $to);
434 private function isShowAlways()
436 $captchaMode = (string)$this->captchaData->getConfig(
'mode');
443 && $this->getAllowedAttemptsForSameLogin() === 0
448 $alwaysFor = $this->captchaData->getConfig(
'always_for');
449 foreach ($alwaysFor as $nodeFormId => $isAlwaysFor) {
450 if ($isAlwaysFor && $this->formId == $nodeFormId) {
463 private function isEnabled()
465 return (
string)$this->captchaData->getConfig(
'enable');
475 private function getTargetForms()
477 $formsString = (string)$this->captchaData->getConfig(
'forms');
478 return explode(
',', $formsString);
488 $sessionData = $this->session->getData($this->getFormIdKey(self::SESSION_WORD));
489 return time() < $sessionData[
'expires'] ? $sessionData[
'data'] :
null;
501 $this->session->setData(
502 $this->getFormIdKey(self::SESSION_WORD),
514 private function clearWord()
516 $this->session->unsetData($this->getFormIdKey(self::SESSION_WORD));
531 return \Magento\Framework\Math\Random::getRandomNumber(280, 300) / 100;
546 protected function gc()
556 private function getResourceModel()
558 return $this->resLogFactory->create();
const DEFAULT_WORD_LENGTH_FROM
setShowCaptchaInSession($value=true)
__construct(\Magento\Framework\Session\SessionManagerInterface $session, \Magento\Captcha\Helper\Data $captchaData, \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory, $formId)
const DEFAULT_WORD_LENGTH_TO