protected function Markdown::encodeEntityObfuscatedAttribute in Markdown 7
* Input: some text to obfuscate, e.g. "mailto:foo@example.com" * * Output: the same text but with most characters encoded as either a * decimal or hex entity, in the hopes of foiling most address * harvesting spam bots. E.g.: * * mailto:foo * @example.co * m * * Note: the additional output $tail is assigned the same value as the * ouput, minus the number of characters specified by $head_length. * * Based by a filter by Matthew Wickline, posted to BBEdit-Talk. * With some optimizations by Milian Wolff. Forced encoding of HTML * attribute special characters by Allan Odgaard. * *
Parameters
string $text: * @param string &$tail * @param integer $head_length * @return string
1 call to Markdown::encodeEntityObfuscatedAttribute()
- Markdown::encodeURLAttribute in includes/
Markdown.php - * Encode text for a double-quoted HTML attribute containing a URL, * applying the URL filter if set. Also generates the textual * representation for the URL (removing mailto: or tel:) storing it in $text. * This function is *not* suitable for…
File
- includes/
Markdown.php, line 1687
Class
- Markdown
- Markdown Parser Class
Namespace
MichelfCode
protected function encodeEntityObfuscatedAttribute($text, &$tail = null, $head_length = 0) {
if ($text == "") {
return $tail = "";
}
$chars = preg_split('/(?<!^)(?!$)/', $text);
$seed = (int) abs(crc32($text) / strlen($text));
// Deterministic seed.
foreach ($chars as $key => $char) {
$ord = ord($char);
// Ignore non-ascii chars.
if ($ord < 128) {
$r = $seed * (1 + $key) % 100;
// Pseudo-random function.
// roughly 10% raw, 45% hex, 45% dec
// '@' *must* be encoded. I insist.
// '"' and '>' have to be encoded inside the attribute
if ($r > 90 && strpos('@"&>', $char) === false) {
/* do nothing */
}
else {
if ($r < 45) {
$chars[$key] = '&#x' . dechex($ord) . ';';
}
else {
$chars[$key] = '&#' . $ord . ';';
}
}
}
}
$text = implode('', $chars);
$tail = $head_length ? implode('', array_slice($chars, $head_length)) : $text;
return $text;
}