Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
DefaultModel Class Reference
Inheritance diagram for DefaultModel:
CaptchaInterface

Public Member Functions

 __construct (\Magento\Framework\Session\SessionManagerInterface $session, \Magento\Captcha\Helper\Data $captchaData, \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory, $formId)
 
 getBlockName ()
 
 isRequired ($login=null)
 
 isShownToLoggedInUser ()
 
 isCaseSensitive ()
 
 getFont ()
 
 getExpiration ()
 
 getTimeout ()
 
 getImgDir ()
 
 getImgUrl ()
 
 isCorrect ($word)
 
 getImgSrc ()
 
 logAttempt ($login)
 
 setShowCaptchaInSession ($value=true)
 
 getWordLen ()
 
 getWord ()
 
- Public Member Functions inherited from CaptchaInterface
 generate ()
 

Data Fields

const SESSION_WORD = 'word'
 
const DEFAULT_WORD_LENGTH_FROM = 3
 
const DEFAULT_WORD_LENGTH_TO = 5
 

Protected Member Functions

 generateWord ()
 
 setWord ($word)
 
 randomSize ()
 
 gc ()
 

Protected Attributes

 $captchaData
 
 $expiration
 
 $fsize = 22
 
 $formId
 
 $resLogFactory
 
 $keepSession = true
 
 $session
 

Detailed Description

Implementation of \Zend\Captcha\Image

@api

Since
100.0.2

Definition at line 16 of file DefaultModel.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Session\SessionManagerInterface  $session,
\Magento\Captcha\Helper\Data  $captchaData,
\Magento\Captcha\Model\ResourceModel\LogFactory  $resLogFactory,
  $formId 
)
Parameters
\Magento\Framework\Session\SessionManagerInterface$session
\Magento\Captcha\Helper\Data$captchaData
ResourceModel\LogFactory$resLogFactory
string$formId
Exceptions

Definition at line 88 of file DefaultModel.php.

93  {
94  parent::__construct();
95  $this->session = $session;
96  $this->captchaData = $captchaData;
97  $this->resLogFactory = $resLogFactory;
98  $this->formId = $formId;
99  }

Member Function Documentation

◆ gc()

gc ( )
protected

Overlap of the parent method

Returns
void

Now deleting old captcha images make crontab script

See also
\Magento\Captcha\Cron\DeleteExpiredImages::execute

Added SuppressWarnings since this method is declared in parent class and we can not use other method name. @SuppressWarnings(PHPMD.ShortMethodName)

Since
100.2.0

Definition at line 546 of file DefaultModel.php.

547  {
548  //do nothing
549  }

◆ generateWord()

generateWord ( )
protected

Generate word used for captcha render

Returns
string
Exceptions

Definition at line 378 of file DefaultModel.php.

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  }
$i
Definition: gallery.phtml:31

◆ getBlockName()

getBlockName ( )

Get Block Name

Returns
string

Implements CaptchaInterface.

Definition at line 117 of file DefaultModel.php.

118  {
119  return \Magento\Captcha\Block\Captcha\DefaultCaptcha::class;
120  }

◆ getExpiration()

getExpiration ( )

After this time isCorrect() is going to return FALSE even if word was guessed correctly

Returns
int

as "timeout" configuration parameter specifies timeout in minutes - we multiply it on 60 to set expiration in seconds

Definition at line 264 of file DefaultModel.php.

265  {
266  if (!$this->expiration) {
271  $this->expiration = (int)$this->captchaData->getConfig('timeout') * 60;
272  }
273  return $this->expiration;
274  }

◆ getFont()

getFont ( )

Get font to use when generating captcha

Returns
string

Definition at line 244 of file DefaultModel.php.

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  }

◆ getImgDir()

getImgDir ( )

Get captcha image directory

Returns
string

Definition at line 291 of file DefaultModel.php.

292  {
293  return $this->captchaData->getImgDir();
294  }

◆ getImgSrc()

getImgSrc ( )

Return full URL to captcha image

Returns
string

Definition at line 333 of file DefaultModel.php.

334  {
335  return $this->getImgUrl() . $this->getId() . $this->getSuffix();
336  }

◆ getImgUrl()

getImgUrl ( )

Get captcha image base URL

Returns
string

Definition at line 301 of file DefaultModel.php.

302  {
303  return $this->captchaData->getImgUrl();
304  }

◆ getTimeout()

getTimeout ( )

Get timeout for session token

Returns
int

Definition at line 281 of file DefaultModel.php.

282  {
283  return $this->getExpiration();
284  }

◆ getWord()

getWord ( )

Get captcha word

Returns
string

Definition at line 486 of file DefaultModel.php.

487  {
488  $sessionData = $this->session->getData($this->getFormIdKey(self::SESSION_WORD));
489  return time() < $sessionData['expires'] ? $sessionData['data'] : null;
490  }

◆ getWordLen()

getWordLen ( )

Returns length for generating captcha word. This value may be dynamic.

Returns
int
Exceptions

Definition at line 406 of file DefaultModel.php.

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  }

◆ isCaseSensitive()

isCaseSensitive ( )

Whether to respect case while checking the answer

Returns
bool

Definition at line 234 of file DefaultModel.php.

235  {
236  return (string)$this->captchaData->getConfig('case_sensitive');
237  }

◆ isCorrect()

isCorrect (   $word)

Checks whether captcha was guessed correctly by user

Parameters
string$word
Returns
bool

Implements CaptchaInterface.

Definition at line 312 of file DefaultModel.php.

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  }

◆ isRequired()

isRequired (   $login = null)

Whether captcha is required to be inserted to this form

Parameters
null | string$login
Returns
bool

Definition at line 128 of file DefaultModel.php.

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  }

◆ isShownToLoggedInUser()

isShownToLoggedInUser ( )

Check if CAPTCHA has to be shown to logged in user on this form

Returns
bool

Definition at line 151 of file DefaultModel.php.

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  }

◆ logAttempt()

logAttempt (   $login)

Log attempt

Parameters
string$login
Returns
$this

Definition at line 344 of file DefaultModel.php.

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  }

◆ randomSize()

randomSize ( )
protected

Override function to generate less curly captcha that will not cut off

See also
\Zend\Captcha\Image::_randomSize()
Returns
int
Exceptions

Definition at line 529 of file DefaultModel.php.

530  {
531  return \Magento\Framework\Math\Random::getRandomNumber(280, 300) / 100;
532  }

◆ setShowCaptchaInSession()

setShowCaptchaInSession (   $value = true)

Set show_captcha flag in session

Parameters
bool$value
Returns
void
Since
100.1.0

Definition at line 362 of file DefaultModel.php.

363  {
364  if ($value !== true) {
365  $value = false;
366  }
367 
368  $this->session->setData($this->getFormIdKey('show_captcha'), $value);
369  }
$value
Definition: gender.phtml:16

◆ setWord()

setWord (   $word)
protected

Set captcha word

Parameters
string$word
Returns
$this
Since
100.2.0

Definition at line 499 of file DefaultModel.php.

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  }

Field Documentation

◆ $captchaData

$captchaData
protected

Definition at line 37 of file DefaultModel.php.

◆ $expiration

$expiration
protected

Definition at line 44 of file DefaultModel.php.

◆ $formId

$formId
protected

Definition at line 59 of file DefaultModel.php.

◆ $fsize

$fsize = 22
protected

Definition at line 52 of file DefaultModel.php.

◆ $keepSession

$keepSession = true
protected

Definition at line 73 of file DefaultModel.php.

◆ $resLogFactory

$resLogFactory
protected

Definition at line 65 of file DefaultModel.php.

◆ $session

$session
protected

Definition at line 79 of file DefaultModel.php.

◆ DEFAULT_WORD_LENGTH_FROM

const DEFAULT_WORD_LENGTH_FROM = 3

Min captcha lengths default value

Definition at line 26 of file DefaultModel.php.

◆ DEFAULT_WORD_LENGTH_TO

const DEFAULT_WORD_LENGTH_TO = 5

Max captcha lengths default value

Definition at line 31 of file DefaultModel.php.

◆ SESSION_WORD

const SESSION_WORD = 'word'

Key in session for captcha code

Definition at line 21 of file DefaultModel.php.


The documentation for this class was generated from the following file: