Skip to content

Instantly share code, notes, and snippets.

@efoken
Last active December 31, 2015 20:28
Show Gist options
  • Save efoken/8039961 to your computer and use it in GitHub Desktop.
Save efoken/8039961 to your computer and use it in GitHub Desktop.
WordPress email obfuscation
<?php
/**
* Live email obfuscation using the "otliam" CSS module.
*
* @package eMSN
*/
define( 'EMSN_OTLIAM_REPLACE_ALWAYS', serialize( array( '.', '@' ) ) );
/**
* Filters most content to obfuscate appearing email addresses.
*
* What this stuff basically does, is looking up plaintext email addresses
* appearing somewhere in the content and reversing the strings (i.e.
* "[email protected]" will end up as "ed.nsme@ofni"). Afterwards the emails will be
* reversed back using CSS.
*
* Besides that, this function although converts email address to HTML entities,
* as you can see in the following examples:
*
* >>> emsn_obfuscate_email( '<a href="mailto:[email protected]">Foobar</a>' );
* <a href="mailto:pe&#116;er&#46;tae&#110;ze&#114;&#64;ems&#x6e;&#x2e;de">Foobar</a>
* >>> emsn_obfuscate_email( '[email protected]' );
* <span class="otliam">e&#x64;&#46;n&#115;me&#x40;rezne&#97;&#x74;&#x2e;re&#x74;e&#112;</span>
*
* @link http://perishablepress.com/best-method-for-email-obfuscation/
* @link http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
*
* @param string $text
* @param array $args
* @return string
*/
function emsn_obfuscate_email( $text, $args = array() ) {
// This isn't making any attempts to only recognize valid email address
// syntax. Basically anything roughly like "[email protected]" looks like an email.
$email_regex = "([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})";
$text = ' ' . $text . ' ';
// This matches all emails, except for those appearing in tag attributes.
$text = preg_replace_callback( "/(?!<.*?){$email_regex}(?![^<>]*?>)/i", 'emsn_obfuscate_email_cb', $text );
// When checking again, the only emails that are left are those in attributes.
$text = preg_replace_callback( "/{$email_regex}/i", 'emsn_obfuscate_email_in_attribute_cb', $text );
return trim( $text );
}
add_filter( 'link_description', 'emsn_obfuscate_email', 15 );
add_filter( 'link_notes', 'emsn_obfuscate_email', 15 );
add_filter( 'bloginfo', 'emsn_obfuscate_email', 15 );
add_filter( 'nav_menu_description', 'emsn_obfuscate_email', 15 );
add_filter( 'term_description', 'emsn_obfuscate_email', 15 );
add_filter( 'the_title', 'emsn_obfuscate_email', 15 );
add_filter( 'the_content', 'emsn_obfuscate_email', 15 );
add_filter( 'get_the_excerpt', 'emsn_obfuscate_email', 15 );
add_filter( 'comment_text', 'emsn_obfuscate_email', 15 );
add_filter( 'list_cats', 'emsn_obfuscate_email', 15 );
add_filter( 'widget_text', 'emsn_obfuscate_email', 15 );
add_filter( 'the_author_email', 'emsn_obfuscate_email', 15 );
add_filter( 'get_comment_author_email', 'emsn_obfuscate_email', 15 );
/**
* Callback function for obfuscating plaintext emails.
*
* @param string $matches The matching email parts to obfuscate.
* @return string The obfuscated email.
*/
function emsn_obfuscate_email_cb( $matches, $allow_markup = true ) {
$email_name = $matches[1];
$email_host = $matches[2];
// Reverse the email string if HTML markup is allowd.
if ( $allow_markup )
$email = strrev( $email_host ) . '@' . strrev( $email_name );
else
$email = $email_name . '@' . $email_host;
$replace_always = unserialize( EMSN_OTLIAM_REPLACE_ALWAYS );
$email_encoded = '';
for ( $i = 0; $i < strlen( $email ); $i++ ) {
// Encode 25% of chars including several that always should be encoded.
if ( in_array( $email[$i], $replace_always ) || mt_rand( 1, 100 ) < 25 ) {
if ( mt_rand( 0, 1 ) )
$email_encoded .= '&#' . ord( $email[$i] ) . ';';
else
$email_encoded .= '&#x' . dechex( ord( $email[$i] ) ) . ';';
} else {
$email_encoded .= $email[$i];
}
}
if ( $allow_markup )
$email_encoded = '<span class="otliam">' . $email_encoded . '</span>';
return $email_encoded;
}
/**
* Callback function for obfuscating emails appearing in tag attributes.
*
* @param string $matches The matching email parts to obfuscate.
* @return string The obfuscated email.
*/
function emsn_obfuscate_email_in_attribute_cb( $matches ) {
return emsn_obfuscate_email_cb( $matches, false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment