Skip to content

Instantly share code, notes, and snippets.

@howellcc
Created October 28, 2015 16:17
Show Gist options
  • Save howellcc/e9493effe68d7748f49e to your computer and use it in GitHub Desktop.
Save howellcc/e9493effe68d7748f49e to your computer and use it in GitHub Desktop.
MuraCMS Email obfuscator: Place this code inside of the theme-level eventHandler.cfc
function onRenderEnd (required any $){
request.__muraresponse__ = hideEmails(request.__muraresponse__);
}
string function hideEmails(required string contentBlock){
local.emailRegEx = '[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}';
//first do the mailto links
local.mailToLinks = reMatchNoCase("mailto:#local.emailRegEx#", arguments.contentBlock);
for(local.mailToLink in local.mailToLinks){
local.newLink = rEReplaceNoCase(local.mailToLink,"mailto:(.*)@(.*)","javascript:noSpam('\1','\2')");
contentBlock = replaceNoCase(contentBlock,local.mailToLink,local.newLink,"all");
}
//then do the plain test emails
local.emailAddresses = reMatchNoCase("#local.emailRegEx#", arguments.contentBlock);
for(local.emailAddress in local.emailAddresses){
local.newEmail = htmlObfuscateString(local.emailAddress);
contentBlock = replaceNoCase(contentBlock,local.emailAddress,local.newEmail,"all");
}
return contentBlock;
}
string function htmlObfuscateString(required string stringToEncode){
local.encodedString = "";
for(local.i=1; local.i lte len(stringToEncode); local.i++){
switch(local.i mod 3){
case 0: //ascii encode
local.encodedString &= "&###asc(mid(stringToEncode,local.i,1))#;";
break;
case 1: //hex encode
local.encodedString &= "&##x#FormatBaseN(asc(mid(stringToEncode, local.i, 1)), 16)#;";
break;
case 2: //normal
local.encodedString &= mid(stringToEncode, local.i, 1);
break;
}
}
return local.encodedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment