- /**
- * Checks the config settings and saves given config values into the config file
- *
- * @param string $basePath The basepath of Friendica
- * @param App\Mode $mode The Application mode
- *
- * @return bool True, if something has been saved
- */
- public static function saveConfigToFile($basePath, App\Mode $mode)
- {
- $configFileLoader = new ConfigFileLoader($basePath, $mode);
- $configCache = new Config\Cache\ConfigCache();
- $configFileLoader->setupCache($configCache, true);
- $configFileSaver = new ConfigFileSaver($basePath);
-
- $updated = false;
-
- if (self::updateConfigEntry($configCache, $configFileSaver,'config', 'hostname')) {
- $updated = true;
- };
-
- 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;
- }
-
- DBA::delete('config', ['cat' => 'config', 'k' => 'hostname']);
- DBA::delete('config', ['cat' => 'system', 'k' => 'basepath']);
-
- return true;
- }
-
- /**
- * Adds a value to the ConfigFileSave in case it isn't already updated
- *
- * @param IConfigCache $configCache The cached config file
- * @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, $default = '')
- {
- // check if the config file differs from the whole configuration (= The db contains other values)
- $fileConfig = $configCache->get($cat, $key);
-
- $savedConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
-
- if (!DBA::isResult($savedConfig)) {
- return false;
- }
-
- 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']]);
- }
-
- return true;
- }
-