Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Providers/SettingsServiceProvider.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Providers;
4
5
use Psr\Log\LoggerInterface as Log;
6
use Illuminate\Database\QueryException;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Contracts\Encryption\Encrypter;
9
use Illuminate\Contracts\Encryption\DecryptException;
10
use Illuminate\Contracts\Config\Repository as ConfigRepository;
11
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
12
13
class SettingsServiceProvider extends ServiceProvider
14
{
15
/**
16
* An array of configuration keys to override with database values
17
* if they exist.
18
*/
19
protected array $keys = [
20
'app:name',
21
'app:locale',
22
'recaptcha:enabled',
23
'recaptcha:secret_key',
24
'recaptcha:website_key',
25
'pterodactyl:guzzle:timeout',
26
'pterodactyl:guzzle:connect_timeout',
27
'pterodactyl:console:count',
28
'pterodactyl:console:frequency',
29
'pterodactyl:auth:2fa_required',
30
'pterodactyl:client_features:allocations:enabled',
31
'pterodactyl:client_features:allocations:range_start',
32
'pterodactyl:client_features:allocations:range_end',
33
];
34
35
/**
36
* Keys specific to the mail driver that are only grabbed from the database
37
* when using the SMTP driver.
38
*/
39
protected array $emailKeys = [
40
'mail:mailers:smtp:host',
41
'mail:mailers:smtp:port',
42
'mail:mailers:smtp:encryption',
43
'mail:mailers:smtp:username',
44
'mail:mailers:smtp:password',
45
'mail:from:address',
46
'mail:from:name',
47
];
48
49
/**
50
* Keys that are encrypted and should be decrypted when set in the
51
* configuration array.
52
*/
53
protected static array $encrypted = [
54
'mail:mailers:smtp:password',
55
];
56
57
/**
58
* Boot the service provider.
59
*/
60
public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log, SettingsRepositoryInterface $settings): void
61
{
62
// Only set the email driver settings from the database if we
63
// are configured using SMTP as the driver.
64
if ($config->get('mail.default') === 'smtp') {
65
$this->keys = array_merge($this->keys, $this->emailKeys);
66
}
67
68
try {
69
$values = $settings->all()->mapWithKeys(function ($setting) {
70
return [$setting->key => $setting->value];
71
})->toArray();
72
} catch (QueryException $exception) {
73
$log->notice('A query exception was encountered while trying to load settings from the database: ' . $exception->getMessage());
74
75
return;
76
}
77
78
foreach ($this->keys as $key) {
79
$value = array_get($values, 'settings::' . $key, $config->get(str_replace(':', '.', $key)));
80
if (in_array($key, self::$encrypted)) {
81
try {
82
$value = $encrypter->decrypt($value);
83
} catch (DecryptException $exception) {
84
}
85
}
86
87
switch (strtolower($value)) {
88
case 'true':
89
case '(true)':
90
$value = true;
91
break;
92
case 'false':
93
case '(false)':
94
$value = false;
95
break;
96
case 'empty':
97
case '(empty)':
98
$value = '';
99
break;
100
case 'null':
101
case '(null)':
102
$value = null;
103
}
104
105
$config->set(str_replace(':', '.', $key), $value);
106
}
107
}
108
109
public static function getEncryptedKeys(): array
110
{
111
return self::$encrypted;
112
}
113
}
114
115