PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/captcha/Captcha.php

https://gitlab.com/brucealdridge/yii2
PHP | 182 lines | 83 code | 13 blank | 86 comment | 11 complexity | af4e134a520630249d7fb337b9d13117 MD5 | raw file
  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. namespace yii\captcha;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Url;
  11. use yii\helpers\Html;
  12. use yii\helpers\Json;
  13. use yii\widgets\InputWidget;
  14. /**
  15. * Captcha renders a CAPTCHA image and an input field that takes user-entered verification code.
  16. *
  17. * Captcha is used together with [[CaptchaAction]] provide [CAPTCHA](http://en.wikipedia.org/wiki/Captcha) - a way
  18. * of preventing Website spamming.
  19. *
  20. * The image element rendered by Captcha will display a CAPTCHA image generated by
  21. * an action whose route is specified by [[captchaAction]]. This action must be an instance of [[CaptchaAction]].
  22. *
  23. * When the user clicks on the CAPTCHA image, it will cause the CAPTCHA image
  24. * to be refreshed with a new CAPTCHA.
  25. *
  26. * You may use [[\yii\captcha\CaptchaValidator]] to validate the user input matches
  27. * the current CAPTCHA verification code.
  28. *
  29. * The following example shows how to use this widget with a model attribute:
  30. *
  31. * ```php
  32. * echo Captcha::widget([
  33. * 'model' => $model,
  34. * 'attribute' => 'captcha',
  35. * ]);
  36. * ```
  37. *
  38. * The following example will use the name property instead:
  39. *
  40. * ```php
  41. * echo Captcha::widget([
  42. * 'name' => 'captcha',
  43. * ]);
  44. * ```
  45. *
  46. * You can also use this widget in an [[yii\widgets\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
  47. * method, for example like this:
  48. *
  49. * ```php
  50. * <?= $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::classname(), [
  51. * // configure additional widget properties here
  52. * ]) ?>
  53. * ```
  54. *
  55. * @author Qiang Xue <qiang.xue@gmail.com>
  56. * @since 2.0
  57. */
  58. class Captcha extends InputWidget
  59. {
  60. /**
  61. * @var string|array the route of the action that generates the CAPTCHA images.
  62. * The action represented by this route must be an action of [[CaptchaAction]].
  63. * Please refer to [[\yii\helpers\Url::toRoute()]] for acceptable formats.
  64. */
  65. public $captchaAction = 'site/captcha';
  66. /**
  67. * @var array HTML attributes to be applied to the CAPTCHA image tag.
  68. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  69. */
  70. public $imageOptions = [];
  71. /**
  72. * @var string the template for arranging the CAPTCHA image tag and the text input tag.
  73. * In this template, the token `{image}` will be replaced with the actual image tag,
  74. * while `{input}` will be replaced with the text input tag.
  75. */
  76. public $template = '{image} {input}';
  77. /**
  78. * @var array the HTML attributes for the input tag.
  79. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  80. */
  81. public $options = ['class' => 'form-control'];
  82. /**
  83. * Initializes the widget.
  84. */
  85. public function init()
  86. {
  87. parent::init();
  88. $this->checkRequirements();
  89. if (!isset($this->imageOptions['id'])) {
  90. $this->imageOptions['id'] = $this->options['id'] . '-image';
  91. }
  92. }
  93. /**
  94. * Renders the widget.
  95. */
  96. public function run()
  97. {
  98. $this->registerClientScript();
  99. if ($this->hasModel()) {
  100. $input = Html::activeTextInput($this->model, $this->attribute, $this->options);
  101. } else {
  102. $input = Html::textInput($this->name, $this->value, $this->options);
  103. }
  104. $route = $this->captchaAction;
  105. if (is_array($route)) {
  106. $route['v'] = uniqid();
  107. } else {
  108. $route = [$route, 'v' => uniqid()];
  109. }
  110. $image = Html::img($route, $this->imageOptions);
  111. echo strtr($this->template, [
  112. '{input}' => $input,
  113. '{image}' => $image,
  114. ]);
  115. }
  116. /**
  117. * Registers the needed JavaScript.
  118. */
  119. public function registerClientScript()
  120. {
  121. $options = $this->getClientOptions();
  122. $options = empty($options) ? '' : Json::htmlEncode($options);
  123. $id = $this->imageOptions['id'];
  124. $view = $this->getView();
  125. CaptchaAsset::register($view);
  126. $view->registerJs("jQuery('#$id').yiiCaptcha($options);");
  127. }
  128. /**
  129. * Returns the options for the captcha JS widget.
  130. * @return array the options
  131. */
  132. protected function getClientOptions()
  133. {
  134. $route = $this->captchaAction;
  135. if (is_array($route)) {
  136. $route[CaptchaAction::REFRESH_GET_VAR] = 1;
  137. } else {
  138. $route = [$route, CaptchaAction::REFRESH_GET_VAR => 1];
  139. }
  140. $options = [
  141. 'refreshUrl' => Url::toRoute($route),
  142. 'hashKey' => "yiiCaptcha/{$route[0]}",
  143. ];
  144. return $options;
  145. }
  146. /**
  147. * Checks if there is graphic extension available to generate CAPTCHA images.
  148. * This method will check the existence of ImageMagick and GD extensions.
  149. * @return string the name of the graphic extension, either "imagick" or "gd".
  150. * @throws InvalidConfigException if neither ImageMagick nor GD is installed.
  151. */
  152. public static function checkRequirements()
  153. {
  154. if (extension_loaded('imagick')) {
  155. $imagick = new \Imagick();
  156. $imagickFormats = $imagick->queryFormats('PNG');
  157. if (in_array('PNG', $imagickFormats)) {
  158. return 'imagick';
  159. }
  160. }
  161. if (extension_loaded('gd')) {
  162. $gdInfo = gd_info();
  163. if (!empty($gdInfo['FreeType Support'])) {
  164. return 'gd';
  165. }
  166. }
  167. throw new InvalidConfigException('Either GD PHP extension with FreeType support or ImageMagick PHP extension with PNG support is required.');
  168. }
  169. }