Email and phone templatetag obfuscator

  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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
import math
import random

from django import template
from django.conf import settings

register = template.Library()

class ObfuscateNode(template.Node):
    def __init__(self, email, display=None, title=None):
        self.email = email
        self.display = display
        self.title = title

    def render(self, context):
        if isinstance(self.email, template.FilterExpression):
            self.email = self.email.resolve(context).lower()
        if isinstance(self.title, template.FilterExpression):
            self.title = self.title.resolve(context)
        if isinstance(self.display, template.FilterExpression):
            self.display = self.display.resolve(context)
        coded = ""
        unmixedkey = u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@"
        inprogresskey = unmixedkey
        mixedkey = ""
        unshuffled = len(unmixedkey)
        for i in range(len(unmixedkey)):
            pos = int(math.ceil(random.random() * (unshuffled - 1)))
            nextchar = inprogresskey[pos]
            mixedkey += nextchar
            before = inprogresskey[0:pos]
            after = inprogresskey[pos + 1:]
            inprogresskey = before + after
            unshuffled -= 1
        cipher = mixedkey
        shift = len(self.email)
        for j in range(len(self.email)):
            if cipher.find(self.email[j]) == -1 :
                chr = self.email[j]
                coded += self.email[j]
            else :
                chr = (cipher.find(self.email[j]) + shift) % len(cipher)
                coded += cipher[chr]
        
        txt =   "<script type=\"text/javascript\">\n\
                 <!-- \n\
                   var coded = '" + coded + "';\n\
                   var key = '" + cipher + "';\n\
                   var shift = coded.length;\n\
                   var link = []; var ltr;\n\
                   for (var i=0; i<coded.length; i++) {\n\
                     if (key.indexOf(coded.charAt(i))==-1) {\n\
                       ltr = coded.charAt(i);\n\
                       link.push(ltr);\n\
                     }\n\
                     else {     \n\
                       ltr = (key.indexOf(coded.charAt(i)) - shift + key.length) % key.length;\n\
                       link.push( key.charAt(ltr) );\n\
                     }\n\
                   }\n"
        if not self.title == None :
            txt += "document.write('<a href=\"mailto:' + link.join('') + '\" title=\"" + self.title + "\">"
            if not self.display == None:
                txt += self.display
            else :
                txt += "' + link.join('') + '"
            txt += "</a>');"
        else :
            txt += "document.write(link.join(''));"
        txt += "\n//-->\n</script>"
        return txt
        
def do_obfuscate(parser, token):
    """
    Email link and general text obfuscator
    If link.title is provided an HTML link is created

        {% obfuscate link.email link.title %}

    """
    args = token.split_contents()
    tag = args[0]
    
    if len(args) not in (2, 3, 4):
        raise template.TemplateSyntaxError("Invalid syntax. Expected \
            '{%% %s 'text' %%} or '\
            '{%% %s 'email' 'title' %%} or '\
            '{%% %s 'email' 'title' 'innertext' %%}'" % (tag, tag, tag))

    if (args[1][0] == args[1][-1] and args[1][0] in ('"', "'")):
        email = args[1][1:-1]
    else:
        email = parser.compile_filter(args[1])
    title, display = None, None
    if len(args) >= 3:
        if (args[2][0] == args[2][-1] and args[2][0] in ('"', "'")):
            title = args[2][1:-1]
        else:
            title = parser.compile_filter(args[2])
    if len(args) >= 4:
        if (args[3][0] == args[3][-1] and args[3][0] in ('"', "'")):
            display = args[3][1:-1]
        else:
            display = parser.compile_filter(args[3])
    return ObfuscateNode(email, display=display, title=title)

register.tag('obfuscate', do_obfuscate)

More like this

  1. Email-obfuscator Template Tag by gronimo 1 year, 11 months ago
  2. Email obfuscation filter using ROT13 by worksology 4 years, 12 months ago
  3. email rendered via javascript (trick spam crawlers) by rizumu 4 years, 8 months ago
  4. Template Tag to protect the E-mail address by nitinhayaran 4 years, 2 months ago
  5. Email Munger by cootetom 5 years, 3 months ago

Comments

skarphace (on August 17, 2011):

If you want this to work in template iteration, self.email, self.display, and self.title in ObfuscateNode.render() need to be converted to local vars.

For instance:

self.email = self.email.resolve(context).lower()

Becomes:

email = self.email.resolve(context).lower()

#

(Forgotten your password?)