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 | if ($this->hasModel()) { |
||
108 | $input = Html::activeTextInput($this->model, $this->attribute, $this->options); |
||
109 | } else { |
||
110 | $input = Html::textInput($this->name, $this->value, $this->options); |
||
111 | } |
||
112 | $route = $this->captchaAction; |
||
113 | if (is_array($route)) { |
||
114 | $route['v'] = uniqid(); |
||
115 | } else { |
||
116 | $route = [$route, 'v' => uniqid()]; |
||
117 | } |
||
118 | $image = Html::img($route, $this->imageOptions); |
||
119 | echo strtr($this->template, [ |
||
120 | '{input}' => $input, |
||
121 | '{image}' => $image, |
||
122 | ]); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Registers the needed JavaScript. |
||
127 | */ |
||
128 | public function registerClientScript() |
||
137 | |||
138 | /** |
||
139 | * Returns the options for the captcha JS widget. |
||
140 | * @return array the options |
||
141 | */ |
||
142 | protected function getClientOptions() |
||
143 | { |
||
144 | $route = $this->captchaAction; |
||
145 | if (is_array($route)) { |
||
146 | $route[CaptchaAction::REFRESH_GET_VAR] = 1; |
||
147 | } else { |
||
148 | $route = [$route, CaptchaAction::REFRESH_GET_VAR => 1]; |
||
149 | } |
||
150 | |||
151 | $options = [ |
||
152 | 'refreshUrl' => Url::toRoute($route), |
||
153 | 'hashKey' => 'yiiCaptcha/' . trim($route[0], '/'), |
||
154 | ]; |
||
155 | |||
156 | return $options; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Checks if there is graphic extension available to generate CAPTCHA images. |
||
161 | * This method will check the existence of ImageMagick and GD extensions. |
||
162 | * @return string the name of the graphic extension, either "imagick" or "gd". |
||
163 | * @throws InvalidConfigException if neither ImageMagick nor GD is installed. |
||
164 | */ |
||
165 | 1 | public static function checkRequirements() |
|
181 | } |
||
182 |