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