Perforce Chronicle 2012.2/486814
API Documentation
|
Obfuscate plain-text email addresses into javascript. More...
Public Member Functions | |
filter ($text) | |
Obfuscate plain-text email addresses into javascript. | |
jsHexEncode ($input) | |
Hex encode the input text for javascript strings (e.g. |
Obfuscate plain-text email addresses into javascript.
Identifies all email addresses in the input text and replaces them with an anonymous function passed to document.write (or window.location for mailto's).
P4Cms_Filter_ObfuscateEmail::filter | ( | $ | text | ) |
Obfuscate plain-text email addresses into javascript.
string | $text | the text to obfuscate email addresses in. |
{ $filter = $this; $user = "[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*"; $domain = "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"; $text = preg_replace_callback( "/(href=(['\"]?)mailto:)?($user)@($domain)/i", function($matches) use ($filter) { // hex encode '@', 'mailto', the user and the domain. $at = $filter->jsHexEncode('@'); $mailto = $filter->jsHexEncode('mailto:'); $user = $filter->jsHexEncode($matches[3]); $domain = $filter->jsHexEncode($matches[4]); // construct js function to produce email. $email = 'function(d,u){' . 'return u+"' . $at . '"+d;}("' . $domain . '","' . $user . '")'; // if mailto... if ($matches[1]) { $quote = $matches[2]; $js = 'window.location.href="' . $mailto . '"+' . $email . ';'; return 'href=' . $quote . 'javascript:' . htmlentities($js); } else { return '<script type="text/javascript">' . 'document.write(' . $email . ');' . '</script>'; } }, $text ); return $text; }
P4Cms_Filter_ObfuscateEmail::jsHexEncode | ( | $ | input | ) |
Hex encode the input text for javascript strings (e.g.
'@' => '').
string | $input | the text to encode. |
{ return preg_replace('/(..)/', '\x\\1', bin2hex($input)); }