1 | <?php |
||
61 | class Captcha extends InputWidget |
||
62 | { |
||
63 | /** |
||
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. |
||
67 | */ |
||
68 | public $captchaAction = 'site/captcha'; |
||
69 | /** |
||
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. |
||
72 | */ |
||
73 | public $imageOptions = []; |
||
74 | /** |
||
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. |
||
78 | */ |
||
79 | public $template = '{image} {input}'; |
||
80 | /** |
||
81 | * @var array the HTML attributes for the input tag. |
||
82 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
83 | */ |
||
84 | public $options = ['class' => 'form-control']; |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Initializes the widget. |
||
89 | */ |
||
90 | 1 | public function init() |
|
91 | { |
||
92 | 1 | parent::init(); |
|
93 | |||
94 | 1 | static::checkRequirements(); |
|
95 | |||
96 | 1 | if (!isset($this->imageOptions['id'])) { |
|
97 | 1 | $this->imageOptions['id'] = $this->options['id'] . '-image'; |
|
98 | } |
||
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * Renders the widget. |
||
103 | */ |
||
104 | public function run() |
||
105 | { |
||
106 | $this->registerClientScript(); |
||
107 | $input = $this->renderInputHtml('text'); |
||
108 | $route = $this->captchaAction; |
||
109 | if (is_array($route)) { |
||
110 | $route['v'] = uniqid(); |
||
111 | } else { |
||
112 | $route = [$route, 'v' => uniqid()]; |
||
113 | } |
||
114 | $image = Html::img($route, $this->imageOptions); |
||
115 | echo strtr($this->template, [ |
||
116 | '{input}' => $input, |
||
117 | '{image}' => $image, |
||
118 | ]); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Registers the needed JavaScript. |
||
123 | */ |
||
124 | public function registerClientScript() |
||
125 | { |
||
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);"); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the options for the captcha JS widget. |
||
136 | * @return array the options |
||
137 | */ |
||
138 | protected function getClientOptions() |
||
154 | |||
155 | /** |
||
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. |
||
160 | */ |
||
161 | 1 | public static function checkRequirements() |
|
177 | } |
||
178 |