Created
January 11, 2021 19:13
-
-
Save digitalbricks/c6445e7ef0ff40be1895426bef0889d4 to your computer and use it in GitHub Desktop.
A quite simple e-mail address obfuscation function using PHP and JS
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
<h2>Test</h2> | |
<?=obfuscateEmailWithJS("info@example.com",true)?><br> | |
<?=obfuscateEmailWithJS("doris.mustermann@example1.com",true,"Lorem ipsum")?><br> | |
<p>Lorem ipsum, dolor sit amet consectetur <?=obfuscateEmailWithJS("info@example.com",true)?> adipisicing elit. Cumque, veritatis? Cumque ut aut quidem suscipit saepe earum ducimus sunt nobis, cupiditate autem quaerat soluta atque veritatis similique minus sequi quasi?</p> | |
<?php | |
/** | |
* obfuscateEmailWithJS | |
* | |
* @param string $email e-mail adress | |
* @param bool $link output link or not | |
* @param mixed $nojsText custom no-js placeholder text | |
* @param string $prefix prefix of css class names and JS vars (may be changed to avoid conflicts) | |
* @return void | |
*/ | |
function obfuscateEmailWithJS(string $email, bool $link =false, $nojsText = false, $prefix="obf_"){ | |
// classname of anchor element | |
$anchorname = $prefix.md5($email); | |
if($link){ | |
$anchorname = $anchorname."_link"; | |
} | |
// set default placeholder text, if none is given | |
// NOTE: using email-like text in html comment **may** confuse spam bots | |
if(!$nojsText){ | |
$nojsText = "<!-- fakemail@example.com --> <!-- mailto:fakemail@example.com -->"; | |
} | |
// split mail adress at "@" char | |
$segments = explode('@',$email); | |
// abort if splitting results in just one segment (no @ sign present in given $email) | |
if(!count($segments) OR count($segments)==1){ | |
return; | |
} | |
// use ROT13 on segments, add quotes before and after | |
$segments_rot = array(); | |
foreach ($segments as $key=>$value){ | |
$segments_rot[$key] = '"'.str_rot13($value).'"'; | |
} | |
// prepare js array of ROT13 segments | |
$js_array = '['.implode(',',$segments_rot).']'; | |
// output an empty HTML element as anchor for | |
// js generated output in order to avoid document.write() | |
// because document.write() should be avoided based on a | |
// warnin in google chrome | |
$output = "<span class='{$prefix}anchor {$anchorname}'>{$nojsText}</span>"; | |
// javascript output | |
// -- Source of rot13(): https://codereview.stackexchange.com/a/132140 | |
$output.= '<script>'; | |
$output.= 'function rot13(str) { | |
var input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
var output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; | |
var index = x => input.indexOf(x); | |
var translate = x => index(x) > -1 ? output[index(x)] : x; | |
return str.split("").map(translate).join(""); | |
}; | |
var '.$prefix.'segments = '.$js_array.'; | |
var '.$prefix.'segments2 = []; | |
var '.$prefix.'anchor = document.getElementsByClassName("'.$anchorname.'"); | |
'.$prefix.'segments.forEach(function(entry){ | |
'.$prefix.'segments2.push(rot13(entry)); | |
});'; | |
// to link or not to link | |
if(!$link){ | |
$output.= 'for (let el of '.$prefix.'anchor) { | |
el.innerText = '.$prefix.'segments2.join("@"); | |
}'; | |
} else { | |
$output.= 'for (let el of '.$prefix.'anchor) { | |
el.innerHTML = "<a href=\'mailto:" + '.$prefix.'segments2.join("@") + "\'>" + '.$prefix.'segments2.join("@") + "</a>"; | |
}'; | |
} | |
$output.= '</script>'; | |
return $output; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment