View source
<?php
function nofollowlist_menu($may_cache) {
$items[] = array(
'path' => 'admin/settings/nofollowlist',
'title' => t('Nofollowlist'),
'description' => t('Add sites to the nofollowlist and determine whether links to those sites are followable or not.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'nofollowlist_settings_form',
),
'access' => user_access('administer nofollowlist'),
);
return $items;
}
function nofollowlist_perm() {
return array(
'administer nofollowlist',
);
}
function nofollowlist_settings_form() {
$form['nofollowlist_option'] = array(
'#type' => 'radios',
'#title' => t('Hosts list option'),
'#description' => t('If you choose the whitelist option, be sure to add your own site to the list!'),
'#options' => array(
'black' => t('Blacklist: Add rel="nofollow" to links leading to the listed hosts.'),
'white' => t('Whitelist: Add rel="nofollow" to all links <b>except</b> the listed hosts.'),
),
'#default_value' => variable_get('nofollowlist_option', 'black'),
);
$form['nofollowlist_hosts'] = array(
'#type' => 'textarea',
'#title' => t('Nofollowlist hosts'),
'#description' => t('Add one host per line. Ex: en.wikipedia.org'),
'#default_value' => variable_get('nofollowlist_hosts', 'en.wikipedia.org'),
);
return system_settings_form($form);
}
function nofollowlist_help($section) {
switch ($section) {
case 'admin/help#nofollowlist':
$output = '<p>' . t('This module implements a simple filter to add the nofollow tag to sites that are on your blacklist or to all sites except those on your whitelist.') . '</p>';
return $output;
}
}
function nofollowlist_filter_tips($delta, $format, $long = false) {
$output .= '<p>' . t('Links to specified hosts will have a rel="nofollow" added to them.') . "</p>\n";
return $output;
}
function nofollowlist_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Nofollow list filter'),
);
case 'description':
return t('Links to specified hosts will have a rel="nofollow" added to them.');
case "process":
$text = preg_replace_callback('!<a.*?href="([^"]+)".*?>!', 'nofollowlist_replace', $text);
return $text;
default:
return $text;
}
}
function nofollowlist_replace($match) {
$url = parse_url($match[1]);
$list = preg_split('/\\s+/', variable_get('nofollowlist_hosts', 'en.wikipedia.org'));
$link = $match[0];
if (variable_get('nofollowlist_option', 'black') == 'black' && in_array($url['host'], $list) || variable_get('nofollowlist_option', 'black') == 'white' && !in_array($url['host'], $list)) {
if (strpos($match[0], 'nofollow') === FALSE) {
if (strpos($match[0], 'rel="') === FALSE) {
$link = substr($match[0], 0, -1);
$link .= ' rel="nofollow">';
}
else {
$link = preg_replace('!rel="([^"]+)"!', 'rel="${1} nofollow"', $match[0]);
}
}
}
return $link;
}