Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\captcha;
13 years ago
use Yii;
use yii\base\InvalidConfigException;
use yii\validators\ValidationAsset;
use yii\validators\Validator;
13 years ago
/**
13 years ago
* CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
13 years ago
*
13 years ago
* CaptchaValidator should be used together with [[CaptchaAction]].
13 years ago
*
11 years ago
* 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.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class CaptchaValidator extends Validator
13 years ago
{
/**
* @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.');
}
}
13 years ago
/**
* @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 \yii\base\Controller $controller */
list($controller, $actionID) = $ca;
$action = $controller->createAction($actionID);
if ($action !== null) {
return $action;
}
}
throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
}
13 years ago
/**
* @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' => strtr($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
]),
];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
13 years ago
ValidationAsset::register($view);
return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
}
13 years ago
}