You are here

function invisimail_encode_string in Invisimail 7

Encodes all email addresses in a string using the specified encoder.

Parameters

string $string: The string to encode.

string $encoder_id: The machine name of the encoder to use.

array $settings: The settings to use for the specified encoder.

Return value

string The original string with email addresses obfuscated from spambots.

1 call to invisimail_encode_string()
invisimail_encode_process in ./invisimail.module
Process callback for all invisimail encoders.

File

./invisimail.module, line 238
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_encode_string($string, $encoder_id, array $settings = array()) {
  $encoder = invisimail_get_encoder($encoder_id);

  // PHP doens't allow preg callbacks to take arguments, so we have to toss
  // the configuration options into a global temporarily.  This is so disgusting.
  $settings['callback'] = $encoder['encode callback'];
  $GLOBALS['invisimail_settings'] = $settings;
  $patterns = _invisimail_email_matching_regexes();

  // Chunking will help determine in which portion of the overall string a call
  // to preg_replace_callback may fail.
  if (isset($settings['chunk']) && $settings['chunk']) {
    $substrings = preg_split('/(<a [^>]*>.+?<\\/a>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
  }
  else {
    $substrings = array(
      $string,
    );
  }
  $return = '';
  foreach ($substrings as $phrase) {

    // Call preg_replace_callback twice to avoid prolems with hitting the
    // backtrack limit, as the more complex $pattern has issues with domains more
    // than 13 characters.
    $replaced_phrase = preg_replace_callback($patterns['diff_link'], '_invisimail_process_emails_callback', $phrase);
    if ($error_code = preg_last_error()) {
      watchdog('invisimail', 'preg_replace_callback failed on first pass with preg_last_error() of !code with pattern %pattern on text: !text', array(
        '!code' => $error_code,
        '%pattern' => check_plain($patterns['same_link']),
        '!text' => check_plain($phrase),
      ), WATCHDOG_ERROR, request_uri());
    }
    $replaced_phrase = preg_replace_callback($patterns['same_link'], '_invisimail_process_emails_callback', $replaced_phrase);
    if ($error_code = preg_last_error()) {
      watchdog('invisimail', 'preg_replace_callback failed on second pass with preg_last_error() of !code with pattern %pattern on text: !text', array(
        '!code' => $error_code,
        '%pattern' => check_plain($patterns['diff_link']),
        '!text' => check_plain($phrase),
      ), WATCHDOG_ERROR, request_uri());
    }
    $return .= $replaced_phrase;
  }

  // Clean up our global.
  unset($GLOBALS['invisimail_settings']);
  return $return;
}