diff --git a/config/config.php b/config/config.php index e1d1d50d..2950632d 100755 --- a/config/config.php +++ b/config/config.php @@ -4,6 +4,7 @@ define('RASPI_BRAND_TEXT', 'RaspAP'); define('RASPI_CONFIG', '/etc/raspap'); define('RASPI_CONFIG_NETWORK', RASPI_CONFIG.'/networking/defaults.json'); define('RASPI_CONFIG_PROVIDERS', 'config/vpn-providers.json'); +define('RASPI_CONFIG_API', RASPI_CONFIG.'/api'); define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth'); define('RASPI_WIFI_AP_INTERFACE', 'wlan0'); define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap'); diff --git a/includes/defaults.php b/includes/defaults.php index 7227a955..bc442e7d 100755 --- a/includes/defaults.php +++ b/includes/defaults.php @@ -9,6 +9,7 @@ $defaults = [ 'RASPI_VERSION' => '3.0.8', 'RASPI_CONFIG_NETWORK' => RASPI_CONFIG.'/networking/defaults.json', 'RASPI_CONFIG_PROVIDERS' => 'config/vpn-providers.json', + 'RASPI_CONFIG_API' => RASPI_CONFIG.'/api', 'RASPI_ADMIN_DETAILS' => RASPI_CONFIG.'/raspap.auth', 'RASPI_WIFI_AP_INTERFACE' => 'wlan0', 'RASPI_CACHE_PATH' => sys_get_temp_dir() . '/raspap', diff --git a/src/RaspAP/DotEnv/DotEnv.php b/src/RaspAP/DotEnv/DotEnv.php index a29a4e99..1e8397a1 100644 --- a/src/RaspAP/DotEnv/DotEnv.php +++ b/src/RaspAP/DotEnv/DotEnv.php @@ -17,13 +17,17 @@ class DotEnv protected $envFile; protected $data = []; - public function __construct($envFile = '.env') + public function __construct($envFile = RASPI_CONFIG_API. '/.env') { $this->envFile = $envFile; } public function load() { + if (!file_exists($this->envFile)) { + $this->createEnv(); + } + if (file_exists($this->envFile)) { $this->data = parse_ini_file($this->envFile); foreach ($this->data as $key => $value) { @@ -68,7 +72,19 @@ class DotEnv // if key doesn't exist, append it $content .= "$key=$value\n"; } - file_put_contents($this->envFile, $content); + file_put_contents("/tmp/.env", $content); + system('sudo mv /tmp/.env '.$this->envFile, $result); + if ($result !== 0) { + throw new Exception("Unable to move .env file: ". $this->envFile); + } + } + + protected function createEnv() + { + exec('sudo touch '. escapeshellarg($this->envFile), $output, $result); + if ($result !== 0) { + throw new Exception("Unable to create .env file: ". $this->envFile); + } } }