« QBit/WebInterface/Controller/Template/Provider.pm
↑ Summary
QBit/WebInterface/Controller/Form/Field/hidden.pm »
| 1 | package QBit::WebInterface::Controller::Form::Field::reCAPTCHA; | ||
| 2 | $QBit::WebInterface::Controller::Form::Field::reCAPTCHA::VERSION = '0.033'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::WebInterface::Controller::Form::Field); | ||
| 6 | |||
| 7 | use LWP::UserAgent; | ||
| 8 | |||
| 9 | use Exception::Form; | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | my ($self) = @_; | ||
| 13 | |||
| 14 | $self->SUPER::init(); | ||
| 15 | |||
| 16 | $self->{'name'} = 'captcha' . $self; | ||
| 17 | $self->{'theme'} = 'clean' unless defined($self->{'theme'}); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub check { | ||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | $self->{'__UA__'} ||= LWP::UserAgent->new(); | ||
| 24 | |||
| 25 | my $response = $self->{'__UA__'}->post( | ||
| 26 | 'http://www.google.com/recaptcha/api/verify', | ||
| 27 | { | ||
| 28 | privatekey => ( | ||
| 29 | $self->form->controller->{'app'}->get_option('reCAPTCHA_privkey') | ||
| 30 | || throw gettext('Missed required option "%s"', 'reCAPTCHA_privkey') | ||
| 31 | ), | ||
| 32 | remoteip => $self->form->controller->request->remote_addr(), | ||
| 33 | challenge => $self->form->controller->request->param('recaptcha_challenge_field'), | ||
| 34 | response => $self->form->controller->request->param('recaptcha_response_field'), | ||
| 35 | }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | throw Exception::Form gettext('HTTP error: %s', $response->status_line()) | ||
| 39 | unless $response->is_success(); | ||
| 40 | |||
| 41 | my ($result, $error) = split(/[\r\n]+/, $response->decoded_content()); | ||
| 42 | |||
| 43 | throw Exception::Form $error eq 'incorrect-captcha-sol' | ||
| 44 | ? gettext('Incorred CAPTCHA') | ||
| 45 | : gettext('Cannot validate CAPTCHA: %s', $error) | ||
| 46 | if $result ne 'true'; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub control_html { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | my $key = $self->form->controller->{'app'}->get_option('reCAPTCHA_pubkey') | ||
| 53 | || throw gettext('Missed required option "%s"', 'reCAPTCHA_pubkey'); | ||
| 54 | |||
| 55 | my $text_values = join(",", map {"$_:'$self->{$_}'"} grep {defined($self->{$_})} qw(lang theme)); | ||
| 56 | |||
| 57 | my $html = qq{<script type="text/javascript">var RecaptchaOptions = {$text_values}</script>} | ||
| 58 | . qq{<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=$key"></script>}; | ||
| 59 | |||
| 60 | $html .= $self->_html_error(); | ||
| 61 | |||
| 62 | return $html; | ||
| 63 | } | ||
| 64 | |||
| 65 | TRUE; |