ASP4x-Captcha-Imager

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


2011-05-09      v0.003
  - content-length is no longer set.  It was causing problems when combined with mod_gzip.

2010-06-03      v0.002
  - "Scaling" the captcha larger/smaller works better now, so you're not stuck
    with the default width and height.

2010-03-23      v0.001
  - Initial release.

inc/Module/Install.pm  view on Meta::CPAN

	close FH or die "close($_[0]): $!";
}

# _version is for processing module versions (eg, 1.03_05) not
# Perl versions (eg, 5.8.1).

sub _version ($) {
	my $s = shift || 0;
	   $s =~ s/^(\d+)\.?//;
	my $l = $1 || 0;
	my @v = map { $_ . '0' x (3 - length $_) } $s =~ /(\d{1,3})\D?/g;
	   $l = $l . '.' . join '', @v if @v;
	return $l + 0;
}

# Cloned from Params::Util::_CLASS
sub _CLASS ($) {
	(
		defined $_[0]
		and
		! ref $_[0]

inc/Module/Install/Makefile.pm  view on Meta::CPAN

	for my $subdir (@_) {
		push @$subdirs, $subdir;
	}
}

sub clean_files {
	my $self  = shift;
	my $clean = $self->makemaker_args->{clean} ||= {};
	  %$clean = (
		%$clean,
		FILES => join ' ', grep { length $_ } ($clean->{FILES} || (), @_),
	);
}

sub realclean_files {
	my $self      = shift;
	my $realclean = $self->makemaker_args->{realclean} ||= {};
	  %$realclean = (
		%$realclean,
		FILES => join ' ', grep { length $_ } ($realclean->{FILES} || (), @_),
	);
}

sub libs {
	my $self = shift;
	my $libs = ref $_[0] ? shift : [ shift ];
	$self->makemaker_args( LIBS => $libs );
}

sub inc {

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

  $Response->SetHeader('content-type' => 'image/png');
  $Response->ContentType( 'image/png' );
  $Response->Write( $str );
}# end run()


sub generate_pair
{
  my ($s, $context) = @_;

  my $len = eval { $Config->system->settings->captcha_length } || 4;
  my $chars = join '', ( 'A'..'H', 'J'..'N', 'P'..'Z', 1..9 );

  my $word = '';
  while( length($word) < $len )
  {
    $word .= substr($chars, int(rand()*length($chars)), 1);
  }# end while()
  
  my $key = md5_hex( lc($word) . ( eval { $Config->system->settings->captcha_key } || '' ) );
  
  return ( $word, $key );
}# end generate_pair()


sub font
{

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

=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;

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

=item * Shorter Captchas are probably good enough.

Unless you've got yourself the next Facebook, you could probably get away with
4 characters in your Captcha.  Long captchas will just annoy humans.

=item * Where to use Captcha

Any form that might be attacked by a script including registration forms, email forms, etc.
is a good candidate for a Captcha.  Since it's so easy to use Captchas there really
isn't any reason not to use them anywhere you think B<might> benefit.  If you keep
the Captcha length short (see the first point in this list) then the humans won't
be too bothered by them and may actually be pleased with your consideration of their privacy.

=back

=head2 What About Fonts?

Because Linux systems tend to put fonts in several different places, I recommend
copying the font file (*.ttf) into the C<etc/> folder of your website and referencing it
(just like you see in the C<t/> folder of this distribution and in the SYNOPSIS example above.

t/010-basic/020-basic.t  view on Meta::CPAN

use ASP4::API;

my $api; BEGIN { $api = ASP4::API->new }

ok(
  my $res = $api->ua->get('/handlers/dev.captcha'),
  "Got res"
);

ok(
  length($res->content),
  "Got res.content"
);

is(
  $res->header('content-type') => "image/png",
  "content-type is image/png"
);

my $Session = $api->context->session;

my $wordLength = $api->context->config->system->settings->captcha_length;
my $secret = $api->context->config->system->settings->captcha_key;

my ($word) = grep {
  length($_) == $wordLength &&
  md5_hex($_ . $secret) eq $Session->{asp4captcha}->{$_};
} keys %{ $Session->{asp4captcha} };

ok( $word, "Found word '$word'");

my $hashed = md5_hex($word . $secret);
is($hashed => $Session->{asp4captcha}->{$word}, "Hashing is correct" );


t/010-basic/030-form.t  view on Meta::CPAN

    "Got the captcha image"
  );
  ok(
    my ($form) = HTML::Form->parse( $res->content, '/' ),
    "Got the form"
  );


  # Figure out what the security code is:
  my $Session = $api->context->session;
  my $wordLength = $api->context->config->system->settings->captcha_length;
  my $secret = $api->context->config->system->settings->captcha_key;
  my ($word) = grep {
    length($_) == $wordLength &&
    md5_hex($_ . $secret) eq $Session->{asp4captcha}->{$_};
  } keys %{ $Session->{asp4captcha} };


  # Fill out the form - lowercase:
  $form->find_input('security_code')->value( lc($word) );
  $res = $api->ua->submit_form( $form );
  is( $res->content => "CORRECT", "Validated correctly (A)" );


t/conf/asp4-config.json  view on Meta::CPAN

    "load_modules": [
    ],
    "env_vars": {
    },
    "settings": {
      "captcha_key":      "Captcha-Is-Teh-Awesome",
      "captcha_font":     "@ServerRoot@/etc/LiberationSans-Regular.ttf",
      "captcha_width":    140,
      "captcha_height":   40,
      "captcha_bg_color": "FFFFFF",
      "captcha_length":   4
    }
  },
  "errors": {
    "error_handler":    "ASP4::ErrorHandler",
    "mail_errors_to":   "you@your-server.com",
    "mail_errors_from": "root@localhost",
    "smtp_server":      "localhost"
  },
  "web": {
    "application_name": "ASP4xCaptcha",



( run in 0.336 second using v1.01-cache-2.11-cpan-65fba6d93b7 )