URISchemeRegistry.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Registry for retrieving specific URI scheme validator objects.
  4. */
  5. class HTMLPurifier_URISchemeRegistry
  6. {
  7. /**
  8. * Retrieve sole instance of the registry.
  9. * @param $prototype Optional prototype to overload sole instance with,
  10. * or bool true to reset to default registry.
  11. * @note Pass a registry object $prototype with a compatible interface and
  12. * the function will copy it and return it all further times.
  13. */
  14. public static function instance($prototype = null) {
  15. static $instance = null;
  16. if ($prototype !== null) {
  17. $instance = $prototype;
  18. } elseif ($instance === null || $prototype == true) {
  19. $instance = new HTMLPurifier_URISchemeRegistry();
  20. }
  21. return $instance;
  22. }
  23. /**
  24. * Cache of retrieved schemes.
  25. */
  26. protected $schemes = array();
  27. /**
  28. * Retrieves a scheme validator object
  29. * @param $scheme String scheme name like http or mailto
  30. * @param $config HTMLPurifier_Config object
  31. * @param $config HTMLPurifier_Context object
  32. */
  33. public function getScheme($scheme, $config, $context) {
  34. if (!$config) $config = HTMLPurifier_Config::createDefault();
  35. // important, otherwise attacker could include arbitrary file
  36. $allowed_schemes = $config->get('URI.AllowedSchemes');
  37. if (!$config->get('URI.OverrideAllowedSchemes') &&
  38. !isset($allowed_schemes[$scheme])
  39. ) {
  40. return;
  41. }
  42. if (isset($this->schemes[$scheme])) return $this->schemes[$scheme];
  43. if (!isset($allowed_schemes[$scheme])) return;
  44. $class = 'HTMLPurifier_URIScheme_' . $scheme;
  45. if (!class_exists($class)) return;
  46. $this->schemes[$scheme] = new $class();
  47. return $this->schemes[$scheme];
  48. }
  49. /**
  50. * Registers a custom scheme to the cache, bypassing reflection.
  51. * @param $scheme Scheme name
  52. * @param $scheme_obj HTMLPurifier_URIScheme object
  53. */
  54. public function register($scheme, $scheme_obj) {
  55. $this->schemes[$scheme] = $scheme_obj;
  56. }
  57. }
  58. // vim: et sw=4 sts=4