| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Kwiki::Scode; | 
| 2 | 1 |  |  | 1 |  | 1391 | use Kwiki::Plugin -Base; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | use mixin 'Kwiki::Installer'; | 
| 4 |  |  |  |  |  |  | use GD; | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | const class_id => 'scode'; | 
| 8 |  |  |  |  |  |  | const class_title => 'Scode prevents wiki spam'; | 
| 9 |  |  |  |  |  |  | const cgi_class => 'Kwiki::Scode::CGI'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | field 'captcha_code'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $tmpdir = "/tmp/"; | 
| 14 |  |  |  |  |  |  | my $scode_length = 6; | 
| 15 |  |  |  |  |  |  | my $scode_maxtmp = 50; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub register { | 
| 18 |  |  |  |  |  |  | my $reg = shift; | 
| 19 |  |  |  |  |  |  | $reg->add(action => 'captcha'); | 
| 20 |  |  |  |  |  |  | $reg->add(hook => 'edit:edit', post => 'generate_scode'); | 
| 21 |  |  |  |  |  |  | $reg->add(hook => 'edit:save', pre => 'check_scode'); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub generate_scode { | 
| 25 |  |  |  |  |  |  | my $hook = pop; | 
| 26 |  |  |  |  |  |  | my $scode = $self->hub->load_class('scode'); | 
| 27 |  |  |  |  |  |  | srand int (time/10)+$$; | 
| 28 |  |  |  |  |  |  | my $code = int rand($scode->scode_tmp()); | 
| 29 |  |  |  |  |  |  | $code++; | 
| 30 |  |  |  |  |  |  | $scode->scode_create($code); | 
| 31 |  |  |  |  |  |  | $scode->captcha_code($code); | 
| 32 |  |  |  |  |  |  | my ($html) = $hook->returned; | 
| 33 |  |  |  |  |  |  | my $img_html = $scode->template_process('scode_image.html', captcha_code => $code); | 
| 34 |  |  |  |  |  |  | $html =~ s{ | 
| 35 |  |  |  |  |  |  | return $html; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub check_scode { | 
| 39 |  |  |  |  |  |  | my $hook = pop; | 
| 40 |  |  |  |  |  |  | my $scode = $self->hub->load_class('scode'); | 
| 41 |  |  |  |  |  |  | unless($scode->scode_granted) { | 
| 42 |  |  |  |  |  |  | $hook->code(undef); | 
| 43 |  |  |  |  |  |  | return $self->redirect($self->redirect($self->pages->current->uri)); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub scode_granted { | 
| 48 |  |  |  |  |  |  | my $answer = $self->scode_get($self->cgi->code); | 
| 49 |  |  |  |  |  |  | $answer eq $self->cgi->captcha; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub captcha { | 
| 53 |  |  |  |  |  |  | my $code = $self->cgi->code; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # Calculate code | 
| 56 |  |  |  |  |  |  | my $scode = $self->scode_get($code); | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | # lets define the image | 
| 59 |  |  |  |  |  |  | my $im_length = ($self->scode_len()+1)*10; | 
| 60 |  |  |  |  |  |  | my $im = new GD::Image($im_length,25); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # define the color we going to use | 
| 63 |  |  |  |  |  |  | my $c_background = $im->colorAllocate(224,224,224); | 
| 64 |  |  |  |  |  |  | my $c_border = $im->colorAllocate(0,0,0); | 
| 65 |  |  |  |  |  |  | my $c_line = $im->colorAllocate(192,192,192); | 
| 66 |  |  |  |  |  |  | my $c_code = $im->colorAllocate(128,128,128); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # Fill in the background | 
| 69 |  |  |  |  |  |  | $im->fill(50,50,$c_background); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Draw the borders lines | 
| 72 |  |  |  |  |  |  | for (my $i=0;$i<$im_length;$i+=5) { | 
| 73 |  |  |  |  |  |  | $im->line($i,0,$i,24,$c_line); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | for (my $i=0;$i<25;$i+=5) { | 
| 77 |  |  |  |  |  |  | $im->line(0,$i,$im_length-1,$i,$c_line); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | $im->rectangle(0,0,$im_length-1,24,$c_border); | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | # Write the code | 
| 83 |  |  |  |  |  |  | $im->string(gdGiantFont,8,5,$scode,$c_code); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | binmode STDOUT; | 
| 86 |  |  |  |  |  |  | for(qw(png jpeg gif)) { | 
| 87 |  |  |  |  |  |  | my $img = $im->$_; | 
| 88 |  |  |  |  |  |  | if($img) { | 
| 89 |  |  |  |  |  |  | $self->hub->headers->content_type("image/$_"); | 
| 90 |  |  |  |  |  |  | $self->hub->headers->print; | 
| 91 |  |  |  |  |  |  | print $img; | 
| 92 |  |  |  |  |  |  | last; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | return; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | ## Following code comes from MT::Scode plugin ########## | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub scode_len { | 
| 102 |  |  |  |  |  |  | return $scode_length; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub scode_tmp { | 
| 106 |  |  |  |  |  |  | return $scode_maxtmp; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub scode_generate { | 
| 110 |  |  |  |  |  |  | return int rand( (10**($scode_length)) - (10**($scode_length-1)) ) + | 
| 111 |  |  |  |  |  |  | 10**($scode_length-1); | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub scode_create { | 
| 115 |  |  |  |  |  |  | my $code = shift; | 
| 116 |  |  |  |  |  |  | $tmpdir = $self->plugin_directory . '/'; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | return if (-e $tmpdir.$code); | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | if ($code>0 && $code<=$scode_maxtmp) { | 
| 121 |  |  |  |  |  |  | my $scode = scode_generate(); | 
| 122 |  |  |  |  |  |  | open(OUTFILE,">${tmpdir}${code}"); | 
| 123 |  |  |  |  |  |  | print OUTFILE $scode; | 
| 124 |  |  |  |  |  |  | close(OUTFILE); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub scode_delete { | 
| 129 |  |  |  |  |  |  | my $code = shift; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | if ($code>0 && $code<=$scode_maxtmp) { | 
| 132 |  |  |  |  |  |  | unlink $tmpdir.$code; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub scode_get { | 
| 137 |  |  |  |  |  |  | my $code = shift; | 
| 138 |  |  |  |  |  |  | $tmpdir = $self->plugin_directory . '/'; | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | srand time; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | # Random number back...if have not initialized | 
| 143 |  |  |  |  |  |  | if ($code<=0 || $code>$scode_maxtmp || !-e $tmpdir.$code ) { | 
| 144 |  |  |  |  |  |  | return $self->scode_generate(); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | open(INFILE, $tmpdir.$code); | 
| 148 |  |  |  |  |  |  | my $scode = ; | 
| 149 |  |  |  |  |  |  | close(INFILE); | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | $scode =~ s/\D//g; | 
| 152 |  |  |  |  |  |  | return $scode; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | package Kwiki::Scode::CGI; | 
| 156 |  |  |  |  |  |  | use base 'Kwiki::CGI'; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | cgi 'code'; | 
| 159 |  |  |  |  |  |  | cgi 'captcha'; | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | package Kwiki::Scode; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | __DATA__ |