You are here

recaptcha_mailhide.module in reCAPTCHA 7

Protects email addresses using the reCAPTCHA web service.

File

recaptcha_mailhide/recaptcha_mailhide.module
View source
<?php

/**
 * @file
 * Protects email addresses using the reCAPTCHA web service.
 */

/**
 * Implements hook_help().
 */
function recaptcha_mailhide_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/modules#name':
      $output .= t('reCAPTCHA');
      break;
    case 'admin/modules#description':
      $output .= t('Uses the <a href="@url" target="_blank">reCAPTCHA</a> web service to protect email addresses.', array(
        '@url' => url('https://www.google.com/recaptcha/mailhide/apikey'),
      ));
      break;
    case 'admin/help#recaptcha':
      $output .= '<p>' . t('Uses the reCAPTCHA web service to protect email addresses. For more information on what reCAPTCHA Mailhide is, visit <a href="@url" target="_blank">the official website</a>.', array(
        '@url' => url('https://www.google.com/recaptcha/mailhide/apikey'),
      )) . '</p><h3>' . t('Configuration') . '</h3><p>' . t('Head over to the <a href="@inputformats">input format settings</a> and add the <a href="@url" target="_blank">reCAPTCHA Mailhide</a> input filter to hide posted emails.', array(
        '@inputformats' => url('admin/settings/filter'),
        '@url' => url('https://www.google.com/recaptcha/mailhide/apikey'),
      )) . '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_filter_info().
 */
function recaptcha_mailhide_filter_info() {
  $filters['filter_recaptcha_mailhide'] = array(
    'title' => t('reCAPTCHA Mailhide'),
    'description' => _filter_recaptcha_mailhide_tips('', ''),
    'process callback' => '_filter_recaptcha_mailhide',
    'settings callback' => '_filter_recaptcha_mailhide_settings',
    'tips callback' => '_filter_recaptcha_mailhide_tips',
    'cache' => FALSE,
  );
  return $filters;
}

/**
 * Filter callbacks.
 */
function _filter_recaptcha_mailhide($text, $filter, $format) {
  global $_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $_recaptcha_mailhide_nokey_warn;
  _recaptcha_mailhide_load_library();
  $_recaptcha_mailhide_public_key = $filter->settings['recaptcha_mailhide_public_key'];
  $_recaptcha_mailhide_private_key = $filter->settings['recaptcha_mailhide_private_key'];
  $text = ' ' . $text . ' ';
  $text = preg_replace_callback("!(<p>|<li>|<br\\s*/?>|[ \n\r\t\\(])([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\\.[A-Za-z]{2,4})([.,?]?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))!i", '_recaptcha_replace', $text);
  $text = drupal_substr($text, 1, -1);
  unset($_recaptcha_mailhide_public_key);
  unset($_recaptcha_mailhide_private_key);
  unset($_recaptcha_mailhide_nokey_warn);
  return $text;
}

/**
 *
 */
function _filter_recaptcha_mailhide_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  _recaptcha_mailhide_load_library();
  $public = isset($filter->settings['recaptcha_mailhide_public_key']) ? $filter->settings['recaptcha_mailhide_public_key'] : '';
  $private = isset($filter->settings['recaptcha_mailhide_private_key']) ? $filter->settings['recaptcha_mailhide_private_key'] : '';
  $settings['recaptcha_mailhide_public_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Public Key'),
    '#default_value' => $public,
    '#maxlength' => 50,
    '#description' => t('Your public Mailhide key obtained from <a href="@url" target="_blank">reCAPTCHA</a>.', array(
      '@url' => 'https://www.google.com/recaptcha/mailhide/apikey',
    )),
  );
  $settings['recaptcha_mailhide_private_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Private Key'),
    '#default_value' => $private,
    '#maxlength' => 50,
    '#description' => t('Your private Mailhide key obtained from <a href="@url" target="_blank">reCAPTCHA</a>.', array(
      '@url' => 'https://www.google.com/recaptcha/mailhide/apikey',
    )),
  );
  return $settings;
}

/**
 *
 */
function _filter_recaptcha_mailhide_tips($filter, $format, $long = FALSE) {
  return t('E-Mail addresses are hidden with <a href="@url" target="_blank">reCAPTCHA Mailhide</a>.', array(
    '@url' => 'https://www.google.com/recaptcha/mailhide/apikey',
  ));
}

/**
 * Private reCAPTCHA function to replace an email regex match.
 */
function _recaptcha_replace($match) {
  global $_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $_recaptcha_mailhide_nokey_warn;

  // If we invoke recaptchalib without setting the keys, it will die(). Instead,
  // fail gracefully.
  if (empty($_recaptcha_mailhide_public_key) || empty($_recaptcha_mailhide_private_key) || !function_exists('mcrypt_encrypt')) {
    if ($_recaptcha_mailhide_nokey_warn != TRUE) {
      if (!function_exists('mcrypt_encrypt')) {
        drupal_set_message(t('Addresses cannot be hidden because Mcrypt is not installed.'), 'error');
      }
      else {
        drupal_set_message(t('Addresses cannot be hidden because the administrator has not set the reCAPTCHA Mailhide keys.'), 'error');
      }
      $_recaptcha_mailhide_nokey_warn = TRUE;
    }
    $email = $match[2];
  }
  else {
    $email = recaptcha_mailhide_html($_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $match[2]);
  }
  return $match[1] . $email . $match[3];
}

/**
 * Load the recaptcha library.
 */
function _recaptcha_mailhide_load_library() {
  module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
}

Functions

Namesort descending Description
recaptcha_mailhide_filter_info Implements hook_filter_info().
recaptcha_mailhide_help Implements hook_help().
_filter_recaptcha_mailhide Filter callbacks.
_filter_recaptcha_mailhide_settings
_filter_recaptcha_mailhide_tips
_recaptcha_mailhide_load_library Load the recaptcha library.
_recaptcha_replace Private reCAPTCHA function to replace an email regex match.