function Auth_OpenID_urinorm($uri)
{
    global $__uri_pattern, $__authority_pattern, $__pct_encoded_pattern;
    $uri_matches = array();
    preg_match($__uri_pattern, $uri, $uri_matches);
    if (count($uri_matches) < 9) {
        for ($i = count($uri_matches); $i <= 9; $i++) {
            $uri_matches[] = '';
        }
    }
    $scheme = $uri_matches[2];
    if ($scheme) {
        $scheme = strtolower($scheme);
    }
    $scheme = $uri_matches[2];
    if ($scheme === '') {
        // No scheme specified
        return null;
    }
    $scheme = strtolower($scheme);
    if (!in_array($scheme, array('http', 'https'))) {
        // Not an absolute HTTP or HTTPS URI
        return null;
    }
    $authority = $uri_matches[4];
    if ($authority === '') {
        // Not an absolute URI
        return null;
    }
    $authority_matches = array();
    preg_match($__authority_pattern, $authority, $authority_matches);
    if (count($authority_matches) === 0) {
        // URI does not have a valid authority
        return null;
    }
    if (count($authority_matches) < 4) {
        for ($i = count($authority_matches); $i <= 4; $i++) {
            $authority_matches[] = '';
        }
    }
    list($_whole, $userinfo, $host, $port) = $authority_matches;
    if ($userinfo === null) {
        $userinfo = '';
    }
    if (strpos($host, '%') !== -1) {
        $host = strtolower($host);
        $host = preg_replace_callback($__pct_encoded_pattern, '_pct_encoded_replace', $host);
        // NO IDNA.
        // $host = unicode($host, 'utf-8').encode('idna');
    } else {
        $host = strtolower($host);
    }
    if ($port) {
        if ($port == ':' || $scheme == 'http' && $port == ':80' || $scheme == 'https' && $port == ':443') {
            $port = '';
        }
    } else {
        $port = '';
    }
    $authority = $userinfo . $host . $port;
    $path = $uri_matches[5];
    $path = preg_replace_callback($__pct_encoded_pattern, '_pct_encoded_replace_unreserved', $path);
    $path = remove_dot_segments($path);
    if (!$path) {
        $path = '/';
    }
    $query = $uri_matches[6];
    if ($query === null) {
        $query = '';
    }
    $fragment = $uri_matches[8];
    if ($fragment === null) {
        $fragment = '';
    }
    return $scheme . '://' . $authority . $path . $query . $fragment;
}
Exemple #2
0
function normalize_uri($uri)
{
    // Make sure file URIs have either 0 or 3 slashes after the colon
    if (strtolower(substr($uri, 0, 6)) == 'file:/') {
        $uri = substr($uri, 0, 5) . '///' . substr($uri, 6);
    }
    $u = parse_uri($uri);
    // Convert the scheme name to lowercase
    if (isset($u['scheme'])) {
        $u['scheme'] = strtolower($u['scheme']);
    }
    // Convert the host part to lowercase
    if (isset($u['host'])) {
        $u['host'] = strtolower($u['host']);
    }
    // Remove multiple slashes (it's technically invalid URI syntax anyway)
    $u['path'] = remove_multiple_slashes($u['path']);
    // 6.2.2.3. Path Segment Normalization (of absolute paths)
    $u['path'] = remove_dot_segments($u['path']);
    // 6.2.3. Scheme-Based Normalization
    if (isset($u['scheme'])) {
        if ($u['scheme'] == 'http') {
            if (empty($u['port']) or $u['port'] == 80) {
                unset($u['port']);
            }
            if (empty($u['path'])) {
                $u['path'] = '/';
            }
        } elseif ($u['scheme'] == 'https') {
            if (empty($u['port']) or $u['port'] == 443) {
                unset($u['port']);
            }
            if (empty($u['path'])) {
                $u['path'] = '/';
            }
        } elseif ($u['scheme'] == 'ftp') {
            if (empty($u['port']) or $u['port'] == 21) {
                unset($u['port']);
            }
            if (empty($u['path'])) {
                $u['path'] = '/';
            }
        }
    }
    return unparse_uri($u);
}