'.$parts[0].'
[at]
'.$parts[1].'';
if ($display_name !== '') {
$output .= '
('.self::obfuscateEmail($display_name).')';
}
$output .= '
';
return $output;
}
/**
* This function obfuscates emails by finding the ascii hex codes
* for each letter of the email and randomly selects if it will
* output the hex code or the actual letter.
*
* @param string $email the email or display name to be obfuscated
*
* @return string $safe the string of obfuscated values
*/
private static function obfuscateEmail($email) {
$safe = '';
if (preg_match('//u', $email))
{
// if $email is UTF8, use the str_split_unicode function so that
// email is split correctly (str_split does not correctly handle
// UTF8 strings
$email_array = self::str_split_unicode($email);
}
else
{
$email_array = str_split($email);
}
foreach ($email_array as $letter)
{
switch (($letter === '@') ? rand(1, 2) : rand(1, 3))
{
// HTML entity code
case 1:
$letter = self::get_decoded_unicode_string($letter);
$safe .= ''.ord($letter).';';
break;
// Hex character code
case 2:
$letter = self::get_decoded_unicode_string($letter);
$safe .= ''.dechex(ord($letter)).';';
break;
// Raw (no) encoding
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Found the following function in a comment on the str_split documentation
* by user qeremy@gmail.com
* http://php.net/manual/en/function.str-split.php
* This function essentially safely splits a utf8 string, which fixes a
* problem I encountered when we switched the tech reports db to use utf8
* everything looked great except when you tried to push the name with
* utf8 characters throught the create_email_link function, which called
* str_split, which did not properly split the string when it had special
* characters. Using this function instead to do the split fixes the
* problem.
*/
private static function str_split_unicode($string, $split_length = 0)
{
if ($split_length > 0)
{
$return = array();
$string_length = mb_strlen($string, 'UTF-8');
for ($i = 0; $i < $string_length; $i += $split_length)
{
// perform a multi-byte safe substr() operation based on a
// number of characters
$return[] = mb_substr($string, $i, $split_length, 'UTF-8');
}
return $return;
}
else
{
return preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY);
}
}
/**
* if the $letter that is provided is in UTF8 format, this function decodes
* the letter from UTF8 format and returns it. If the letter is not UTF8
* it just returns the letter as is
*
* @param string $letter the letter that should be decoded if it is UTF8
*
* @return string $letter either decoded or unchanged depending on whether
* or not the letter was in UTF8 originally
*/
private static function get_decoded_unicode_string($letter)
{
// the preg_match returns true if the $letter is in UTF8
if (preg_match('//u', $letter))
{
// if the email is utf8, decode it so that when we try to convert
// it to hex we don't have problems.
$letter = utf8_decode($letter);
}
return $letter;
}
}
?>