HTMLPurifier_URIParser::parse PHP Method

parse() public method

Parses a URI.
public parse ( $uri ) : HTMLPurifier_URI
$uri string URI to parse
return HTMLPurifier_URI representation of URI. This representation has not been validated yet and may not conform to RFC.
    public function parse($uri)
    {
        $uri = $this->percentEncoder->normalize($uri);
        // Regexp is as per Appendix B.
        // Note that ["<>] are an addition to the RFC's recommended
        // characters, because they represent external delimeters.
        $r_URI = '!' . '(([a-zA-Z0-9\\.\\+\\-]+):)?' . '(//([^/?#"<>]*))?' . '([^?#"<>]*)' . '(\\?([^#"<>]*))?' . '(#([^"<>]*))?' . '!';
        $matches = array();
        $result = preg_match($r_URI, $uri, $matches);
        if (!$result) {
            return false;
        }
        // *really* invalid URI
        // seperate out parts
        $scheme = !empty($matches[1]) ? $matches[2] : null;
        $authority = !empty($matches[3]) ? $matches[4] : null;
        $path = $matches[5];
        // always present, can be empty
        $query = !empty($matches[6]) ? $matches[7] : null;
        $fragment = !empty($matches[8]) ? $matches[9] : null;
        // further parse authority
        if ($authority !== null) {
            $r_authority = "/^((.+?)@)?(\\[[^\\]]+\\]|[^:]*)(:(\\d*))?/";
            $matches = array();
            preg_match($r_authority, $authority, $matches);
            $userinfo = !empty($matches[1]) ? $matches[2] : null;
            $host = !empty($matches[3]) ? $matches[3] : '';
            $port = !empty($matches[4]) ? (int) $matches[5] : null;
        } else {
            $port = $host = $userinfo = null;
        }
        return new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
    }

Usage Example

Beispiel #1
0
 function testURIDefinitionValidation()
 {
     $parser = new HTMLPurifier_URIParser();
     $uri = $parser->parse('http://example.com');
     $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
     generate_mock_once('HTMLPurifier_URIDefinition');
     $uri_def = new HTMLPurifier_URIDefinitionMock();
     $uri_def->expectOnce('filter', array($uri, '*', '*'));
     $uri_def->setReturnValue('filter', true, array($uri, '*', '*'));
     $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
     $uri_def->setReturnValue('postFilter', true, array($uri, '*', '*'));
     $uri_def->setup = true;
     // Since definitions are no longer passed by reference, we need
     // to muck around with the cache to insert our mock. This is
     // technically a little bad, since the cache shouldn't change
     // behavior, but I don't feel too good about letting users
     // overload entire definitions.
     generate_mock_once('HTMLPurifier_DefinitionCache');
     $cache_mock = new HTMLPurifier_DefinitionCacheMock();
     $cache_mock->setReturnValue('get', $uri_def);
     generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
     $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
     $old = HTMLPurifier_DefinitionCacheFactory::instance();
     HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
     $factory_mock->setReturnValue('create', $cache_mock);
     $this->assertDef('http://example.com');
     HTMLPurifier_DefinitionCacheFactory::instance($old);
 }
All Usage Examples Of HTMLPurifier_URIParser::parse
HTMLPurifier_URIParser