<?php
/* $Id: cCaptcha.php,v 0.1 2009/09/11 07:48:54 aedavies Exp $ */
/*
* Copyright (c) 2009,2010,2011 Archzilon Eshun-Davies <hide@address.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* @Coder: Archzilon Eshun-Davies
* @Description: Captcha For Web Forms
* @Date: September 11, 2009
* @update: Added interface declaration and other
* Bug fixes =) 2010/10/14
* Added Session Class 2010/10/17
*/
require_once( 'cSession.php' );
interface iCaptcha {
public function generatecaptcha( $nLen ); # the len of the Captcha Text
public function showcaptcha(); # show captcha image(png)
public static function chkcaptchacode( $strInput ); # for form validation
# Check stored captcha against
# user provided input
}
final class cCaptcha implements iCaptcha {
/*
* @Function Name: generatecaptcha
* @Purpose: Generate a random String of $nLen long
* @Parameters: $nLen - Length of String to Generate
*/
public function generatecaptcha( $nLen )
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdfgijkmnpqrstuvwxyz0123456789";
$code = ""; # Init to null
$clen = strlen($chars) - 1; # a variable with the fixed length of
# chars correct for the fence post issue
while( strlen( $code ) < $nLen )
{
$code .= $chars[mt_rand( 0, $clen )]; /* mt_rand's range is inclusive - this is why we need 0 to n-1 */
}
return $code;
}
/*
* @Function Name: showcaptcha
* @Purpose: Display Captcha Image
* @Parameters: NONE
*/
public function showcaptcha()
{
# Some servers don't chroot like OpenBSD :(
putenv( 'GDFONTPATH='.realpath('.') );
$session = new cSession;
$text = $this->generatecaptcha( 5 ); # Default Captcha Length(in Chars) 5
# TODO: Must Loop To Allow Variable length specified by Developer.
$char1 = substr( $text, -5, 1 );
$char2 = substr( $text, -4, 1 );
$char3 = substr( $text, -3, 1 );
$char4 = substr( $text, -2, 1 );
$char5 = substr( $text, -1, 1 );
$session->set( "captcha_code", $text ); # Store Captcha Code For Future Checks
$height = 43; # Captcha image height
$width = 160; # Captcha image Width
$font = 'ooc/font.ttf'; # Captcha font(font file and location)
$image_p = imagecreate( $width, $height ); # Create Image
$black = imagecolorallocate( $image_p, 255, 255, 255 ); # Set Color to Black
$white = imagecolorallocate( $image_p, 0, 0, 0 ); # And White
# Get Char from font file as Image
imagettftext( $image_p, 22, rand( -50, 50 ), 10, 30, $white, $font, $char1 );
imagettftext( $image_p, 22, rand( -50, 50 ), 40, 30, $white, $font, $char2 );
imagettftext( $image_p, 22, rand( -50, 50 ), 70, 30, $white, $font, $char3 );
imagettftext( $image_p, 22, rand( -50, 50 ), 100, 30, $white, $font, $char4 );
imagettftext( $image_p, 22, rand( -50, 50 ), 130, 30, $white, $font, $char5 );
# Done
imageline( $image_p, 0, 0, 159, 0, $white );
imageline( $image_p, 0, 42, 159, 42, $white );
imageline( $image_p, 0, 0, 0, 42, $white );
imageline( $image_p, 159, 0, 159, 42, $white );
# Draw Mesh
$i = 7;
while( $i <= 43 )
{
imageline( $image_p, 1, $i, 170, $i, $white );
$i = $i + 7;
}
$j = 7;
while( $j <= 160 )
{
imageline( $image_p, $j, 1, $j, 43, $white );
$j = $j + 7;
}
/* Done */
#imagejpeg( $image_p, null, 80 ); /* Image Ready; Show it man */
imagepng( $image_p, null ); # Gotta love png ;=)
}
/*
* Function Name: chkcaptchacode
* Purpose: Check if User Entered The Correct Captcha Code
* Paramenters: $userinput - The Code the User Entered(eg. $_POST['sec_code')
*/
public static function chkcaptchacode( $userinput )
{
$session = new cSession;
$captcha_code = $session->get( 'captcha_code' );
if( $captcha_code != null )
{
if( $userinput == $captcha_code )
return 1;
else
return 0;
} else {
return 0;
}
}
}
?>