TargetBlank.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // must be called POST validation
  3. /**
  4. * Adds target="blank" to all outbound links. This transform is
  5. * only attached if Attr.TargetBlank is TRUE. This works regardless
  6. * of whether or not Attr.AllowedFrameTargets
  7. */
  8. class HTMLPurifier_AttrTransform_TargetBlank extends HTMLPurifier_AttrTransform
  9. {
  10. /**
  11. * @type HTMLPurifier_URIParser
  12. */
  13. private $parser;
  14. public function __construct()
  15. {
  16. $this->parser = new HTMLPurifier_URIParser();
  17. }
  18. /**
  19. * @param array $attr
  20. * @param HTMLPurifier_Config $config
  21. * @param HTMLPurifier_Context $context
  22. * @return array
  23. */
  24. public function transform($attr, $config, $context)
  25. {
  26. if (!isset($attr['href'])) {
  27. return $attr;
  28. }
  29. // XXX Kind of inefficient
  30. $url = $this->parser->parse($attr['href']);
  31. $scheme = $url->getSchemeObj($config, $context);
  32. if ($scheme->browsable && !$url->isBenign($config, $context)) {
  33. $attr['target'] = '_blank';
  34. }
  35. return $attr;
  36. }
  37. }
  38. // vim: et sw=4 sts=4