Skip to content

Instantly share code, notes, and snippets.

@piotr-cz
Last active December 19, 2015 07:09
Show Gist options
  • Select an option

  • Save piotr-cz/5916613 to your computer and use it in GitHub Desktop.

Select an option

Save piotr-cz/5916613 to your computer and use it in GitHub Desktop.

Revisions

  1. Piotr revised this gist Jul 3, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion JHtmlEmail
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ abstract class JHtmlEmail
    if ($email)
    {
    // Convert text
    $text = self::_convertEncoding($text);
    $text = self::convertEncoding($text);

    // Split email by @ symbol
    $text = explode('@', $text);
  2. Piotr revised this gist Jul 3, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion JHtmlEmail
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,8 @@ abstract class JHtmlEmail
    */
    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);

    @@ -48,7 +50,6 @@ abstract class JHtmlEmail
    $document = JFactory::getDocument();
    $isXHTML = ($document->getMimeEncoding() != 'text/html'); // application/xhtml+xml
    $isHTML5 = ($document->isHTML5());
    $isXHTML = false;

    // XHTML compliance no Javascript text handling
    $replacement = "\n<noscript " . ($isXHTML ? "id=\"noscript{$rand}\" " : '' ) . "title=\"" . JText::_('JLIB_HTML_CLOAKING') . "\">";
  3. Piotr created this gist Jul 3, 2013.
    164 changes: 164 additions & 0 deletions JHtmlEmail
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,164 @@
    <?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')
    {
    // 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());
    $isXHTML = false;

    // 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("' + '&#46;' + '", $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] . "' + '&#64;' + '" . implode("' + '&#46;' + '", @$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, '&#109;a' + 'i&#108;' + '&#116;o:', 'hr' + 'ef', '" . @$mail[0] . "' + '&#64;'));";
    $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', '&#97;', $text);
    $text = str_replace('e', '&#101;', $text);
    $text = str_replace('i', '&#105;', $text);
    $text = str_replace('o', '&#111;', $text);
    $text = str_replace('u', '&#117;', $text);

    return $text;
    }
    }