[Shuttlebb-svn] SF.net SVN: shuttlebb: [85] branch/sources/class_captcha.php
Brought to you by:
afterlife69,
danb00
|
From: <aft...@us...> - 2006-12-06 05:34:37
|
Revision: 85
http://svn.sourceforge.net/shuttlebb/?rev=85&view=rev
Author: afterlife69
Date: 2006-12-05 21:34:36 -0800 (Tue, 05 Dec 2006)
Log Message:
-----------
captcha
Modified Paths:
--------------
branch/sources/class_captcha.php
Modified: branch/sources/class_captcha.php
===================================================================
--- branch/sources/class_captcha.php 2006-12-05 16:33:46 UTC (rev 84)
+++ branch/sources/class_captcha.php 2006-12-06 05:34:36 UTC (rev 85)
@@ -1,8 +1,496 @@
<?php
/**
+ * Captcha abstraction layer
+ *
+ * This handler is responcible for caching of all kinds, it will
+ * generate files, check expiration dates, remove expired files
+ * and load cached data into other classes throughout the software.
+ *
* $Id$
* $Date$
* $Rev$
*/
-?>
\ No newline at end of file
+/**
+ * Constants
+ */
+define ( CAPTCHA_TABLE, $table_prefix . 'captcha' );
+
+/*
+CREATE TABLE `sbb_captcha` (
+ `captcha_id` INT( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
+ `captcha_string` TEXT NOT NULL ,
+ `user_id` TEXT NOT NULL
+);
+*/
+
+
+/**
+ * CAPTCHA Class.
+ * Allows advanced CAPTCHA creation using simple wrapper functions.
+ *
+ * @todo This class should be an abstract master layer (abstract = parent only)
+ *
+ * @author Tyler King <ai...@ya...>
+ * @author Dean Newman <web...@au...>
+ */
+class captcha
+{
+ /**
+ * Contains the directory path to the TFF fonts.
+ *
+ * @var string
+ */
+ var $font_path;
+
+ /**
+ * Contains the directory path to the image backgrounds.
+ *
+ * @var string
+ */
+ var $background_path;
+
+ /**
+ * Contains the CAPTCHA configuration.
+ *
+ * @var string
+ */
+ var $data;
+
+ /**
+ * Contains randomly assigned colors.
+ *
+ * @var string
+ */
+ var $rgb;
+
+ /**
+ * Initilizes the class.
+ */
+ function captcha ( )
+ {
+ // Set up the in-class variables.
+ $this->font_path = './ttf/';
+ $this->background_path = './backgrounds/';
+ $this->data = array ( );
+ $this->rgb = $this->gen_rand_color ( );
+ }
+
+ /**
+ * Generates random RGB colors.
+ *
+ * @param integer $start
+ * @param integer $end
+ * @return array
+ */
+ function gen_rand_color ( $start = 0, $end = 255 )
+ {
+ return array ( rand ( $start, $end ), rand ( $start, $end ), rand ( $start, $end ) );
+ }
+
+ /**
+ * Converts hex values to RGB values.
+ *
+ * @param string $color
+ * @return array
+ */
+ function hex_rgb ( $color )
+ {
+ // Remove the hex identifier.
+ $color = str_replace ( '#', '', $color );
+ for ( $i = 0; $i < 3; $i++ )
+ {
+ // Convert to RGB decimal values.
+ $rgb[ ] = hexdec ( substr ( $color, ( 2 * $i ), 2 ) );
+ }
+
+ return $rgb;
+ }
+
+ /**
+ * Walk a directory to return it's files.
+ *
+ * @param string $dir_path
+ * @return array
+ */
+ function walk_dir ( $dir_path )
+ {
+ // Open the directory. Set up the file array.
+ $dh = @opendir ( $dir_path );
+ $files = array ( );
+
+ while ( $file = @readdir ( $dh ) )
+ {
+ // A hidden file ?
+ if ( $file == '.' || $file == '..' || $file{0} == '.' )
+ {
+ // Yes, it is hidden. Skip it.
+ continue;
+ }
+ else
+ {
+ // No, it is not hidden. Append it.
+ $files[ ] = $file;
+ }
+ }
+ @closedir ( $dh );
+
+ return $files;
+ }
+
+ /**
+ * Resizes a background image to the CAPTCHA dimensions.
+ *
+ * @param string $image
+ * @return object
+ */
+ function resize_image ( $image )
+ {
+ // Get the background's height and width.
+ $image_data = getimagesize ( $image );
+
+ // Resize it.
+ $im = imagecreatetruecolor( $this->data[ 'width' ], $this->data[ 'height' ] );
+ $image = imagecreatefrompng ( $image );
+ imagecopyresampled ( $im, $image, 0, 0, 0, 0, $this->data[ 'width' ], $this->data[ 'width' ], $image_data[ 0 ], $image_data[ 1 ] );
+
+ return $im;
+ }
+
+ /**
+ * Check if a CAPTCHA code entered by a user is correct.
+ *
+ * @param string $code
+ * @return boolean
+ */
+ function confirm ( $code )
+ {
+ global $db;
+ global $userdata;
+
+ // Get the CAPTCHAs.
+ $sql = 'SELECT * FROM ' . CAPTCHA_TABLE;
+ $result = $db->sql_query ( $sql );
+ $row = $db->sql_fetchrow ( $result );
+
+ // Does the string entered by user match the database ?
+ if ( strtolower ( $row[ 'captcha_string' ] ) == strtolower ( $code ) )
+ {
+ // Yes. Delete it. Return true for correct.
+ $sql = 'DELETE FROM ' . CAPTCHA_TABLE . ' WHERE user_id = ' . $userdata[ 'user_id' ];
+ $db->sql_query ( $sql );
+
+ return true;
+ }
+ else
+ {
+ // No. Delete it. Return false for incorrect.
+ $sql = 'DELETE FROM ' . CAPTCHA_TABLE . ' WHERE user_id = ' . $userdata[ 'user_id' ];
+ $db->sql_query ( $sql );
+
+ return false;
+ }
+ }
+
+ /**
+ * (Wrapper) Set the size for the CAPTCHA.
+ *
+ * @param integer $height
+ * @param integer $width
+ */
+ function size ( $height, $width )
+ {
+ $this->data[ 'width' ] = $width;
+ $this->data[ 'height' ] = $height;
+ }
+
+ /**
+ * (Wrapper) Set the color for strings and backgrounds of the CAPTCHA.
+ *
+ * @param string $color
+ */
+ function color ( $color = '' )
+ {
+ // Is it a hex value ?
+ if ( strstr ( $color, '#' ) )
+ {
+ // Yes. Convert it to RGB.
+ $this->data[ 'color' ] = $this->hex_rgb ( $background );
+ }
+ else
+ {
+ // No. Randomize a color.
+ $this->data[ 'color' ] = $this->gen_rand_color ( 200, 255 );
+ }
+ }
+
+ /**
+ * (Wrapper) Set the CAPTCHA code length.
+ *
+ * @param integer $length
+ */
+ function string_length ( $length )
+ {
+ $this->data[ 'string.length' ] = $length;
+ }
+
+ /**
+ * (Wrapper) Set the background image for the CAPTCHA.
+ *
+ * @param boolean/string $background
+ */
+ function background ( $background = true )
+ {
+ // True = random background image. String = background image.
+ $this->data[ 'background' ] = $background;
+ }
+
+ /**
+ * (Wrapper) Set lines for the CAPTCHA.
+ *
+ * @param integer $lines
+ * @param string $layer
+ */
+ function lines ( $lines = 0, $layer = 'background' )
+ {
+ $this->data[ 'lines.layer' ] = $layer;
+ $this->data[ 'lines' ] = ( $lines > 0 ) ? $lines : rand ( 4, 5 );
+ }
+
+ /**
+ * (Wrapper) Set noise for the CAPTCHA.
+ *
+ * @param integer $amount
+ * @param string $layer
+ */
+ function noise ( $amount = '', $layer = 'background' )
+ {
+ $this->data[ 'noise.layer' ] = $layer;
+ $this->data[ 'noise' ] = ( $amount > 0 ) ? $amount : rand ( 4, 5 );
+ }
+
+ /**
+ * (Wrapper) Set ellipses for the CAPTCHA.
+ *
+ * @param integer $amount
+ * @param string $layer
+ */
+ function ellipse ( $amount = '', $layer = 'background' )
+ {
+ $this->data[ 'ellipse.layer' ] = $layer;
+ $this->data[ 'ellipse' ] = ( $amount > 0 ) ? $amount : rand ( 4, 5 );
+ }
+
+ /**
+ * (Wrapper) Set filters for the CAPTCHA
+ *
+ * @param integer $amount
+ * @param string $filter
+ */
+ function filter ( $amount = '', $filter = 'random' )
+ {
+ if ( strtolower ( $filter ) == 'blur' )
+ {
+ $type = IMG_FILTER_GAUSSIAN_BLUR;
+ }
+ else if ( strtolower ( $filter ) == 'contrast' )
+ {
+ $type = IMG_FILTER_CONTRAST;
+ }
+ else if ( strtolower ( $filter ) == 'colorize' )
+ {
+ $type = IMG_FILTER_COLORIZE;
+ }
+ else if ( strtolower ( $filter ) == 'emboss' )
+ {
+ $type = IMG_FILTER_EMBOSS;
+ }
+ else if ( strtolower ( $filter ) == 'highlight' )
+ {
+ $type = IMG_FILTER_EDGEDETECT;
+ }
+ else if ( strtolower ( $filter ) == 'sketch' )
+ {
+ $type = IMG_FILTER_MEAN_REMOVAL;
+ }
+ else if ( strtolower ( $filter ) == 'smooth' )
+ {
+ $type = IMG_FILTER_SMOOTH;
+ }
+ else
+ {
+ // Nothing set, randomize.
+ $type = rand ( ) & count ( array ( IMG_FILTER_GAUSSIAN_BLUR, IMG_FILTER_CONTRAST, IMG_FILTER_COLORIZE, IMG_FILTER_EMBOSS, IMG_FILTER_EDGEDETECT, IMG_FILTER_MEAN_REMOVAL, IMG_FILTER_SMOOTH0 ) );
+ }
+
+ $this->data[ 'filter' ] = $type;
+ $this->data[ 'filter.amount' ] = $amount;
+ }
+
+ /**
+ * Render the CAPTCHA image with all the configurations set.
+ *
+ * @return object.
+ */
+ function render ( )
+ {
+ global $db;
+ global $userdata;
+
+ // Walk the directories to return the files.
+ $walked = $this->walk_dir ( $this->font_path );
+ $backgrounds = $this->walk_dir ( $this->background_path );
+
+ if ( !empty ( $this->data[ 'background' ] ) )
+ {
+ // Background set. Resize the background.
+ $captcha = $this->resize_image ( $this->background_path . $backgrounds[ rand ( ) % count ( $backgrounds ) ] );
+ }
+ else
+ {
+ // No background set.
+ $captcha = imagecreate ( $this->data[ 'width' ], $this->data[ 'height' ] );
+ }
+
+ if ( !empty ( $this->data[ 'string.length' ] ) )
+ {
+ // Length set.
+ $string_length = strlen ( $this->data[ 'string.length' ] );
+ }
+ else
+ {
+ // No length set, randomize.
+ $string_length = rand ( 4, 5 );
+ }
+
+ for ( $i = 0; $i < $string_length; $i++ )
+ {
+ // Convert the integers into characters.
+ $string .= chr ( rand ( 65, 90 ) );
+ }
+
+ if ( !empty ( $this->data[ 'color' ] ) )
+ {
+ // Color set (already converted into RGB).
+ $colors = $this->data[ 'color' ];
+ }
+ else
+ {
+ // No color set, randomize.
+ $colors = $this->gen_rand_color ( 200, 255 );
+ }
+
+ $back_color = imagecolorallocate ( $captcha, $colors[ 0 ], $colors[ 1 ], $colors[ 2 ] );
+ if ( !empty ( $this->data[ 'lines.layer' ] ) && $this->data[ 'lines.layer' ] == 'background' )
+ {
+ for ( $i = 0; $i < $this->data[ 'lines' ]; $i++ )
+ {
+ // All the X, Y and length of things.
+ $rx1 = rand ( 0, $this->data[ 'width' ] );
+ $rx2 = rand ( 0, $this->data[ 'width' ] );
+ $ry1 = rand ( 0, $this->data[ 'height' ] );
+ $ry2 = rand ( 0, $this->data[ 'height' ] );
+ $rcVal = rand ( 0, 255 );
+
+ $rc1 = imagecolorallocate ( $captcha, rand ( 0, 255 ), rand ( 0, 255 ), rand ( 100, 255 ) );
+ imageline ( $captcha, $rx1, $ry1, $rx2, $ry2, $rc1 );
+ }
+ }
+
+ if ( !empty ( $this->data[ 'noise.layer' ] ) && $this->data[ 'noise.layer' ] == 'background' )
+ {
+ for ( $i = 0; $i< $this->data[ 'noise' ]; $i++ )
+ {
+ // Set where the pixel is and it's color.
+ imagesetpixel ( $captcha, rand ( 0, $this->data[ 'width' ] ), rand ( 0, $this->data[ 'height' ] ), rand ( 0, 255 ) );
+ }
+ }
+
+ if ( !empty ( $this->data[ 'ellipse.layer' ] ) && $this->data[ 'ellipse.layer' ] == 'background' )
+ {
+ for ( $i = 1; $i < $this->data[ 'ellipse' ]; $i++ )
+ {
+ // Set where the ellipse is and it's color. (Random colors).
+ imageellipse ( $captcha, rand ( 1, 200 ), rand ( 1, 50 ), rand ( 50, 100 ), rand ( 12, 25 ), $colors[ rand ( 0, 24 ) ] );
+ }
+
+ for ( $i = 1; $i < $this->data[ 'ellipse' ]; $i++ )
+ {
+ // Set where the ellipse is and it's color. (Set colors).
+ imageellipse ( $captcha, rand ( 1, 200 ), rand ( 1, 50 ), rand ( 50, 100 ), rand ( 12, 25 ), $back_color );
+ }
+ }
+
+ for ( $i = 0; $i < 25; $i++ )
+ {
+ // Get some random colors to use.
+ $rgb = $this->gen_rand_color ( rand ( 0, 125 ), rand ( 125, 255 ) );
+ $colors[ ] = imagecolorallocate ( $captcha, $rgb[ 0 ], $rgb[ 1 ], $rgb[ 2 ] );
+ }
+
+ for ( $i = 1; $i <= $string_length; $i++ )
+ {
+ $c_c = ( rand ( 1, 2 ) == 1 ) ? rand ( 0, 45 ) : rand ( 315, 360 );
+
+ // Add in a character with color and a random font.
+ imagettftext ( $captcha, rand ( 14, 20 ), $c_c, ( $i * 25 ), 30, $colors[ rand ( 0, 24 ) ], $this->font_path . $walked[ rand ( ) % count ( $walked ) ], substr ( $string, ( $i - 1 ), 1 ) );
+ }
+
+ if ( !empty ( $this->data[ 'ellipse.layer' ] ) && $this->data[ 'ellipse.layer' ] == 'foreground' )
+ {
+ for ( $i = 1; $i < $this->data[ 'ellipse' ]; $i++ )
+ {
+ // Set where the ellipse is and it's color. (Random colors).
+ imageellipse ( $captcha, rand ( 1, 200 ), rand ( 1, 50 ), rand ( 50, 100 ), rand ( 12, 25 ), $colors[ rand ( 0, 24 ) ] );
+ }
+
+ for ( $i = 1; $i < $this->data[ 'ellipse' ]; $i++ )
+ {
+ // Set where the ellipse is and it's color. (Set colors).
+ imageellipse ( $captcha, rand ( 1, 200 ), rand ( 1, 50 ), rand ( 50, 100 ), rand ( 12, 25 ), $backcolor );
+ }
+ }
+
+ if ( !empty ( $this->data[ 'noise.layer' ] ) && $this->data[ 'noise.layer' ] == 'foreground' )
+ {
+ for ( $i = 0; $i < $this->data[ 'noise' ]; $i++ )
+ {
+ // Set where the pixel is and it's color.
+ imagesetpixel ( $captcha, rand ( 0, $this->data[ 'width' ] ), rand ( 0, $this->data[ 'height' ] ), rand ( 0, 255 ) );
+ }
+ }
+
+ if ( !empty ( $this->data[ 'lines.layer' ] ) && $this->data[ 'lines.layer' ] == 'foreground' )
+ {
+ for ( $i = 0; $i < $this->data[ 'lines' ]; $i++ )
+ {
+ // All the X, Y and length of things.
+ $rx1 = rand ( 0, $this->data[ 'width' ] );
+ $rx2 = rand ( 0, $this->data[ 'width' ] );
+ $ry1 = rand ( 0, $this->data[ 'height' ] );
+ $ry2 = rand ( 0, $this->data[ 'height' ] );
+ $rcVal = rand ( 0, 255 );
+
+ $rc1 = imagecolorallocate ( $captcha, rand ( 0, 255 ), rand ( 0, 255 ), rand ( 100, 255 ) );
+ imageline ( $captcha, $rx1, $ry1, $rx2, $ry2, $rc1 );
+ }
+ }
+
+ if ( !empty ( $this->data[ 'filter' ] ) && phpversion ( ) >= 5 )
+ {
+ // Assign the filter if the PHP version is not less then 5.
+ imagefilter ( $captcha, $type, ( !empty ( $this->data[ 'filter.amount' ] ) ) ? $this->data[ 'filter.amount' ] : 50 );
+ }
+
+ // Insert it into the database for later.
+ $sql = 'INSERT INTO ' . CAPTCHA_TABLE . ' (captcha_id, captcha_string, user_id) VALUES ("NULL", "' . $string . '", "' . $userdata[ 'user_id' ] . '")';
+ $db->sql_query ( $sql );
+
+ // Output it.
+ header ( 'Content-Type: image/png' );
+ imagepng ( $captcha );
+ imagedestroy ( $captcha );
+ }
+}
+
+?>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|