Path: blob/1.0-develop/app/Traits/Commands/EnvironmentWriterTrait.php
10284 views
<?php12namespace Pterodactyl\Traits\Commands;34use Pterodactyl\Exceptions\PterodactylException;56trait EnvironmentWriterTrait7{8/**9* Escapes an environment value by looking for any characters that could10* reasonably cause environment parsing issues. Those values are then wrapped11* in quotes before being returned.12*/13public function escapeEnvironmentValue(?string $value): string14{15if (is_null($value)) {16return '';17}1819if (!preg_match('/^\"(.*)\"$/', $value) && preg_match('/([^\w.\-+\/])+/', $value)) {20return sprintf('"%s"', addslashes($value));21}2223return $value;24}2526/**27* Update the .env file for the application using the passed in values.28*29* @throws PterodactylException30*/31public function writeToEnvironment(array $values = []): void32{33$path = base_path('.env');34if (!file_exists($path)) {35throw new PterodactylException('Cannot locate .env file, was this software installed correctly?');36}3738$saveContents = file_get_contents($path);39collect($values)->each(function ($value, $key) use (&$saveContents) {40$key = strtoupper($key);41$saveValue = sprintf('%s=%s', $key, $this->escapeEnvironmentValue($value));4243if (preg_match_all('/^' . $key . '=(.*)$/m', $saveContents) < 1) {44$saveContents = $saveContents . PHP_EOL . $saveValue;45} else {46$saveContents = preg_replace('/^' . $key . '=(.*)$/m', $saveValue, $saveContents);47}48});4950file_put_contents($path, $saveContents);51}52}535455