Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Filter_ObfuscateEmail Class Reference

Obfuscate plain-text email addresses into javascript. More...

List of all members.

Public Member Functions

 filter ($text)
 Obfuscate plain-text email addresses into javascript.
 jsHexEncode ($input)
 Hex encode the input text for javascript strings (e.g.

Detailed Description

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).

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4Cms_Filter_ObfuscateEmail::filter ( text)

Obfuscate plain-text email addresses into javascript.

Parameters:
string$textthe text to obfuscate email addresses in.
Returns:
string the text with obfuscated email addresses.
    {
        $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.

'@' => '').

Parameters:
string$inputthe text to encode.
Returns:
string the encoded text.
    {
        return preg_replace('/(..)/', '\x\\1', bin2hex($input));
    }

The documentation for this class was generated from the following file: