Skip to content

Commit 5f9db43

Browse files
author
Wazabii
committed
Path
1 parent 132531d commit 5f9db43

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Dir.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
class Dir implements DirInterface
1010
{
1111
private $dir;
12+
private $publicDirPath;
1213

1314
public function __construct($dir)
1415
{
1516
$this->dir = $dir;
17+
18+
// Will move this
19+
$envDir = getenv("APP_PUBLIC_DIR");
20+
$this->publicDirPath = "public/";
21+
if (is_string($envDir) && $this->validateDir($envDir)) {
22+
$this->publicDirPath = ltrim(rtrim($envDir, "/"), "/")."/";
23+
}
1624
}
1725

1826
/**
@@ -52,7 +60,7 @@ public function getResources(string $path = ""): string
5260
*/
5361
public function getPublic(string $path = ""): string
5462
{
55-
return $this->getDir("public/{$path}");
63+
return $this->getDir("{$this->publicDirPath}{$path}");
5664
}
5765

5866
/**
@@ -84,4 +92,10 @@ public function getCaches(string $path = ""): string
8492
{
8593
return $this->getStorage("caches/{$path}");
8694
}
95+
96+
public function validateDir(string $path): bool
97+
{
98+
$fullPath = realpath($_ENV['APP_DIR'].$path);
99+
return (is_string($fullPath) && strpos($fullPath, $_ENV['APP_DIR']) === 0);
100+
}
87101
}

Url.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)