HTMLPurifier_URISchemeRegistry::getScheme PHP Метод

getScheme() публичный Метод

Retrieves a scheme validator object
public getScheme ( string $scheme, HTMLPurifier_Config $config, HTMLPurifier_Context $context ) : HTMLPurifier_URIScheme
$scheme string String scheme name like http or mailto
$config HTMLPurifier_Config
$context HTMLPurifier_Context
Результат HTMLPurifier_URIScheme
    public function getScheme($scheme, $config, $context)
    {
        if (!$config) {
            $config = HTMLPurifier_Config::createDefault();
        }
        // important, otherwise attacker could include arbitrary file
        $allowed_schemes = $config->get('URI.AllowedSchemes');
        if (!$config->get('URI.OverrideAllowedSchemes') && !isset($allowed_schemes[$scheme])) {
            return;
        }
        if (isset($this->schemes[$scheme])) {
            return $this->schemes[$scheme];
        }
        if (!isset($allowed_schemes[$scheme])) {
            return;
        }
        $class = 'HTMLPurifier_URIScheme_' . $scheme;
        if (!class_exists($class)) {
            return;
        }
        $this->schemes[$scheme] = new $class();
        return $this->schemes[$scheme];
    }

Usage Example

 function test()
 {
     generate_mock_once('HTMLPurifier_URIScheme');
     $config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => true));
     $context = new HTMLPurifier_Context();
     $registry = new HTMLPurifier_URISchemeRegistry();
     $this->assertIsA($registry->getScheme('http', $config, $context), 'HTMLPurifier_URIScheme_http');
     $scheme_http = new HTMLPurifier_URISchemeMock();
     $scheme_telnet = new HTMLPurifier_URISchemeMock();
     $scheme_foobar = new HTMLPurifier_URISchemeMock();
     // register a new scheme
     $registry->register('telnet', $scheme_telnet);
     $this->assertIdentical($registry->getScheme('telnet', $config, $context), $scheme_telnet);
     // overload a scheme, this is FINAL (forget about defaults)
     $registry->register('http', $scheme_http);
     $this->assertIdentical($registry->getScheme('http', $config, $context), $scheme_http);
     // when we register a scheme, it's automatically allowed
     $registry->register('foobar', $scheme_foobar);
     $this->assertIdentical($registry->getScheme('foobar', $config, $context), $scheme_foobar);
     // now, test when overriding is not allowed
     $config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => false));
     $this->assertNull($registry->getScheme('foobar', $config, $context));
     // scheme not allowed and never registered
     $this->assertNull($registry->getScheme('ftp', $config, $context));
 }
HTMLPurifier_URISchemeRegistry