2017-12-15 06:38:29 +08:00
|
|
|
<?php
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
namespace App\Services\Installer;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2019-07-18 21:26:33 +08:00
|
|
|
use Exception;
|
|
|
|
use function extension_loaded;
|
2018-02-26 05:27:20 +08:00
|
|
|
use Illuminate\Encryption\Encrypter;
|
2019-07-18 21:26:33 +08:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use function is_bool;
|
2018-01-29 08:50:43 +08:00
|
|
|
use Nwidart\Modules\Support\Stub;
|
2018-02-26 05:27:20 +08:00
|
|
|
use PDO;
|
2017-12-18 06:58:53 +08:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class ConfigService extends Service
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2021-04-11 05:19:45 +08:00
|
|
|
protected static $defaultValues = [
|
2021-04-11 06:00:03 +08:00
|
|
|
'APP_ENV' => 'prod',
|
2021-04-11 05:19:45 +08:00
|
|
|
'APP_KEY' => '',
|
|
|
|
'APP_DEBUG' => true,
|
|
|
|
'APP_LOCALE' => 'en',
|
|
|
|
'DEBUG_TOOLBAR' => false,
|
|
|
|
'SITE_NAME' => '',
|
|
|
|
'SITE_URL' => 'http://phpvms.test',
|
|
|
|
'DB_CONNECTION' => '',
|
|
|
|
'DB_HOST' => '',
|
|
|
|
'DB_PORT' => 3306,
|
|
|
|
'DB_DATABASE' => '',
|
|
|
|
'DB_USERNAME' => '',
|
|
|
|
'DB_PASSWORD' => '',
|
|
|
|
'DB_PREFIX' => '',
|
|
|
|
'DB_EMULATE_PREPARES' => false,
|
|
|
|
'CACHE_DRIVER' => 'array',
|
|
|
|
'CACHE_PREFIX' => '',
|
|
|
|
'MAIL_DRIVER' => 'smtp',
|
|
|
|
'MAIL_HOST' => '',
|
|
|
|
'MAIL_PORT' => 587,
|
|
|
|
'MAIL_ENCRYPTION' => '',
|
|
|
|
'MAIL_USERNAME' => '',
|
|
|
|
'MAIL_PASSWORD' => '',
|
|
|
|
'MAIL_FROM_NAME' => 'phpVMS Admin',
|
|
|
|
'MAIL_FROM_ADDRESS' => 'no-reply@phpvms.net',
|
|
|
|
];
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
2021-04-11 05:19:45 +08:00
|
|
|
* Create the .env file. This is called by an initial install
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-30 03:16:39 +08:00
|
|
|
* @param $attrs
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2021-04-11 05:19:45 +08:00
|
|
|
* @throws FileException
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
2018-01-30 03:16:39 +08:00
|
|
|
public function createConfigFiles($attrs): bool
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2021-04-11 05:19:45 +08:00
|
|
|
$opts = array_merge(static::$defaultValues, $attrs);
|
|
|
|
if (empty($opts['APP_KEY'])) {
|
|
|
|
$opts['APP_KEY'] = $this->createAppKey();
|
|
|
|
}
|
2018-01-30 03:16:39 +08:00
|
|
|
|
2018-01-11 09:15:42 +08:00
|
|
|
$opts = $this->determinePdoOptions($opts);
|
2019-07-18 21:26:33 +08:00
|
|
|
$opts = $this->configCacheDriver($opts);
|
|
|
|
$opts = $this->configQueueDriver($opts);
|
2017-12-15 07:12:42 +08:00
|
|
|
|
2018-01-30 03:16:39 +08:00
|
|
|
$this->writeConfigFiles($opts);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-11 05:19:45 +08:00
|
|
|
/**
|
|
|
|
* Rewrite the config files - this means mapping the values that are currently
|
|
|
|
* loaded in the config and rewriting them into the env.php file, and then renaming
|
|
|
|
* the config.php files to config.bak.php
|
|
|
|
*
|
|
|
|
* This is called from the migrations which removes the old config.php file
|
|
|
|
*/
|
|
|
|
public function rewriteConfigFiles()
|
|
|
|
{
|
|
|
|
/*$cfg_file = App::environmentPath().'/config.php';
|
|
|
|
if (!file_exists($cfg_file)) {
|
|
|
|
Log::info('Main config.php file is missing, migration already completed');
|
|
|
|
return;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
$db_opts = config('database.connections.mysql.options');
|
|
|
|
$emulate_prepares = $db_opts[PDO::ATTR_EMULATE_PREPARES] ? 'true' : 'false';
|
|
|
|
|
|
|
|
$opts = array_merge(static::$defaultValues, [
|
|
|
|
'APP_ENV' => config('app.env'),
|
|
|
|
'APP_KEY' => config('app.key'),
|
|
|
|
'APP_DEBUG' => config('app.debug') ? 'true' : 'false',
|
|
|
|
'APP_LOCALE' => config('app.locale'),
|
|
|
|
'DEBUG_TOOLBAR' => config('app.debug_toolbar') ? 'true' : 'false',
|
|
|
|
'SITE_NAME' => config('app.name'),
|
|
|
|
'SITE_URL' => config('app.url'),
|
|
|
|
'DB_CONNECTION' => config('database.default'),
|
|
|
|
'DB_HOST' => config('database.connections.mysql.host'),
|
|
|
|
'DB_PORT' => config('database.connections.mysql.port'),
|
|
|
|
'DB_DATABASE' => config('database.connections.mysql.database'),
|
|
|
|
'DB_USERNAME' => config('database.connections.mysql.username'),
|
|
|
|
'DB_PASSWORD' => config('database.connections.mysql.password'),
|
|
|
|
'DB_PREFIX' => config('database.connections.mysql.prefix'),
|
|
|
|
'DB_EMULATE_PREPARES' => $emulate_prepares,
|
|
|
|
'CACHE_DRIVER' => config('cache.default'),
|
|
|
|
'CACHE_PREFIX' => config('cache.prefix'),
|
|
|
|
'MAIL_DRIVER' => config('mail.default'),
|
|
|
|
'MAIL_HOST' => config('mail.mailers.smtp.host'),
|
|
|
|
'MAIL_PORT' => config('mail.mailers.smtp.port'),
|
|
|
|
'MAIL_ENCRYPTION' => config('mail.mailers.smtp.encryption'),
|
|
|
|
'MAIL_USERNAME' => config('mail.mailers.smtp.username'),
|
|
|
|
'MAIL_PASSWORD' => config('mail.mailers.smtp.password'),
|
|
|
|
'MAIL_FROM_NAME' => config('mail.from.name'),
|
|
|
|
'MAIL_FROM_ADDRESS' => config('mail.from.address'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->writeConfigFiles($opts);
|
|
|
|
|
|
|
|
// Rename the old config file
|
|
|
|
$cfg_file = App::environmentPath().'/config.php';
|
|
|
|
if (file_exists($cfg_file)) {
|
|
|
|
rename($cfg_file, App::environmentPath().'/config.bak.php');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 09:24:06 +08:00
|
|
|
/**
|
|
|
|
* Update the environment file and update certain keys/values
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 09:24:06 +08:00
|
|
|
* @param array $kvp
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 09:24:06 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateKeysInEnv(array $kvp)
|
|
|
|
{
|
|
|
|
$app = app();
|
|
|
|
|
|
|
|
$env_file = file_get_contents($app->environmentFilePath());
|
2019-08-27 00:32:46 +08:00
|
|
|
foreach ($kvp as $key => $value) {
|
2018-01-19 10:40:25 +08:00
|
|
|
$key = strtoupper($key);
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// cast for any boolean values
|
|
|
|
if (is_bool($value)) {
|
2018-01-19 09:24:06 +08:00
|
|
|
$value = $value === true ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// surround by quotes if there are any spaces in the value
|
|
|
|
if (strpos($value, ' ') !== false) {
|
2018-01-19 10:40:25 +08:00
|
|
|
$value = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
Log::info('Replacing "'.$key.'" with '.$value);
|
2018-01-19 10:40:25 +08:00
|
|
|
|
2018-01-19 09:24:06 +08:00
|
|
|
$env_file = preg_replace(
|
2019-08-27 00:32:46 +08:00
|
|
|
'/^'.$key.'(.*)?/m',
|
|
|
|
$key.'='.$value,
|
2018-01-19 09:24:06 +08:00
|
|
|
$env_file
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($app->environmentFilePath(), $env_file);
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
/**
|
|
|
|
* Generate a fresh new APP_KEY
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-19 10:40:25 +08:00
|
|
|
protected function createAppKey(): string
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
2021-04-11 05:19:45 +08:00
|
|
|
return 'base64:'.base64_encode(Encrypter::generateKey(config('app.cipher')));
|
2017-12-15 07:12:42 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
/**
|
2018-01-19 09:24:06 +08:00
|
|
|
* Change a few options within the PDO driver, depending on the version
|
|
|
|
* of mysql/maria, etc used. ATM, only make a change for MariaDB
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-10 13:05:52 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-10 13:05:52 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function determinePdoOptions($opts)
|
|
|
|
{
|
2021-04-11 05:19:45 +08:00
|
|
|
if ($opts['DB_CONNECTION'] !== 'mysql') {
|
2018-01-19 09:24:06 +08:00
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
$dsn = "mysql:host=$opts[DB_HOST];port=$opts[DB_PORT];";
|
2019-08-27 00:32:46 +08:00
|
|
|
Log::info('Connection string: '.$dsn);
|
2018-01-10 13:05:52 +08:00
|
|
|
|
2021-04-11 05:19:45 +08:00
|
|
|
$conn = new PDO($dsn, $opts['DB_USERNAME'], $opts['DB_PASSWORD']);
|
2018-01-10 13:05:52 +08:00
|
|
|
$version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION));
|
2018-01-10 13:17:06 +08:00
|
|
|
Log::info('Detected DB Version: '.$version);
|
2018-01-10 13:05:52 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// If it's mariadb, enable the emulation for prepared statements
|
|
|
|
// seems to be throwing a problem on 000webhost
|
|
|
|
// https://github.com/nabeelio/phpvms/issues/132
|
|
|
|
if (strpos($version, 'mariadb') !== false) {
|
2018-01-10 13:17:06 +08:00
|
|
|
Log::info('Detected MariaDB, setting DB_EMULATE_PREPARES to true');
|
2018-01-10 13:05:52 +08:00
|
|
|
$opts['DB_EMULATE_PREPARES'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
/**
|
|
|
|
* Determine is APC is installed, if so, then use it as a cache driver
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-07-18 21:26:33 +08:00
|
|
|
protected function configCacheDriver($opts)
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
2019-07-18 21:26:33 +08:00
|
|
|
// Set the cache prefix
|
2021-04-11 09:13:34 +08:00
|
|
|
$prefix = substr(str_slug($opts['SITE_NAME'], '_'), 0, 8);
|
|
|
|
$opts['CACHE_PREFIX'] = strtolower(uniqid($prefix.'_'));
|
2019-07-18 21:26:33 +08:00
|
|
|
|
|
|
|
// Figure out what cache driver to initially use, depending on
|
|
|
|
// what is installed. It won't detect redis or anything, though
|
|
|
|
foreach (config('installer.cache.drivers') as $ext => $driver) {
|
|
|
|
if (extension_loaded($ext)) {
|
|
|
|
Log::info('Detected extension "'.$ext.'", setting driver to "'.$driver.'"');
|
|
|
|
$opts['CACHE_DRIVER'] = $driver;
|
|
|
|
return $opts;
|
|
|
|
}
|
2017-12-15 07:12:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 21:26:33 +08:00
|
|
|
Log::info('No extension detected, using file cache');
|
|
|
|
$opts['CACHE_DRIVER'] = config('installer.cache.default');
|
2017-12-15 07:12:42 +08:00
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup a queue driver that's not the default "sync"
|
|
|
|
* driver, if a database is being used
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-07-18 21:26:33 +08:00
|
|
|
protected function configQueueDriver($opts)
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
// If we're setting up a database, then also setup
|
|
|
|
// the default queue driver to use the database
|
2021-04-11 05:19:45 +08:00
|
|
|
if ($opts['DB_CONNECTION'] === 'mysql' || $opts['DB_CONNECTION'] === 'postgres') {
|
2017-12-15 07:12:42 +08:00
|
|
|
$opts['QUEUE_DRIVER'] = 'database';
|
|
|
|
} else {
|
|
|
|
$opts['QUEUE_DRIVER'] = 'sync';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2018-02-26 05:13:04 +08:00
|
|
|
/**
|
|
|
|
* Remove the config files
|
|
|
|
*/
|
|
|
|
public function removeConfigFiles()
|
|
|
|
{
|
2019-07-18 21:26:33 +08:00
|
|
|
$env_file = App::environmentFilePath();
|
|
|
|
$config_file = App::environmentPath().'/config.php';
|
2018-02-26 05:13:04 +08:00
|
|
|
|
|
|
|
if (file_exists($env_file)) {
|
|
|
|
try {
|
|
|
|
unlink($env_file);
|
2019-07-18 21:26:33 +08:00
|
|
|
} catch (Exception $e) {
|
2018-02-26 05:13:04 +08:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
if (file_exists($config_file)) {
|
2018-02-26 05:13:04 +08:00
|
|
|
try {
|
|
|
|
unlink($config_file);
|
2019-07-18 21:26:33 +08:00
|
|
|
} catch (Exception $e) {
|
2018-02-26 05:13:04 +08:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Get the template file name and write it out
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2021-04-11 05:19:45 +08:00
|
|
|
* @throws FileException
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
2018-01-30 03:16:39 +08:00
|
|
|
protected function writeConfigFiles($opts)
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2018-01-30 03:16:39 +08:00
|
|
|
Stub::setBasePath(resource_path('/stubs/installer'));
|
|
|
|
|
2019-07-18 21:26:33 +08:00
|
|
|
$env_file = App::environmentFilePath();
|
2019-08-27 00:32:46 +08:00
|
|
|
if (file_exists($env_file) && !is_writable($env_file)) {
|
2017-12-18 06:58:53 +08:00
|
|
|
Log::error('Permissions on existing env.php is not writable');
|
2019-08-27 00:32:46 +08:00
|
|
|
|
2017-12-18 06:58:53 +08:00
|
|
|
throw new FileException('Can\'t write to the env.php file! Check the permissions');
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
/*
|
2018-01-30 03:16:39 +08:00
|
|
|
* First write out the env file
|
|
|
|
*/
|
2018-01-29 08:50:43 +08:00
|
|
|
try {
|
|
|
|
$stub = new Stub('/env.stub', $opts);
|
|
|
|
$stub->render();
|
2019-07-18 21:26:33 +08:00
|
|
|
$stub->saveTo(App::environmentPath(), App::environmentFile());
|
2019-08-27 00:32:46 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new FileException('Couldn\'t write env.php. ('.$e.')');
|
2018-01-29 08:50:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
/*
|
2018-01-30 03:16:39 +08:00
|
|
|
* Next write out the config file. If there's an error here,
|
|
|
|
* then throw an exception but delete the env file first
|
|
|
|
*/
|
2021-04-11 05:19:45 +08:00
|
|
|
/*try {
|
2018-01-30 03:16:39 +08:00
|
|
|
$stub = new Stub('/config.stub', $opts);
|
|
|
|
$stub->render();
|
2019-07-18 21:26:33 +08:00
|
|
|
$stub->saveTo(App::environmentPath(), 'config.php');
|
|
|
|
} catch (Exception $e) {
|
2019-08-27 00:32:46 +08:00
|
|
|
unlink(App::environmentPath().'/'.App::environmentFile());
|
|
|
|
|
|
|
|
throw new FileException('Couldn\'t write config.php. ('.$e.')');
|
2021-04-11 05:19:45 +08:00
|
|
|
}*/
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
}
|