20 function smarty_outputfilter_obfuscate_email($output, Smarty_Internal_Template $template) {
21 $encodePercentage = 50;
24 $alwaysEncode = [
'.',
'@',
'+'];
25 $octHexEncodeChar =
function($char) use ($alwaysEncode, $encodePercentage) {
26 if (in_array($char, $alwaysEncode) || random_int(1, 100) < $encodePercentage) {
27 if (random_int(0, 1)) {
28 return '&#'.ord($char).
';';
31 return '&#x'.dechex(ord($char)).
';';
38 $neverEncode = [
'.',
'@',
'+'];
39 $urlEncodeChar =
function($char) use ($neverEncode, $encodePercentage) {
40 if (!in_array($char, $neverEncode) && random_int(1, 100) < $encodePercentage) {
41 $charCode = ord($char);
42 return '%'.dechex(($charCode >> 4) & 0xF).dechex($charCode & 0xF);
50 $output = preg_replace_callback(
51 '!<a\s([^>]*)href=["\']mailto:([^"\'\?]+)([^"\']*)["\']([^>]*)>(.*?)</a[^>]*>!is',
52 function($matches) use ($octHexEncodeChar, $urlEncodeChar) {
60 $address = $matches[2];
62 $encodedAddress = preg_replace_callback(
'/./',
function($m) use ($urlEncodeChar) {
63 return $urlEncodeChar($m[0]);
66 $obfuscatedLink = preg_replace_callback(
'/./',
function($m) use ($octHexEncodeChar) {
67 return $octHexEncodeChar($m[0]);
68 },
'mailto:'.$encodedAddress);
70 $params = trim($matches[3]);
72 $extra = trim($matches[1].
" ".$matches[4]);
74 if (!preg_match(
'/rel=["\']nofollow["\']/', $extra)) {
75 $extra = trim($extra.
' rel="nofollow"');
81 $obfuscatedText =
null;
82 if (strpos($text, $address) !==
false) {
83 $obfuscatedText = preg_replace_callback(
'/./',
function($m) use ($octHexEncodeChar) {
84 return $octHexEncodeChar($m[0]);
88 $replace =
'<a href="'.$obfuscatedLink.$params.
'" '.$extra.
'>'.($obfuscatedText ? urldecode($obfuscatedText) : $text).
'</a>';