Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 lines
3.4KB

  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 boolean whether to skip this validator if the input is empty.
  28. */
  29. public $skipOnEmpty = false;
  30. /**
  31. * @var boolean 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($object, $attribute, $view)
  79. {
  80. $captcha = $this->createCaptchaAction();
  81. $code = $captcha->getVerifyCode(false);
  82. $hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
  83. $options = [
  84. 'hash' => $hash,
  85. 'hashKey' => 'yiiCaptcha/' . $captcha->getUniqueId(),
  86. 'caseSensitive' => $this->caseSensitive,
  87. 'message' => Yii::$app->getI18n()->format($this->message, [
  88. 'attribute' => $object->getAttributeLabel($attribute),
  89. ], Yii::$app->language),
  90. ];
  91. if ($this->skipOnEmpty) {
  92. $options['skipOnEmpty'] = 1;
  93. }
  94. ValidationAsset::register($view);
  95. return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  96. }
  97. }