2018-02-28 03:28:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware\Api\Client;
|
|
|
|
|
|
|
|
use Closure;
|
2020-08-20 03:21:12 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-04-05 02:54:59 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Illuminate\Container\Container;
|
2018-08-25 21:43:21 +00:00
|
|
|
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
|
|
|
|
class SubstituteClientApiBindings extends ApiSubstituteBindings
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Perform substitution of route parameters without triggering
|
|
|
|
* a 404 error if a model is not found.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Closure $next
|
2018-02-28 03:28:43 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
// Override default behavior of the model binding to use a specific table
|
|
|
|
// column rather than the default 'id'.
|
|
|
|
$this->router->bind('server', function ($value) use ($request) {
|
|
|
|
try {
|
2019-05-02 04:45:39 +00:00
|
|
|
$column = 'uuidShort';
|
|
|
|
if (preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $value)) {
|
|
|
|
$column = 'uuid';
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
return Container::getInstance()->make(ServerRepositoryInterface::class)->findFirstWhere([
|
2019-05-02 04:45:39 +00:00
|
|
|
[$column, '=', $value],
|
2018-02-28 03:28:43 +00:00
|
|
|
]);
|
|
|
|
} catch (RecordNotFoundException $ex) {
|
|
|
|
$request->attributes->set('is_missing_model', true);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->router->bind('database', function ($value) {
|
2020-07-10 03:36:08 +00:00
|
|
|
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
|
2018-08-25 21:43:21 +00:00
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
return Database::query()->where('id', $id)->firstOrFail();
|
2018-08-25 21:43:21 +00:00
|
|
|
});
|
|
|
|
|
2020-09-27 17:39:18 +00:00
|
|
|
$this->router->bind('backup', function ($value) {
|
2020-04-05 02:54:59 +00:00
|
|
|
return Backup::query()->where('uuid', $value)->firstOrFail();
|
|
|
|
});
|
|
|
|
|
2020-09-27 17:39:18 +00:00
|
|
|
$this->router->bind('user', function ($value) {
|
2020-08-20 03:21:12 +00:00
|
|
|
return User::query()->where('uuid', $value)->firstOrFail();
|
|
|
|
});
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
return parent::handle($request, $next);
|
|
|
|
}
|
|
|
|
}
|