ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PathHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
28trait PathHelper
29{
30 protected function isPathIgnored(string $path, Options $options): bool
31 {
32 $regex = '(' . implode('|', $options->getIgnoredPathSnippets()) . ')';
33 return preg_match($regex, $path) > 0;
34 }
35
36 protected function ensureDirectorySeperator(string $path): string
37 {
38 return rtrim($path, "/") . "/";
39 }
40
44 private function realpath(string $path): string
45 {
46 $path = array_reduce(explode('/', $path), function ($a, $b) {
47 if ($a === null) {
48 $a = "/";
49 }
50 if ($b === "" || $b === ".") {
51 return $a;
52 }
53 if ($b === "..") {
54 return dirname($a);
55 }
56
57 return preg_replace("/\/+/", "/", "$a/$b");
58 });
59 return trim((string) $path, "/");
60 }
61
62 protected function normalizePath($path, $separator = '\\/'): string
63 {
64 // if the ZIP name ist just a ../name.zip we stop here since it's hard to generate to real path for that.
65 // consumers must adopt their code
66 if (str_starts_with((string) $path, '..')) {
67 throw new \InvalidArgumentException('The ZIP name must not start with ../. Please provide a real path for the output file.');
68 }
69
70 // we prepend a ./ to paths without a leading slash to make sure that the path is relative
71 if (!str_starts_with((string) $path, './')) {
72 $path = './' . $path;
73 }
74
75 if (str_starts_with((string) $path, './') && ($realpath = realpath($path)) !== false) {
76 $path = $realpath;
77 }
78
79 $normalized = preg_replace('#\p{C}+|^\./#u', '', (string) $path);
80 $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', (string) $normalized);
81 $regex = '#\/*[^/\.]+/\.\.#Uu';
82
83 while (preg_match($regex, (string) $normalized)) {
84 $normalized = preg_replace($regex, '', (string) $normalized);
85 }
86
87 if (preg_match('#/\.{2}|\.{2}/#', (string) $normalized)) {
88 throw new \LogicException(
89 'Path is outside of the defined root, path: [' . $path . '], resolved: [' . $normalized . ']'
90 );
91 }
92
93 // We prepend a ./ to paths without a leading slash to make sure that the path is relative
94 if (!str_starts_with((string) $normalized, './') && !str_starts_with((string) $normalized, '/')) {
95 $normalized = './' . $normalized;
96 }
97
98 // Check if this is a path we can use
99 LegacyPathHelper::deriveLocationFrom($normalized); // Throws exception if path is invalid
100
101 return $normalized;
102 }
103}
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
static deriveLocationFrom(string $absolute_path)
$path
Definition: ltiservices.php:30
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples