HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/AttrTransform/Nofollow.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // must be called POST validation
00004 
00009 class HTMLPurifier_AttrTransform_Nofollow extends HTMLPurifier_AttrTransform
00010 {
00011     private $parser;
00012 
00013     public function __construct() {
00014         $this->parser = new HTMLPurifier_URIParser();
00015     }
00016 
00017     public function transform($attr, $config, $context) {
00018 
00019         if (!isset($attr['href'])) {
00020             return $attr;
00021         }
00022 
00023         // XXX Kind of inefficient
00024         $url = $this->parser->parse($attr['href']);
00025         $scheme = $url->getSchemeObj($config, $context);
00026 
00027         if ($scheme->browsable && !$url->isLocal($config, $context)) {
00028             if (isset($attr['rel'])) {
00029                 $rels = explode(' ', $attr);
00030                 if (!in_array('nofollow', $rels)) {
00031                     $rels[] = 'nofollow';
00032                 }
00033                 $attr['rel'] = implode(' ', $rels);
00034             } else {
00035                 $attr['rel'] = 'nofollow';
00036             }
00037         }
00038 
00039         return $attr;
00040 
00041     }
00042 
00043 }
00044 
00045 // vim: et sw=4 sts=4