Projet

Général

Profil

Paste
Télécharger (5,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / recaptcha / recaptcha_mailhide.module @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Protects email addresses using the reCAPTCHA web service.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function recaptcha_mailhide_help($path, $arg) {
12
  $output = '';
13
  switch ($path) {
14
    case 'admin/modules#name':
15
      $output .= t('reCAPTCHA');
16
      break;
17
    case 'admin/modules#description':
18
      $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')));
19
      break;
20
    case 'admin/help#recaptcha':
21
      $output .= '<p>' .
22
        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/'))) .
23
        '</p><h3>' .
24
        t('Configuration') .
25
        '</h3><p>' .
26
        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/'))) .
27
        '</p>';
28
      break;
29
  }
30
  return $output;
31
}
32

    
33
/**
34
 * Implements hook_filter_info().
35
 */
36
function recaptcha_mailhide_filter_info() {
37
  $filters['filter_recaptcha_mailhide'] = array(
38
    'title' => t('reCAPTCHA Mailhide'),
39
    'description' => _filter_recaptcha_mailhide_tips('', ''),
40
    'process callback' => '_filter_recaptcha_mailhide',
41
    'settings callback' => '_filter_recaptcha_mailhide_settings',
42
    'tips callback' => '_filter_recaptcha_mailhide_tips',
43
    'cache' => FALSE,
44
  );
45
  return $filters;
46
}
47

    
48
/**
49
 * Filter callbacks.
50
 */
51
function _filter_recaptcha_mailhide($text, $filter, $format) {
52
  global $_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $_recaptcha_mailhide_nokey_warn;
53
  _recaptcha_mailhide_load_library();
54
  $_recaptcha_mailhide_public_key = $filter->settings['recaptcha_mailhide_public_key'];
55
  $_recaptcha_mailhide_private_key = $filter->settings['recaptcha_mailhide_private_key'];
56

    
57
  $text = ' ' . $text . ' ';
58
  $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);
59
  $text = drupal_substr($text, 1, -1);
60

    
61
  unset($_recaptcha_mailhide_public_key);
62
  unset($_recaptcha_mailhide_private_key);
63
  unset($_recaptcha_mailhide_nokey_warn);
64
  return $text;
65
}
66

    
67
function _filter_recaptcha_mailhide_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
68
  _recaptcha_mailhide_load_library();
69
  $public = isset($filter->settings['recaptcha_mailhide_public_key']) ? $filter->settings['recaptcha_mailhide_public_key'] : '';
70
  $private = isset($filter->settings['recaptcha_mailhide_private_key']) ? $filter->settings['recaptcha_mailhide_private_key'] : '';
71
  $settings['recaptcha_mailhide_public_key'] = array(
72
    '#type' => 'textfield',
73
    '#title' => t('Public Key'),
74
    '#default_value' => $public,
75
    '#maxlength' => 50,
76
    '#description' => t('Your public Mailhide key obtained from <a href="@url" target="_blank">reCAPTCHA</a>.', array('@url' => 'https://www.google.com/recaptcha/mailhide/apikey')),
77
    );
78
  $settings['recaptcha_mailhide_private_key'] = array(
79
    '#type' => 'textfield',
80
    '#title' => t('Private Key'),
81
    '#default_value' => $private,
82
    '#maxlength' => 50,
83
    '#description' => t('Your private Mailhide key obtained from <a href="@url" target="_blank">reCAPTCHA</a>.', array('@url' => 'https://www.google.com/recaptcha/mailhide/apikey')),
84
    );
85
  return $settings;
86
}
87

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

    
92
/**
93
 * Private reCAPTCHA function to replace an email regex match
94
 */
95
function _recaptcha_replace($match) {
96
  global $_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $_recaptcha_mailhide_nokey_warn;
97
  // recaptchalib will die if we invoke without setting the keys. Fail gracefully in this case.
98
  if (empty($_recaptcha_mailhide_public_key) || empty($_recaptcha_mailhide_private_key) || !function_exists('mcrypt_encrypt')) {
99
    if ($_recaptcha_mailhide_nokey_warn != TRUE) {
100
      if (!function_exists('mcrypt_encrypt')) {
101
        drupal_set_message(t('Addresses cannot be hidden because Mcrypt is not installed.'), 'error');
102
      }
103
      else {
104
        drupal_set_message(t('Addresses cannot be hidden because the administrator has not set the reCAPTCHA Mailhide keys.'), 'error');
105
      }
106
      $_recaptcha_mailhide_nokey_warn = TRUE;
107
    }
108
    $email = $match[2];
109
  }
110
  else {
111
    $email = recaptcha_mailhide_html($_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $match[2]);
112
  }
113
  return $match[1] . $email . $match[3];
114
}
115

    
116
/**
117
 * Load the recaptcha library.
118
 */
119
function _recaptcha_mailhide_load_library() {
120
  module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
121
}