CCaptcha.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * CCaptcha class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CCaptcha renders a CAPTCHA image element.
  12. *
  13. * CCaptcha is used together with {@link CCaptchaAction} to provide {@link http://en.wikipedia.org/wiki/Captcha CAPTCHA}
  14. * - a way of preventing site spam.
  15. *
  16. * The image element rendered by CCaptcha will display a CAPTCHA image generated
  17. * by an action of class {@link CCaptchaAction} belonging to the current controller.
  18. * By default, the action ID should be 'captcha', which can be changed by setting {@link captchaAction}.
  19. *
  20. * CCaptcha may also render a button next to the CAPTCHA image. Clicking on the button
  21. * will change the CAPTCHA image to be a new one in an AJAX way.
  22. *
  23. * If {@link clickableImage} is set true, clicking on the CAPTCHA image
  24. * will refresh the CAPTCHA.
  25. *
  26. * A {@link CCaptchaValidator} may be used to validate that the user enters
  27. * a verification code matching the code displayed in the CAPTCHA image.
  28. *
  29. * @author Qiang Xue <qiang.xue@gmail.com>
  30. * @package system.web.widgets.captcha
  31. * @since 1.0
  32. */
  33. class CCaptcha extends CWidget
  34. {
  35. /**
  36. * @var string the ID of the action that should provide CAPTCHA image. Defaults to 'captcha',
  37. * meaning the 'captcha' action of the current controller. This property may also
  38. * be in the format of 'ControllerID/ActionID'. Underneath, this property is used
  39. * by {@link CController::createUrl} to create the URL that would serve the CAPTCHA image.
  40. * The action has to be of {@link CCaptchaAction}.
  41. */
  42. public $captchaAction='captcha';
  43. /**
  44. * @var boolean whether to display a button next to the CAPTCHA image. Clicking on the button
  45. * will cause the CAPTCHA image to be changed to a new one. Defaults to true.
  46. */
  47. public $showRefreshButton=true;
  48. /**
  49. * @var boolean whether to allow clicking on the CAPTCHA image to refresh the CAPTCHA letters.
  50. * Defaults to false. Hint: you may want to set {@link showRefreshButton} to false if you set
  51. * this property to be true because they serve for the same purpose.
  52. * To enhance accessibility, you may set {@link imageOptions} to provide hints to end-users that
  53. * the image is clickable.
  54. */
  55. public $clickableImage=false;
  56. /**
  57. * @var string the label for the refresh button. Defaults to 'Get a new code'.
  58. */
  59. public $buttonLabel;
  60. /**
  61. * @var string the type of the refresh button. This should be either 'link' or 'button'.
  62. * The former refers to hyperlink button while the latter a normal push button.
  63. * Defaults to 'link'.
  64. */
  65. public $buttonType='link';
  66. /**
  67. * @var array HTML attributes to be applied to the rendered image element.
  68. */
  69. public $imageOptions=array();
  70. /**
  71. * @var array HTML attributes to be applied to the rendered refresh button element.
  72. */
  73. public $buttonOptions=array();
  74. /**
  75. * Renders the widget.
  76. */
  77. public function run()
  78. {
  79. if(self::checkRequirements('imagick') || self::checkRequirements('gd'))
  80. {
  81. $this->renderImage();
  82. $this->registerClientScript();
  83. }
  84. else
  85. throw new CException(Yii::t('yii','GD with FreeType or ImageMagick PHP extensions are required.'));
  86. }
  87. /**
  88. * Renders the CAPTCHA image.
  89. */
  90. protected function renderImage()
  91. {
  92. if(!isset($this->imageOptions['id']))
  93. $this->imageOptions['id']=$this->getId();
  94. $url=$this->getController()->createUrl($this->captchaAction,array('v'=>uniqid()));
  95. $alt=isset($this->imageOptions['alt'])?$this->imageOptions['alt']:'';
  96. echo CHtml::image($url,$alt,$this->imageOptions);
  97. }
  98. /**
  99. * Registers the needed client scripts.
  100. */
  101. public function registerClientScript()
  102. {
  103. $cs=Yii::app()->clientScript;
  104. $id=$this->imageOptions['id'];
  105. $url=$this->getController()->createUrl($this->captchaAction,array(CCaptchaAction::REFRESH_GET_VAR=>true));
  106. $js="";
  107. if($this->showRefreshButton)
  108. {
  109. // reserve a place in the registered script so that any enclosing button js code appears after the captcha js
  110. $cs->registerScript('Yii.CCaptcha#'.$id,'// dummy');
  111. $label=$this->buttonLabel===null?Yii::t('yii','Get a new code'):$this->buttonLabel;
  112. $options=$this->buttonOptions;
  113. if(isset($options['id']))
  114. $buttonID=$options['id'];
  115. else
  116. $buttonID=$options['id']=$id.'_button';
  117. if($this->buttonType==='button')
  118. $html=CHtml::button($label, $options);
  119. else
  120. $html=CHtml::link($label, $url, $options);
  121. $js="jQuery('#$id').after(".CJSON::encode($html).");";
  122. $selector="#$buttonID";
  123. }
  124. if($this->clickableImage)
  125. $selector=isset($selector) ? "$selector, #$id" : "#$id";
  126. if(!isset($selector))
  127. return;
  128. $js.="
  129. jQuery(document).on('click', '$selector', function(){
  130. jQuery.ajax({
  131. url: ".CJSON::encode($url).",
  132. dataType: 'json',
  133. cache: false,
  134. success: function(data) {
  135. jQuery('#$id').attr('src', data['url']);
  136. jQuery('body').data('{$this->captchaAction}.hash', [data['hash1'], data['hash2']]);
  137. }
  138. });
  139. return false;
  140. });
  141. ";
  142. $cs->registerScript('Yii.CCaptcha#'.$id,$js);
  143. }
  144. /**
  145. * Checks if specified graphic extension support is loaded.
  146. * @param string $extension name to be checked. Possible values are 'gd', 'imagick' and null.
  147. * Default value is null meaning that both extensions will be checked. This parameter
  148. * is available since 1.1.13.
  149. * @return boolean true if ImageMagick extension with PNG support or GD with FreeType support is loaded,
  150. * otherwise false
  151. * @since 1.1.5
  152. */
  153. public static function checkRequirements($extension=null)
  154. {
  155. if(extension_loaded('imagick'))
  156. {
  157. $imagick=new Imagick();
  158. $imagickFormats=$imagick->queryFormats('PNG');
  159. }
  160. if(extension_loaded('gd'))
  161. {
  162. $gdInfo=gd_info();
  163. }
  164. if($extension===null)
  165. {
  166. if(isset($imagickFormats) && in_array('PNG',$imagickFormats))
  167. return true;
  168. if(isset($gdInfo) && $gdInfo['FreeType Support'])
  169. return true;
  170. }
  171. elseif($extension=='imagick' && isset($imagickFormats) && in_array('PNG',$imagickFormats))
  172. return true;
  173. elseif($extension=='gd' && isset($gdInfo) && $gdInfo['FreeType Support'])
  174. return true;
  175. return false;
  176. }
  177. }