#!/usr/bin/perl

################ (C) COPYRIGHT ##########################################
#		Programmer: 	Tri Dung Nguyen				#
#		Website:	www.vitinh.de				#
#		Mirrors:	www.freeperlscripts.de			#
#				www.tridung.de				#
#########################################################################

my $flck = 1; # '1' or '0' ('1' if your provider allows "flock" and '0' if not)
my $grid = 2; # '0' = don't show grid; '1' = show grid; '2' = randomly (on the captcha)
	
################ MULTIPLE USAGE OF FPG ON ONE SERVER ####################
# Change the two varialbles below if you want to install more than one  #
# FPG on the same server. For example:					#
#	my $dir = "fpg_files_2";					#
# for the second album and so on... Do the same with the files fpg.cgi	#
# and install.cgi							#
#########################################################################

################ FPG on a Windows Machine with IIS ######################
# Use absolute path for the varialble below if you want to use FPG   	#
# on a Windows Machine with IIS. For example:				#
#	my $dir = "c\:\\myaccount\\mydir\\fpg_files";			#
#########################################################################

my $dir = "fpg_files";

####################### BEGIN OF PROGRAM CODE ###########################
						
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use CGI qw(:standard);
#use strict;

my $configs = "$dir/configs";
my $session = "$dir/session";
my $antispam = "$dir/antispam";

my $gd = 0;
eval {require GD;}; unless($@){$gd = 1;import GD;} # Comment this line if you get a GD-Error

my %configs = &configs;
my $cgi = new CGI;
my $id = $cgi->param('id');

if(($configs{'ANTISPAM_METHOD'}) == 1 and $gd){
	&captcha;
}
else{
	&showpic;
}

########### Get And Show Image ##########################################
sub showpic{
	opendir(DH, $antispam) or die "Cannot open $antispam! $!";
	my @imgs = grep !/^\.\.?$/, readdir(DH);
	closedir(DH);
	&randomize(\@imgs) if(@imgs);
	open(DH,">$session/$id.aid") or die "Cannot open $session/$id.aid! $!";
	print DH "$imgs[0]";
	close(DH);
	
	my($name,$ext) = split(/\./, $imgs[0]);
	$ext = lc $ext;
	my %mimes = (
		'jpg' => 'image/jpeg',
		'jpeg' => 'image/jpeg',
		'gif' => 'image/gif',
		'png' => 'image/png'
		);
	open(DH,"<$antispam/$imgs[0]") or die "Cannot open image! $!";
	binmode(DH);
	print "Content-Type: $mimes{$ext}\n\n";
	while(<DH>){
		print;
	}
	close(DH);
}
########### Generate And Show Images ####################################
sub captcha{
	my $lang = $cgi->param('lang');
	   $lang =~ s/\///g;
	   $lang =~ s/\\//g;
	   $lang =~ s/\.//g;
	print "Content-type: image/jpeg\n\n";
	my @nums = ("A" .. "Z");
	my $captcha = join ("", @nums[map{rand @nums}(1 .. 6)]);
	if(defined $lang and $lang and (-e "$dir/languages/$lang.lang") and (-e "$dir/languages/$lang.cap")){
		my @words = ();
		open(DH,"<$dir/languages/$lang.cap") or die "Cannot open $dir/languages/$lang.cap! $!";
		while(<DH>){
			next if($_ =~ /^\#/);
			$_ =~ s/\r//g;
			$_ =~ s/\n//g;
			$_ =~ s/\s//g;
			push @words, $_ if($_ and ($_ =~ /^[a-z]+$/i) and (length $_ > 2));
		}
		close(DH);
		if(scalar @words > 20){
			&randomize(\@words);
			($captcha) = $words[0] =~ /^(.{3,6}).*?/;
			$captcha = uc $captcha;
		}
	}
	open(DH,">$session/$id.aid") or die "Cannot open $session/$id.aid! $!";
	print DH $captcha;
	close(DH);
	my $img = new GD::Image(60,20);  
	my $bg = $img->colorAllocate(255,0,0); # background color of the Captcha (RGB)
	my $fg = $img->colorAllocate(255,255,255); # foreground color of the Captcha (RGB)
	my $ln = $img->colorAllocate(0,0,0); # line color
	my @grids = (0,1);
	&randomize(\@grids);
	$grid = $grids[0] if($grid == 2);
	if($grid){
		$img->line(0,5,60,5,$ln);
		$img->line(0,10,60,10,$ln);
		$img->line(0,15,60,15,$ln);
		$img->line(5,0,5,20,$ln);
		$img->line(10,0,10,20,$ln);
		$img->line(15,0,15,20,$ln);
		$img->line(20,0,20,20,$ln);
		$img->line(25,0,25,20,$ln);
		$img->line(30,0,30,20,$ln);
		$img->line(35,0,35,20,$ln);
		$img->line(40,0,40,20,$ln);
		$img->line(45,0,45,20,$ln);
		$img->line(50,0,50,20,$ln);
		$img->line(55,0,55,20,$ln);
	}
	$img->string(gdLargeFont(),6,2,$captcha,$fg);
	binmode STDOUT;
	print $img->jpeg;
}

########### Shuffle Array ###############################################
sub randomize{
	my $array = shift;
	my ($i, $j);
	for ($i = @$array; --$i;)
	{
		$j = int (rand($i+1));
		next if ($i == $j);
		@$array[$i,$j] = @$array[$j,$i];
	}
}
########### Get Configs #################################################
sub configs{
	open(DH, "< $configs/general.fpg") or die "Cannot open $configs/general.fpg! $!";
	flock(DH, 2) if($flck);
	my @temp = <DH>;
	flock(DH, 8) if($flck);
	close(DH);
	my %configs;
	foreach(@temp){
		chomp;
		next if(($_ =~ /^\#/) or (!$_));
		my($key,$value) = split(/\<==\>/, $_) if($_);
		$configs{$key} = $value if(defined $key);
	}
	return %configs;
}
