Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultModel.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Captcha\Model;
7 
9 
16 class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model\CaptchaInterface
17 {
21  const SESSION_WORD = 'word';
22 
27 
32 
37  protected $captchaData;
38 
44  protected $expiration;
45 
52  protected $fsize = 22;
53 
59  protected $formId;
60 
65  protected $resLogFactory;
66 
73  protected $keepSession = true;
74 
79  protected $session;
80 
88  public function __construct(
89  \Magento\Framework\Session\SessionManagerInterface $session,
90  \Magento\Captcha\Helper\Data $captchaData,
91  \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
92  $formId
93  ) {
94  parent::__construct();
95  $this->session = $session;
96  $this->captchaData = $captchaData;
97  $this->resLogFactory = $resLogFactory;
98  $this->formId = $formId;
99  }
100 
107  private function getFormIdKey($key)
108  {
109  return $this->formId . '_' . $key;
110  }
111 
117  public function getBlockName()
118  {
119  return \Magento\Captcha\Block\Captcha\DefaultCaptcha::class;
120  }
121 
128  public function isRequired($login = null)
129  {
130  if (($this->isUserAuth()
131  && !$this->isShownToLoggedInUser())
132  || !$this->isEnabled()
133  || !in_array(
134  $this->formId,
135  $this->getTargetForms()
136  )
137  ) {
138  return false;
139  }
140 
141  return $this->isShowAlways()
142  || $this->isOverLimitAttempts($login)
143  || $this->session->getData($this->getFormIdKey('show_captcha'));
144  }
145 
151  public function isShownToLoggedInUser()
152  {
153  $forms = (array)$this->captchaData->getConfig('shown_to_logged_in_user');
154  foreach ($forms as $formId => $isShownToLoggedIn) {
155  if ($isShownToLoggedIn && $this->formId == $formId) {
156  return true;
157  }
158  }
159  return false;
160  }
161 
168  private function isOverLimitAttempts($login)
169  {
170  return $this->isOverLimitIpAttempt() || $this->isOverLimitLoginAttempts($login);
171  }
172 
178  private function getAllowedAttemptsForSameLogin()
179  {
180  return (int)$this->captchaData->getConfig('failed_attempts_login');
181  }
182 
188  private function getAllowedAttemptsFromSameIp()
189  {
190  return (int)$this->captchaData->getConfig('failed_attempts_ip');
191  }
192 
198  private function isOverLimitIpAttempt()
199  {
200  $countAttemptsByIp = $this->getResourceModel()->countAttemptsByRemoteAddress();
201  return $countAttemptsByIp >= $this->getAllowedAttemptsFromSameIp();
202  }
203 
210  private function isOverLimitLoginAttempts($login)
211  {
212  if ($login != false) {
213  $countAttemptsByLogin = $this->getResourceModel()->countAttemptsByUserLogin($login);
214  return $countAttemptsByLogin >= $this->getAllowedAttemptsForSameLogin();
215  }
216  return false;
217  }
218 
224  private function isUserAuth()
225  {
226  return $this->session->isLoggedIn();
227  }
228 
234  public function isCaseSensitive()
235  {
236  return (string)$this->captchaData->getConfig('case_sensitive');
237  }
238 
244  public function getFont()
245  {
246  $font = (string)$this->captchaData->getConfig('font');
247  $fonts = $this->captchaData->getFonts();
248 
249  if (isset($fonts[$font])) {
250  $fontPath = $fonts[$font]['path'];
251  } else {
252  $fontData = array_shift($fonts);
253  $fontPath = $fontData['path'];
254  }
255 
256  return $fontPath;
257  }
258 
264  public function getExpiration()
265  {
266  if (!$this->expiration) {
271  $this->expiration = (int)$this->captchaData->getConfig('timeout') * 60;
272  }
273  return $this->expiration;
274  }
275 
281  public function getTimeout()
282  {
283  return $this->getExpiration();
284  }
285 
291  public function getImgDir()
292  {
293  return $this->captchaData->getImgDir();
294  }
295 
301  public function getImgUrl()
302  {
303  return $this->captchaData->getImgUrl();
304  }
305 
312  public function isCorrect($word)
313  {
314  $storedWord = $this->getWord();
315  $this->clearWord();
316 
317  if (!$word || !$storedWord) {
318  return false;
319  }
320 
321  if (!$this->isCaseSensitive()) {
322  $storedWord = strtolower($storedWord);
323  $word = strtolower($word);
324  }
325  return $word === $storedWord;
326  }
327 
333  public function getImgSrc()
334  {
335  return $this->getImgUrl() . $this->getId() . $this->getSuffix();
336  }
337 
344  public function logAttempt($login)
345  {
346  if ($this->isEnabled() && in_array($this->formId, $this->getTargetForms())) {
347  $this->getResourceModel()->logAttempt($login);
348  if ($this->isOverLimitLoginAttempts($login)) {
349  $this->setShowCaptchaInSession(true);
350  }
351  }
352  return $this;
353  }
354 
362  public function setShowCaptchaInSession($value = true)
363  {
364  if ($value !== true) {
365  $value = false;
366  }
367 
368  $this->session->setData($this->getFormIdKey('show_captcha'), $value);
369  }
370 
378  protected function generateWord()
379  {
380  $word = '';
381  $symbols = $this->getSymbols();
382  $wordLen = $this->getWordLen();
383  for ($i = 0; $i < $wordLen; $i++) {
384  $word .= $symbols[array_rand($symbols)];
385  }
386  return $word;
387  }
388 
394  private function getSymbols()
395  {
396  return str_split((string)$this->captchaData->getConfig('symbols'));
397  }
398 
406  public function getWordLen()
407  {
408  $from = 0;
409  $to = 0;
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];
415  }
416  } else {
417  $from = (int)$length;
418  $to = (int)$length;
419  }
420 
421  if ($to < $from || $from < 1 || $to < 1) {
424  }
425 
426  return \Magento\Framework\Math\Random::getRandomNumber($from, $to);
427  }
428 
434  private function isShowAlways()
435  {
436  $captchaMode = (string)$this->captchaData->getConfig('mode');
437 
438  if ($captchaMode === Data::MODE_ALWAYS) {
439  return true;
440  }
441 
442  if ($captchaMode === Data::MODE_AFTER_FAIL
443  && $this->getAllowedAttemptsForSameLogin() === 0
444  ) {
445  return true;
446  }
447 
448  $alwaysFor = $this->captchaData->getConfig('always_for');
449  foreach ($alwaysFor as $nodeFormId => $isAlwaysFor) {
450  if ($isAlwaysFor && $this->formId == $nodeFormId) {
451  return true;
452  }
453  }
454 
455  return false;
456  }
457 
463  private function isEnabled()
464  {
465  return (string)$this->captchaData->getConfig('enable');
466  }
467 
475  private function getTargetForms()
476  {
477  $formsString = (string)$this->captchaData->getConfig('forms');
478  return explode(',', $formsString);
479  }
480 
486  public function getWord()
487  {
488  $sessionData = $this->session->getData($this->getFormIdKey(self::SESSION_WORD));
489  return time() < $sessionData['expires'] ? $sessionData['data'] : null;
490  }
491 
499  protected function setWord($word)
500  {
501  $this->session->setData(
502  $this->getFormIdKey(self::SESSION_WORD),
503  ['data' => $word, 'expires' => time() + $this->getTimeout()]
504  );
505  $this->word = $word;
506  return $this;
507  }
508 
514  private function clearWord()
515  {
516  $this->session->unsetData($this->getFormIdKey(self::SESSION_WORD));
517  $this->word = null;
518  return $this;
519  }
520 
529  protected function randomSize()
530  {
531  return \Magento\Framework\Math\Random::getRandomNumber(280, 300) / 100;
532  }
533 
546  protected function gc()
547  {
548  //do nothing
549  }
550 
556  private function getResourceModel()
557  {
558  return $this->resLogFactory->create();
559  }
560 }
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Session\SessionManagerInterface $session, \Magento\Captcha\Helper\Data $captchaData, \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory, $formId)
$i
Definition: gallery.phtml:31