You are here

function nofollowlist_replace in Nofollow List 5

Same name and namespace in other branches
  1. 6 nofollowlist.module \nofollowlist_replace()

Inserts a nofollow class if the domain passed in is in the blacklist.

Parameters

string $input:

Return value

boolean true if all the hosts are on the whitelist, false if any host is not.

1 string reference to 'nofollowlist_replace'
nofollowlist_filter in ./nofollowlist.module
Implementation of hook_filter. Defines a filter, "Nofollow list filter", that can be used in conjunction with the built in HTML filter to automatically add the rel="nofollow" to links to certain sites

File

./nofollowlist.module, line 119

Code

function nofollowlist_replace($match) {
  $url = parse_url($match[1]);
  $list = preg_split('/\\s+/', variable_get('nofollowlist_hosts', 'en.wikipedia.org'));

  // Default in case it doesn't get changed.
  $link = $match[0];

  // Matches if the list is set as a blacklist and the host is in the list or if
  // the list is set as a whitelist and the host is not found in the list.
  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) {

      // Only add the nofollow if it doesn't already exist.
      if (strpos($match[0], 'rel="') === FALSE) {

        // It is faster to use PHP's string functions then complex regexes
        // In this case we strip off the last > in the <a> and then append our class and close the tag
        $link = substr($match[0], 0, -1);
        $link .= ' rel="nofollow">';
      }
      else {

        // Append outgoing class if this <a> already has a class.
        $link = preg_replace('!rel="([^"]+)"!', 'rel="${1} nofollow"', $match[0]);
      }
    }
  }
  return $link;
}