Login

Template Tag to protect the E-mail address

Author:
nitinhayaran
Posted:
February 2, 2010
Language:
Python
Version:
1.1
Tags:
template-tag email django template bots spider obfuscate javascript
Score:
3 (after 3 ratings)

This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load.

It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link.

Usage

{% encrypt_email user.email %}

More Info Template Tag to protect the E-mail address

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re
from django import template

register = template.Library()

class EncryptEmail(template.Node):
    def __init__(self, context_var):
        self.context_var = template.Variable(context_var)# context_var
    def render(self, context):
        import random
        email_address = self.context_var.resolve(context)
        character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
        char_list = list(character_set)
        random.shuffle(char_list)
        
        key = ''.join(char_list)
        
        cipher_text = ''
        id = 'e' + str(random.randrange(1,999999999))

        for a in email_address:
            cipher_text += key[ character_set.find(a) ]
            
        script = 'var a="'+key+'";var b=a.split("").sort().join("");var c="'+cipher_text+'";var d="";'
        script += 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));'
        script += 'document.getElementById("'+id+'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"'
        
        
        script = "eval(\""+ script.replace("\\","\\\\").replace('"','\\"') + "\")"
        script = '<script type="text/javascript">/*<![CDATA[*/'+script+'/*]]>*/</script>'
        
        return '<span id="'+ id + '">[javascript protected email address]</span>'+ script
        

def  encrypt_email(parser, token):
    """
        {% encrypt_email user.email %}
    """

    tokens = token.contents.split()
    if len(tokens)!=2:
        raise template.TemplateSyntaxError("%r tag accept two argument" % tokens[0])
    return EncryptEmail(tokens[1])

register.tag('encrypt_email', encrypt_email)

More like this

  1. Template Tag to protect the E-mail address by end0 3 weeks, 3 days ago
  2. Simple e-mail template tag by dchandek 7 years, 1 month ago
  3. email_links by sansmojo 7 years, 12 months ago
  4. Validation for full e-mails (e.g. "Joe Hacker <joe@hacker.com>") by akaihola 3 years, 5 months ago
  5. Protect anti robots template tag by marinho 7 years ago

Comments

end0 (on May 4, 2015):

https://djangosnippets.org/snippets/10482/

#

Please login first before commenting.