Created
May 10, 2013 11:41
-
-
Save andrewgleave/5553895 to your computer and use it in GitHub Desktop.
Django template tag for MailHide. Based on http://iamtgc.com/recaptcha-mailhide-template-filter-for-django/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import binascii | |
from Crypto.Cipher import AES | |
from django.template import Library | |
from django.utils.safestring import mark_safe | |
from django.conf import settings | |
register = Library() | |
@register.simple_tag | |
def mailhide(email, **kwargs): | |
"""Tag to disguse an email address using the reCaptcha's Mailhide service""" | |
if not email or '@' not in email: | |
return u'' | |
numpad = 16 - (len(email) % 16) | |
padded_value = email + numpad * chr(numpad) | |
key = binascii.unhexlify(settings.MAILHIDE_PRIVATE_KEY) | |
obj = AES.new(key, AES.MODE_CBC, '\000' * 16) | |
encrypted_value = obj.encrypt(padded_value) | |
fmt_args = { | |
'public_key': settings.MAILHIDE_PUBLIC_KEY, | |
'encrypted_email': base64.urlsafe_b64encode(encrypted_value) | |
} | |
display_string = kwargs.pop('display_string', None) | |
if not display_string: | |
display_string = u'...@' % value[value.index('@') + 1:] | |
fmt_args['display_string'] = display_string | |
return mark_safe(u'''<a href="http://mailhide.recaptcha.net/d?k=%(public_key)s&c=%(encrypted_email)s" | |
onclick="window.open('http://mailhide.recaptcha.net/d?k=%(public_key)s&c=%(encrypted_email)s', '', | |
'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=280'); | |
return false;" title="Reveal this e-mail address">%(display_string)s</a>''' % fmt_args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment