diff --git a/.gitignore b/.gitignore index 4be289b30d..b8ba5a10e3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ Thumbs.db .vscode ui composer.lock +.env +.env.local test.php core/config/common.config.php diff --git a/core/config/common.config.env.php b/core/config/common.config.env.php new file mode 100644 index 0000000000..eebb733422 --- /dev/null +++ b/core/config/common.config.env.php @@ -0,0 +1,47 @@ + [ + 'host' => $databaseConfig['host'], + 'port' => (string) ($databaseConfig['port'] ?? 3306), + 'dbname' => ltrim($databaseConfig['path'], '/'), + 'username' => $databaseConfig['user'], + 'password' => $databaseConfig['pass'], + ], +]; + +function readEnv(string $name): array +{ + $dotenv = dirname(__DIR__, 2) . '/'. $name; + if (!is_readable($dotenv)) { + return []; + } + + return parse_ini_file($dotenv, false) ?: []; +} \ No newline at end of file diff --git a/core/php/core.inc.php b/core/php/core.inc.php index 2502e1ea54..c6e20e33ab 100644 --- a/core/php/core.inc.php +++ b/core/php/core.inc.php @@ -17,7 +17,7 @@ */ date_default_timezone_set('Europe/Brussels'); require_once __DIR__ . '/../../vendor/autoload.php'; -require_once __DIR__ . '/../config/common.config.php'; +require_once __DIR__ . '/../config/common.config.env.php'; require_once __DIR__ . '/../class/DB.class.php'; require_once __DIR__ . '/../class/config.class.php'; require_once __DIR__ . '/../class/jeedom.class.php'; diff --git a/index.php b/index.php index 56e40f71c0..24c8c33b78 100644 --- a/index.php +++ b/index.php @@ -16,13 +16,14 @@ * along with Jeedom. If not, see . */ try { - //no config, install Jeedom! - if (!file_exists(__DIR__ . '/core/config/common.config.php')) { - echo 'Jeedom not configure, no common.config.php found'; - die(); - } + require_once __DIR__ . '/core/config/common.config.env.php'; +} catch (\RuntimeException $e) { + echo 'Jeedom not configure, no common.config.php found'; + die(); +} - require_once __DIR__ . "/core/php/core.inc.php"; +try { + require_once __DIR__ . "/core/php/core.inc.php"; if ((!isset($_GET['ajax']) || $_GET['ajax'] != 1) && count(system::ps('install/restore.php', 'sudo')) > 0) { require_once __DIR__.'/restoring.php'; diff --git a/install/OS_specific/Docker/init.sh b/install/OS_specific/Docker/init.sh index 1bb9c50f77..bd09fa8e53 100644 --- a/install/OS_specific/Docker/init.sh +++ b/install/OS_specific/Docker/init.sh @@ -75,7 +75,10 @@ echo 'Start init' # $WEBSERVER_HOME and $VERSION env variables comes from Dockerfile -if [ -f ${WEBSERVER_HOME}/core/config/common.config.php ]; then +if [ -f ${WEBSERVER_HOME}/.env ]; then + echo 'Jeedom is already install' + JEEDOM_INSTALL=1 +elif [ -f ${WEBSERVER_HOME}/core/config/common.config.php ]; then echo 'Jeedom is already install' JEEDOM_INSTALL=1 else @@ -95,12 +98,15 @@ else echo "DROP DATABASE IF EXISTS jeedom;" | mysql echo "CREATE DATABASE jeedom;" | mysql echo "GRANT ALL PRIVILEGES ON jeedom.* TO 'jeedom'@'localhost';" | mysql + # Part to remove cp ${WEBSERVER_HOME}/core/config/common.config.sample.php ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#PASSWORD#/${MYSQL_JEEDOM_PASSWD}/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#DBNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#USERNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#PORT#/3306/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#HOST#/localhost/g" ${WEBSERVER_HOME}/core/config/common.config.php + + echo "DATABASE_DSN=mysql://jeedom:${MYSQL_JEEDOM_PASSWD}@localhost/jeedom" > ${WEBSERVER_HOME}/.env /root/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME} /root/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME} fi diff --git a/install/backup.php b/install/backup.php index 43c7225e58..50c75ca514 100644 --- a/install/backup.php +++ b/install/backup.php @@ -141,6 +141,8 @@ '.gitignore', 'node_modules', '.log', + '.env', + '.env.local', 'core/config/common.config.php', 'data/imgOs', 'python_venv', diff --git a/install/database.php b/install/database.php index 0dfb23fb2a..40b7142cdf 100644 --- a/install/database.php +++ b/install/database.php @@ -20,7 +20,7 @@ */ require_once dirname(__DIR__).'/core/php/console.php'; -require_once __DIR__ . '/../core/config/common.config.php'; +require_once __DIR__ . '/../core/config/common.config.env.php'; require_once __DIR__ . '/../core/class/DB.class.php'; echo "[START CHECK AND FIX DB]\n"; try { diff --git a/install/install.php b/install/install.php index 598d71503f..a584206925 100644 --- a/install/install.php +++ b/install/install.php @@ -28,7 +28,7 @@ try { date_default_timezone_set('Europe/Brussels'); require_once __DIR__ . '/../vendor/autoload.php'; - require_once __DIR__ . '/../core/config/common.config.php'; + require_once __DIR__ . '/../core/config/common.config.env.php'; require_once __DIR__ . '/../core/class/DB.class.php'; require_once __DIR__ . '/../core/class/system.class.php'; if (count(system::ps('install/install.php', 'sudo')) > 1) { diff --git a/install/install.sh b/install/install.sh index 11e7acfc55..735a834d89 100644 --- a/install/install.sh +++ b/install/install.sh @@ -309,12 +309,15 @@ step_9_jeedom_configuration() { mariadb_sql "CREATE DATABASE jeedom;" mariadb_sql "GRANT ALL PRIVILEGES ON jeedom.* TO 'jeedom'@'localhost';" + # Path to remove cp ${WEBSERVER_HOME}/core/config/common.config.sample.php ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#PASSWORD#/${MARIADB_JEEDOM_PASSWD}/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#DBNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#USERNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#PORT#/3306/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#HOST#/localhost/g" ${WEBSERVER_HOME}/core/config/common.config.php + + echo "DATABASE_DSN=\"mysql://jeedom:${MARIADB_JEEDOM_PASSWD}@localhost:3306/jeedom\"" > ${WEBSERVER_HOME}/.env fi chmod 775 -R ${WEBSERVER_HOME} chown -R www-data:www-data ${WEBSERVER_HOME} diff --git a/install/packages.php b/install/packages.php index c6f62a3c06..2215019815 100644 --- a/install/packages.php +++ b/install/packages.php @@ -20,7 +20,7 @@ */ require_once dirname(__DIR__).'/core/php/console.php'; -require_once __DIR__ . '/../core/config/common.config.php'; +require_once __DIR__ . '/../core/config/common.config.env.php'; require_once __DIR__ . '/../core/class/system.class.php'; echo "[START CHECK AND FIX PACKAGES]\n"; try { diff --git a/install/restore.php b/install/restore.php index 880fba1251..e4457b8d32 100644 --- a/install/restore.php +++ b/install/restore.php @@ -93,7 +93,15 @@ if (!copy(__DIR__ . '/../core/config/common.config.php', '/tmp/common.config.php')) { echo 'Cannot copy ' . __DIR__ . "/../core/config/common.config.php\n"; } - + + if (!copy(__DIR__ . '/../.env', '/tmp/.env')) { + echo 'Cannot copy ' . dirname(__DIR__) . ".env\n"; + } + + if (!copy(__DIR__ . '/../.env', '/tmp/.env.local')) { + echo 'Cannot copy ' . dirname(__DIR__) . ".env.local\n"; + } + echo "OK\n"; try { @@ -111,6 +119,8 @@ '.git', '.log', 'core/config/common.config.php', + '.env', + '.env.local', '/vendor', config::byKey('backup::path'), ); @@ -187,7 +197,19 @@ copy('/tmp/common.config.php', __DIR__ . '/../core/config/common.config.php'); echo "OK\n"; } - + + if (!file_exists(dirname(__DIR__) . '/.env')) { + echo "Restoring .env file..."; + copy('/tmp/.env', dirname(__DIR__) . '/.env'); + echo "OK\n"; + } + + if (!file_exists(dirname(__DIR__) . '/.env.local')) { + echo "Restoring .env.local file..."; + copy('/tmp/.env.local', dirname(__DIR__) . '/.env.local'); + echo "OK\n"; + } + echo "Restoring cache..."; try { cache::restore(); diff --git a/sick.php b/sick.php index 0f8f290cea..4bcd054015 100644 --- a/sick.php +++ b/sick.php @@ -63,13 +63,13 @@ echo "Driver mysql disponible.\n"; } // check database configuration -if(!file_exists(__DIR__ . '/core/config/common.config.php')){ - echo 'Configuration manquante ! core/config/common.config.php non généré.'; - exit(1); +try { + require_once __DIR__ . '/core/config/common.config.env.php'; +} catch (\RuntimeException $e) { + echo 'Configuration manquante : ' . $e->getMessage(); + exit(1); } -require_once __DIR__ . '/core/config/common.config.php'; - // check local socket if localhost is configured if(isset($CONFIG['db']['unix_socket']) || (isset($CONFIG['db']['host']) && $CONFIG['db']['host'] == 'localhost')) {