From 40fd16074326d910f647dc76bda45c76361d152e Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 1 Oct 2008 14:40:01 +0000 Subject: [PATCH] FEATURE Added Email::obfuscate() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63462 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- email/Email.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/email/Email.php b/email/Email.php index 0fe39329d..4d029c238 100755 --- a/email/Email.php +++ b/email/Email.php @@ -342,7 +342,18 @@ class Email extends ViewableData { return $result; } - + + /** + * Used as a default sender address in the {@link Email} class + * unless overwritten. Also shown to users on live environments + * as a contact address on system error pages. + * + * @usedby Email->send() + * @usedby Email->sendPlain() + * @usedby Debug->friendlyError() + * + * @param string $newEmail + */ public static function setAdminEmail( $newEmail ) { self::$admin_email_address = $newEmail; } @@ -419,6 +430,34 @@ class Email extends ViewableData { return preg_match("!^$addr_spec$!", $email) ? 1 : 0; } + + /** + * Encode an email-address to protect it from spambots. + * At the moment only simple string substitutions, + * which are not 100% safe from email harvesting. + * + * @todo Integrate javascript-based solution + * + * @param string $email Email-address + * @param string $method Method for obfuscating/encoding the address + * - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash]) + * - 'hex': Hexadecimal URL-Encoding - useful for mailto: links + * @return string + */ + public static function obfuscate($email, $method = 'visible') { + switch($method) { + case 'visible' : + $obfuscated = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); + return strtr($email, $obfuscated); + case 'hex' : + $encoded = ''; + for ($x=0; $x < strlen($email); $x++) $encoded .= '&#x' . bin2hex($email{$x}).';'; + return $encoded; + default: + user_error('Email::obfuscate(): Unknown obfuscation method', E_USER_NOTICE); + return $email; + } + } } /**