Completed
Push — 2.1 ( 83fa82...a136f4 )
by
unknown
12:35
created

Captcha   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 92
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A run() 0 16 2
A registerClientScript() 0 9 2
A getClientOptions() 0 16 2
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
8
namespace yii\captcha;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Html;
13
use yii\helpers\Json;
14
use yii\helpers\Url;
15
use yii\widgets\InputWidget;
16
17
/**
18
 * Captcha renders a CAPTCHA image and an input field that takes user-entered verification code.
19
 *
20
 * Captcha is used together with [[CaptchaAction]] to provide [CAPTCHA](http://en.wikipedia.org/wiki/Captcha) - a way
21
 * of preventing website spamming.
22
 *
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]].
25
 *
26
 * When the user clicks on the CAPTCHA image, it will cause the CAPTCHA image
27
 * to be refreshed with a new CAPTCHA.
28
 *
29
 * You may use [[\yii\captcha\CaptchaValidator]] to validate the user input matches
30
 * the current CAPTCHA verification code.
31
 *
32
 * The following example shows how to use this widget with a model attribute:
33
 *
34
 * ```php
35
 * echo Captcha::widget([
36
 *     'model' => $model,
37
 *     'attribute' => 'captcha',
38
 * ]);
39
 * ```
40
 *
41
 * The following example will use the name property instead:
42
 *
43
 * ```php
44
 * echo Captcha::widget([
45
 *     'name' => 'captcha',
46
 * ]);
47
 * ```
48
 *
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:
51
 *
52
 * ```php
53
 * <?= $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::class, [
54
 *     // configure additional widget properties here
55
 * ]) ?>
56
 * ```
57
 *
58
 * @author Qiang Xue <[email protected]>
59
 * @since 2.0
60
 */
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 2
    public function init()
91
    {
92 2
        parent::init();
93
94 2
        if (!isset($this->imageOptions['id'])) {
95 2
            $this->imageOptions['id'] = $this->options['id'] . '-image';
96
        }
97 2
    }
98
99
    /**
100
     * Renders the widget.
101
     */
102 1
    public function run()
103
    {
104 1
        $this->registerClientScript();
105 1
        $input = $this->renderInputHtml('text');
106 1
        $route = $this->captchaAction;
107 1
        if (is_array($route)) {
108
            $route['v'] = uniqid();
109
        } else {
110 1
            $route = [$route, 'v' => uniqid()];
111
        }
112 1
        $image = Html::img($route, $this->imageOptions);
113 1
        return strtr($this->template, [
114 1
            '{input}' => $input,
115 1
            '{image}' => $image,
116
        ]);
117
    }
118
119
    /**
120
     * Registers the needed JavaScript.
121
     */
122 1
    public function registerClientScript()
123
    {
124 1
        $options = $this->getClientOptions();
125 1
        $options = empty($options) ? '' : Json::htmlEncode($options);
126 1
        $id = $this->imageOptions['id'];
127 1
        $view = $this->getView();
128 1
        CaptchaAsset::register($view);
129 1
        $view->registerJs("jQuery('#$id').yiiCaptcha($options);");
130 1
    }
131
132
    /**
133
     * Returns the options for the captcha JS widget.
134
     * @return array the options
135
     */
136 1
    protected function getClientOptions()
137
    {
138 1
        $route = $this->captchaAction;
139 1
        if (is_array($route)) {
140
            $route[CaptchaAction::REFRESH_GET_VAR] = 1;
141
        } else {
142 1
            $route = [$route, CaptchaAction::REFRESH_GET_VAR => 1];
143
        }
144
145
        $options = [
146 1
            'refreshUrl' => Url::toRoute($route),
147 1
            'hashKey' => 'yiiCaptcha/' . trim($route[0], '/'),
148
        ];
149
150 1
        return $options;
151
    }
152
}
153