]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TCaptchaValidator.php
2758cac77aeecf6d8f13cbc7be2b9eaefc616aa3
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TCaptchaValidator.php
1 <?php
2 /**
3  * TCaptchaValidator class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Web.UI.WebControls
10  */
11
12 Prado::using('System.Web.UI.WebControls.TBaseValidator');
13 Prado::using('System.Web.UI.WebControls.TCaptcha');
14
15 /**
16  * TCaptchaValidator class
17  *
18  * Notice: while this class is easy to use and implement, it does not provide full security.
19  * In fact, it's easy to bypass the checks reusing old, already-validated tokens (reply attack).
20  * A better alternative is provided by {@link TReCaptchaValidator}.
21  *
22  * TCaptchaValidator validates user input against a CAPTCHA represented by
23  * a {@link TCaptcha} control. The input control fails validation if its value
24  * is not the same as the token displayed in CAPTCHA. Note, if the user does
25  * not enter any thing, it is still considered as failing the validation.
26  *
27  * To use TCaptchaValidator, specify the {@link setControlToValidate ControlToValidate}
28  * to be the ID path of the input control (usually a {@link TTextBox} control}.
29  * Also specify the {@link setCaptchaControl CaptchaControl} to be the ID path of
30  * the CAPTCHA control that the user input should be compared with.
31  *
32  * @author Qiang Xue <qiang.xue@gmail.com>
33  * @package System.Web.UI.WebControls
34  * @since 3.1.1
35  */
36 class TCaptchaValidator extends TBaseValidator
37 {
38         /**
39          * Gets the name of the javascript class responsible for performing validation for this control.
40          * This method overrides the parent implementation.
41          * @return string the javascript class name
42          */
43         protected function getClientClassName()
44         {
45                 return 'Prado.WebUI.TCaptchaValidator';
46         }
47
48         /**
49          * @return string the ID path of the CAPTCHA control to validate
50          */
51         public function getCaptchaControl()
52         {
53                 return $this->getViewState('CaptchaControl','');
54         }
55
56         /**
57          * Sets the ID path of the CAPTCHA control to validate.
58          * The ID path is the dot-connected IDs of the controls reaching from
59          * the validator's naming container to the target control.
60          * @param string the ID path
61          */
62         public function setCaptchaControl($value)
63         {
64                 $this->setViewState('CaptchaControl',TPropertyValue::ensureString($value),'');
65         }
66
67         /**
68          * This method overrides the parent's implementation.
69          * The validation succeeds if the input control has the same value
70          * as the one displayed in the corresponding CAPTCHA control.
71          *
72          * @return boolean whether the validation succeeds
73          */
74         protected function evaluateIsValid()
75         {
76                 $value=$this->getValidationValue($this->getValidationTarget());
77                 $control=$this->findCaptchaControl();
78                 return $control->validate(trim($value));
79         }
80
81         /**
82          * @return TCaptchaControl the CAPTCHA control to be validated against
83          * @throws TConfigurationException if the CAPTCHA cannot be found according to {@link setCaptchaControl CaptchaControl}
84          */
85         protected function findCaptchaControl()
86         {
87                 if(($id=$this->getCaptchaControl())==='')
88                         throw new TConfigurationException('captchavalidator_captchacontrol_required');
89                 else if(($control=$this->findControl($id))===null)
90                         throw new TConfigurationException('captchavalidator_captchacontrol_inexistent',$id);
91                 else if(!($control instanceof TCaptcha))
92                         throw new TConfigurationException('captchavalidator_captchacontrol_invalid',$id);
93                 else
94                         return $control;
95         }
96
97         /**
98          * Returns an array of javascript validator options.
99          * @return array javascript validator options.
100          */
101         protected function getClientScriptOptions()
102         {
103                 $options=parent::getClientScriptOptions();
104                 $control=$this->findCaptchaControl();
105                 if($control->getCaseSensitive())
106                 {
107                         $options['TokenHash']=$this->generateTokenHash($control->getToken());
108                         $options['CaseSensitive']=true;
109                 }
110                 else
111                 {
112                         $options['TokenHash']=$this->generateTokenHash(strtoupper($control->getToken()));
113                         $options['CaseSensitive']=false;
114                 }
115                 return $options;
116         }
117
118         private function generateTokenHash($token)
119         {
120                 for($h=0,$i=strlen($token)-1;$i>=0;--$i)
121                         $h+=ord($token[$i]);
122                 return $h;
123         }
124 }
125