CaptchaValidator.php 3.6 KB

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