Location: PHPKode > projects > SEO Filter > seofilter-1.2.3/src/seofilter/plugins/outgoing_links/outgoing_links.plugin.php
<?php

/**
 * outgoing_links.plugin.php
 *
 * Scans the buffer and if it finds external links
 * adds rel="nofollow" attribute to avoid page rank leaking.
 *
 * @author   Svetoslav Marinov <hide@address.com>
 * @version  1.0
 * @package  outgoing_links
 */

class seofilter_plugin_outgoing_links extends seofilter_plugin {

    var $version = 1.0;

    /**
     * Contructor
     * Plugin can be executed as
     * PRE, POST, INPUT, OUTPUT Filter
     *
     * @param   void
     * @access  public
     */
    function seofilter_plugin_outgoing_links() {
    }

    /**
     * $event can be executed
     * PRE, POST, INPUT, OUTPUT Filter
     *
     * @param   constant
     * @access  public
     * @return  bool
     */
    function execute($event)
    {
      if ($event == SEOFILTER_EVENT_PRE) {

        $params = $this->params();
        $buffer = $this->content();

        if (false !== strpos($buffer, 'href')) {

          // Mozilla's Web Developer Extension makes nofollow links
          // with red background
          $normalize_color = '';
          if (!empty($params['normalize_nofollow_links'])) {
            $normalize_color = 'style="background-color:inherit !important;"';
          }

          if (!empty($params['white_list'])) {
            $white_list_links = (array) ($params['white_list']);
            //$white_list_links     = explode  ('|', $params['white_list']);
            //$white_list_links     = array_map('trim', $white_list_links);
            //$white_list_links_ar  = array_map('preg_quote', $white_list_links);
            // those links will be skipped from nofollow attribute somehow ;)

            foreach ($white_list_links as $key => $value) {
              $white_list_links[$key] = preg_replace("#https?\://(?:www\d*\.)?#si", "", $white_list_links[$key]);
              $white_list_links[$key] = preg_replace("#\/*$#si", "", $white_list_links[$key]);
              // Prepare for regex.
              $white_list_links[$key] = preg_quote($white_list_links[$key]);
            }

            $white_list_ready4regex = '(' . join('|', $white_list_links) . ')';
//seofilter_info("#(<a )([^>]*?href=[\"\'\s]*https?\:\/\/(?!{$white_list_ready4regex})[^>]*>)#si", '1');
            // Replace links starting with http or https not followed by *white list* domains.
            $buffer = preg_replace("#(<a )([^>]*?href=[\"\'\s]*https?\:\/\/(?!{$white_list_ready4regex})[^>]*>)#si",
                  '\\1' . $normalize_color . ' rel="nofollow" ' . '\\2', $buffer);
          } else {
            // all outbound links will be replaced now
              $buffer = preg_replace("#(<a )([^>]*?href=[\"\'\s]*https?://[^>]*>)#si",
                  '\\1' . $normalize_color . ' rel="nofollow" ' . '\\2', $buffer);
          }

          $this->status(SEOFILTER_STATUS_CONTENT_CHANGED);
          $this->content($buffer);
        }
      }

      return true;
    }
}

?>
Return current item: SEO Filter