URIDefinition.php 3.3 KB

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