1 # Copyright 2006-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: captcha.coretag,v 1.4 2007-03-30 23:55:57 pajamian Exp $
10 UserTag captcha Order function
11 UserTag captcha attrAlias func function
12 UserTag captcha addAttr
13 UserTag captcha Description Generate captcha codes for authentication check
14 UserTag captcha Version $Revision: 1.4 $
16 UserTag captcha Routine <<EOR
19 require Authen::Captcha;
24 my ($func, $opt) = @_;
29 ::logError("Use of captcha tag without Authen::Captcha, skipping");
34 $func =~ s/[^a-z]+//g;
37 $result = $Vend::Session->{captcha};
41 my $en = $opt->{error_name} || 'captcha';
43 my $subdir = $opt->{image_subdir}
44 || $::Variable->{CAPTCHA_IMAGE_SUBDIR}
46 my $tmpdir = "$Vend::Cfg->{ScratchDir}/$subdir";
48 mkdir($tmpdir) unless -d $tmpdir;
50 my $imgdir = $opt->{image_location} || $::Variable->{CAPTCHA_IMAGE_LOCATION};
53 if(! $Global::NoAbsolute and $::Variable->{DOCROOT}) {
54 $imgdir = "$::Variable->{DOCROOT}$::Variable->{IMAGE_DIR}/$subdir";
57 $imgdir = "images/$subdir";
61 my $imgpath = $opt->{image_path}
62 || $::Variable->{CAPTCHA_IMAGE_PATH}
63 || "$::Variable->{IMAGE_DIR}/$subdir";
66 my $captcha = Authen::Captcha->new(
67 data_folder => $tmpdir,
68 output_folder => $imgdir,
71 my $guess = $opt->{guess} || $CGI::values{mv_captcha_guess};
72 my $code = $opt->{source};
74 if($func eq 'check') {
76 my $check_against = $code || $Vend::Session->{captcha};
77 my $status = $captcha->check_code($guess, $check_against);
82 $Tag->error( { name => $en, set => "Code not checked: error" });
85 elsif($status == -1) {
86 $Tag->error( { name => $en, set => "Code expired" });
89 elsif($status == -2) {
90 $Tag->error( { name => $en, set => "Code never generated" });
93 elsif($status == -3) {
94 $Tag->error( { name => $en, set => "Code doesn't match" });
99 my $save_u = umask($::Variable->{CAPTCHA_UMASK} || 2);
102 undef $Vend::Captcha;
103 delete $Vend::Session->{captcha};
107 $code ||= $Vend::Session->{captcha};
110 if($func eq 'code' and $code) {
116 unless( Vend::File::allowed_file($imgdir, 1) ) {
117 my $msg = errmsg("No permission to write directory '%s'", $imgdir);
118 $Tag->error( { name => $en, set => $msg });
122 mkdir($imgdir) unless -d $imgdir;
125 $code = $Vend::Session->{captcha} = $captcha->generate_code($opt->{length});
126 $Vend::Captcha = $code;
132 $Tag->error( { name => $en, set => "Error: $@" });
136 if($func eq 'code') {
140 # Now probably an image function.
142 unless ($func =~ /ima?ge?/) {
145 set => errmsg("Unknown function %s", $func),
150 my $path = $opt->{relative} ? "$subdir/$code.png" : "$imgpath/$code.png";
152 if(! $opt->{name_only}) {
153 return $Tag->image($path);
163 UserTag captcha Documentation <<EOD
166 Interchange [captcha] tag
170 [captcha function="check|code|image|relative_image|image_tag"
172 image-subdir="captcha"
173 image-location="images/captcha"
174 image-path="/standard/images/captcha"
175 source="[cgi mv_captcha_source]"
177 guess="[cgi mv_captcha_guess]"
182 This tag generates and/or checks "captcha" images to authenticate user input.
183 If called for the first time in a page, it generates a code/image pair and
184 sets the code in the session (at $Vend::Session->{captcha}).
186 There are several functions.
192 Checks the captcha source code (presumably from the previous page) against
193 the guess. If it matches, returns 1. If not, returns 0 and puts error
198 Returns the generated code. Generates one if not done previously in session.
202 Returns an IMG tag as generated by Interchange's [image] tag. If the
203 name-only=1 option is passed, no surrounding IMG tag will be generated,
204 only the image name. If the C<relative=1> option is passed, that name
205 will not be prefaced with the ImageDir.
209 The additional options are:
215 The input from the user when the function is C<check>. Default is the
216 contents of [cgi mv_captcha_guess].
220 The image subdirectory (based in images directory) which will
225 The base path for URL generation. Default is the Interchange IMAGE_DIR
230 The directory where image files will be generated. Default is the
231 Interchange IMAGE_DIR variable based in the Interchange DOCROOT
232 variable, with the subdirectory above, i.e. C<[var DOCROOT][var IMAGE_DIR]/captcha>.
236 Length of the input for the captcha. Default is 4 characters.
240 When set, tells the image function to not generate an HTML IMG tag.
244 When set, tells the image function (when in name-only mode) to
245 return relative path.
249 Normally only one captcha code / image will be generated per page
250 transaction. If this is set, you can generate another one -- though
251 you would have to take care of saving the generated code yourself,
252 as $Session->{captcha} is overwritten.
256 The captcha base to guess against for the C<check> function. Default is the
257 contents of the last-generated captcha, or [cgi mv_captcha_source].
263 [if cgi mv_captcha_guess]
264 [tmp good][captcha check][/tmp]
274 [captcha function=image]
276 <form action="[process href="@@MV_PAGE@@"]">
277 <input type=text name=mv_captcha_guess size value="">
278 <input type=submit value="Guess">
289 Mike Heins, <mike AT THE DOMAIN perusion.com>.