- Author:
- sunn
- Posted:
- August 4, 2009
- Language:
- HTML/template
- Version:
- Not specified
- Score:
- 2 (after 2 ratings)
Template tag to obfuscate emails and other spam sensitive information.
Usage
obfuscate '[email protected]' 'Link display' 'Link title' obfuscate '[email protected]' 'Link display' obfuscate 'phone number'
Renders complex xhmtl compliant javascript.
May need caching as it renders a new code every time.
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
- Bootstrap Accordian by Netplay4 5 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Reusable form template with generic view by roldandvg 8 years, 11 months ago
- Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago
Comments
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:
Becomes:
#
Please login first before commenting.