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 # Used for [captcha-refresh] if requested
100 $::Instance->{last_captcha_build_opt} = { %$opt };
102 my $save_u = umask($::Variable->{CAPTCHA_UMASK} || 2);
105 undef $Vend::Captcha;
106 delete $Vend::Session->{captcha};
110 $code ||= $Vend::Session->{captcha};
113 if($func eq 'code' and $code) {
119 unless( Vend::File::allowed_file($imgdir, 1) ) {
120 my $msg = errmsg("No permission to write directory '%s'", $imgdir);
121 $Tag->error( { name => $en, set => $msg });
125 mkdir($imgdir) unless -d $imgdir;
128 $code = $Vend::Session->{captcha} = $captcha->generate_code($opt->{length});
129 $Vend::Captcha = $code;
135 $Tag->error( { name => $en, set => "Error: $@" });
139 if($func eq 'code') {
143 # Now probably an image function.
145 unless ($func =~ /ima?ge?/) {
148 set => errmsg("Unknown function %s", $func),
153 my $path = $opt->{relative} ? "$subdir/$code.png" : "$imgpath/$code.png";
155 if(! $opt->{name_only}) {
156 return $Tag->image($path);
166 UserTag captcha Documentation <<EOD
169 Interchange [captcha] tag
173 [captcha function="check|code|image|relative_image|image_tag"
175 image-subdir="captcha"
176 image-location="images/captcha"
177 image-path="/standard/images/captcha"
178 source="[cgi mv_captcha_source]"
180 guess="[cgi mv_captcha_guess]"
185 This tag generates and/or checks "captcha" images to authenticate user input.
186 If called for the first time in a page, it generates a code/image pair and
187 sets the code in the session (at $Vend::Session->{captcha}).
189 There are several functions.
195 Checks the captcha source code (presumably from the previous page) against
196 the guess. If it matches, returns 1. If not, returns 0 and puts error
201 Returns the generated code. Generates one if not done previously in session.
205 Returns an IMG tag as generated by Interchange's [image] tag. If the
206 name-only=1 option is passed, no surrounding IMG tag will be generated,
207 only the image name. If the C<relative=1> option is passed, that name
208 will not be prefaced with the ImageDir.
212 The additional options are:
218 The input from the user when the function is C<check>. Default is the
219 contents of [cgi mv_captcha_guess].
223 The image subdirectory (based in images directory) which will
228 The base path for URL generation. Default is the Interchange IMAGE_DIR
233 The directory where image files will be generated. Default is the
234 Interchange IMAGE_DIR variable based in the Interchange DOCROOT
235 variable, with the subdirectory above, i.e. C<[var DOCROOT][var IMAGE_DIR]/captcha>.
239 Length of the input for the captcha. Default is 4 characters.
243 When set, tells the image function to not generate an HTML IMG tag.
247 When set, tells the image function (when in name-only mode) to
248 return relative path.
252 Normally only one captcha code / image will be generated per page
253 transaction. If this is set, you can generate another one -- though
254 you would have to take care of saving the generated code yourself,
255 as $Session->{captcha} is overwritten.
259 The captcha base to guess against for the C<check> function. Default is the
260 contents of the last-generated captcha, or [cgi mv_captcha_source].
266 [if cgi mv_captcha_guess]
267 [tmp good][captcha check][/tmp]
277 [captcha function=image]
279 <form action="[process href="@@MV_PAGE@@"]">
280 <input type=text name=mv_captcha_guess size value="">
281 <input type=submit value="Guess">
292 Mike Heins, <mike AT THE DOMAIN perusion.com>.