ASP4x-Captcha-Imager

 view release on metacpan or  search on metacpan

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN


=head1 NAME

ASP4x::Captcha::Imager - Imager-based CAPTCHA for your ASP4 web application.

=head1 SYNOPSIS

=head2 In Your asp4-config.conf

  {
    ...
    "system": {
      ...
      "settings": {
        ...
        "captcha_key":      "Some random string of any length",
        "captcha_font":     "@ServerRoot@/etc/LiberationSans-Regular.ttf",
        "captcha_width":    140,
        "captcha_height":   40,
        "captcha_bg_color": "FFFFFF",
        "captcha_length":   4
        ...
      }
    }
  }

=head2 In a handler

Simply subclass C<ASP4x::Captcha::Imager> as shown below:

  package dev::captcha;

  use strict;
  use warnings 'all';
  use base 'ASP4x::Captcha::Imager';
  use vars __PACKAGE__->VARS;

  1;# return true:

=head2 In your ASP Script:

Render the Captcha image:

  <html>
  <head>
  <style type="text/css">
  LABEL {
    display:        block;
    width:          265px;
    text-align:     right;
    float:          left;
    padding-right:  5px;
  }
  
  IMG {
    border: dotted 1px #AAA;
  }
  </style>
  </head>
  <body>
    <form action="/handlers/dev.validate" method="post">
      <p>
        <label>Enter the code you see below:</label>
        <input type="text" name="security_code" />
      </p>
      <p>
        <label>&nbsp;</label>
        <img id="captcha" src="/handlers/dev.captcha?r=<%= rand() %>" alt="Security Code" />
        <a href="" onclick="document.getElementById('captcha').src = '/handlers/dev.captcha?r=' + Math.random(); return false">
          (Click for a new Image)
        </a>
      </p>
      <p>
        <label>&nbsp;</label>
        <input type="submit" value="Submit" />
      </p>
    </form>
  </body>
  </html>

=head2 Validate the Captcha

  package dev::validate;

  use strict;
  use warnings 'all';
  use base 'ASP4::FormHandler';
  use vars __PACKAGE__->VARS;

  sub run
  {
    my ($s, $context) = @_;
    
    my $secret = $Config->system->settings->captcha_key;
    my $code = lc($Form->{security_code});
    
    # It should exist in the session and have the correct value:
    if( exists($Session->{asp4captcha}->{$code}) )
    {
      # Ding ding ding ding ding!
      $Response->Write("CORRECT");
    }
    else
    {
      # Bzzzzzzzzzzt: WRONG!
      $Response->Write("WRONG");
    }# end if()
  }# end run()

  1;# return true:

=head1 DESCRIPTION

"CAPTCHA" is the little security image containing a hard-to-read code that you may
have seen on some websites.  They are common on sign-up forms and email forms.  The
idea is that a bot or script can't read the image and can't guess the code.

C<ASP4x::Captcha::Imager> uses L<Imager> to generate an image of a random string
of numbers and letters.

=head2 What Does the Captcha Image Look Like?



( run in 1.359 second using v1.01-cache-2.11-cpan-ceb78f64989 )