1 # -*- coding: utf-8 -*-
2 from os
.path
import dirname
, join
3 from urllib
import urlopen
5 from werkzeug
import escape
, url_encode
9 from zine
.widgets
import Widget
10 from zine
.views
.admin
import flash
, render_admin_response
11 from zine
.models
import COMMENT_BLOCKED_SPAM
, Comment
12 from zine
.privileges
import BLOG_ADMIN
, require_privilege
13 from zine
.utils
.validators
import ValidationError
, check
14 from zine
.utils
.http
import redirect_to
15 from zine
.utils
import forms
17 # Check do we have the recaptcha library installed
20 from recaptcha
.client
import captcha
23 have_recaptcha
= False
25 USER_AGENT
= 'Zine /%s | reCatpcha/1.11' % zine
.__version
__
26 #AKISMET_URL_BASE = 'rest.akismet.com'
27 #AKISMET_VERSION = '1.1'
28 TEMPLATES
= join(dirname(__file__
), 'templates')
29 #BLOCKED_MSG = 'blocked by akismet'
32 #: because we need the information about verified keys quite often
33 #: we store verified keys here.
34 _verified_keys
= set()
37 class ConfigurationForm(forms
.Form
):
38 """The configuration form."""
39 pubkey
= forms
.TextField()
40 privkey
= forms
.TextField()
43 def do_spamcheck(req
, comment
):
44 """Do spamchecking for all new comments."""
45 # something blocked the comment already. no need to check for
50 resp
= captcha
.submit(
51 recaptcha_challenge_field
=req
.values
['recaptcha_challenge_field'],
52 recaptcha_response_field
=req
.values
['recaptcha_response_field'],
53 private_key
=req
.app
.cfg
['recaptcha_spam_filter/privkey'],
54 remoteip
=comment
.submitter_ip
61 comment
.status
= COMMENT_BLOCKED_SPAM
62 comment
.blocked_msg
= "Blocked for failing the captcha"
65 def add_recaptcha_link(req
, navigation_bar
):
66 """Add a button for recaptcha to the comments page."""
67 if req
.user
.has_privilege(BLOG_ADMIN
):
68 for link_id
, url
, title
, children
in navigation_bar
:
69 if link_id
== 'options':
70 children
.insert(-3, ('recaptcha_spam_filter',
71 url_for('recaptcha_spam_filter/config'),
75 @require_privilege(BLOG_ADMIN
)
76 def show_recaptcha_config(req
):
77 """Show the recaptcha control panel."""
78 form
= ConfigurationForm(initial
=dict(
79 pubkey
=req
.app
.cfg
['recaptcha_spam_filter/pubkey'],
80 privkey
=req
.app
.cfg
['recaptcha_spam_filter/privkey']
83 if req
.method
== 'POST' and form
.validate(req
.form
):
85 req
.app
.cfg
.change_single('recaptcha_spam_filter/pubkey',
87 req
.app
.cfg
.change_single('recaptcha_spam_filter/privkey',
90 flash(_('reCAPTCHA has been successfully enabled.'), 'ok')
92 flash(_('reCAPTCHA disabled.'), 'ok')
93 return redirect_to('recaptcha_spam_filter/config')
94 return render_admin_response('admin/recaptcha_spam_filter.html',
95 'options.recaptcha_spam_filter',
96 form
=form
.as_widget())
99 def add_captcha(req
, post
):
100 return captcha
.displayhtml(req
.app
.cfg
['recaptcha_spam_filter/pubkey'])
102 def setup(app
, plugin
):
103 if not have_recaptcha
:
104 raise SetupError("The reCAPTCHA plugin requires the recaptcha python library")
105 app
.add_config_var('recaptcha_spam_filter/pubkey',
106 forms
.TextField(default
=u
''))
107 app
.add_config_var('recaptcha_spam_filter/privkey',
108 forms
.TextField(default
=u
''))
109 app
.add_url_rule('/options/recaptcha', prefix
='admin',
110 endpoint
='recaptcha_spam_filter/config',
111 view
=show_recaptcha_config
)
112 app
.connect_event('before-comment-editor-buttons-rendered', add_captcha
)
113 app
.connect_event('before-comment-saved', do_spamcheck
)
114 app
.connect_event('modify-admin-navigation-bar', add_recaptcha_link
)
115 app
.add_template_searchpath(TEMPLATES
)