View source
<?php
define('GEO_FILTER_AT_SYMBOL', t('[at]'));
define('GEO_FILTER_DOT_SYMBOL', t('[dot]'));
function geo_filter_init() {
drupal_add_js(drupal_get_path('module', 'geo_filter') . '/geo_filter.js');
}
function geo_filter_menu() {
$items = array();
$items['contact/%/%/%'] = array(
'title' => 'Email',
'page callback' => 'geo_filter_contact_redirect',
'page arguments' => array(
1,
2,
3,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function geo_filter_contact_redirect($name, $domain, $tld) {
$hidestring = $name . GEO_FILTER_AT_SYMBOL . $domain . GEO_FILTER_DOT_SYMBOL . $tld;
$realstring = $name . '@' . $domain . '.' . $tld;
$cid = db_result(db_query("SELECT cid FROM {contact} WHERE category = '%s'", array(
$hidestring,
)));
if (!$cid) {
drupal_goto('contact', '', NULL, 301);
}
drupal_goto('contact', 'edit[cid]=' . $cid, NULL, 301);
}
function geo_filter_filter_tips($delta, $format, $long = FALSE) {
switch ($delta) {
case 0:
return t('Email addresses and mailto: links will be obfuscated to hide them from spambots but still be readable to javascript-enabled browsers.');
break;
}
}
function geo_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Email link obfuscator'),
);
case 'description':
return t('Hides email addresses and mailto: links from spam bots.');
case 'prepare':
return $text;
case 'process':
return geo_filter_obfuscate($text, $format);
case 'settings':
return _geo_filter_filter_settings($format);
}
}
function _geo_filter_filter_settings($format) {
$form['geo_filter_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Email link obfuscator filter'),
'#collapsible' => TRUE,
);
$form['geo_filter_filter']["geo_filter_show_not_exists_{$format}"] = array(
'#type' => 'checkbox',
'#title' => t('Display notification on new email'),
'#default_value' => variable_get("geo_filter_show_not_exists_{$format}", FALSE),
'#description' => t('When this is selected, the user will be notified each time they create a mailto: link to an email address that has not been added to the contact form.'),
);
return $form;
}
function geo_filter_obfuscate($text, $format) {
$geo_filter_replace_callback = create_function('$matches', '$hidestring = $matches[1] . GEO_FILTER_AT_SYMBOL . $matches[2] . GEO_FILTER_DOT_SYMBOL . $matches[3]; ' . '$realstring = $matches[1] . "@" . $matches[2] . "." . $matches[3]; ' . '$cid = db_result(db_query("SELECT cid FROM {contact} WHERE category = \'%s\'", array($hidestring))); ' . 'if (!$cid && variable_get("geo_filter_show_not_exists_' . $format . '", false) && user_access("administer site-wide contact form")) { ' . 'drupal_set_message(t("!email does not exist in the contact form. In order to best support non-javascript browsers you should add it !link", array("!email" => $realstring, "!link" => "<a href=\\"" . url("admin/build/contact/add", array("query" => "edit[category]=" . $hidestring . "&edit[recipients]=" . $realstring)) . "\\">here</a>")), "warning"); ' . '} ' . 'return "/contact/" . $matches[1] . "/" . $matches[2] . "/" . $matches[3]; ');
$text = preg_replace_callback("/\"mailto:([a-z0-9._%-]+)\\@([a-z0-9._%-]+)\\.([a-z]{2,4})\"/i", $geo_filter_replace_callback, $text);
$text = preg_replace("/([a-z0-9._%-]+)\\@([a-z0-9._%-]+)\\.([a-z]{2,4})/i", "\$1" . GEO_FILTER_AT_SYMBOL . "\$2" . GEO_FILTER_DOT_SYMBOL . "\$3", $text);
return $text;
}