2017-12-14 21:05:26 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-12-14 21:05:26 -06:00
|
|
|
namespace Pterodactyl\Providers;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-12-30 20:53:34 -06:00
|
|
|
use Illuminate\Database\QueryException;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Arr;
|
2017-12-14 21:05:26 -06:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Psr\Log\LoggerInterface as Log;
|
|
|
|
|
use Pterodactyl\Models\Setting;
|
2017-12-14 21:05:26 -06:00
|
|
|
|
|
|
|
|
class SettingsServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* An array of configuration keys to override with database values
|
|
|
|
|
* if they exist.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @var list<string>
|
2017-12-14 21:05:26 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
protected array $keys = [
|
2017-12-14 21:05:26 -06:00
|
|
|
'app:name',
|
2026-11-05 14:23:59 -05:00
|
|
|
'app:logo',
|
2017-12-14 21:05:26 -06:00
|
|
|
'app:locale',
|
|
|
|
|
'recaptcha:enabled',
|
|
|
|
|
'recaptcha:secret_key',
|
|
|
|
|
'recaptcha:website_key',
|
|
|
|
|
'pterodactyl:guzzle:timeout',
|
|
|
|
|
'pterodactyl:guzzle:connect_timeout',
|
|
|
|
|
'pterodactyl:console:count',
|
|
|
|
|
'pterodactyl:console:frequency',
|
|
|
|
|
'pterodactyl:auth:2fa_required',
|
2020-10-31 14:58:15 -07:00
|
|
|
'pterodactyl:client_features:allocations:enabled',
|
|
|
|
|
'pterodactyl:client_features:allocations:range_start',
|
|
|
|
|
'pterodactyl:client_features:allocations:range_end',
|
2026-10-20 21:09:33 +02:00
|
|
|
'pterodactyl:client_features:avatars:enabled',
|
2017-12-14 21:05:26 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Keys specific to the mail driver that are only grabbed from the database
|
|
|
|
|
* when using the SMTP driver.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @var list<string>
|
2017-12-14 21:05:26 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
protected array $emailKeys = [
|
2022-12-12 14:21:47 -07:00
|
|
|
'mail:mailers:smtp:host',
|
|
|
|
|
'mail:mailers:smtp:port',
|
|
|
|
|
'mail:mailers:smtp:encryption',
|
|
|
|
|
'mail:mailers:smtp:username',
|
|
|
|
|
'mail:mailers:smtp:password',
|
2017-12-14 21:05:26 -06:00
|
|
|
'mail:from:address',
|
|
|
|
|
'mail:from:name',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Keys that are encrypted and should be decrypted when set in the
|
|
|
|
|
* configuration array.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @var list<string>
|
2017-12-14 21:05:26 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
protected static array $encrypted = [
|
2022-12-12 14:31:49 -07:00
|
|
|
'mail:mailers:smtp:password',
|
2017-12-14 21:05:26 -06:00
|
|
|
];
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* @return list<string>
|
|
|
|
|
*/
|
|
|
|
|
public static function getEncryptedKeys(): array
|
|
|
|
|
{
|
|
|
|
|
return self::$encrypted;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 21:05:26 -06:00
|
|
|
/**
|
|
|
|
|
* Boot the service provider.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log): void
|
2017-12-14 21:05:26 -06:00
|
|
|
{
|
|
|
|
|
// Only set the email driver settings from the database if we
|
|
|
|
|
// are configured using SMTP as the driver.
|
2022-10-23 20:51:20 -04:00
|
|
|
if ($config->get('mail.default') === 'smtp') {
|
2017-12-14 21:05:26 -06:00
|
|
|
$this->keys = array_merge($this->keys, $this->emailKeys);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-30 20:53:34 -06:00
|
|
|
try {
|
2026-10-05 18:10:18 -04:00
|
|
|
$values = Setting::query()->pluck('value', 'key')->all();
|
|
|
|
|
} catch (QueryException $queryException) {
|
|
|
|
|
$log->notice('A query exception was encountered while trying to load settings from the database: '.$queryException->getMessage());
|
2017-12-30 20:53:34 -06:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-14 21:05:26 -06:00
|
|
|
|
|
|
|
|
foreach ($this->keys as $key) {
|
2026-10-11 19:15:22 -04:00
|
|
|
$default = $config->get(str_replace(':', '.', $key));
|
|
|
|
|
$value = Arr::get($values, 'settings::'.$key, $default);
|
2026-10-05 18:10:18 -04:00
|
|
|
if (in_array($key, self::$encrypted, true) && is_string($value)) {
|
2017-12-14 21:05:26 -06:00
|
|
|
try {
|
|
|
|
|
$value = $encrypter->decrypt($value);
|
2026-10-05 18:10:18 -04:00
|
|
|
} catch (DecryptException) {
|
2017-12-14 21:05:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
switch (is_string($value) ? mb_strtolower($value) : $value) {
|
2017-12-17 13:07:38 -06:00
|
|
|
case 'true':
|
|
|
|
|
case '(true)':
|
|
|
|
|
$value = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'false':
|
|
|
|
|
case '(false)':
|
|
|
|
|
$value = false;
|
|
|
|
|
break;
|
|
|
|
|
case 'empty':
|
|
|
|
|
case '(empty)':
|
|
|
|
|
$value = '';
|
|
|
|
|
break;
|
|
|
|
|
case 'null':
|
|
|
|
|
case '(null)':
|
|
|
|
|
$value = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 19:15:22 -04:00
|
|
|
$config->set(str_replace(':', '.', $key), $this->cast($value, $default));
|
2017-12-14 21:05:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-11 19:15:22 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Settings are stored as strings, so a stored value takes the type of the
|
|
|
|
|
* configuration value it replaces. Keys without a typed default keep the
|
|
|
|
|
* stored value.
|
|
|
|
|
*/
|
|
|
|
|
protected function cast(mixed $value, mixed $default): mixed
|
|
|
|
|
{
|
|
|
|
|
if (! is_string($value)) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match (true) {
|
|
|
|
|
is_int($default) => (int) $value,
|
|
|
|
|
is_float($default) => (float) $value,
|
|
|
|
|
is_bool($default) => filter_var($value, FILTER_VALIDATE_BOOL),
|
|
|
|
|
default => $value,
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-12-14 21:05:26 -06:00
|
|
|
}
|