package Authen::Captcha;
# $Source: /usr/local/cvs/Captcha/pm/Captcha.pm,v $
# $Revision: 1.23 $
# $Date: 2003/12/18 04:44:34 $
# $Author: jmiller $
# License: GNU General Public License Version 2 (see license.txt)
use 5.00503;
use strict;
use GD;
use Digest::MD5 qw(md5_hex);
use Carp;
# these are used to find default images dir
use File::Basename;
use File::Spec;
use vars qw($VERSION);
$VERSION = sprintf "%d.%03d", q$Revision: 1.23 $ =~ /(\d+)/g;
# get our file name, used to find the default images
my $default_images_folder;
{
my $this_file = __FILE__;
my $this_dir = dirname($this_file);
my @this_dirs = File::Spec->splitdir( $this_dir );
$default_images_folder = File::Spec->catdir(@this_dirs,'Captcha','images');
}
my $num_of_soundfile_versions = 10;
# Preloaded methods go here.
sub new
{
my ($this) = shift;
my $class = ref($this) || $this;
my $self = {};
bless( $self, $class );
my %opts = @_;
# default character source images
my $type = defined($opts{type}) ? $opts{type} : 'image';
$self->type($type);
my $src_images = (defined($opts{images_folder}) && (-d $opts{images_folder}))
? $opts{images_folder} : $default_images_folder;
$self->images_folder($src_images);
my $debug = (defined($opts{debug}) && ($opts{debug} =~ /^\d+$/))
? $opts{debug} : 0;
$self->debug($debug);
$self->data_folder($opts{data_folder}) if($opts{data_folder});
$self->output_folder($opts{output_folder}) if($opts{output_folder});
my $expire = (defined($opts{expire}) && ($opts{expire} =~ /^\d+$/))
? $opts{expire} : 300;
$self->expire($expire);
my $width = (defined($opts{width}) && ($opts{width} =~ /^\d+$/))
? $opts{width} : 25;
$self->width($width);
my $height = (defined($opts{height}) && ($opts{height} =~ /^\d+$/))
? $opts{height} : 35;
$self->height($height);
my $keep_failures = (defined($opts{keep_failures}) && $opts{keep_failures})
? 1 : 0;
$self->keep_failures($keep_failures);
# create a random seed if perl version less than 5.004
if ($] < 5.005)
{ # have to seed rand. using a fairly good seed
srand( time() ^ ($$ + ($$ << 15)) );
} # else, we're just going to let perl do it's thing
return $self;
}
sub type
{
ref(my $self = shift) or croak "instance variable needed";
if (@_)
{
if ($_[0] =~ /^(jpg|png|gif|image|picture)$/i)
{
$self->{_type} = 'image';
} elsif ($_[0] =~ /^(sound|snd|wav|mp3)$/i) {
$self->{_type} = 'sound';
}
return $self->{_type};
} else {
return $self->{_type};
}
}
sub debug
{
ref(my $self = shift) or croak "instance variable needed";
if (@_)
{
$self->{_debug} = $_[0];
=1= |