URI.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Validates a URI as defined by RFC 3986.
  4. * @note Scheme-specific mechanics deferred to HTMLPurifier_URIScheme
  5. */
  6. class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
  7. {
  8. protected $parser;
  9. protected $embedsResource;
  10. /**
  11. * @param $embeds_resource_resource Does the URI here result in an extra HTTP request?
  12. */
  13. public function __construct($embeds_resource = false) {
  14. $this->parser = new HTMLPurifier_URIParser();
  15. $this->embedsResource = (bool) $embeds_resource;
  16. }
  17. public function make($string) {
  18. $embeds = (bool) $string;
  19. return new HTMLPurifier_AttrDef_URI($embeds);
  20. }
  21. public function validate($uri, $config, $context) {
  22. if ($config->get('URI.Disable')) return false;
  23. $uri = $this->parseCDATA($uri);
  24. // parse the URI
  25. $uri = $this->parser->parse($uri);
  26. if ($uri === false) return false;
  27. // add embedded flag to context for validators
  28. $context->register('EmbeddedURI', $this->embedsResource);
  29. $ok = false;
  30. do {
  31. // generic validation
  32. $result = $uri->validate($config, $context);
  33. if (!$result) break;
  34. // chained filtering
  35. $uri_def = $config->getDefinition('URI');
  36. $result = $uri_def->filter($uri, $config, $context);
  37. if (!$result) break;
  38. // scheme-specific validation
  39. $scheme_obj = $uri->getSchemeObj($config, $context);
  40. if (!$scheme_obj) break;
  41. if ($this->embedsResource && !$scheme_obj->browsable) break;
  42. $result = $scheme_obj->validate($uri, $config, $context);
  43. if (!$result) break;
  44. // Post chained filtering
  45. $result = $uri_def->postFilter($uri, $config, $context);
  46. if (!$result) break;
  47. // survived gauntlet
  48. $ok = true;
  49. } while (false);
  50. $context->destroy('EmbeddedURI');
  51. if (!$ok) return false;
  52. // back to string
  53. return $uri->toString();
  54. }
  55. }
  56. // vim: et sw=4 sts=4