Nofollow.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // must be called POST validation
  3. /**
  4. * Adds rel="nofollow" to all outbound links. This transform is
  5. * only attached if Attr.Nofollow is TRUE.
  6. */
  7. class HTMLPurifier_AttrTransform_Nofollow extends HTMLPurifier_AttrTransform
  8. {
  9. private $parser;
  10. public function __construct() {
  11. $this->parser = new HTMLPurifier_URIParser();
  12. }
  13. public function transform($attr, $config, $context) {
  14. if (!isset($attr['href'])) {
  15. return $attr;
  16. }
  17. // XXX Kind of inefficient
  18. $url = $this->parser->parse($attr['href']);
  19. $scheme = $url->getSchemeObj($config, $context);
  20. if ($scheme->browsable && !$url->isLocal($config, $context)) {
  21. if (isset($attr['rel'])) {
  22. $rels = explode(' ', $attr['rel']);
  23. if (!in_array('nofollow', $rels)) {
  24. $rels[] = 'nofollow';
  25. }
  26. $attr['rel'] = implode(' ', $rels);
  27. } else {
  28. $attr['rel'] = 'nofollow';
  29. }
  30. }
  31. return $attr;
  32. }
  33. }
  34. // vim: et sw=4 sts=4