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