Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 9... Línea 9...
9
/**
9
/**
10
 * Resolves a URI reference in the context of a base URI and the opposite way.
10
 * Resolves a URI reference in the context of a base URI and the opposite way.
11
 *
11
 *
12
 * @author Tobias Schultze
12
 * @author Tobias Schultze
13
 *
13
 *
14
 * @link https://tools.ietf.org/html/rfc3986#section-5
14
 * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5
15
 */
15
 */
16
final class UriResolver
16
final class UriResolver
17
{
17
{
18
    /**
18
    /**
19
     * Removes dot segments from a path and returns the new path.
19
     * Removes dot segments from a path and returns the new path.
20
     *
20
     *
21
     * @link http://tools.ietf.org/html/rfc3986#section-5.2.4
21
     * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
22
     */
22
     */
23
    public static function removeDotSegments(string $path): string
23
    public static function removeDotSegments(string $path): string
24
    {
24
    {
25
        if ($path === '' || $path === '/') {
25
        if ($path === '' || $path === '/') {
26
            return $path;
26
            return $path;
Línea 38... Línea 38...
38
 
38
 
Línea 39... Línea 39...
39
        $newPath = implode('/', $results);
39
        $newPath = implode('/', $results);
40
 
40
 
41
        if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
41
        if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
42
            // Re-add the leading slash if necessary for cases like "/.."
42
            // Re-add the leading slash if necessary for cases like "/.."
43
            $newPath = '/' . $newPath;
43
            $newPath = '/'.$newPath;
44
        } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
44
        } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
45
            // Add the trailing slash if necessary
45
            // Add the trailing slash if necessary
46
            // If newPath is not empty, then $segment must be set and is the last segment from the foreach
46
            // If newPath is not empty, then $segment must be set and is the last segment from the foreach
Línea 51... Línea 51...
51
    }
51
    }
Línea 52... Línea 52...
52
 
52
 
53
    /**
53
    /**
54
     * Converts the relative URI into a new URI that is resolved against the base URI.
54
     * Converts the relative URI into a new URI that is resolved against the base URI.
55
     *
55
     *
56
     * @link http://tools.ietf.org/html/rfc3986#section-5.2
56
     * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2
57
     */
57
     */
58
    public static function resolve(UriInterface $base, UriInterface $rel): UriInterface
58
    public static function resolve(UriInterface $base, UriInterface $rel): UriInterface
59
    {
59
    {
60
        if ((string) $rel === '') {
60
        if ((string) $rel === '') {
Línea 78... Línea 78...
78
            } else {
78
            } else {
79
                if ($rel->getPath()[0] === '/') {
79
                if ($rel->getPath()[0] === '/') {
80
                    $targetPath = $rel->getPath();
80
                    $targetPath = $rel->getPath();
81
                } else {
81
                } else {
82
                    if ($targetAuthority != '' && $base->getPath() === '') {
82
                    if ($targetAuthority != '' && $base->getPath() === '') {
83
                        $targetPath = '/' . $rel->getPath();
83
                        $targetPath = '/'.$rel->getPath();
84
                    } else {
84
                    } else {
85
                        $lastSlashPos = strrpos($base->getPath(), '/');
85
                        $lastSlashPos = strrpos($base->getPath(), '/');
86
                        if ($lastSlashPos === false) {
86
                        if ($lastSlashPos === false) {
87
                            $targetPath = $rel->getPath();
87
                            $targetPath = $rel->getPath();
88
                        } else {
88
                        } else {
89
                            $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath();
89
                            $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1).$rel->getPath();
90
                        }
90
                        }
91
                    }
91
                    }
92
                }
92
                }
93
                $targetPath = self::removeDotSegments($targetPath);
93
                $targetPath = self::removeDotSegments($targetPath);
94
                $targetQuery = $rel->getQuery();
94
                $targetQuery = $rel->getQuery();
Línea 125... Línea 125...
125
     *
125
     *
126
     *    echo UriResolver::relativize($base, new Uri('/a/b/c'));  // prints 'c' as well
126
     *    echo UriResolver::relativize($base, new Uri('/a/b/c'));  // prints 'c' as well
127
     */
127
     */
128
    public static function relativize(UriInterface $base, UriInterface $target): UriInterface
128
    public static function relativize(UriInterface $base, UriInterface $target): UriInterface
129
    {
129
    {
130
        if ($target->getScheme() !== '' &&
130
        if ($target->getScheme() !== ''
131
            ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
131
            && ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
132
        ) {
132
        ) {
133
            return $target;
133
            return $target;
134
        }
134
        }
Línea 135... Línea 135...
135
 
135
 
Línea 183... Línea 183...
183
            } else {
183
            } else {
184
                break;
184
                break;
185
            }
185
            }
186
        }
186
        }
187
        $targetSegments[] = $targetLastSegment;
187
        $targetSegments[] = $targetLastSegment;
188
        $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
188
        $relativePath = str_repeat('../', count($sourceSegments)).implode('/', $targetSegments);
Línea 189... Línea 189...
189
 
189
 
190
        // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
190
        // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
191
        // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
191
        // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
192
        // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
192
        // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.