friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/URISchemeRegistry.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
/**
* Registry for retrieving specific URI scheme validator objects.
*/
class HTMLPurifier_URISchemeRegistry
{
/**
* Retrieve sole instance of the registry.
2016-02-09 10:06:17 +00:00
* @param HTMLPurifier_URISchemeRegistry $prototype Optional prototype to overload sole instance with,
2010-09-09 03:14:17 +00:00
* or bool true to reset to default registry.
2016-02-09 10:06:17 +00:00
* @return HTMLPurifier_URISchemeRegistry
2010-09-09 03:14:17 +00:00
* @note Pass a registry object $prototype with a compatible interface and
* the function will copy it and return it all further times.
*/
2016-02-09 10:06:17 +00:00
public static function instance($prototype = null)
{
2010-09-09 03:14:17 +00:00
static $instance = null;
if ($prototype !== null) {
$instance = $prototype;
} elseif ($instance === null || $prototype == true) {
$instance = new HTMLPurifier_URISchemeRegistry();
}
return $instance;
}
/**
* Cache of retrieved schemes.
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_URIScheme[]
2010-09-09 03:14:17 +00:00
*/
protected $schemes = array();
/**
* Retrieves a scheme validator object
2016-02-09 10:06:17 +00:00
* @param string $scheme String scheme name like http or mailto
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return HTMLPurifier_URIScheme
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function getScheme($scheme, $config, $context)
{
if (!$config) {
$config = HTMLPurifier_Config::createDefault();
}
2010-09-09 03:14:17 +00:00
// important, otherwise attacker could include arbitrary file
$allowed_schemes = $config->get('URI.AllowedSchemes');
if (!$config->get('URI.OverrideAllowedSchemes') &&
!isset($allowed_schemes[$scheme])
) {
return;
}
2016-02-09 10:06:17 +00:00
if (isset($this->schemes[$scheme])) {
return $this->schemes[$scheme];
}
if (!isset($allowed_schemes[$scheme])) {
return;
}
2010-09-09 03:14:17 +00:00
$class = 'HTMLPurifier_URIScheme_' . $scheme;
2016-02-09 10:06:17 +00:00
if (!class_exists($class)) {
return;
}
2010-09-09 03:14:17 +00:00
$this->schemes[$scheme] = new $class();
return $this->schemes[$scheme];
}
/**
* Registers a custom scheme to the cache, bypassing reflection.
2016-02-09 10:06:17 +00:00
* @param string $scheme Scheme name
* @param HTMLPurifier_URIScheme $scheme_obj
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function register($scheme, $scheme_obj)
{
2010-09-09 03:14:17 +00:00
$this->schemes[$scheme] = $scheme_obj;
}
}
// vim: et sw=4 sts=4