Don't add nofollow for matching hosts, generalize this code.
[htmlpurifier.git] / library / HTMLPurifier / AttrTransform / Nofollow.php
blobf7fb1209b3be74ddbd3e03f7bcec85a35abe0646
1 <?php
3 // must be called POST validation
5 /**
6 * Adds rel="nofollow" to all outbound links. This transform is
7 * only attached if Attr.Nofollow is TRUE.
8 */
9 class HTMLPurifier_AttrTransform_Nofollow extends HTMLPurifier_AttrTransform
11 private $parser;
13 public function __construct() {
14 $this->parser = new HTMLPurifier_URIParser();
17 public function transform($attr, $config, $context) {
19 if (!isset($attr['href'])) {
20 return $attr;
23 // XXX Kind of inefficient
24 $url = $this->parser->parse($attr['href']);
25 $scheme = $url->getSchemeObj($config, $context);
27 if ($scheme->browsable && !$url->isLocal($config, $context)) {
28 if (isset($attr['rel'])) {
29 $rels = explode(' ', $attr);
30 if (!in_array('nofollow', $rels)) {
31 $rels[] = 'nofollow';
33 $attr['rel'] = implode(' ', $rels);
34 } else {
35 $attr['rel'] = 'nofollow';
39 return $attr;
45 // vim: et sw=4 sts=4