]> git.wh0rd.org - tt-rss.git/blob - lib/htmlpurifier/library/HTMLPurifier/AttrTransform/Nofollow.php
573b42c9c5a1b73abc09caef1e0bbf79f7e1daa6
[tt-rss.git] / lib / htmlpurifier / library / HTMLPurifier / AttrTransform / Nofollow.php
1 <?php
2
3 // must be called POST validation
4
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
10 {
11 private $parser;
12
13 public function __construct() {
14 $this->parser = new HTMLPurifier_URIParser();
15 }
16
17 public function transform($attr, $config, $context) {
18
19 if (!isset($attr['href'])) {
20 return $attr;
21 }
22
23 // XXX Kind of inefficient
24 $url = $this->parser->parse($attr['href']);
25 $scheme = $url->getSchemeObj($config, $context);
26
27 if (!is_null($url->host) && $scheme !== false && $scheme->browsable) {
28 if (isset($attr['rel'])) {
29 $attr['rel'] .= ' nofollow';
30 } else {
31 $attr['rel'] = 'nofollow';
32 }
33 }
34
35 return $attr;
36
37 }
38
39 }
40
41 // vim: et sw=4 sts=4