File indexing completed on 2024-05-12 06:02:04

0001 <?php
0002 
0003 // must be called POST validation
0004 
0005 /**
0006  * Adds rel="nofollow" to all outbound links.  This transform is
0007  * only attached if Attr.Nofollow is TRUE.
0008  */
0009 class HTMLPurifier_AttrTransform_Nofollow extends HTMLPurifier_AttrTransform
0010 {
0011     /**
0012      * @type HTMLPurifier_URIParser
0013      */
0014     private $parser;
0015 
0016     public function __construct()
0017     {
0018         $this->parser = new HTMLPurifier_URIParser();
0019     }
0020 
0021     /**
0022      * @param array $attr
0023      * @param HTMLPurifier_Config $config
0024      * @param HTMLPurifier_Context $context
0025      * @return array
0026      */
0027     public function transform($attr, $config, $context)
0028     {
0029         if (!isset($attr['href'])) {
0030             return $attr;
0031         }
0032 
0033         // XXX Kind of inefficient
0034         $url = $this->parser->parse($attr['href']);
0035         $scheme = $url->getSchemeObj($config, $context);
0036 
0037         if ($scheme->browsable && !$url->isLocal($config, $context)) {
0038             if (isset($attr['rel'])) {
0039                 $rels = explode(' ', $attr['rel']);
0040                 if (!in_array('nofollow', $rels)) {
0041                     $rels[] = 'nofollow';
0042                 }
0043                 $attr['rel'] = implode(' ', $rels);
0044             } else {
0045                 $attr['rel'] = 'nofollow';
0046             }
0047         }
0048         return $attr;
0049     }
0050 }
0051 
0052 // vim: et sw=4 sts=4