3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\InvalidConfigException;
15
use yii\widgets\InputWidget;
18
* Captcha renders a CAPTCHA image and an input field that takes user-entered verification code.
20
* Captcha is used together with [[CaptchaAction]] to provide [CAPTCHA](https://en.wikipedia.org/wiki/CAPTCHA) - a way
21
* of preventing website spamming.
23
* The image element rendered by Captcha will display a CAPTCHA image generated by
24
* an action whose route is specified by [[captchaAction]]. This action must be an instance of [[CaptchaAction]].
26
* When the user clicks on the CAPTCHA image, it will cause the CAPTCHA image
27
* to be refreshed with a new CAPTCHA.
29
* You may use [[\yii\captcha\CaptchaValidator]] to validate the user input matches
30
* the current CAPTCHA verification code.
32
* The following example shows how to use this widget with a model attribute:
35
* echo Captcha::widget([
37
* 'attribute' => 'captcha',
41
* The following example will use the name property instead:
44
* echo Captcha::widget([
45
* 'name' => 'captcha',
49
* You can also use this widget in an [[\yii\widgets\ActiveForm|ActiveForm]] using the [[\yii\widgets\ActiveField::widget()|widget()]]
50
* method, for example like this:
53
* <?= $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::classname(), [
54
* // configure additional widget properties here
58
* @author Qiang Xue <qiang.xue@gmail.com>
61
class Captcha extends InputWidget
64
* @var string|array the route of the action that generates the CAPTCHA images.
65
* The action represented by this route must be an action of [[CaptchaAction]].
66
* Please refer to [[\yii\helpers\Url::toRoute()]] for acceptable formats.
68
public $captchaAction = 'site/captcha';
70
* @var array HTML attributes to be applied to the CAPTCHA image tag.
71
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
73
public $imageOptions = [];
75
* @var string the template for arranging the CAPTCHA image tag and the text input tag.
76
* In this template, the token `{image}` will be replaced with the actual image tag,
77
* while `{input}` will be replaced with the text input tag.
79
public $template = '{image} {input}';
81
* @var array the HTML attributes for the input tag.
82
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
84
public $options = ['class' => 'form-control'];
88
* Initializes the widget.
90
public function init()
94
static::checkRequirements();
96
if (!isset($this->imageOptions['id'])) {
97
$this->imageOptions['id'] = $this->options['id'] . '-image';
102
* Renders the widget.
104
public function run()
106
$this->registerClientScript();
107
$input = $this->renderInputHtml('text');
108
$route = $this->captchaAction;
109
if (is_array($route)) {
110
$route['v'] = uniqid('', true);
112
$route = [$route, 'v' => uniqid('', true)];
114
$image = Html::img($route, $this->imageOptions);
115
echo strtr($this->template, [
122
* Registers the needed JavaScript.
124
public function registerClientScript()
126
$options = $this->getClientOptions();
127
$options = empty($options) ? '' : Json::htmlEncode($options);
128
$id = $this->imageOptions['id'];
129
$view = $this->getView();
130
CaptchaAsset::register($view);
131
$view->registerJs("jQuery('#$id').yiiCaptcha($options);");
135
* Returns the options for the captcha JS widget.
136
* @return array the options
138
protected function getClientOptions()
140
$route = $this->captchaAction;
141
if (is_array($route)) {
142
$route[CaptchaAction::REFRESH_GET_VAR] = 1;
144
$route = [$route, CaptchaAction::REFRESH_GET_VAR => 1];
148
'refreshUrl' => Url::toRoute($route),
149
'hashKey' => 'yiiCaptcha/' . trim($route[0], '/'),
156
* Checks if there is graphic extension available to generate CAPTCHA images.
157
* This method will check the existence of ImageMagick and GD extensions.
158
* @return string the name of the graphic extension, either "imagick" or "gd".
159
* @throws InvalidConfigException if neither ImageMagick nor GD is installed.
161
public static function checkRequirements()
163
if (extension_loaded('imagick')) {
164
$imagickFormats = (new \Imagick())->queryFormats('PNG');
165
if (in_array('PNG', $imagickFormats, true)) {
169
if (extension_loaded('gd')) {
171
if (!empty($gdInfo['FreeType Support'])) {
175
throw new InvalidConfigException('Either GD PHP extension with FreeType support or ImageMagick PHP extension with PNG support is required.');