URIDefinition.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class HTMLPurifier_URIDefinition extends HTMLPurifier_Definition
  3. {
  4. public $type = 'URI';
  5. protected $filters = array();
  6. protected $postFilters = array();
  7. protected $registeredFilters = array();
  8. /**
  9. * HTMLPurifier_URI object of the base specified at %URI.Base
  10. */
  11. public $base;
  12. /**
  13. * String host to consider "home" base, derived off of $base
  14. */
  15. public $host;
  16. /**
  17. * Name of default scheme based on %URI.DefaultScheme and %URI.Base
  18. */
  19. public $defaultScheme;
  20. public function __construct() {
  21. $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal());
  22. $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources());
  23. $this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist());
  24. $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
  25. $this->registerFilter(new HTMLPurifier_URIFilter_Munge());
  26. }
  27. public function registerFilter($filter) {
  28. $this->registeredFilters[$filter->name] = $filter;
  29. }
  30. public function addFilter($filter, $config) {
  31. $r = $filter->prepare($config);
  32. if ($r === false) return; // null is ok, for backwards compat
  33. if ($filter->post) {
  34. $this->postFilters[$filter->name] = $filter;
  35. } else {
  36. $this->filters[$filter->name] = $filter;
  37. }
  38. }
  39. protected function doSetup($config) {
  40. $this->setupMemberVariables($config);
  41. $this->setupFilters($config);
  42. }
  43. protected function setupFilters($config) {
  44. foreach ($this->registeredFilters as $name => $filter) {
  45. $conf = $config->get('URI.' . $name);
  46. if ($conf !== false && $conf !== null) {
  47. $this->addFilter($filter, $config);
  48. }
  49. }
  50. unset($this->registeredFilters);
  51. }
  52. protected function setupMemberVariables($config) {
  53. $this->host = $config->get('URI.Host');
  54. $base_uri = $config->get('URI.Base');
  55. if (!is_null($base_uri)) {
  56. $parser = new HTMLPurifier_URIParser();
  57. $this->base = $parser->parse($base_uri);
  58. $this->defaultScheme = $this->base->scheme;
  59. if (is_null($this->host)) $this->host = $this->base->host;
  60. }
  61. if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
  62. }
  63. public function filter(&$uri, $config, $context) {
  64. foreach ($this->filters as $name => $f) {
  65. $result = $f->filter($uri, $config, $context);
  66. if (!$result) return false;
  67. }
  68. return true;
  69. }
  70. public function postFilter(&$uri, $config, $context) {
  71. foreach ($this->postFilters as $name => $f) {
  72. $result = $f->filter($uri, $config, $context);
  73. if (!$result) return false;
  74. }
  75. return true;
  76. }
  77. }
  78. // vim: et sw=4 sts=4