Path: blob/1.0-develop/app/Providers/SettingsServiceProvider.php
7432 views
<?php12namespace Pterodactyl\Providers;34use Psr\Log\LoggerInterface as Log;5use Illuminate\Database\QueryException;6use Illuminate\Support\ServiceProvider;7use Illuminate\Contracts\Encryption\Encrypter;8use Illuminate\Contracts\Encryption\DecryptException;9use Illuminate\Contracts\Config\Repository as ConfigRepository;10use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;1112class SettingsServiceProvider extends ServiceProvider13{14/**15* An array of configuration keys to override with database values16* if they exist.17*/18protected array $keys = [19'app:name',20'app:locale',21'recaptcha:enabled',22'recaptcha:secret_key',23'recaptcha:website_key',24'pterodactyl:guzzle:timeout',25'pterodactyl:guzzle:connect_timeout',26'pterodactyl:console:count',27'pterodactyl:console:frequency',28'pterodactyl:auth:2fa_required',29'pterodactyl:client_features:allocations:enabled',30'pterodactyl:client_features:allocations:range_start',31'pterodactyl:client_features:allocations:range_end',32];3334/**35* Keys specific to the mail driver that are only grabbed from the database36* when using the SMTP driver.37*/38protected array $emailKeys = [39'mail:mailers:smtp:host',40'mail:mailers:smtp:port',41'mail:mailers:smtp:encryption',42'mail:mailers:smtp:username',43'mail:mailers:smtp:password',44'mail:from:address',45'mail:from:name',46];4748/**49* Keys that are encrypted and should be decrypted when set in the50* configuration array.51*/52protected static array $encrypted = [53'mail:mailers:smtp:password',54];5556/**57* Boot the service provider.58*/59public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log, SettingsRepositoryInterface $settings): void60{61// Only set the email driver settings from the database if we62// are configured using SMTP as the driver.63if ($config->get('mail.default') === 'smtp') {64$this->keys = array_merge($this->keys, $this->emailKeys);65}6667try {68$values = $settings->all()->mapWithKeys(function ($setting) {69return [$setting->key => $setting->value];70})->toArray();71} catch (QueryException $exception) {72$log->notice('A query exception was encountered while trying to load settings from the database: ' . $exception->getMessage());7374return;75}7677foreach ($this->keys as $key) {78$value = array_get($values, 'settings::' . $key, $config->get(str_replace(':', '.', $key)));79if (in_array($key, self::$encrypted)) {80try {81$value = $encrypter->decrypt($value);82} catch (DecryptException $exception) {83}84}8586switch (strtolower($value)) {87case 'true':88case '(true)':89$value = true;90break;91case 'false':92case '(false)':93$value = false;94break;95case 'empty':96case '(empty)':97$value = '';98break;99case 'null':100case '(null)':101$value = null;102}103104$config->set(str_replace(':', '.', $key), $value);105}106}107108public static function getEncryptedKeys(): array109{110return self::$encrypted;111}112}113114115