From d78598b64eae669ecb89d7d16fcdea8f8089a013 Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Mon, 25 Mar 2019 09:39:33 +0100
Subject: [PATCH 1/4] Fixing basepath issue

---
 config/dbstructure.config.php        |  2 +-
 src/Core/Update.php                  | 19 +++++++++++++++----
 src/Util/Config/ConfigFileLoader.php |  5 +++--
 update.php                           |  2 +-
 4 files changed, 20 insertions(+), 8 deletions(-)
 mode change 100644 => 100755 config/dbstructure.config.php

diff --git a/config/dbstructure.config.php b/config/dbstructure.config.php
old mode 100644
new mode 100755
index c2bf5c5eb9..0241d1f724
--- a/config/dbstructure.config.php
+++ b/config/dbstructure.config.php
@@ -34,7 +34,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-	define('DB_UPDATE_VERSION', 1306);
+	define('DB_UPDATE_VERSION', 1307);
 }
 
 return [
diff --git a/src/Core/Update.php b/src/Core/Update.php
index 7cb3212679..d0521a9041 100644
--- a/src/Core/Update.php
+++ b/src/Core/Update.php
@@ -6,6 +6,7 @@ use Friendica\App;
 use Friendica\Core\Config\Cache\IConfigCache;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
+use Friendica\Util\BasePath;
 use Friendica\Util\Config\ConfigFileLoader;
 use Friendica\Util\Config\ConfigFileSaver;
 use Friendica\Util\Strings;
@@ -235,7 +236,7 @@ class Update
 	{
 		$configFileLoader = new ConfigFileLoader($basePath, $mode);
 		$configCache = new Config\Cache\ConfigCache();
-		$configFileLoader->setupCache($configCache);
+		$configFileLoader->setupCache($configCache, true);
 		$configFileSaver = new ConfigFileSaver($basePath);
 
 		$updated = false;
@@ -243,10 +244,16 @@ class Update
 		if (self::updateConfigEntry($configCache, $configFileSaver,'config', 'hostname')) {
 			$updated = true;
 		};
-		if (self::updateConfigEntry($configCache, $configFileSaver,'system', 'basepath')) {
+
+		if (self::updateConfigEntry($configCache, $configFileSaver,'system', 'basepath', BasePath::create(dirname(__DIR__)))) {
 			$updated = true;
 		}
 
+		// In case there is nothing to do, skip the update
+		if (!$updated) {
+			return true;
+		}
+
 		if (!$configFileSaver->saveToConfigFile()) {
 			Logger::alert('Config entry update failed - maybe wrong permission?');
 			return false;
@@ -255,7 +262,7 @@ class Update
 		DBA::delete('config', ['cat' => 'config', 'k' => 'hostname']);
 		DBA::delete('config', ['cat' => 'system', 'k' => 'basepath']);
 
-		return $updated;
+		return true;
 	}
 
 	/**
@@ -265,12 +272,13 @@ class Update
 	 * @param ConfigFileSaver $configFileSaver The config file saver
 	 * @param string          $cat             The config category
 	 * @param string          $key             The config key
+	 * @param string          $default         A default value, if none of the settings are valid
 	 *
 	 * @return boolean True, if a value was updated
 	 *
 	 * @throws \Exception if DBA or Logger doesn't work
 	 */
-	private static function updateConfigEntry(IConfigCache $configCache, ConfigFileSaver $configFileSaver, $cat, $key)
+	private static function updateConfigEntry(IConfigCache $configCache, ConfigFileSaver $configFileSaver, $cat, $key, $default = '')
 	{
 		// check if the config file differs from the whole configuration (= The db contains other values)
 		$fileConfig = $configCache->get($cat, $key);
@@ -284,6 +292,9 @@ class Update
 		if ($fileConfig !== $savedConfig['v']) {
 			Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedConfig['v']]);
 			$configFileSaver->addConfigValue($cat, $key, $savedConfig['v']);
+		} elseif (empty($fileConfig) && empty($savedConfig)) {
+			Logger::info('Using default for config', ['cat' => $cat, 'key' => $key, 'value' => $default]);
+			$configFileSaver->addConfigValue($cat, $key, $default);
 		} else {
 			Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig['v']]);
 		}
diff --git a/src/Util/Config/ConfigFileLoader.php b/src/Util/Config/ConfigFileLoader.php
index d677d4bfe0..3d4f31a9d1 100644
--- a/src/Util/Config/ConfigFileLoader.php
+++ b/src/Util/Config/ConfigFileLoader.php
@@ -34,10 +34,11 @@ class ConfigFileLoader extends ConfigFileManager
 	 * expected local.config.php
 	 *
 	 * @param IConfigCache $config The config cache to load to
+	 * @param bool         $raw    Setup the raw config format
 	 *
 	 * @throws \Exception
 	 */
-	public function setupCache(IConfigCache $config)
+	public function setupCache(IConfigCache $config, $raw = false)
 	{
 		$config->load($this->loadCoreConfig('defaults'));
 		$config->load($this->loadCoreConfig('settings'));
@@ -48,7 +49,7 @@ class ConfigFileLoader extends ConfigFileManager
 		$config->load($this->loadCoreConfig('local'), true);
 
 		// In case of install mode, add the found basepath (because there isn't a basepath set yet
-		if ($this->appMode->isInstall()) {
+		if (!$raw && ($this->appMode->isInstall() || empty($config->get('system', 'basepath')))) {
 			// Setting at least the basepath we know
 			$config->set('system', 'basepath', $this->baseDir);
 		}
diff --git a/update.php b/update.php
index 8626ed2991..0ef74ea005 100644
--- a/update.php
+++ b/update.php
@@ -352,7 +352,7 @@ function update_1298()
  * @see https://github.com/friendica/friendica/pull/6920
  * @return int Success
  */
-function update_1305()
+function update_1307()
 {
 	$app = BaseObject::getApp();
 	if (Update::saveConfigToFile($app->getBasePath(), $app->getMode())) {

From 7d88b800b8a942557893458d2401ec2a8ed2aa02 Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Mon, 25 Mar 2019 20:53:46 +0100
Subject: [PATCH 2/4] Fixing Basepath

---
 src/Core/Update.php | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/Core/Update.php b/src/Core/Update.php
index d0521a9041..3b31b35376 100644
--- a/src/Core/Update.php
+++ b/src/Core/Update.php
@@ -245,7 +245,7 @@ class Update
 			$updated = true;
 		};
 
-		if (self::updateConfigEntry($configCache, $configFileSaver,'system', 'basepath', BasePath::create(dirname(__DIR__)))) {
+		if (self::updateConfigEntry($configCache, $configFileSaver,'system', 'basepath', BasePath::create(dirname(__DIR__) . '/../'))) {
 			$updated = true;
 		}
 
@@ -286,7 +286,7 @@ class Update
 		$savedConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
 
 		if (!DBA::isResult($savedConfig)) {
-			return false;
+			$savedConfig = null;
 		}
 
 		if ($fileConfig !== $savedConfig['v']) {
@@ -297,6 +297,7 @@ class Update
 			$configFileSaver->addConfigValue($cat, $key, $default);
 		} else {
 			Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig['v']]);
+			return false;
 		}
 
 		return true;

From 9ec29f1e8e0534fe9edbc943da16da2722be2115 Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Tue, 26 Mar 2019 08:00:41 +0100
Subject: [PATCH 3/4] straight the config value check

---
 src/Core/Update.php | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/Core/Update.php b/src/Core/Update.php
index 3b31b35376..017e2b82e8 100644
--- a/src/Core/Update.php
+++ b/src/Core/Update.php
@@ -285,22 +285,24 @@ class Update
 
 		$savedConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
 
-		if (!DBA::isResult($savedConfig)) {
-			$savedConfig = null;
-		}
-
-		if ($fileConfig !== $savedConfig['v']) {
+		// If the db contains a config value, check it
+		if (DBA::isResult($savedConfig) && $fileConfig !== $savedConfig['v']) {
 			Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedConfig['v']]);
 			$configFileSaver->addConfigValue($cat, $key, $savedConfig['v']);
-		} elseif (empty($fileConfig) && empty($savedConfig)) {
+			return true;
+
+		// If both config values are empty, use the default value
+		} elseif (empty($fileConfig) && !DBA::isResult($savedConfig)) {
 			Logger::info('Using default for config', ['cat' => $cat, 'key' => $key, 'value' => $default]);
 			$configFileSaver->addConfigValue($cat, $key, $default);
+			return true;
+
+		// If either the file config value isn't empty or the db value is the same as the
+		// file config value, skip it
 		} else {
 			Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig['v']]);
 			return false;
 		}
-
-		return true;
 	}
 
 	/**

From 040504dca99e07388a685f1d6056b750ed28be3e Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Tue, 26 Mar 2019 08:13:49 +0100
Subject: [PATCH 4/4] fixing config value setting

---
 src/Core/Update.php | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/Core/Update.php b/src/Core/Update.php
index 017e2b82e8..0d7b348b42 100644
--- a/src/Core/Update.php
+++ b/src/Core/Update.php
@@ -285,14 +285,20 @@ class Update
 
 		$savedConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
 
+		if (DBA::isResult($savedConfig)) {
+			$savedValue = $savedConfig['v'];
+		} else {
+			$savedValue = null;
+		}
+
 		// If the db contains a config value, check it
-		if (DBA::isResult($savedConfig) && $fileConfig !== $savedConfig['v']) {
-			Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedConfig['v']]);
-			$configFileSaver->addConfigValue($cat, $key, $savedConfig['v']);
+		if (isset($savedValue) && $fileConfig !== $savedValue) {
+			Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedValue]);
+			$configFileSaver->addConfigValue($cat, $key, $savedValue);
 			return true;
 
-		// If both config values are empty, use the default value
-		} elseif (empty($fileConfig) && !DBA::isResult($savedConfig)) {
+		// If both config values are not set, use the default value
+		} elseif (!isset($fileConfig) && !isset($savedValue)) {
 			Logger::info('Using default for config', ['cat' => $cat, 'key' => $key, 'value' => $default]);
 			$configFileSaver->addConfigValue($cat, $key, $default);
 			return true;
@@ -300,7 +306,7 @@ class Update
 		// If either the file config value isn't empty or the db value is the same as the
 		// file config value, skip it
 		} else {
-			Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig['v']]);
+			Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedValue]);
 			return false;
 		}
 	}