| 1 | package Webqq::Client::Plugin::PostImgVerifycode; | ||
| 2 | use IO::Socket::INET; | ||
| 3 | use Webqq::Client::Util qw(console); | ||
| 4 | use HTTP::Date; | ||
| 5 | use IO::Handle; | ||
| 6 | use Sys::HostIP qw(ips); | ||
| 7 | use Mail::SendEasy; | ||
| 8 | use File::Basename; | ||
| 9 | sub call{ | ||
| 10 | my $client = shift; | ||
| 11 | my($img_verifycode_file,$smtp) =@_; | ||
| 12 | unless(ref $smtp eq 'HASH'){ | ||
| 13 | console "PostImgVerifycode需要正确的smtp信息\n"; | ||
| 14 | exit; | ||
| 15 | } | ||
| 16 | my $img_path = basename($img_verifycode_file); | ||
| 17 | my ($internet_host) = grep { | ||
| 18 | $_ ne '127.0.0.1' | ||
| 19 | and $_ ne '::1' | ||
| 20 | and $_ !~/^(10\.|172\.16\.|192\.168\.)/ | ||
| 21 | } @{ ips() }; | ||
| 22 | my $status = Mail::SendEasy::send( | ||
| 23 | smtp =>$smtp->{smtp}, | ||
| 24 | user =>$smtp->{user}, | ||
| 25 | pass =>$smtp->{pass}, | ||
| 26 | from =>$smtp->{from}, | ||
| 27 | from_title => $smtp->{from_title}, | ||
| 28 | subject =>$smtp->{subject}, | ||
| 29 | to =>$smtp->{to}, | ||
| 30 | anex => $img_verifycode_file, | ||
| 31 | msg =>"主人,登录需要验证码,请点击以下链接输入验证码: http://$internet_host:1987/post_img_code", | ||
| 32 | ); | ||
| 33 | my $s = IO::Socket::INET->new( | ||
| 34 | LocalPort => 1987, | ||
| 35 | Proto => 'tcp' , | ||
| 36 | Reuse => 1, | ||
| 37 | Blocking => 1, | ||
| 38 | Listen => SOMAXCONN, | ||
| 39 | ) or die $!; | ||
| 40 | my $html= <<"HTML"; | ||
| 41 | <html> | ||
| 42 | <head> | ||
| 43 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
| 44 | </head> | ||
| 45 | <body> | ||
| 46 | <form action="http://$internet_host:1987/img_code" method="get"> | ||
| 47 | <div><img src="http://$internet_host:1987/$img_path"></img></div> | ||
| 48 | <nobr>验证码:</nobr> | ||
| 49 | <input type="text" maxlength="4" size="4" name="code"></input> | ||
| 50 | <input type="submit"></input> | ||
| 51 | </form> | ||
| 52 | </body> | ||
| 53 | </html> | ||
| 54 | HTML | ||
| 55 | |||
| 56 | while(my $c = $s->accept()){ | ||
| 57 | my $uri; | ||
| 58 | $c->autoflush(1); | ||
| 59 | while(<$c>){ | ||
| 60 | last if /^\s+$/; | ||
| 61 | (undef,$uri,undef)= split /\s+/,$_ if $_ =~/^GET/; | ||
| 62 | } | ||
| 63 | if($uri eq "/$img_path"){ | ||
| 64 | my $data; | ||
| 65 | open my $img,$img_verifycode_file or die $!; | ||
| 66 | while((read $img,my $buf,4096)!=0){ | ||
| 67 | $data .= $buf; | ||
| 68 | } | ||
| 69 | close $img; | ||
| 70 | my $len = length($data); | ||
| 71 | print $c | ||
| 72 | "HTTP/1.1 200 OK\r\n" . | ||
| 73 | "Date: " . time2str() . "\r\n" . | ||
| 74 | "Content-Type: image/jpeg\r\n" . | ||
| 75 | "Content-Length: $len\r\n" . | ||
| 76 | "\r\n" . | ||
| 77 | $data; | ||
| 78 | } | ||
| 79 | elsif($uri eq '/post_img_code'){ | ||
| 80 | my $len = length($html); | ||
| 81 | print $c | ||
| 82 | "HTTP/1.1 200 OK\r\n" . | ||
| 83 | "Date: " . time2str() . "\r\n" . | ||
| 84 | "Content-Type: text/html;charset=utf-8\r\n" . | ||
| 85 | "Content-Length: $len\r\n" ; | ||
| 86 | print $c "\r\n"; | ||
| 87 | print $c $html; | ||
| 88 | } | ||
| 89 | elsif($uri =~ /\/img_code\?code=(.{4})/){ | ||
| 90 | my $code = $1; | ||
| 91 | my $data = "验证码已提交" ; | ||
| 92 | my $len = length($data); | ||
| 93 | print $c | ||
| 94 | "HTTP/1.1 200 OK\r\n" . | ||
| 95 | "Date: " . time2str() . "\r\n" . | ||
| 96 | "Content-Type: text/html;charset=utf-8\r\n" . | ||
| 97 | "Content-Length: $len\r\n" . | ||
| 98 | "\r\n" . | ||
| 99 | $data; | ||
| 100 | return $code if defined $code and length($code)==4; | ||
| 101 | } | ||
| 102 | else{ | ||
| 103 | print $c | ||
| 104 | "HTTP/1.1 404 Not Found\r\n" . | ||
| 105 | "Date: " . time2str() . "\r\n" . | ||
| 106 | "Content-Length: 0\r\n"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |