View source  
  <?php
function nofollowlist_menu() {
  $items['admin/settings/nofollowlist'] = array(
    'title' => 'Nofollowlist',
    'description' => 'Add sites to the nofollowlist and determine whether links to those sites are followable or not.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'nofollowlist_settings_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      '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($path, $arg) {
  switch ($path) {
    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 (empty($url['host'])) {
    $url['host'] = parse_url($GLOBALS['base_url'], PHP_URL_HOST);
  }
  
  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;
}