Home Wendy
Web engine software

Wendy Home Page / Docs / Modules docs / Captcha
 download   mods   docs   contact 
  To know more about what is CAPTCHA see CAPTCHA Wikipedia article.

About

This module provides a way to create and validate captchas in Wendy handlers. It uses GD::SecurityImage for picture creation. Only visual captchas are supported yet.

Package contents

  captcha.wpm# Wendy module description file
  captcha.data/Captcha.pm# Wendy::Mofules::Captcha Perl module
  captcha.data/arial.ttf# Arial True Type font file
  captcha.data/mod_captcha_test# Captcha test page template
  captcha.data/mod_captcha_test.pl# Captcha test page handler

Example using Wendy::Modules::Captcha

After you install a module on your host, example will be installed at address http://www.yourexamplehost.com/mod/captcha/test/. On that page you should be able to see and test generated captchas yourself.

Module API

Simplest example on how to create captcha:

 
use Wendy::Modules::Captcha;
...

my $cap = Wendy::Modules::Captcha -> new();
...


&add_replace( 'PUBLIC' => $cap -> get_public(),
	      'CAP_IMG_SRC' => $cap -> captcha_uri() );


Validating captcha:

 
use Wendy::Modules::Captcha;
...

my $cgi = $WOBJ -> { "CGI" }; # About $WOBJ
my $public = $cgi -> param( "public" );
my $user_guess = $cgi -> param( "user_guess" );

if( &check_captcha( $public,
                    $user_guess ) )
{
# Correct
} else
{
# Wrong!
}

Following methods are provided by this module:

  • new

    Create mew Wendy::Modules::Captcha object. Example:

     
    my $captcha = Wendy::Modules::Captcha -> new( scramble => 0,
                                                  width    => 200,
                                                  height   => 60,
                                                  font     => '/usr/local/fonts/times.ttf' );
    

    Arguments:

    • Any GD::SecurityImage -> new(); arguments.

    Defaults:

    • scramble - 1
    • width - 300
    • height - 80
    • lines - 10
    • font - arial.ttf supplied with module

  • set_text

    Sets captcha text. If called with empty arguments, text chosen randomly, length is default GD::SecurityImage length (rndmax). Be sure that text length will fit in captcha width. Example:

     
    $captcha -> set_text( 'Hello' );
    

    Arguments:

    • Text to set on captcha.

  • get_public

    Get public captcha key. This key goes to html page form, along with captcha image. It contains password, and hash of ( $password . $captcha_text ). Thats all we need to check user (or robot :) guess. Example:

     
    my $public = $captcha -> get_public();
    

  • get_pass

    Returns captcha password. Example:

     
    my $pass = $captcha -> get_pass();
    

  • get_text

    Returns captcha text. Example:

     
    my $text = $captcha -> get_text();
    

  • my_create

    Actually creates captcha, like GD::SecurityImage's create. Called automatically. Example:

     
    $captcha -> my_create();
    

  • write_png_to_file

    Writes captcha image to png file. Will be called automatically with correct path and random file name, when you request captcha image URL. Example:

     
    $captcha -> write_png_to_file( '/tmp/test.png' );
    
    # or:
    
    use Wendy::Config 'CONF_VARPATH';
    ...
    
    my $rstr = md5_hex( rand() );
    my $dst_file = File::Spec -> catfile( CONF_VARPATH,
                                          'hosts',
                                          $WOBJ -> { 'HOST' } -> { 'host' },
                                          'htdocs',
                                          'mod',
                                          'captcha',
                                          'var',
                                          $rstr . ".png" );
    $captcha -> write_png_to_file( $dst_file );
    

    Arguments:

    • Path to file, to which we should write PNG captcha data.

  • captcha_uri

    Rerurns captcha image to embed into your form into <img src="... Automatically calls set_text, my_create, write_png_to_file, etc, if needed. Returns ready and existing captcha URL. Example:

     
    my $img_url = $captcha -> captcha_uri();
    

  • check_captcha

    Validates user input. Example:

     
    if( &check_captcha( $public,
                        $user_guess ) )
    {
    # Correct
    } else
    {
    # Wrong!
    }
    

    Arguments:

    • $public - captcha public string (see get_public method)
    • $user_guess - what user (or robot :) entered as our captcha guess

That's all about this module.

eugene kuzin, 2007-2011