Changeset 257
- Timestamp:
- 10/05/06 16:27:10 (2 years ago)
- Files:
-
- captcha/trunk/plugins/captcha/captcha.pl (modified) (2 diffs)
- captcha/trunk/plugins/captcha/captcha_js.cgi (modified) (3 diffs)
- captcha/trunk/plugins/captcha/captcha_js.fcgi (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
captcha/trunk/plugins/captcha/captcha.pl
r256 r257 14 14 use File::Spec; 15 15 use File::Basename; 16 use Data::Dumper; 16 17 use Authen::Captcha; 17 18 use base 'MT::Plugin'; 18 our $VERSION = '0.1 0';19 our $VERSION = '0.11'; 19 20 20 21 my $dirname = dirname(__FILE__); … … 90 91 my $plugin = shift; 91 92 92 my $res = ''; 93 my @blogs = MT::Blog->load; 94 for my $blog (@blogs) { 95 my $cfg = $plugin->get_config_hash('blog:' . $blog->id); 96 next unless $cfg->{captcha_enable}; 97 $res .= $blog->id . ',' . 98 $cfg->{captcha_ttl} . ',' . 99 $cfg->{captcha_secret} . ',' . 100 $cfg->{captcha_length} . ',' . 101 $cfg->{captcha_images_url} . ',' . 102 $cfg->{captcha_images_path} . "\n"; 93 my $config; 94 for my $blog (MT::Blog->load) { 95 $config->{$blog->id} = $plugin->get_config_hash('blog:' . $blog->id); 103 96 } 104 97 105 my $c fg_file = File::Spec->catfile(dirname(__FILE__), 'data', 'config.txt');98 my $config_file = File::Spec->catfile(dirname(__FILE__), 'data', 'config.txt'); 106 99 local(*FH); 107 open FH, ">$c fg_file" or die "Can't open File: $cfg_file\n";100 open FH, ">$config_file" or die "Can't open File: $config_file\n"; 108 101 flock FH, 2; 109 print FH $res;102 print FH Data::Dumper->Dump([$config], ['config']); 110 103 close(FH); 111 104 } captcha/trunk/plugins/captcha/captcha_js.cgi
r254 r257 12 12 data_folder => File::Spec->catdir($dirname, 'data') 13 13 ); 14 my $c fg_file = File::Spec->catfile($dirname, 'data', 'config.txt');14 my $config_file = File::Spec->catfile($dirname, 'data', 'config.txt'); 15 15 my $config; 16 16 17 17 my $q = CGI->new; 18 18 eval { 19 $config = read_config($c fg_file);20 gen _code($q);19 $config = read_config($config_file); 20 generate_code($q); 21 21 }; 22 22 if ($@) { … … 25 25 26 26 sub read_config { 27 my($cfg_file) = @_; 27 my($config_file) = @_; 28 29 local(*FH); 30 open FH, $config_file or die "Can't open File: $config_file."; 31 flock FH, 1; # read lock 32 my @lines = <FH>; 33 close(FH); 34 28 35 my $config; 29 30 local(*FH, $_, $/); 31 $/ = "\n"; 32 open FH, $cfg_file or die "Can't open File: $cfg_file\n"; 33 flock FH, 1; 34 while (<FH>) { 35 chomp; 36 my @c = split(',', $_); 37 next unless scalar @c == 6; 38 $config->{$c[0]} = { 39 captcha_ttl => $c[1], 40 captcha_secret => $c[2], 41 captcha_length => $c[3], 42 captcha_images_url => $c[4], 43 captcha_images_path => $c[5], 44 }; 45 } 46 close(FH); 47 $config; 36 eval join('', @lines); 48 37 } 49 38 50 sub gen _code {39 sub generate_code { 51 40 my $q = shift; 52 41 my $blog_id = $q->param('blog_id') || 1; 53 my $c onf= $config->{$blog_id} or die "blog_id is not properly given.";42 my $cfg = $config->{$blog_id} or die "blog_id is not properly given."; 54 43 55 $captcha->expire($conf->{captcha_ttl} || 3600); 56 $captcha->secret($conf->{captcha_secret} || ''); 57 $captcha->output_folder($conf->{captcha_images_path}); 44 die "CAPTCHA test is disabled for this blog (BLOG_ID:$blog_id)." 45 unless $cfg->{captcha_enable}; 58 46 59 my $captcha_images_url = $conf->{captcha_images_url}; 60 $captcha_images_url .= '/' if $captcha_images_url !~ m!/$!; 61 62 my $captcha_length = $conf->{captcha_length} || 5; 47 $captcha->expire($cfg->{captcha_ttl} || 3600); 48 $captcha->secret($cfg->{captcha_secret} || ''); 49 $captcha->output_folder($cfg->{captcha_images_path}); 50 my $captcha_length = $cfg->{captcha_length} || 5; 51 my $captcha_md5 = $captcha->generate_code($captcha_length); 52 my $captcha_img = $cfg->{captcha_images_url}; 53 $captcha_img .= '/' if $captcha_img !~ m!/$!; 54 $captcha_img .= $captcha_md5 . '.png'; 63 55 my $captcha_img_width = 25 * $captcha_length; 64 56 my $captcha_img_height = 35; 65 66 my $captcha_md5 = $captcha->generate_code($captcha_length);67 57 68 58 print $q->header('text/javascript'); … … 72 62 document.writeln('<input type="hidden" name="captcha_md5" value="$captcha_md5" />'); 73 63 document.writeln('<label for="comment-captcha">CAPTCHA™ Code:</label>'); 74 document.writeln('<img src="$captcha_im ages_url$captcha_md5.png" width="$captcha_img_width" height="$captcha_img_height" alt="CAPTCHA Image" />');64 document.writeln('<img src="$captcha_img" width="$captcha_img_width" height="$captcha_img_height" alt="CAPTCHA Image" />'); 75 65 document.writeln('<input type="text" id="comment-captcha" name="captcha_code" value="" length="$captcha_length" maxlength="$captcha_length" />'); 76 66 document.writeln('</div>'); captcha/trunk/plugins/captcha/captcha_js.fcgi
r254 r257 12 12 data_folder => File::Spec->catdir($dirname, 'data') 13 13 ); 14 my $c fg_file = File::Spec->catfile($dirname, 'data', 'config.txt');14 my $config_file = File::Spec->catfile($dirname, 'data', 'config.txt'); 15 15 my $config; 16 16 … … 18 18 while (my $q = CGI::Fast->new) { 19 19 eval { 20 my $ctime_current = (stat($c fg_file))[9];20 my $ctime_current = (stat($config_file))[9]; 21 21 if ($ctime_current > $ctime) { 22 $config = read_config($c fg_file);22 $config = read_config($config_file); 23 23 $ctime = $ctime_current; 24 24 } 25 gen _code($q);25 generate_code($q); 26 26 }; 27 27 print $q->header("text/plain"), $@ if $@; … … 29 29 30 30 sub read_config { 31 my($cfg_file) = @_; 31 my($config_file) = @_; 32 33 local(*FH); 34 open FH, $config_file or die "Can't open File: $config_file."; 35 flock FH, 1; # read lock 36 my @lines = <FH>; 37 close(FH); 38 32 39 my $config; 33 34 local(*FH, $_, $/); 35 $/ = "\n"; 36 open FH, $cfg_file or die "Can't open File: $cfg_file\n"; 37 flock FH, 1; 38 while (<FH>) { 39 chomp; 40 my @c = split(',', $_); 41 next unless scalar @c == 6; 42 $config->{$c[0]} = { 43 captcha_ttl => $c[1], 44 captcha_secret => $c[2], 45 captcha_length => $c[3], 46 captcha_images_url => $c[4], 47 captcha_images_path => $c[5], 48 }; 49 } 50 close(FH); 51 $config; 40 eval join('', @lines); 52 41 } 53 42 54 sub gen _code {43 sub generate_code { 55 44 my $q = shift; 56 45 my $blog_id = $q->param('blog_id') || 1; 57 my $c onf= $config->{$blog_id} or die "blog_id is not properly given.";46 my $cfg = $config->{$blog_id} or die "blog_id is not properly given."; 58 47 59 $captcha->expire($conf->{captcha_ttl} || 3600); 60 $captcha->secret($conf->{captcha_secret} || ''); 61 $captcha->output_folder($conf->{captcha_images_path}); 48 die "CAPTCHA test is disabled for this blog (BLOG_ID:$blog_id)." 49 unless $cfg->{captcha_enable}; 62 50 63 my $captcha_images_url = $conf->{captcha_images_url}; 64 $captcha_images_url .= '/' if $captcha_images_url !~ m!/$!; 65 66 my $captcha_length = $conf->{captcha_length} || 5; 51 $captcha->expire($cfg->{captcha_ttl} || 3600); 52 $captcha->secret($cfg->{captcha_secret} || ''); 53 $captcha->output_folder($cfg->{captcha_images_path}); 54 my $captcha_length = $cfg->{captcha_length} || 5; 55 my $captcha_md5 = $captcha->generate_code($captcha_length); 56 my $captcha_img = $cfg->{captcha_images_url}; 57 $captcha_img .= '/' if $captcha_img !~ m!/$!; 58 $captcha_img .= $captcha_md5 . '.png'; 67 59 my $captcha_img_width = 25 * $captcha_length; 68 60 my $captcha_img_height = 35; 69 70 my $captcha_md5 = $captcha->generate_code($captcha_length);71 61 72 62 print $q->header('text/javascript'); … … 76 66 document.writeln('<input type="hidden" name="captcha_md5" value="$captcha_md5" />'); 77 67 document.writeln('<label for="comment-captcha">CAPTCHA™ Code:</label>'); 78 document.writeln('<img src="$captcha_im ages_url$captcha_md5.png" width="$captcha_img_width" height="$captcha_img_height" alt="CAPTCHA Image" />');68 document.writeln('<img src="$captcha_img" width="$captcha_img_width" height="$captcha_img_height" alt="CAPTCHA Image" />'); 79 69 document.writeln('<input type="text" id="comment-captcha" name="captcha_code" value="" length="$captcha_length" maxlength="$captcha_length" />'); 80 70 document.writeln('</div>');
