Last active
December 19, 2015 07:09
-
-
Save piotr-cz/5916613 to your computer and use it in GitHub Desktop.
Updated JHtmlEmail.
Changes:
- optimized javascript code
- when javascript is disabled, message is now shown in <noscript /> tag
- fixed bug in XHTML documents
- added placeholder character to show when javascript is disabled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Libraries | |
* @subpackage HTML | |
* | |
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die; | |
/** | |
* Utility class for cloaking email addresses | |
* | |
* @package Joomla.Libraries | |
* @subpackage HTML | |
* @since 1.5 | |
*/ | |
abstract class JHtmlEmail | |
{ | |
/** | |
* Simple JavaScript email cloaker | |
* | |
* By default replaces an email with a mailto link with email cloaked | |
* | |
* @param string $mail The -mail address to cloak. | |
* @param boolean $mailto True if text and mailing address differ | |
* @param string $text Text for the link | |
* @param boolean $email True if text is an e-mail address | |
* | |
* @return string The cloaked email. | |
* | |
* @since 1.5 | |
*/ | |
public static function cloak($mail, $mailto = true, $text = '', $email = true, $placeholder_char = 'x') | |
{ | |
$placeholder = str_repeat($placeholder_char, strlen($mail)); | |
// Convert text | |
$mail = self::convertEncoding($mail); | |
// Split email by @ symbol | |
$mail = explode('@', $mail); | |
$mail_parts = explode('.', $mail[1]); | |
// Random number | |
$rand = rand(1, 100000); | |
// Get document information | |
$document = JFactory::getDocument(); | |
$isXHTML = ($document->getMimeEncoding() != 'text/html'); // application/xhtml+xml | |
$isHTML5 = ($document->isHTML5()); | |
// XHTML compliance no Javascript text handling | |
$replacement = "\n<noscript " . ($isXHTML ? "id=\"noscript{$rand}\" " : '' ) . "title=\"" . JText::_('JLIB_HTML_CLOAKING') . "\">"; | |
$replacement .= "\n {$placeholder}"; | |
$replacement .= "\n</noscript>"; | |
// Prepare replacement | |
$replacement .= "\n<script" . (!$isHTML5 ? ' type="text/javascript">' : ">"); | |
$replacement .= ($isXHTML ? "\n//<![CDATA[" : '' ); | |
$replacement .= "\n(function(d, prefix, path, addy{$rand}){"; | |
$replacement .= "\n addy{$rand} += '" . implode("' + '.' + '", $mail_parts) . "';"; | |
$replacement .= ($isXHTML ? "\n var el, ref = d.getElementById('noscript{$rand}'), helper = d.createElement('div');" : ''); | |
if ($mailto) | |
{ | |
// Create a hyperlink element and href | |
if (!$isXHTML) | |
{ | |
$replacement .= "\n d.write('<a ' + path + '=' + prefix + addy{$rand} + '>');"; | |
} | |
else | |
{ | |
$replacement .= "\n helper.innerHTML = prefix + addy{$rand};"; | |
$replacement .= "\n el = d.createElement('a');"; | |
$replacement .= "\n el[path] = helper.firstChild.nodeValue;"; | |
} | |
// Special handling when mail text is different from mail address | |
if ($text) | |
{ | |
if ($email) | |
{ | |
// Convert text | |
$text = self::convertEncoding($text); | |
// Split email by @ symbol | |
$text = explode('@', $text); | |
$text_parts = explode('.', $text[1]); | |
$replacement .= "\n var addy_text{$rand} = '" . @$text[0] . "' + '@' + '" . implode("' + '.' + '", @$text_parts) | |
. "';"; | |
} | |
else | |
{ | |
$replacement .= "\n var addy_text{$rand} = '{$text}';"; | |
} | |
// Set hyperlink text | |
if (!$isXHTML) | |
{ | |
$replacement .= "\n d.write(addy_text{$rand} + '</a>');"; | |
} | |
else | |
{ | |
$replacement .= "\n el.innerHTML = addy_text{$rand};"; | |
} | |
} | |
// Set hyperlink text | |
elseif (!$isXHTML) | |
{ | |
$replacement .= "\n d.write(addy{$rand} + '</a>')"; | |
} | |
else | |
{ | |
$replacement .= "\n el.innerHTML = addy{$rand};"; | |
} | |
} | |
// Decode html entities and create Text node | |
elseif (!$isXHTML) | |
{ | |
$replacement .= "\n d.write('<a ' + path + '=\"' + prefix + addy{$rand} + '\">';"; | |
$replacement .= "\n d.write(addy{$rand} + '</a>');"; | |
} | |
else | |
{ | |
$replacement .= "\n helper.innerHTML = addy{$rand};"; | |
$replacement .= "\n el = d.createTextNode(helper.firstChild.nodeValue);"; | |
} | |
// Insert before <noscript /> element | |
if ($isXHTML) | |
{ | |
$replacement .= "\n ref.parentNode.insertBefore(el, ref);"; | |
} | |
// Close replacement | |
$replacement .= "\n}(document, 'ma' + 'il' + 'to:', 'hr' + 'ef', '" . @$mail[0] . "' + '@'));"; | |
$replacement .= ($isXHTML ? "\n//]]>" : ''); | |
$replacement .= "\n</script>"; | |
return $replacement; | |
} | |
/** | |
* Convert encoded text | |
* | |
* @param string $text Text to convert | |
* | |
* @return string The converted text. | |
* | |
* @since 1.5 | |
*/ | |
protected static function convertEncoding($text) | |
{ | |
// Replace vowels with character encoding | |
$text = str_replace('a', 'a', $text); | |
$text = str_replace('e', 'e', $text); | |
$text = str_replace('i', 'i', $text); | |
$text = str_replace('o', 'o', $text); | |
$text = str_replace('u', 'u', $text); | |
return $text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment