| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: spamspan.module,v 1.6 2007/01/15 23:50:53 lakka Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* This module implements the spamspan technique ( http://www.spamspan.com ) for hiding
|
| 8 |
* email addresses from spambots. If javascript is disabled on the client-side, addresses
|
| 9 |
* appear as example [at] example [dot] com.
|
| 10 |
*
|
| 11 |
* (c) 2006 - 2007 Lawrence Akka
|
| 12 |
*
|
| 13 |
* Licenced under GPL v 2 - see http://www.gnu.org/licenses/gpl.txt
|
| 14 |
*/
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_help().
|
| 20 |
*
|
| 21 |
*/
|
| 22 |
function spamspan_help($section) {
|
| 23 |
switch ($section) {
|
| 24 |
case 'admin/modules#description':
|
| 25 |
// This description is shown in the listing at admin/modules.
|
| 26 |
$output = t('Disguises email addresses in an attempt to reduce spam.');
|
| 27 |
if (module_exists('spamspan')) {
|
| 28 |
$output .= t(' <a href="%url">more help</a>', array('%url' => url('admin/help/spamspan')));
|
| 29 |
}
|
| 30 |
return $output;
|
| 31 |
case 'admin/help#spamspan':
|
| 32 |
return t('<p>The SpamSpan module obfuscates email addresses to help prevent spambots from collecting them. It will produce clickable links if JavaScript is enabled, and will show the email address as <code>example [at] example [dot] com</code> if the browser does not support JavaScript.</p><p>To configure the module, select "configure" next to the <a href="%url">input format</a> you\'d like to use. Enable "Hide Email Addresses" and submit the form. Then select the "configure" tab to choose relevant options.</p>', array('%url' => url('admin/filters')));
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Implementation of hook_filter_tips().
|
| 39 |
*
|
| 40 |
*/
|
| 41 |
function spamspan_filter_tips($delta, $format, $long = FALSE) {
|
| 42 |
switch ($delta) {
|
| 43 |
case 0:
|
| 44 |
return t('Each email address will be obfuscated in a human readble fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.');
|
| 45 |
break;
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_filter().
|
| 52 |
*
|
| 53 |
*/
|
| 54 |
function spamspan_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 55 |
if ($op == 'list') {
|
| 56 |
return array(0 => t('Hide email addresses'));
|
| 57 |
}
|
| 58 |
|
| 59 |
switch ($delta) {
|
| 60 |
|
| 61 |
case 0:
|
| 62 |
|
| 63 |
switch ($op) {
|
| 64 |
case 'description':
|
| 65 |
return t('Attempt to hide email addresses from spam-bots.');
|
| 66 |
|
| 67 |
case 'prepare':
|
| 68 |
return $text;
|
| 69 |
|
| 70 |
case 'process':
|
| 71 |
return spamspan($text, $format);
|
| 72 |
|
| 73 |
case 'settings':
|
| 74 |
$form['spamspan_settings'] = array(
|
| 75 |
'#type' => 'fieldset',
|
| 76 |
'#title' => t('SpamSpan email address encoding filter'),
|
| 77 |
'#description' => t('You probably do not need to change any of these settings'),
|
| 78 |
'#collapsible' => true, '#collapsed' => true
|
| 79 |
);
|
| 80 |
|
| 81 |
$form['spamspan_settings']['spamspan_userclass_'.$format] = array(
|
| 82 |
'#type' => 'textfield',
|
| 83 |
'#title' => t('Local part class'),
|
| 84 |
'#default_value' => variable_get('spamspan_userclass_'.$format, 'u'),
|
| 85 |
'#required' => TRUE,
|
| 86 |
'#description' => t('The class name of the <span> element enclosing the part of the address before the @')
|
| 87 |
);
|
| 88 |
|
| 89 |
$form['spamspan_settings']['spamspan_domainclass_'.$format] = array(
|
| 90 |
'#type' => 'textfield',
|
| 91 |
'#title' => t('Domain part class'),
|
| 92 |
'#default_value' => variable_get('spamspan_domainclass_'.$format, 'd'),
|
| 93 |
'#required' => TRUE,
|
| 94 |
'#description' => t('The class name of the <span> element enclosing the part of the address after the @')
|
| 95 |
);
|
| 96 |
|
| 97 |
$form['spamspan_settings']['spamspan_at_'.$format] = array(
|
| 98 |
'#type' => 'textfield',
|
| 99 |
'#title' => t('@ text'),
|
| 100 |
'#default_value' => variable_get('spamspan_at_'.$format, ' [at] '),
|
| 101 |
'#required' => TRUE,
|
| 102 |
'#description' => t('Replace @ with this text when javascript is disabled')
|
| 103 |
|
| 104 |
);
|
| 105 |
return $form;
|
| 106 |
}
|
| 107 |
break;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Implementation of hook_footer().
|
| 114 |
*
|
| 115 |
*/
|
| 116 |
function spamspan_footer($main = 0) {
|
| 117 |
|
| 118 |
// Add the javascript to each page
|
| 119 |
drupal_add_js(drupal_get_path("module", "spamspan") . '/spamspan.compressed.js');
|
| 120 |
// pass necessary variables to the javascript
|
| 121 |
global $spamspan_format;
|
| 122 |
drupal_add_js(array(
|
| 123 |
'spamspan' => array(
|
| 124 |
'm' => 'spamspan',
|
| 125 |
'u' => variable_get('spamspan_userclass_'.$spamspan_format, 'u'),
|
| 126 |
'd' => variable_get('spamspan_domainclass_'.$spamspan_format, 'd'),
|
| 127 |
't' => variable_get('spamspan_anchorclass_'.$spamspan_format, 't')
|
| 128 |
)
|
| 129 |
), 'setting');
|
| 130 |
}
|
| 131 |
|
| 132 |
function spamspan_callback($matches) {
|
| 133 |
// Replace .'s in domain part with [dot]
|
| 134 |
global $spamspan_format;
|
| 135 |
$domain = str_replace("."," [dot] ", $matches[3]);
|
| 136 |
return "$matches[1]<span class=\"spamspan\"><span class=\"" . variable_get('spamspan_userclass_'.$spamspan_format, 'u') . "\">$matches[2]</span>" . variable_get('spamspan_at_'.$spamspan_format, ' [at] ') . "<span class=\"" . variable_get('spamspan_domainclass_'.$spamspan_format, 'd') . "\">$domain</span></span>";
|
| 137 |
}
|
| 138 |
|
| 139 |
function spamspan ($string, $format) {
|
| 140 |
/* We are aiming for code like this:
|
| 141 |
<span class="spamspan">
|
| 142 |
<span class="u">user</span>
|
| 143 |
[at]
|
| 144 |
<span class="d">example [dot] com</span>
|
| 145 |
</span>
|
| 146 |
*/
|
| 147 |
$GLOBALS['spamspan_format'] = $format;
|
| 148 |
$pattern = "!(<p>|<li>|<br\s*/?>|[ \n\r\t\(])(?:([-\.\w\+^@]+)@((?:[-\w]+\.)+[A-Z]{2,4}))(?:[.,]?)(?=(?:</p>|</li>|<br\s*/?>|[ \n\r\t\)]))!i";
|
| 149 |
return preg_replace_callback($pattern, 'spamspan_callback', $string);
|
| 150 |
}
|