-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error 401 When Accessing Parent Folder with "+" in Name (#138)
* test * fix parent * fix parent * fix parent
- Loading branch information
Showing
3 changed files
with
80 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,29 +13,34 @@ | |
/** | ||
* @author Arthur Gribet <[email protected]> | ||
*/ | ||
class FileManager { | ||
class FileManager | ||
{ | ||
const VIEW_THUMBNAIL = 'thumbnail'; | ||
const VIEW_LIST = 'list'; | ||
|
||
/** | ||
* FileManager constructor. | ||
*/ | ||
public function __construct(private array $queryParameters, private array $configuration, private RouterInterface $router, private EventDispatcherInterface $dispatcher, private string $webDir) { | ||
public function __construct(private array $queryParameters, private array $configuration, private RouterInterface $router, private EventDispatcherInterface $dispatcher, private string $webDir) | ||
{ | ||
// Check Security | ||
$this->checkSecurity(); | ||
} | ||
|
||
public function getDirName(): string { | ||
public function getDirName(): string | ||
{ | ||
return \dirname($this->getBasePath()); | ||
} | ||
|
||
public function getBaseName(): string { | ||
public function getBaseName(): string | ||
{ | ||
return basename($this->getBasePath()); | ||
} | ||
|
||
public function getRegex(): string { | ||
public function getRegex(): string | ||
{ | ||
if (isset($this->configuration['regex'])) { | ||
return '/'.$this->configuration['regex'].'/i'; | ||
return '/' . $this->configuration['regex'] . '/i'; | ||
} | ||
|
||
return match ($this->getType()) { | ||
|
@@ -45,28 +50,29 @@ public function getRegex(): string { | |
}; | ||
} | ||
|
||
public function getCurrentRoute(): ?string { | ||
if ($this->getRoute()) { | ||
return urldecode($this->getRoute()); | ||
} | ||
|
||
return null; | ||
} | ||
// public function getCurrentRoute(): ?string { | ||
// if ($this->getRoute()) { | ||
// return urldecode($this->getRoute()); | ||
// } | ||
// | ||
// return null; | ||
// } | ||
|
||
public function getCurrentPath(): bool|string { | ||
return realpath($this->getBasePath().$this->getCurrentRoute()); | ||
public function getCurrentPath(): bool|string | ||
{ | ||
return realpath($this->getBasePath() . $this->getRoute()); | ||
} | ||
|
||
// parent url | ||
public function getParent(): ?string { | ||
public function getParent(): ?string | ||
{ | ||
$queryParentParameters = $this->queryParameters; | ||
|
||
if ($this->getCurrentRoute()) { | ||
|
||
$parentRoute = \dirname($this->getCurrentRoute()); | ||
if ($this->getRoute()) { | ||
|
||
$parentRoute = \dirname($this->getRoute()); | ||
if (\DIRECTORY_SEPARATOR !== $parentRoute) { | ||
$queryParentParameters['route'] = \dirname($this->getCurrentRoute()); | ||
$queryParentParameters['route'] = \dirname($this->getRoute()); | ||
} else { | ||
unset($queryParentParameters['route']); | ||
} | ||
|
@@ -79,28 +85,33 @@ public function getParent(): ?string { | |
return null; | ||
} | ||
|
||
public function getImagePath(): bool|string { | ||
public function getImagePath(): bool|string | ||
{ | ||
$baseUrl = $this->getBaseUrl(); | ||
|
||
if ($baseUrl) { | ||
return $baseUrl.$this->getCurrentRoute().'/'; | ||
$routePath = $this->getRoutePath(); | ||
return $baseUrl . $routePath . '/'; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getBaseUrl(): bool|string { | ||
private function getBaseUrl(): bool|string | ||
{ | ||
$webPath = $this->webDir; | ||
$dirl = new \SplFileInfo($this->getConfiguration()['dir']); | ||
$base = $dirl->getPathname(); | ||
|
||
if (0 === mb_strpos($base, $webPath)) { | ||
if (str_starts_with($base, $webPath)) { | ||
return mb_substr($base, mb_strlen($webPath)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function checkSecurity(): void { | ||
private function checkSecurity(): void | ||
{ | ||
if (!isset($this->configuration['dir'])) { | ||
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Please define a "dir" parameter in your config.yml'); | ||
} | ||
|
@@ -115,67 +126,86 @@ private function checkSecurity(): void { | |
$currentPath = $this->getCurrentPath(); | ||
|
||
// check Path security | ||
if (false === $currentPath || 0 !== mb_strpos($currentPath, $this->getBasePath())) { | ||
if (false === $currentPath || !str_starts_with($currentPath, $this->getBasePath())) { | ||
throw new HttpException(Response::HTTP_UNAUTHORIZED, 'You are not allowed to access this folder.'); | ||
} | ||
$event = new GenericEvent($this, ['path' => $currentPath]); | ||
$this->dispatcher->dispatch($event, FileManagerEvents::POST_CHECK_SECURITY); | ||
|
||
} | ||
|
||
public function getModule(): ?string { | ||
public function getModule(): ?string | ||
{ | ||
return $this->getQueryParameters()['module'] ?? null; | ||
} | ||
|
||
public function getType(): ?string { | ||
public function getType(): ?string | ||
{ | ||
return $this->mergeConfAndQuery('type'); | ||
} | ||
|
||
public function getRoute(): ?string { | ||
public function getRoute(): ?string | ||
{ | ||
return isset($this->getQueryParameters()['route']) && '/' !== $this->getQueryParameters()['route'] ? $this->getQueryParameters()['route'] : null; | ||
} | ||
|
||
public function getBasePath(): bool|string { | ||
public function getRoutePath(): ?string | ||
{ | ||
return implode('/', array_map('rawurlencode', explode('/', $this->getRoute()))); | ||
} | ||
|
||
public function getBasePath(): bool|string | ||
{ | ||
return realpath($this->getConfiguration()['dir']); | ||
} | ||
|
||
public function getQueryParameters(): array { | ||
public function getQueryParameters(): array | ||
{ | ||
return $this->queryParameters; | ||
} | ||
|
||
public function getRouter(): RouterInterface { | ||
public function getRouter(): RouterInterface | ||
{ | ||
return $this->router; | ||
} | ||
|
||
public function setRouter(RouterInterface $router): void { | ||
public function setRouter(RouterInterface $router): void | ||
{ | ||
$this->router = $router; | ||
} | ||
|
||
public function getConfiguration(): array { | ||
public function getConfiguration(): array | ||
{ | ||
return $this->configuration; | ||
} | ||
|
||
public function setConfiguration(array $configuration): void { | ||
public function setConfiguration(array $configuration): void | ||
{ | ||
$this->configuration = $configuration; | ||
} | ||
|
||
public function getTree(): bool { | ||
public function getTree(): bool | ||
{ | ||
return $this->mergeQueryAndConf('tree', true); | ||
} | ||
|
||
public function getView(): string { | ||
public function getView(): string | ||
{ | ||
return $this->mergeQueryAndConf('view', 'list'); | ||
} | ||
|
||
public function getQueryParameter(string $parameter) { | ||
public function getQueryParameter(string $parameter) | ||
{ | ||
return $this->getQueryParameters()[$parameter] ?? null; | ||
} | ||
|
||
public function getConfigurationParameter(string $parameter) { | ||
public function getConfigurationParameter(string $parameter) | ||
{ | ||
return $this->getConfiguration()[$parameter] ?? null; | ||
} | ||
|
||
private function mergeQueryAndConf(string $parameter, $default = null) { | ||
private function mergeQueryAndConf(string $parameter, $default = null) | ||
{ | ||
if (null !== $this->getQueryParameter($parameter)) { | ||
return $this->getQueryParameter($parameter); | ||
} | ||
|
@@ -186,7 +216,8 @@ private function mergeQueryAndConf(string $parameter, $default = null) { | |
return $default; | ||
} | ||
|
||
private function mergeConfAndQuery(string $parameter, $default = null) { | ||
private function mergeConfAndQuery(string $parameter, $default = null) | ||
{ | ||
if (null !== $this->getConfigurationParameter($parameter)) { | ||
return $this->getConfigurationParameter($parameter); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters