Skip to content

Instantly share code, notes, and snippets.

@weichenlin
Created March 15, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weichenlin/117171171d3c7947218ef3dca6bf3f66 to your computer and use it in GitHub Desktop.
Save weichenlin/117171171d3c7947218ef3dca6bf3f66 to your computer and use it in GitHub Desktop.
encode email and generate js code
<?php
function rand_string($min_len, $max_len, $charset = "abcdefghijklmnopqrstuvwxyz")
{
$set_len = strlen($charset);
if (0 == $set_len) { return "";}
$out_len = rand($min_len, $max_len);
$out = "";
for ($i = 0; $i < $out_len; $i++) {
$index = rand(0, $set_len - 1);
$out .= $charset[$index];
}
return $out;
}
function make_script($mail)
{
$mail_html = "<a href=\"mailto:{$mail}\">{$mail}</a>";
$jselements = "";
$xor = rand(1, 255);
for ($i=0, $j=strlen($mail_html); $i < $j; $i++) {
$hex = sprintf("%02s", dechex(ord($mail_html[$i]) ^ $xor));
$jselements .= ("" == $jselements ? "\"\\x{$hex}\"" : ",\"\\x{$hex}\"");
}
$encoded_var_name = rand_string(2, 10);
$jscode = "var {$encoded_var_name} = [ {$jselements} ];";
$jscode .= "for(var i=0; i<{$encoded_var_name}.length; i++){document.write(String.fromCharCode({$encoded_var_name}[i].charCodeAt(0) ^ {$xor}));}";
$jscode = "<script type=\"text/javascript\">{$jscode}</script>";
return $jscode;
}
$mail = "";
$ok = false;
$jscode = "";
if (isset($_POST['email']) && "" != $_POST['email']) {
$mail = $_POST['email'];
$jscode = make_script($mail);
$ok = true;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Email Encoder</title>
</head>
<body>
<p>請填入要編碼的Email</p>
<form method="post">
E-mail: <input type="email" name="email" value="<?=$mail?>"><br>
<input type="submit" value="送出">
</form>
<?php if ($ok) { ?>
<p>
編碼已完成,請複製以下JavaScript程式,貼在HTML要顯示Email處即可。
</p>
<textarea cols="100" rows="10"><?=htmlentities($jscode)?></textarea>
<p>
預覽:
<?=$jscode?>
</p>
<?php } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment