Archive for maio, 2009

Catalyst::Plugin::Captcha

maio 15, 2009

Simples “como fazer” para usar o Catalyst::Plugin::Captcha.

Criando um projeto:


catalyst.pl MyApp

cd MyApp

Inserindo o plugin no projeto:

No Arquivo, altere:

vim lib/MyApp.pm

use Catalyst qw/-Debug
ConfigLoader
Static::Simple/;

para:

use Catalyst qw/-Debug
ConfigLoader
Static::Simple
Captcha
Session
Session::Store::FastMmap
Session::State::Cookie/;

Acrescente a configuração de como será a imagem de Captcha:

MyApp->config->{captcha} = {
session_name => ‘captcha_string’,
new => {
width      => 100,
height     => 80,
ptsize     => 28,
lines      => 10,
thickness  => 1,
rndmax     => 6,
scramble   => 0,
send_ctobg => 1,
gd_font    => ‘Giant’

},
create => [ qw/ normal rect/ ],
particle => [ 100  ],
out => {force => ‘png’}
};

Criando o Controller e as Views

Crie o controller:

script/myapp_create.pl controller home

Abra o controller

vim lib/MyApp/Controller/home.pm

Altere a o conteúdo:

sub index :Path :Args(0) {
my ( $self, $c ) = @_;

$c->response->body(‘Matched MyApp::Controller::home in home.’);
}

Para:

sub index :Path :Args(0) {
my ( $self, $c ) = @_;

$c->stash->{template} = ‘test.tt’;
}

Adicione os metódos:

sub captcha : Local {
my ($self, $c) = @_;
$c->create_captcha();
}

sub do_post : Local {
my ($self, $c) = @_;
if ($c->validate_captcha($c->req->param(‘validate’))){
$c->response->body(“ok”);
} else {
$c->response->body(“not ok”);
}
}

Crie as Views:

script/myapp_create.pl view TT TT

Para renderizar o Captcha:

vim root/test.tt

<h1> Catalyst::Plugin::Captcha </h1>

<img src=”[% c.uri_for(‘captcha’) -%]” />
<br>
<form action=”do_post”>
<input type=”text” name=”validate” >
<input type=”submit”>
</form>

Agora é só testar:

script/myapp_server.pl

http://localhost:3000/home

Mais:

http://search.cpan.org/~kazeburo/Catalyst-Plugin-Captcha-0.03/lib/Catalyst/Plugin/Captcha.pm

http://search.cpan.org/~burak/GD-SecurityImage-1.70/lib/GD/SecurityImage.pm