You are here

function invisimail_encoder_js_entities_encode in Invisimail 7

Encoding callback for the js_entities encoder.

Note that a link that is already a link cannot be Javascript encoded, since the link element is already created. Instead we just fallback to HTML encoding in that case. A best-attempt will be made, however, to handle non-existing-links properly, possibly turning them into links if so configured.

1 string reference to 'invisimail_encoder_js_entities_encode'
invisimail_invisimail_encoder_info in ./invisimail.module
Implements hook_invisimail_encoder_info().

File

./invisimail.module, line 537
This module provides a filter that will search content for email addresses and replace them with their ascii equivalents before display. This is not a complete protection from spam harvesters, but it is some help.

Code

function invisimail_encoder_js_entities_encode($string, $settings = array(), $is_link = FALSE) {
  static $counter = 1;

  // First encode the string as HTML entities, which will then be Javascript-
  // injected into the page.
  $encode = invisimail_encode_html($string);

  // If the email address is already inside a link we cannot use JS encoding on
  // it, since we cannot modify the <a> element and cannot inject our placeholder
  // span inside the href itself.  We therefore don't bother and just use the
  // basic HTML encoding.
  if ($is_link) {
    return $encode;
  }
  if (isset($settings['link']) && $settings['link']) {
    $mailto = INVISIMAIL_MAILTO_ASCII;
    $encode = "<a href=\"{$mailto}{$encode}\">{$encode}</a>";
  }

  // We have to obfuscate the ID to avoid having a regular pattern for the names.
  // if we had an ID format like "invisimail_$counter", then spambots could
  // easily detect that string and know to parse the HTML entities that follow
  // back into an email address.  We use sha1() instead of md5() because md5()
  // is not allowed at all on government sites due to paranoia.
  $link_id = sha1("mailto_link_{$counter}");
  $counter++;
  $output = <<<JS
  <span id="{<span class="php-variable">$link_id</span>}"></span>
<script type="text/javascript"> <!--
  document.getElementById('{<span class="php-variable">$link_id</span>}').innerHTML = '{<span class="php-variable">$encode</span>}';
// --> </script>
JS;
  return $output;
}