1313/**
1414 * @author Arthur Gribet <a.gribet@gmail.com>
1515 */
16- class FileManager {
16+ class FileManager
17+ {
1718 const VIEW_THUMBNAIL = 'thumbnail ' ;
1819 const VIEW_LIST = 'list ' ;
1920
2021 /**
2122 * FileManager constructor.
2223 */
23- public function __construct (private array $ queryParameters , private array $ configuration , private RouterInterface $ router , private EventDispatcherInterface $ dispatcher , private string $ webDir ) {
24+ public function __construct (private array $ queryParameters , private array $ configuration , private RouterInterface $ router , private EventDispatcherInterface $ dispatcher , private string $ webDir )
25+ {
2426 // Check Security
2527 $ this ->checkSecurity ();
2628 }
2729
28- public function getDirName (): string {
30+ public function getDirName (): string
31+ {
2932 return \dirname ($ this ->getBasePath ());
3033 }
3134
32- public function getBaseName (): string {
35+ public function getBaseName (): string
36+ {
3337 return basename ($ this ->getBasePath ());
3438 }
3539
36- public function getRegex (): string {
40+ public function getRegex (): string
41+ {
3742 if (isset ($ this ->configuration ['regex ' ])) {
38- return '/ ' . $ this ->configuration ['regex ' ]. '/i ' ;
43+ return '/ ' . $ this ->configuration ['regex ' ] . '/i ' ;
3944 }
4045
4146 return match ($ this ->getType ()) {
@@ -45,28 +50,29 @@ public function getRegex(): string {
4550 };
4651 }
4752
48- public function getCurrentRoute (): ?string {
49- if ($ this ->getRoute ()) {
50- return urldecode ($ this ->getRoute ());
51- }
52-
53- return null ;
54- }
53+ // public function getCurrentRoute(): ?string {
54+ // if ($this->getRoute()) {
55+ // return urldecode($this->getRoute());
56+ // }
57+ //
58+ // return null;
59+ // }
5560
56- public function getCurrentPath (): bool |string {
57- return realpath ($ this ->getBasePath ().$ this ->getCurrentRoute ());
61+ public function getCurrentPath (): bool |string
62+ {
63+ return realpath ($ this ->getBasePath () . $ this ->getRoute ());
5864 }
5965
6066 // parent url
61- public function getParent (): ?string {
67+ public function getParent (): ?string
68+ {
6269 $ queryParentParameters = $ this ->queryParameters ;
6370
64- if ($ this ->getCurrentRoute ()) {
65-
66- $ parentRoute = \dirname ($ this ->getCurrentRoute ());
71+ if ($ this ->getRoute ()) {
6772
73+ $ parentRoute = \dirname ($ this ->getRoute ());
6874 if (\DIRECTORY_SEPARATOR !== $ parentRoute ) {
69- $ queryParentParameters ['route ' ] = \dirname ($ this ->getCurrentRoute ());
75+ $ queryParentParameters ['route ' ] = \dirname ($ this ->getRoute ());
7076 } else {
7177 unset($ queryParentParameters ['route ' ]);
7278 }
@@ -79,28 +85,33 @@ public function getParent(): ?string {
7985 return null ;
8086 }
8187
82- public function getImagePath (): bool |string {
88+ public function getImagePath (): bool |string
89+ {
8390 $ baseUrl = $ this ->getBaseUrl ();
91+
8492 if ($ baseUrl ) {
85- return $ baseUrl .$ this ->getCurrentRoute ().'/ ' ;
93+ $ routePath = $ this ->getRoutePath ();
94+ return $ baseUrl . $ routePath . '/ ' ;
8695 }
8796
8897 return false ;
8998 }
9099
91- private function getBaseUrl (): bool |string {
100+ private function getBaseUrl (): bool |string
101+ {
92102 $ webPath = $ this ->webDir ;
93103 $ dirl = new \SplFileInfo ($ this ->getConfiguration ()['dir ' ]);
94104 $ base = $ dirl ->getPathname ();
95105
96- if (0 === mb_strpos ($ base , $ webPath )) {
106+ if (str_starts_with ($ base , $ webPath )) {
97107 return mb_substr ($ base , mb_strlen ($ webPath ));
98108 }
99109
100110 return false ;
101111 }
102112
103- private function checkSecurity (): void {
113+ private function checkSecurity (): void
114+ {
104115 if (!isset ($ this ->configuration ['dir ' ])) {
105116 throw new HttpException (Response::HTTP_INTERNAL_SERVER_ERROR , 'Please define a "dir" parameter in your config.yml ' );
106117 }
@@ -115,67 +126,86 @@ private function checkSecurity(): void {
115126 $ currentPath = $ this ->getCurrentPath ();
116127
117128 // check Path security
118- if (false === $ currentPath || 0 !== mb_strpos ($ currentPath , $ this ->getBasePath ())) {
129+ if (false === $ currentPath || ! str_starts_with ($ currentPath , $ this ->getBasePath ())) {
119130 throw new HttpException (Response::HTTP_UNAUTHORIZED , 'You are not allowed to access this folder. ' );
120131 }
121132 $ event = new GenericEvent ($ this , ['path ' => $ currentPath ]);
122133 $ this ->dispatcher ->dispatch ($ event , FileManagerEvents::POST_CHECK_SECURITY );
123134
124135 }
125136
126- public function getModule (): ?string {
137+ public function getModule (): ?string
138+ {
127139 return $ this ->getQueryParameters ()['module ' ] ?? null ;
128140 }
129141
130- public function getType (): ?string {
142+ public function getType (): ?string
143+ {
131144 return $ this ->mergeConfAndQuery ('type ' );
132145 }
133146
134- public function getRoute (): ?string {
147+ public function getRoute (): ?string
148+ {
135149 return isset ($ this ->getQueryParameters ()['route ' ]) && '/ ' !== $ this ->getQueryParameters ()['route ' ] ? $ this ->getQueryParameters ()['route ' ] : null ;
136150 }
137151
138- public function getBasePath (): bool |string {
152+ public function getRoutePath (): ?string
153+ {
154+ return implode ('/ ' , array_map ('rawurlencode ' , explode ('/ ' , $ this ->getRoute ())));
155+ }
156+
157+ public function getBasePath (): bool |string
158+ {
139159 return realpath ($ this ->getConfiguration ()['dir ' ]);
140160 }
141161
142- public function getQueryParameters (): array {
162+ public function getQueryParameters (): array
163+ {
143164 return $ this ->queryParameters ;
144165 }
145166
146- public function getRouter (): RouterInterface {
167+ public function getRouter (): RouterInterface
168+ {
147169 return $ this ->router ;
148170 }
149171
150- public function setRouter (RouterInterface $ router ): void {
172+ public function setRouter (RouterInterface $ router ): void
173+ {
151174 $ this ->router = $ router ;
152175 }
153176
154- public function getConfiguration (): array {
177+ public function getConfiguration (): array
178+ {
155179 return $ this ->configuration ;
156180 }
157181
158- public function setConfiguration (array $ configuration ): void {
182+ public function setConfiguration (array $ configuration ): void
183+ {
159184 $ this ->configuration = $ configuration ;
160185 }
161186
162- public function getTree (): bool {
187+ public function getTree (): bool
188+ {
163189 return $ this ->mergeQueryAndConf ('tree ' , true );
164190 }
165191
166- public function getView (): string {
192+ public function getView (): string
193+ {
167194 return $ this ->mergeQueryAndConf ('view ' , 'list ' );
168195 }
169196
170- public function getQueryParameter (string $ parameter ) {
197+ public function getQueryParameter (string $ parameter )
198+ {
171199 return $ this ->getQueryParameters ()[$ parameter ] ?? null ;
172200 }
173201
174- public function getConfigurationParameter (string $ parameter ) {
202+ public function getConfigurationParameter (string $ parameter )
203+ {
175204 return $ this ->getConfiguration ()[$ parameter ] ?? null ;
176205 }
177206
178- private function mergeQueryAndConf (string $ parameter , $ default = null ) {
207+ private function mergeQueryAndConf (string $ parameter , $ default = null )
208+ {
179209 if (null !== $ this ->getQueryParameter ($ parameter )) {
180210 return $ this ->getQueryParameter ($ parameter );
181211 }
@@ -186,7 +216,8 @@ private function mergeQueryAndConf(string $parameter, $default = null) {
186216 return $ default ;
187217 }
188218
189- private function mergeConfAndQuery (string $ parameter , $ default = null ) {
219+ private function mergeConfAndQuery (string $ parameter , $ default = null )
220+ {
190221 if (null !== $ this ->getConfigurationParameter ($ parameter )) {
191222 return $ this ->getConfigurationParameter ($ parameter );
192223 }
0 commit comments