URIDefinition.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. {
  22. $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal());
  23. $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources());
  24. $this->registerFilter(new HTMLPurifier_URIFilter_DisableResources());
  25. $this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist());
  26. $this->registerFilter(new HTMLPurifier_URIFilter_SafeIframe());
  27. $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
  28. $this->registerFilter(new HTMLPurifier_URIFilter_Munge());
  29. }
  30. public function registerFilter($filter)
  31. {
  32. $this->registeredFilters[$filter->name] = $filter;
  33. }
  34. public function addFilter($filter, $config)
  35. {
  36. $r = $filter->prepare($config);
  37. if ($r === false) return; // null is ok, for backwards compat
  38. if ($filter->post) {
  39. $this->postFilters[$filter->name] = $filter;
  40. } else {
  41. $this->filters[$filter->name] = $filter;
  42. }
  43. }
  44. protected function doSetup($config)
  45. {
  46. $this->setupMemberVariables($config);
  47. $this->setupFilters($config);
  48. }
  49. protected function setupFilters($config)
  50. {
  51. foreach ($this->registeredFilters as $name => $filter) {
  52. if ($filter->always_load) {
  53. $this->addFilter($filter, $config);
  54. } else {
  55. $conf = $config->get('URI.' . $name);
  56. if ($conf !== false && $conf !== null) {
  57. $this->addFilter($filter, $config);
  58. }
  59. }
  60. }
  61. unset($this->registeredFilters);
  62. }
  63. protected function setupMemberVariables($config)
  64. {
  65. $this->host = $config->get('URI.Host');
  66. $base_uri = $config->get('URI.Base');
  67. if (!is_null($base_uri)) {
  68. $parser = new HTMLPurifier_URIParser();
  69. $this->base = $parser->parse($base_uri);
  70. $this->defaultScheme = $this->base->scheme;
  71. if (is_null($this->host)) $this->host = $this->base->host;
  72. }
  73. if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
  74. }
  75. public function getDefaultScheme($config, $context)
  76. {
  77. return HTMLPurifier_URISchemeRegistry::instance()->getScheme($this->defaultScheme, $config, $context);
  78. }
  79. public function filter(&$uri, $config, $context)
  80. {
  81. foreach ($this->filters as $name => $f) {
  82. $result = $f->filter($uri, $config, $context);
  83. if (!$result) return false;
  84. }
  85. return true;
  86. }
  87. public function postFilter(&$uri, $config, $context)
  88. {
  89. foreach ($this->postFilters as $name => $f) {
  90. $result = $f->filter($uri, $config, $context);
  91. if (!$result) return false;
  92. }
  93. return true;
  94. }
  95. }
  96. // vim: et sw=4 sts=4