App-ZofCMS-PluginBundle-Naughty

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/Captcha.pm  view on Meta::CPAN

package App::ZofCMS::Plugin::Captcha;

use warnings;
use strict;

our $VERSION = '1.001002'; # VERSION

use base 'App::ZofCMS::Plugin::Base';
use GD::SecurityImage;

sub _key { 'plug_captcha' }
sub _defaults {
    string  => undef,
    file    => undef,
    width   => 80,
    height  => 20,
    lines   => 5,
    particle => 0,
    no_exit => 1,
    style   => 'rect',
    format  => 'gif',
    tcolor  => '#895533',
    lcolor  => '#000000',
}
sub _do {
    my ( $self, $conf, $t, $q, $config ) = @_;

    my $image = GD::SecurityImage->new(
        width   => $conf->{width},
        height  => $conf->{height},
        lines   => $conf->{lines},
        gd_font => 'giant'
    );

    $image->random( $conf->{string} );
    $image->create('normal', @$conf{qw/style tcolor lcolor/} );

    if ( $conf->{particle} ) {
        $image->particle( ref $conf->{particle} ? @{ $conf->{particle} } : () );
    }

    my ( $image_data, $mime_type, $random_number ) = $image->out( force => $conf->{format} );
    $t->{d}{session}{captcha} = $random_number;

    if ( defined $conf->{file} and length $conf->{file} ) {
        if ( open my $fh, '>', $conf->{file} ) {
            binmode $fh;
            print $fh $image_data;
            close $fh;
        }
        else {
            $t->{t}{plug_captcha_error} = $!;
            return;
        }
    }
    else {
        binmode STDOUT;
        print "Content-Type: image/$mime_type\n\n";
        print $image_data;
        exit
            unless $conf->{no_exit};
    }
}

1;
__END__

=encoding utf8

=for stopwords RGB bots captcha captchas crypto cryptocrap runcycle runlevel subref

=head1 NAME

App::ZofCMS::Plugin::Captcha - plugin to utilize security images (captchas)

=head1 SYNOPSIS

    plugins => [
        { Session => 1000 },
        { Captcha => 2000 },
    ],
    plugins2 => [
        qw/Session/
    ],

    plug_captcha => {},

    # Session plugin configuration (i.e. database connection is left out for brevity)

=head1 DESCRIPTION

The module is a plugin for L<App::ZofCMS> that provides means to generate and display
security images, known as "captchas" (i.e. protecting forms from bots).

The plugin was coded with idea that you will be using L<App::ZofCMS::Plugin::Session>
along with it to store the generated random string; however, it's not painfully
necessary to use Session plugin (just easier with it).

This documentation assumes you've read L<App::ZofCMS>, L<App::ZofCMS::Config> and L<App::ZofCMS::Template>

=head1 FIRST-LEVEL ZofCMS TEMPLATE AND MAIN CONFIG FILE KEYS

=head2 C<plugins>

    plugins => [
        { Session => 1000 },
        { Captcha => 2000 },
    ],
    plugins2 => [
        qw/Session/
    ],

B<Mandatory>. You need to include the plugin in the list of plugins to execute. I'm using
Session plugin here to first load existing session and after Captcha is ran, to save
the session.

=head2 C<plug_captcha>



( run in 0.778 second using v1.01-cache-2.11-cpan-437f7b0c052 )