From 00ad50157fe5d8dd11f9450fbd593c78213058c2 Mon Sep 17 00:00:00 2001 From: greggilbert Date: Sun, 14 Apr 2013 11:22:45 -0400 Subject: [PATCH] Bring back Html::mailto and Html::email from L3 --- HtmlBuilder.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/HtmlBuilder.php b/HtmlBuilder.php index 38274e3..90d937d 100644 --- a/HtmlBuilder.php +++ b/HtmlBuilder.php @@ -127,7 +127,7 @@ class HtmlBuilder { return 'attributes($attributes).'>'.$this->entities($title).''; } - + /** * Generate a HTTPS HTML link. * @@ -198,6 +198,68 @@ class HtmlBuilder { return $this->link($this->url->action($action, $parameters), $title, $attributes); } + /** + * Generate a HTML link to a mailto + * @param string $email + * @param string $title + * @param array $attributes + * @return string + */ + public function mailto($email, $title = null, $attributes = array()) + { + $email = static::email($email); + + $title = $title ?: $email; + + $email = static::obfuscate('mailto:') . $email; + + return 'attributes($attributes).'>'.$this->entities($title).''; + } + + /** + * Obfuscate an e-mail address to prevent spam-bots from sniffing it. + * + * @param string $email + * @return string + */ + public static function email($email) + { + return str_replace('@', '@', static::obfuscate($email)); + } + + /** + * Obfuscate a string to prevent spam-bots from sniffing it. + * + * @param string $value + * @return string + */ + protected static function obfuscate($value) + { + $safe = ''; + + foreach (str_split($value) as $letter) + { + // To properly obfuscate the value, we will randomly convert each + // letter to its entity or hexadecimal representation, keeping a + // bot from sniffing the randomly obfuscated letters. + switch (rand(1, 3)) + { + case 1: + $safe .= '&#'.ord($letter).';'; + break; + + case 2: + $safe .= '&#x'.dechex(ord($letter)).';'; + break; + + case 3: + $safe .= $letter; + } + } + + return $safe; + } + /** * Generate an ordered list of items. *