~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to extras/examples/plugins/reCaptcha/lib/reCaptcha.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2008-06-13 23:28:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080613232840-ya4jfxv1jgl45a3d
Tags: 4.2~rc2-1
* New upstream release candidate
* Update Standards-Version (no changes)
* Ensure that schema upgrade message is always seen

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2
 
# This program is distributed under the terms of the
3
 
# GNU General Public License, version 2.
4
 
#
5
 
# $Id: reCaptcha.pm 1174 2008-01-08 21:02:50Z bchoate $
6
 
 
7
 
# reCaptcha plugin for Movable Type
8
 
# Author: Six Apart (http://www.sixapart.com)
9
 
# Released under the Artistic and GPLv2 License
10
 
 
11
 
package reCaptcha;
12
 
 
13
 
use strict;
14
 
use warnings;
15
 
use base qw(MT::ErrorHandler);
16
 
 
17
 
sub form_fields {
18
 
    my $self = shift;
19
 
    my ($blog_id) = @_;
20
 
        
21
 
        my $plugin = MT::Plugin::reCaptcha->instance;
22
 
    my $config = $plugin->get_config_hash("blog:$blog_id");
23
 
    my $publickey = $config->{recaptcha_publickey};
24
 
    my $privatekey = $config->{recaptcha_privatekey};
25
 
        return q() unless $publickey && $privatekey;
26
 
 
27
 
    return <<FORM_FIELD;
28
 
<div id="recaptcha_script" style="display:none">
29
 
<script type="text/javascript"
30
 
   src="http://api.recaptcha.net/challenge?k=$publickey">
31
 
</script>
32
 
 
33
 
<noscript>
34
 
   <iframe src="http://api.recaptcha.net/noscript?k=$publickey"
35
 
       height="300" width="500" frameborder="0"></iframe><br>
36
 
   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
37
 
   </textarea>
38
 
   <input type="hidden" name="recaptcha_response_field" 
39
 
       value="manual_challenge">
40
 
</noscript>
41
 
</div>
42
 
<script type="text/javascript">
43
 
var div = document.getElementById("recaptcha_script");
44
 
if (commenter_name) {
45
 
    div.style.display = "none";
46
 
} else {
47
 
    div.style.display = "block";
48
 
}
49
 
</script>
50
 
FORM_FIELD
51
 
}
52
 
 
53
 
sub validate_captcha {
54
 
    my $self = shift;
55
 
    my ($app) = @_;
56
 
    my $entry_id = $app->param('entry_id')
57
 
      or return 0;
58
 
 
59
 
    my $entry = $app->model('entry')->load($entry_id)
60
 
      or return 0;
61
 
 
62
 
    my $blog_id = $entry->blog_id;
63
 
 
64
 
    my $config = MT::Plugin::reCaptcha->instance->get_config_hash("blog:$blog_id");
65
 
    my $privatekey = $config->{recaptcha_privatekey};
66
 
 
67
 
    my $challenge = $app->param('recaptcha_challenge_field');
68
 
    my $response = $app->param('recaptcha_response_field');
69
 
    my $ua = $app->new_ua({ timeout => 15, max_size => undef });
70
 
    return 0 unless $ua;
71
 
 
72
 
        require HTTP::Request;
73
 
    my $req = HTTP::Request->new(POST => 'http://api-verify.recaptcha.net/verify');
74
 
    $req->content_type("application/x-www-form-urlencoded");
75
 
        require MT::Util;
76
 
        my $content = 'privatekey=' . MT::Util::encode_url($privatekey);
77
 
        $content .= '&remoteip=' . MT::Util::encode_url($app->remote_ip);
78
 
        $content .= '&challenge=' . MT::Util::encode_url($challenge);
79
 
        $content .= '&response=' . MT::Util::encode_url($response);
80
 
    $req->content($content);
81
 
    my $res = $ua->request($req);
82
 
    my $c = $res->content;
83
 
    if (substr($res->code, 0, 1) eq '2') {
84
 
                return 1 if $c =~ /^true\n/;
85
 
        }
86
 
        0;
87
 
}
88
 
    
89
 
sub generate_captcha {
90
 
    #This won't be called since there is no link which requests to "generate_captcha" mode.
91
 
    my $self = shift;
92
 
    1;
93
 
}
94
 
 
95
 
1;