Projet

Général

Profil

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

root / drupal7 / sites / all / modules / recaptcha / recaptcha_mailhide / recaptcha_mailhide.module @ 4f315dab

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

    
14
  switch ($path) {
15
    case 'admin/modules#name':
16
      $output .= t('reCAPTCHA');
17
      break;
18

    
19
    case 'admin/modules#description':
20
      $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')));
21
      break;
22

    
23
    case 'admin/help#recaptcha':
24
      $output .= '<p>' .
25
        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'))) .
26
        '</p><h3>' .
27
        t('Configuration') .
28
        '</h3><p>' .
29
        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'))) .
30
        '</p>';
31
      break;
32
  }
33

    
34
  return $output;
35
}
36

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

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

    
61
  $text = ' ' . $text . ' ';
62
  $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);
63
  $text = drupal_substr($text, 1, -1);
64

    
65
  unset($_recaptcha_mailhide_public_key);
66
  unset($_recaptcha_mailhide_private_key);
67
  unset($_recaptcha_mailhide_nokey_warn);
68
  return $text;
69
}
70

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

    
95
/**
96
 *
97
 */
98
function _filter_recaptcha_mailhide_tips($filter, $format, $long = FALSE) {
99
  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'));
100
}
101

    
102
/**
103
 * Private reCAPTCHA function to replace an email regex match.
104
 */
105
function _recaptcha_replace($match) {
106
  global $_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $_recaptcha_mailhide_nokey_warn;
107
  // If we invoke recaptchalib without setting the keys, it will die(). Instead,
108
  // fail gracefully.
109
  if (empty($_recaptcha_mailhide_public_key) || empty($_recaptcha_mailhide_private_key) || !function_exists('mcrypt_encrypt')) {
110
    if ($_recaptcha_mailhide_nokey_warn != TRUE) {
111
      if (!function_exists('mcrypt_encrypt')) {
112
        drupal_set_message(t('Addresses cannot be hidden because Mcrypt is not installed.'), 'error');
113
      }
114
      else {
115
        drupal_set_message(t('Addresses cannot be hidden because the administrator has not set the reCAPTCHA Mailhide keys.'), 'error');
116
      }
117
      $_recaptcha_mailhide_nokey_warn = TRUE;
118
    }
119
    $email = $match[2];
120
  }
121
  else {
122
    $email = recaptcha_mailhide_html($_recaptcha_mailhide_public_key, $_recaptcha_mailhide_private_key, $match[2]);
123
  }
124
  return $match[1] . $email . $match[3];
125
}
126

    
127
/**
128
 * Load the recaptcha library.
129
 */
130
function _recaptcha_mailhide_load_library() {
131
  module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
132
}