@@ -18,6 +18,7 @@ class Url implements UrlInterface
1818 private $ fullPath ;
1919 private $ dirPath ;
2020 private $ realPath ;
21+ private $ publicDirPath ;
2122
2223 public function __construct (RequestInterface $ request , $ path )
2324 {
@@ -27,6 +28,13 @@ public function __construct(RequestInterface $request, $path)
2728 $ this ->fullPath = $ this ->uri ->getPath ();
2829 $ this ->dirPath = $ this ->getDirPath ();
2930 $ this ->realPath = $ this ->getRealPath ();
31+
32+ // Will move this
33+ // _ENV['APP_DIR'] is alos used and should be corrected
34+ $ envDir = getenv ("APP_PUBLIC_DIR " );
35+ if (is_string ($ envDir ) && $ this ->validateDir ($ envDir )) {
36+ $ this ->publicDirPath = ltrim (rtrim ($ envDir , "/ " ), "/ " )."/ " ;
37+ }
3038 }
3139
3240 public function __toString (): string
@@ -178,11 +186,15 @@ public function getDirPath(): string
178186 $ root = (isset ($ _SERVER ['DOCUMENT_ROOT ' ])) ? $ _SERVER ['DOCUMENT_ROOT ' ] : "" ;
179187 $ root = htmlspecialchars ($ root , ENT_QUOTES , 'UTF-8 ' );
180188 $ this ->dirPath = str_replace ($ root , "" , $ this ->request ->getUri ()->getDir ());
189+
190+ if ($ root .$ this ->dirPath !== $ _ENV ['APP_DIR ' ]) {
191+ throw new \Exception ("Could not validate the dirPath " , 1 );
192+ }
181193 }
182194 if (!is_string ($ this ->dirPath )) {
183195 throw new \Exception ("Could not create dirPath " , 1 );
184196 }
185- return $ this ->dirPath ;
197+ return $ this ->dirPath . $ this -> publicDirPath ;
186198 }
187199
188200 /**
@@ -266,6 +278,7 @@ public function getRoot(string $path = "", bool $endSlash = false): string
266278 if ($ authority = $ this ->getHost ()) {
267279 $ url .= "// {$ authority }" ;
268280 }
281+
269282 if ($ dir = $ this ->getDirPath ()) {
270283 $ url .= rtrim ($ dir , "/ " );
271284 }
@@ -296,6 +309,9 @@ public function getUrl(string $addToPath = ""): string
296309 */
297310 public function getPublic (string $ path = "" ): string
298311 {
312+ if (!is_null ($ this ->publicDirPath )) {
313+ return $ this ->getRoot ("/ {$ path }" );
314+ }
299315 return $ this ->getRoot ("/public/ {$ path }" );
300316 }
301317
@@ -333,4 +349,10 @@ public function getCss(string $path): string
333349 {
334350 return $ this ->getPublic ("css/ {$ path }" );
335351 }
352+
353+ public function validateDir (string $ path ): bool
354+ {
355+ $ fullPath = realpath ($ _ENV ['APP_DIR ' ].$ path );
356+ return (is_string ($ fullPath ) && strpos ($ fullPath , $ _ENV ['APP_DIR ' ]) === 0 );
357+ }
336358}
0 commit comments