2026-10-07 22:24:12 +05:45
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Rules;
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
|
use Pterodactyl\Models\Mount;
|
|
|
|
|
|
|
|
|
|
class NotInPathHierarchy implements ValidationRule
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param list<string> $invalidPaths
|
|
|
|
|
*/
|
2026-10-09 12:49:59 +01:00
|
|
|
public function __construct(protected array $invalidPaths, protected bool $includeDescendants = true) {}
|
2026-10-07 22:24:12 +05:45
|
|
|
|
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
|
|
|
{
|
|
|
|
|
if (! is_string($value)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = Mount::normalizePath($value);
|
|
|
|
|
|
|
|
|
|
foreach ($this->invalidPaths as $blocked) {
|
2026-10-09 12:49:59 +01:00
|
|
|
$blocked = Mount::normalizePath($blocked);
|
2026-10-07 22:24:12 +05:45
|
|
|
|
2026-10-09 12:49:59 +01:00
|
|
|
if ($path === $blocked) {
|
|
|
|
|
$fail($this->includeDescendants
|
2026-11-23 14:51:03 +00:00
|
|
|
? __('validation.internal.path_within', ['field' => $attribute, 'path' => $blocked])
|
|
|
|
|
: __('validation.internal.path_reserved', ['field' => $attribute, 'path' => $blocked]));
|
2026-10-09 12:49:59 +01:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->includeDescendants && str_starts_with($path, $blocked.'/')) {
|
2026-11-23 14:51:03 +00:00
|
|
|
$fail(__('validation.internal.path_within', ['field' => $attribute, 'path' => $blocked]));
|
2026-10-07 22:24:12 +05:45
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|