diff options
| author | xue <> | 2007-08-29 03:03:30 +0000 | 
|---|---|---|
| committer | xue <> | 2007-08-29 03:03:30 +0000 | 
| commit | 491d3956a4f1bc6ce5501bcc0449007a16514288 (patch) | |
| tree | 43ce302d5ecafe77bd52da3d6fd782e8c3aa696a | |
| parent | 30c4bcc8a293506109ef3e8d6c4b5ecfb10db793 (diff) | |
implemented CAPTCHA image generation.
| -rw-r--r-- | .gitattributes | 1 | ||||
| -rw-r--r-- | framework/Web/UI/WebControls/TCaptcha.php | 34 | ||||
| -rw-r--r-- | framework/Web/UI/WebControls/assets/captcha.php | 60 | ||||
| -rw-r--r-- | framework/Web/UI/WebControls/assets/verase.ttf | bin | 0 -> 60280 bytes | 
4 files changed, 85 insertions, 10 deletions
diff --git a/.gitattributes b/.gitattributes index 1e69791d..5a783516 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2506,6 +2506,7 @@ framework/Web/UI/WebControls/TXmlTransform.php -text  framework/Web/UI/WebControls/assets/captcha.php -text  framework/Web/UI/WebControls/assets/keyboard.css -text  framework/Web/UI/WebControls/assets/tabpanel.css -text +framework/Web/UI/WebControls/assets/verase.ttf -text  framework/Xml/TXmlDocument.php -text  framework/interfaces.php -text  framework/powered.gif -text diff --git a/framework/Web/UI/WebControls/TCaptcha.php b/framework/Web/UI/WebControls/TCaptcha.php index f000ca19..921881d8 100644 --- a/framework/Web/UI/WebControls/TCaptcha.php +++ b/framework/Web/UI/WebControls/TCaptcha.php @@ -37,8 +37,15 @@ Prado::using('System.Web.UI.WebControls.TImage');   */
  class TCaptcha extends TImage
  {
 -	const MIN_TOKEN_LENGTH=2;
 +	const MIN_TOKEN_LENGTH=4;
  	const MAX_TOKEN_LENGTH=40;
 +	const MIN_PUBLIC_KEY=6;
 +
 +	public function onInit($param)
 +	{
 +		parent::onInit($param);
 +		$this->checkRequirements();
 +	}
  	/**
  	 * @return integer the minimum length of the token. Defaults to 5.
 @@ -114,6 +121,8 @@ class TCaptcha extends TImage  	 */
  	public function setPublicKey($value)
  	{
 +		if(strlen($value)<self::MIN_PUBLIC_KEY)
 +			throw new TConfigurationException('captcha_publickey_invalid',self::MIN_PUBLIC_KEY);
  		$this->setViewState('PublicKey',$value,'');
  	}
 @@ -189,7 +198,9 @@ class TCaptcha extends TImage  		{
  			$token=$this->getToken();
  			$tokenLength=strlen($token);
 -			$url=$this->getApplication()->getAssetManager()->publishFilePath($this->getCaptchaScriptFile());
 +			$manager=$this->getApplication()->getAssetManager();
 +			$manager->publishFilePath($this->getFontFile());
 +			$url=$manager->publishFilePath($this->getCaptchaScriptFile());
  			$url.='?pk='.urlencode($this->getPublicKey());
  			$url.='&length='.$tokenLength;
  			$url.='&case='.($this->getCaseSensitive()?'1':'0');
 @@ -208,6 +219,11 @@ class TCaptcha extends TImage  		return dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'captcha.php';
  	}
 +	protected function getFontFile()
 +	{
 +		return dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'verase.ttf';
 +	}
 +
  	/**
  	 * Generates a file with a randomly generated private key.
  	 * @return string the path of the file keeping the private key
 @@ -254,13 +270,13 @@ class TCaptcha extends TImage  	/**
  	 * Converts a hash string into a string with characters consisting of alphanumeric characters.
  	 * @param string the hexadecimal representation of the hash string
 -	 * @param string the alphabet used to represent the converted string. If empty, it means 0-9, a-z and A-Z.
 +	 * @param string the alphabet used to represent the converted string. If empty, it means '234578adefhijmnrtwyABDEFGHIJLMNQRTWY', which excludes those confusing characters.
  	 * @return string the converted string
  	 */
  	protected function hash2string($hex,$alphabet='')
  	{
  		if(strlen($alphabet)<2)
 -			$alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 +			$alphabet='234578adefhijmnrtABDEFGHJLMNQRT';
  		$hexLength=strlen($hex);
  		$base=strlen($alphabet);
  		$result='';
 @@ -275,6 +291,16 @@ class TCaptcha extends TImage  		}
  		return $result;
  	}
 +
 +	protected function checkRequirements()
 +	{
 +		if(!extension_loaded('gd'))
 +			throw new TConfigurationException('captcha_gd2_required');
 +		if(!function_exists('imagettftext'))
 +			throw new TConfigurationException('captcha_imagettftext_required');
 +		if(!function_exists('imagepng'))
 +			throw new TConfigurationException('captcha_imagepng_required');
 +	}
  }
  ?>
\ No newline at end of file diff --git a/framework/Web/UI/WebControls/assets/captcha.php b/framework/Web/UI/WebControls/assets/captcha.php index aa3fdcc0..a722772b 100644 --- a/framework/Web/UI/WebControls/assets/captcha.php +++ b/framework/Web/UI/WebControls/assets/captcha.php @@ -1,11 +1,17 @@  <?php
 -require_once(dirname(__FILE__).'/captcha_key.php');
 -
 -if(isset($_GET['pk']))
 -	echo $_GET['pk'].$privateKey;
 +if(isset($_GET['pk']) && strlen($_GET['pk'])>=6 && isset($_GET['length']) && (int)$_GET['length']>=4 && isset($_GET['case']))
 +{
 +	require_once(dirname(__FILE__).'/captcha_key.php');
 +	$publicKey=$_GET['pk'];
 +	$tokenLength=(int)$_GET['length'];
 +	$caseSensitive=!empty($_GET['case']);
 +	$token=generateToken($publicKey,$privateKey,$tokenLength,$caseSensitive);
 +}
  else
 -	echo $privateKey;
 +	$token='error';
 +
 +displayToken($token);
  function generateToken($publicKey,$privateKey,$tokenLength,$caseSensitive)
  {
 @@ -16,7 +22,7 @@ function generateToken($publicKey,$privateKey,$tokenLength,$caseSensitive)  function hash2string($hex,$alphabet='')
  {
  	if(strlen($alphabet)<2)
 -		$alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 +		$alphabet='234578adefhijmnrtABDEFGHJLMNQRT';
  	$hexLength=strlen($hex);
  	$base=strlen($alphabet);
  	$result='';
 @@ -32,4 +38,46 @@ function hash2string($hex,$alphabet='')  	return $result;
  }
 +function displayToken($token)
 +{
 +	$length=strlen($token);
 +	$width=45*$length;
 +	$height=70;
 +	$image=imagecreatetruecolor($width,$height);
 +	$font=dirname(__FILE__).DIRECTORY_SEPARATOR.'verase.ttf';
 +	$vred=rand(0,100);
 +	$vgreen=rand(0,100);
 +	$vblue=rand(0,100);
 +	for($x=0;$x<$width;++$x)
 +	{
 +		for($y=0;$y<$height;++$y)
 +		{
 +			$vred+=rand(-2,2);
 +			$vgreen+=rand(-2,2);
 +			$vblue+=rand(-2,2);
 +			if($vred<0) $vred=0; if($vred>150) $vred=75;
 +			if($vgreen<0) $vgreen=0; if($vgreen>150) $vgreen=75;
 +			if($vblue<0) $vblue=0; if($vblue>150) $vblue=75;
 +			$col = imagecolorallocate($image, $vred, $vgreen, $vblue);
 +			imagesetpixel($image, $x, $y, $col);
 +            imagecolordeallocate($image, $col);
 +		}
 +	}
 +
 +    imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
 +    for($i=0;$i<$length;$i++)
 +	{
 +        $vred = rand(150, 240);
 +		$vgreen = rand(150, 240);
 +		$vblue = rand(150, 240);
 +        $col = imagecolorallocate($image, $vred, $vgreen, $vblue);
 +        $char = $token[$i];
 +        imagettftext($image, rand(40, 50), rand(-10, 20), 13 + (40 * $i), rand(50, imagesy($image) - 10), $col, $font, $char);
 +        imagecolordeallocate($image, $col);
 +    }
 +    imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
 +	imagepng($image);
 +	imagedestroy($image);
 +}
 +
  ?>
\ No newline at end of file diff --git a/framework/Web/UI/WebControls/assets/verase.ttf b/framework/Web/UI/WebControls/assets/verase.ttf Binary files differnew file mode 100644 index 00000000..4b4ecc66 --- /dev/null +++ b/framework/Web/UI/WebControls/assets/verase.ttf  | 
