2017-09-22 05:30:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Traits\Commands;
|
|
|
|
|
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
|
|
|
|
|
|
|
trait EnvironmentWriterTrait
|
|
|
|
{
|
2021-04-24 23:39:56 +00:00
|
|
|
/**
|
|
|
|
* Escapes an environment value by looking for any characters that could
|
2022-10-14 16:59:20 +00:00
|
|
|
* reasonably cause environment parsing issues. Those values are then wrapped
|
2021-04-24 23:39:56 +00:00
|
|
|
* in quotes before being returned.
|
|
|
|
*/
|
|
|
|
public function escapeEnvironmentValue(string $value): string
|
|
|
|
{
|
|
|
|
if (!preg_match('/^\"(.*)\"$/', $value) && preg_match('/([^\w.\-+\/])+/', $value)) {
|
|
|
|
return sprintf('"%s"', addslashes($value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
/**
|
|
|
|
* Update the .env file for the application using the passed in values.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\PterodactylException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function writeToEnvironment(array $values = []): void
|
2017-09-22 05:30:09 +00:00
|
|
|
{
|
|
|
|
$path = base_path('.env');
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!file_exists($path)) {
|
2017-09-22 05:30:09 +00:00
|
|
|
throw new PterodactylException('Cannot locate .env file, was this software installed correctly?');
|
|
|
|
}
|
|
|
|
|
|
|
|
$saveContents = file_get_contents($path);
|
|
|
|
collect($values)->each(function ($value, $key) use (&$saveContents) {
|
|
|
|
$key = strtoupper($key);
|
2021-04-24 23:39:56 +00:00
|
|
|
$saveValue = sprintf('%s=%s', $key, $this->escapeEnvironmentValue($value));
|
2017-09-22 05:30:09 +00:00
|
|
|
|
|
|
|
if (preg_match_all('/^' . $key . '=(.*)$/m', $saveContents) < 1) {
|
|
|
|
$saveContents = $saveContents . PHP_EOL . $saveValue;
|
|
|
|
} else {
|
|
|
|
$saveContents = preg_replace('/^' . $key . '=(.*)$/m', $saveValue, $saveContents);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
file_put_contents($path, $saveContents);
|
|
|
|
}
|
|
|
|
}
|