Nofollow.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /**
  10. * @type HTMLPurifier_URIParser
  11. */
  12. private $parser;
  13. public function __construct()
  14. {
  15. $this->parser = new HTMLPurifier_URIParser();
  16. }
  17. /**
  18. * @param array $attr
  19. * @param HTMLPurifier_Config $config
  20. * @param HTMLPurifier_Context $context
  21. * @return array
  22. */
  23. public function transform($attr, $config, $context)
  24. {
  25. if (!isset($attr['href'])) {
  26. return $attr;
  27. }
  28. // XXX Kind of inefficient
  29. $url = $this->parser->parse($attr['href']);
  30. $scheme = $url->getSchemeObj($config, $context);
  31. if ($scheme->browsable && !$url->isLocal($config, $context)) {
  32. if (isset($attr['rel'])) {
  33. $rels = explode(' ', $attr['rel']);
  34. if (!in_array('nofollow', $rels)) {
  35. $rels[] = 'nofollow';
  36. }
  37. $attr['rel'] = implode(' ', $rels);
  38. } else {
  39. $attr['rel'] = 'nofollow';
  40. }
  41. }
  42. return $attr;
  43. }
  44. }
  45. // vim: et sw=4 sts=4