Skip to content

Commit

Permalink
fix issue #3 and implement #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kminek committed Jul 14, 2020
1 parent 710079e commit 7fcfdb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 13 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@
</head>
<body>
<h1>Email Obfuscator</h1>

<?php
$email = '[email protected]';
?>

<?php
$email1 = EmailObfuscator::obfuscate('[email protected]');
$email1 = EmailObfuscator::obfuscate($email);
?>
<p>This is obfuscated email: <?php echo $email1; ?></p>
<pre><?php echo htmlentities($email1); ?></pre>
<hr>
<?php
$email2 = obfuscate_email('[email protected]', 'Contact us', ['class' => 'some-class', 'id' => 'some-id', 'noscript' => 'Custom noscript contents']);
$email2 = obfuscate_email($email, 'Contact us', ['class' => 'some-class', 'id' => 'some-id', 'noscript' => 'Custom noscript contents']);
?>
<p>This is obfuscated email: <?php echo $email2; ?></p>
<pre><?php echo htmlentities($email2); ?></pre>
<hr>
<?php
$email3 = obfuscate_email($email, null, ['mailto' => false]);
?>
<p>This is obfuscated email: <?php echo $email3; ?></p>
<pre><?php echo htmlentities($email3); ?></pre>
</body>
</html>
10 changes: 8 additions & 2 deletions src/EmailObfuscator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public static function obfuscate($email, $text = null, $options = [])
{
$defaults = [
'noscript' => 'Enable JavaScript to see this email address',
'mailto' => true,
];
$options = array_merge($defaults, $options);
$email = strtolower($email);
$coded = '';
$unmixedkey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@-';
$unmixedkey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@-_+%';
$inprogresskey = $unmixedkey;
$mixedkey = '';
$unshuffled = strlen($unmixedkey);
Expand Down Expand Up @@ -62,6 +63,7 @@ public static function obfuscate($email, $text = null, $options = [])
}
$attrs .= " {$k}=\"{$v}\"";
}
$mailto = $options['mailto'] ? 'true' : 'false';
$javascript = <<<JAVASCRIPT
<script type="text/javascript" language="javascript">
<!--
Expand All @@ -83,7 +85,11 @@ public static function obfuscate($email, $text = null, $options = [])
}
}
{$text};
document.write('<a href="mailto:' + {$vars['link']} + '"{$attrs}>' + {$vars['text']} + '</a>');
var mailto = {$mailto};
var start = mailto ? '<a href="mailto:' + {$vars['link']} + '"{$attrs}>' : '';
var text = {$vars['text']};
var end = mailto ? '</a>' : '';
document.write(start + text + end);
})();
//-->
</script><noscript>{$options['noscript']}</noscript>
Expand Down

0 comments on commit 7fcfdb3

Please sign in to comment.