Blame view

vendor/yiisoft/yii2/captcha/CaptchaValidator.php 3.41 KB
abf1649b   andryeyev   Чистая установка ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  <?php
  /**
   * @link http://www.yiiframework.com/
   * @copyright Copyright (c) 2008 Yii Software LLC
   * @license http://www.yiiframework.com/license/
   */
  
  namespace yii\captcha;
  
  use Yii;
  use yii\base\InvalidConfigException;
  use yii\validators\ValidationAsset;
  use yii\validators\Validator;
  
  /**
   * CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
   *
   * CaptchaValidator should be used together with [[CaptchaAction]].
   *
   * Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result,
   * CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation
   * even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.
   *
   * @author Qiang Xue <qiang.xue@gmail.com>
   * @since 2.0
   */
  class CaptchaValidator extends Validator
  {
      /**
       * @var boolean whether to skip this validator if the input is empty.
       */
      public $skipOnEmpty = false;
      /**
       * @var boolean whether the comparison is case sensitive. Defaults to false.
       */
      public $caseSensitive = false;
      /**
       * @var string the route of the controller action that renders the CAPTCHA image.
       */
      public $captchaAction = 'site/captcha';
  
  
      /**
       * @inheritdoc
       */
      public function init()
      {
          parent::init();
          if ($this->message === null) {
              $this->message = Yii::t('yii', 'The verification code is incorrect.');
          }
      }
  
      /**
       * @inheritdoc
       */
      protected function validateValue($value)
      {
          $captcha = $this->createCaptchaAction();
          $valid = !is_array($value) && $captcha->validate($value, $this->caseSensitive);
  
          return $valid ? null : [$this->message, []];
      }
  
      /**
       * Creates the CAPTCHA action object from the route specified by [[captchaAction]].
       * @return \yii\captcha\CaptchaAction the action object
       * @throws InvalidConfigException
       */
      public function createCaptchaAction()
      {
          $ca = Yii::$app->createController($this->captchaAction);
          if ($ca !== false) {
              /* @var $controller \yii\base\Controller */
              list($controller, $actionID) = $ca;
              $action = $controller->createAction($actionID);
              if ($action !== null) {
                  return $action;
              }
          }
          throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
      }
  
      /**
       * @inheritdoc
       */
      public function clientValidateAttribute($object, $attribute, $view)
      {
          $captcha = $this->createCaptchaAction();
          $code = $captcha->getVerifyCode(false);
          $hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
          $options = [
              'hash' => $hash,
              'hashKey' => 'yiiCaptcha/' . $this->captchaAction,
              'caseSensitive' => $this->caseSensitive,
              'message' => Yii::$app->getI18n()->format($this->message, [
                  'attribute' => $object->getAttributeLabel($attribute),
              ], Yii::$app->language),
          ];
          if ($this->skipOnEmpty) {
              $options['skipOnEmpty'] = 1;
          }
  
          ValidationAsset::register($view);
  
          return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
      }
  }