2017-09-22 00:30:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-09-22 00:30:09 -05:00
|
|
|
namespace Pterodactyl\Traits\Commands;
|
|
|
|
|
|
2026-10-11 16:30:09 -04:00
|
|
|
use Closure;
|
2017-09-22 00:30:09 -05:00
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
|
|
|
|
|
|
|
|
|
trait EnvironmentWriterTrait
|
|
|
|
|
{
|
2021-04-24 16:39:56 -07:00
|
|
|
/**
|
|
|
|
|
* Escapes an environment value by looking for any characters that could
|
2022-10-14 10:59:20 -06:00
|
|
|
* reasonably cause environment parsing issues. Those values are then wrapped
|
2021-04-24 16:39:56 -07:00
|
|
|
* in quotes before being returned.
|
|
|
|
|
*/
|
2025-02-14 13:22:59 -07:00
|
|
|
public function escapeEnvironmentValue(?string $value): string
|
2021-04-24 16:39:56 -07:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
if (($value) === null) {
|
2025-02-14 13:22:59 -07:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (! preg_match('/^\"(.*)\"$/', $value) && preg_match('/([^\w.\-+\/])+/', $value)) {
|
2021-04-24 16:39:56 -07:00
|
|
|
return sprintf('"%s"', addslashes($value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 00:30:09 -05:00
|
|
|
/**
|
|
|
|
|
* Update the .env file for the application using the passed in values.
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @param array<string, EnvironmentValue> $values keyed by environment variable name
|
|
|
|
|
*
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws PterodactylException
|
2017-09-22 00:30:09 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function writeToEnvironment(array $values = []): void
|
2017-09-22 00:30:09 -05:00
|
|
|
{
|
|
|
|
|
$path = base_path('.env');
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_unless(file_exists($path), PterodactylException::class, 'Cannot locate .env file, was this software installed correctly?');
|
2017-09-22 00:30:09 -05:00
|
|
|
|
|
|
|
|
$saveContents = file_get_contents($path);
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($saveContents === false, PterodactylException::class, 'Unable to read the contents of the .env file.');
|
|
|
|
|
|
|
|
|
|
collect($values)->each(function ($value, $key) use (&$saveContents): void {
|
|
|
|
|
$key = mb_strtoupper($key);
|
|
|
|
|
$saveValue = sprintf('%s=%s', $key, $this->escapeEnvironmentValue($value === null ? null : (string) $value));
|
2017-09-22 00:30:09 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (preg_match_all('/^'.$key.'=(.*)$/m', $saveContents) < 1) {
|
|
|
|
|
$saveContents = $saveContents.PHP_EOL.$saveValue;
|
2017-09-22 00:30:09 -05:00
|
|
|
} else {
|
2026-10-05 18:10:18 -04:00
|
|
|
// preg_replace() only returns null on a PCRE engine failure (e.g. the
|
|
|
|
|
// backtrack limit); keep the existing contents rather than losing them.
|
|
|
|
|
$saveContents = preg_replace('/^'.$key.'=(.*)$/m', $saveValue, $saveContents) ?? $saveContents;
|
2017-09-22 00:30:09 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
file_put_contents($path, $saveContents);
|
|
|
|
|
}
|
2026-10-11 16:30:09 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The answer to a prompt, or the option passed in its place. Symfony returns
|
|
|
|
|
* either one untyped: a string, or null for a blank answer without a default.
|
|
|
|
|
*/
|
|
|
|
|
protected function answer(Closure $prompt): ?string
|
|
|
|
|
{
|
2026-10-11 20:11:13 -04:00
|
|
|
$answer = $prompt();
|
2026-10-11 16:30:09 -04:00
|
|
|
|
2026-10-11 20:11:13 -04:00
|
|
|
return is_scalar($answer) ? (string) $answer : null;
|
2026-10-11 16:30:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The value currently configured for a key, offered as a prompt's default.
|
|
|
|
|
*/
|
|
|
|
|
protected function configured(string $key, int|string|null $default = null): ?string
|
|
|
|
|
{
|
2026-10-11 19:15:22 -04:00
|
|
|
$value = config($key, $default);
|
2026-10-11 16:30:09 -04:00
|
|
|
|
2026-10-11 19:15:22 -04:00
|
|
|
return is_scalar($value) ? (string) $value : null;
|
2026-10-11 16:30:09 -04:00
|
|
|
}
|
2017-09-22 00:30:09 -05:00
|
|
|
}
|