From fa31bb6dde8106fcc7e16c1f6c359f2d58723805 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sat, 23 Mar 2019 15:40:09 +0100 Subject: [PATCH] remove basepath and hostname from admin panel and add update path --- config/dbstructure.config.php | 2 +- mod/admin.php | 14 +---- src/Util/Config/ConfigCacheLoader.php | 8 +-- src/Util/Config/ConfigCacheManager.php | 54 +++++++++++++++++-- src/Util/Config/ConfigCacheSaver.php | 41 ++++++++++++-- .../src/Util/Config/ConfigCacheLoaderTest.php | 28 ++++++++-- .../src/Util/Config/ConfigCacheSaverTest.php | 13 +++-- update.php | 30 +++++++++++ view/templates/admin/site.tpl | 2 - view/templates/local.config.tpl | 1 + 10 files changed, 158 insertions(+), 35 deletions(-) diff --git a/config/dbstructure.config.php b/config/dbstructure.config.php index f03132adda..97068415a4 100644 --- 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', 1304); + define('DB_UPDATE_VERSION', 1305); } return [ diff --git a/mod/admin.php b/mod/admin.php index f8a75b7a2d..7808a87ef8 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -1087,6 +1087,8 @@ function admin_page_site_post(App $a) update_table($a, "gcontact", ['connect', 'addr'], $old_host, $new_host); // update config + $configCacheSaver = new \Friendica\Util\Config\ConfigCacheSaver($a->getBasePath()); + $configCacheSaver->saveToConfigFile('system', 'hostname', parse_url($new_url, PHP_URL_HOST)); Config::set('system', 'hostname', parse_url($new_url, PHP_URL_HOST)); Config::set('system', 'url', $new_url); $a->setBaseURL($new_url); @@ -1105,7 +1107,6 @@ function admin_page_site_post(App $a) // end relocate $sitename = (!empty($_POST['sitename']) ? Strings::escapeTags(trim($_POST['sitename'])) : ''); - $hostname = (!empty($_POST['hostname']) ? Strings::escapeTags(trim($_POST['hostname'])) : ''); $sender_email = (!empty($_POST['sender_email']) ? Strings::escapeTags(trim($_POST['sender_email'])) : ''); $banner = (!empty($_POST['banner']) ? trim($_POST['banner']) : false); $shortcut_icon = (!empty($_POST['shortcut_icon']) ? Strings::escapeTags(trim($_POST['shortcut_icon'])) : ''); @@ -1176,7 +1177,6 @@ function admin_page_site_post(App $a) $itemcache_duration = (!empty($_POST['itemcache_duration']) ? intval($_POST['itemcache_duration']) : 0); $max_comments = (!empty($_POST['max_comments']) ? intval($_POST['max_comments']) : 0); $temppath = (!empty($_POST['temppath']) ? Strings::escapeTags(trim($_POST['temppath'])) : ''); - $basepath = (!empty($_POST['basepath']) ? Strings::escapeTags(trim($_POST['basepath'])) : ''); $singleuser = (!empty($_POST['singleuser']) ? Strings::escapeTags(trim($_POST['singleuser'])) : ''); $proxy_disabled = !empty($_POST['proxy_disabled']); $only_tag_search = !empty($_POST['only_tag_search']); @@ -1296,7 +1296,6 @@ function admin_page_site_post(App $a) Config::set('system', 'poco_local_search' , $poco_local_search); Config::set('system', 'nodeinfo' , $nodeinfo); Config::set('config', 'sitename' , $sitename); - Config::set('config', 'hostname' , $hostname); Config::set('config', 'sender_email' , $sender_email); Config::set('system', 'suppress_tags' , $suppress_tags); Config::set('system', 'shortcut_icon' , $shortcut_icon); @@ -1392,10 +1391,6 @@ function admin_page_site_post(App $a) Config::set('system', 'temppath', $temppath); - if ($basepath != '') { - $basepath = BasePath::getRealPath($basepath); - } - Config::set('system', 'basepath' , $basepath); Config::set('system', 'proxy_disabled' , $proxy_disabled); Config::set('system', 'only_tag_search' , $only_tag_search); @@ -1536,9 +1531,6 @@ function admin_page_site(App $a) "develop" => L10n::t("check the development version") ]; - if (empty(Config::get('config', 'hostname'))) { - Config::set('config', 'hostname', $a->getHostName()); - } $diaspora_able = ($a->getURLPath() == ""); $optimize_max_tablesize = Config::get('system', 'optimize_max_tablesize', -1); @@ -1597,7 +1589,6 @@ function admin_page_site(App $a) // name, label, value, help string, extra data... '$sitename' => ['sitename', L10n::t("Site name"), Config::get('config', 'sitename'), ''], - '$hostname' => ['hostname', L10n::t("Host name"), Config::get('config', 'hostname'), ""], '$sender_email' => ['sender_email', L10n::t("Sender Email"), Config::get('config', 'sender_email'), L10n::t("The email address your server shall use to send notification emails from."), "", "", "email"], '$banner' => ['banner', L10n::t("Banner/Logo"), $banner, ""], '$shortcut_icon' => ['shortcut_icon', L10n::t("Shortcut icon"), Config::get('system', 'shortcut_icon'), L10n::t("Link to an icon that will be used for browsers.")], @@ -1675,7 +1666,6 @@ function admin_page_site(App $a) '$itemcache_duration' => ['itemcache_duration', L10n::t("Cache duration in seconds"), Config::get('system', 'itemcache_duration'), L10n::t("How long should the cache files be hold? Default value is 86400 seconds \x28One day\x29. To disable the item cache, set the value to -1.")], '$max_comments' => ['max_comments', L10n::t("Maximum numbers of comments per post"), Config::get('system', 'max_comments'), L10n::t("How much comments should be shown for each post? Default value is 100.")], '$temppath' => ['temppath', L10n::t("Temp path"), Config::get('system', 'temppath'), L10n::t("If you have a restricted system where the webserver can't access the system temp path, enter another path here.")], - '$basepath' => ['basepath', L10n::t("Base path to installation"), Config::get('system', 'basepath'), L10n::t("If the system cannot detect the correct path to your installation, enter the correct path here. This setting should only be set if you are using a restricted system and symbolic links to your webroot.")], '$proxy_disabled' => ['proxy_disabled', L10n::t("Disable picture proxy"), Config::get('system', 'proxy_disabled'), L10n::t("The picture proxy increases performance and privacy. It shouldn't be used on systems with very low bandwidth.")], '$only_tag_search' => ['only_tag_search', L10n::t("Only search in tags"), Config::get('system', 'only_tag_search'), L10n::t("On large systems the text search can slow down the system extremely.")], diff --git a/src/Util/Config/ConfigCacheLoader.php b/src/Util/Config/ConfigCacheLoader.php index f2f4f75422..6eced061ed 100644 --- a/src/Util/Config/ConfigCacheLoader.php +++ b/src/Util/Config/ConfigCacheLoader.php @@ -57,13 +57,13 @@ class ConfigCacheLoader extends ConfigCacheManager /** * Tries to load the specified core-configuration and returns the config array. * - * @param string $name The name of the configuration + * @param string $name The name of the configuration (default is empty, which means 'local') * * @return array The config array (empty if no config found) * * @throws \Exception if the configuration file isn't readable */ - public function loadCoreConfig($name) + public function loadCoreConfig($name = '') { if (!empty($this->getConfigFullName($name))) { return $this->loadConfigFile($this->getConfigFullName($name)); @@ -101,13 +101,13 @@ class ConfigCacheLoader extends ConfigCacheManager /** * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array. * - * @param string $name The name of the config file + * @param string $name The name of the config file (default is empty, which means .htconfig.php) * * @return array The configuration array (empty if no config found) * * @deprecated since version 2018.09 */ - private function loadLegacyConfig($name) + private function loadLegacyConfig($name = '') { $config = []; if (!empty($this->getHtConfigFullName($name))) { diff --git a/src/Util/Config/ConfigCacheManager.php b/src/Util/Config/ConfigCacheManager.php index 1981717706..8fb923216c 100644 --- a/src/Util/Config/ConfigCacheManager.php +++ b/src/Util/Config/ConfigCacheManager.php @@ -10,29 +10,77 @@ abstract class ConfigCacheManager */ const SUBDIRECTORY = 'config'; + /** + * The default name of the user defined config file + * @var string + */ + const CONFIG_LOCAL = 'local'; + + /** + * The default name of the user defined ini file + * @var string + */ + const CONFIG_INI = 'ini'; + + /** + * The default name of the user defined legacy config file + * @var string + */ + const CONFIG_HTCONFIG = 'htconfig'; + protected $baseDir; protected $configDir; + /** + * @param string $baseDir The base directory of Friendica + */ public function __construct($baseDir) { $this->baseDir = $baseDir; $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY; } - protected function getConfigFullName($name) + /** + * Gets the full name (including the path) for a *.config.php (default is local.config.php) + * + * @param string $name The config name (default is empty, which means local.config.php) + * + * @return string The full name or empty if not found + */ + protected function getConfigFullName($name = '') { + $name = !empty($name) ? $name : self::CONFIG_LOCAL; + $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php'; return file_exists($fullName) ? $fullName : ''; } - protected function getIniFullName($name) + /** + * Gets the full name (including the path) for a *.ini.php (default is local.ini.php) + * + * @param string $name The config name (default is empty, which means local.ini.php) + * + * @return string The full name or empty if not found + */ + protected function getIniFullName($name = '') { + $name = !empty($name) ? $name : self::CONFIG_INI; + $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php'; return file_exists($fullName) ? $fullName : ''; } - protected function getHtConfigFullName($name) + /** + * Gets the full name (including the path) for a .*.php (default is .htconfig.php) + * + * @param string $name The config name (default is empty, which means .htconfig.php) + * + * @return string The full name or empty if not found + */ + protected function getHtConfigFullName($name = '') { + $name = !empty($name) ? $name : self::CONFIG_HTCONFIG; + $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php'; return file_exists($fullName) ? $fullName : ''; } diff --git a/src/Util/Config/ConfigCacheSaver.php b/src/Util/Config/ConfigCacheSaver.php index d84ec6a929..41ebca140f 100644 --- a/src/Util/Config/ConfigCacheSaver.php +++ b/src/Util/Config/ConfigCacheSaver.php @@ -20,18 +20,49 @@ class ConfigCacheSaver extends ConfigCacheManager const INDENT = "\t"; /** - * Saves a given value to the config file + * The settings array to save to + * @var array + */ + private $settings = []; + + /** + * Adds a given value to the config file * Either it replaces the current value or it will get added * * @param string $cat The configuration category * @param string $key The configuration key * @param string $value The new value */ - public function saveToConfigFile($cat, $key, $value) + public function addConfigValue($cat, $key, $value) { - $this->saveToLegacyConfig('htpreconfig', $cat, $key, $value); - $this->saveToLegacyConfig('htconfig', $cat, $key, $value); - $this->saveToCoreConfig('local', $cat, $key, $value); + $this->settings[$cat][$key] = $value; + } + + public function reset() + { + $this->settings = []; + } + + public function saveToConfigFile($name = '') + { + $saved = false; + + if (!empty($this->getConfigFullName($name))) { + $this->saveConfigFile($this->getConfigFullName($name)); + $saved = true; + } + + if (!empty($this->getIniFullName($name))) { + $this->saveINIConfigFile($this->getIniFullName($name)); + $saved = true; + } + + if (!empty($this->getHtConfigFullName($name))) { + $this->saveToLegacyConfig($this->getHtConfigFullName($name)); + $saved = true; + } + + return $saved; } /** diff --git a/tests/src/Util/Config/ConfigCacheLoaderTest.php b/tests/src/Util/Config/ConfigCacheLoaderTest.php index be0c5c01a5..45245d47cc 100644 --- a/tests/src/Util/Config/ConfigCacheLoaderTest.php +++ b/tests/src/Util/Config/ConfigCacheLoaderTest.php @@ -68,7 +68,12 @@ class ConfigCacheLoaderTest extends MockedTest { $this->delConfigFile('local.config.php'); - $file = dirname(__DIR__) . DIRECTORY_SEPARATOR; + $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR . + 'local.config.php'; vfsStream::newFile('local.config.php') ->at($this->root->getChild('config')) @@ -95,7 +100,12 @@ class ConfigCacheLoaderTest extends MockedTest { $this->delConfigFile('local.config.php'); - $file = dirname(__DIR__) . DIRECTORY_SEPARATOR; + $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR . + 'local.ini.php'; vfsStream::newFile('local.ini.php') ->at($this->root->getChild('config')) @@ -121,7 +131,12 @@ class ConfigCacheLoaderTest extends MockedTest { $this->delConfigFile('local.config.php'); - $file = dirname(__DIR__) . DIRECTORY_SEPARATOR; + $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR . + '.htconfig.test.php'; vfsStream::newFile('.htconfig.php') ->at($this->root) @@ -165,7 +180,12 @@ class ConfigCacheLoaderTest extends MockedTest vfsStream::create($structure, $this->root); - $file = dirname(__DIR__) . DIRECTORY_SEPARATOR; + $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR . + 'local.config.php'; vfsStream::newFile('test.config.php') ->at($this->root->getChild('addon')->getChild('test')->getChild('config')) diff --git a/tests/src/Util/Config/ConfigCacheSaverTest.php b/tests/src/Util/Config/ConfigCacheSaverTest.php index da2f050eb7..a0d9502c2a 100644 --- a/tests/src/Util/Config/ConfigCacheSaverTest.php +++ b/tests/src/Util/Config/ConfigCacheSaverTest.php @@ -32,30 +32,36 @@ class ConfigCacheSaverTest extends MockedTest { $this->delConfigFile('local.config.php'); $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . - '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php'; + vfsStream::newFile('local.config.php') ->at($this->root->getChild('config')) ->setContent(file_get_contents($file)); + $configCacheSaver = new ConfigCacheSaver($this->root->url()); $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode); $configCache = new ConfigCache(); $configCacheLoader->loadConfigFiles($configCache); + $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email')); - $this->assertEquals('!!', $configCache->get('config', 'test_val')); + $this->assertNull($configCache->get('config', 'test_val')); + $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it'); $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!'); + $newConfigCache = new ConfigCache(); $configCacheLoader->loadConfigFiles($newConfigCache); + $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email')); $this->assertEquals('Testing$!"$with@all.we can!', $newConfigCache->get('config', 'test_val')); $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')); $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old')); $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.tmp')); + $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old')->url())); } /** @@ -65,7 +71,6 @@ class ConfigCacheSaverTest extends MockedTest { $this->delConfigFile('local.config.php'); $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . - '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . @@ -79,7 +84,7 @@ class ConfigCacheSaverTest extends MockedTest $configCache = new ConfigCache(); $configCacheLoader->loadConfigFiles($configCache); $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email')); - $this->assertEquals('!!', $configCache->get('config', 'test_val')); + $this->assertNull($configCache->get('config', 'test_val')); $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it'); $configCacheSaver->saveToConfigFile('config', 'test_val', "Testing@with.all we can"); $newConfigCache = new ConfigCache(); diff --git a/update.php b/update.php index e619ec89dd..140c8fcb58 100644 --- a/update.php +++ b/update.php @@ -12,6 +12,7 @@ use Friendica\Model\Contact; use Friendica\Model\GContact; use Friendica\Model\Item; use Friendica\Model\User; +use Friendica\Util\Config\ConfigCacheSaver; use Friendica\Util\DateTimeFormat; /** @@ -346,3 +347,32 @@ function update_1298() } return Update::SUCCESS; } + +/** + * @see https://github.com/friendica/friendica/pull/6815 + * + * @return int Success + */ +function update_1303() +{ + $app = \Friendica\BaseObject::getApp(); + $configCache = $app->getConfigCache(); + $configCacheSaver = new ConfigCacheSaver($app->getBasePath()); + $updateConfigEntry = function($cat, $key) use ($configCache, $configCacheSaver) { + // check if the config file differs from the whole configuration (= The db contains other values) + $fileConfig = $configCache->get($cat, $key); + if ($fileConfig === '!!') { + $fileConfig = null; + } + $savedConfig = Config::get($cat, $key, null, true); + if ($fileConfig !== $savedConfig) { + Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileConfig, 'saved' => $savedConfig]); + $configCacheSaver->saveToConfigFile($cat, $key, $savedConfig); + } else { + Logger::info('No Difference in config found', ['cat' => $cat, 'key' => $key, 'value' => $fileConfig, 'saved' => $savedConfig]); + } + }; + $updateConfigEntry('config', 'hostname'); + $updateConfigEntry('system', 'basepath'); + return Update::SUCCESS; +} diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl index bc5e34c3c8..6da3e35f56 100644 --- a/view/templates/admin/site.tpl +++ b/view/templates/admin/site.tpl @@ -46,7 +46,6 @@ {{include file="field_input.tpl" field=$sitename}} - {{include file="field_input.tpl" field=$hostname}} {{include file="field_input.tpl" field=$sender_email}} {{include file="field_textarea.tpl" field=$banner}} {{include file="field_input.tpl" field=$shortcut_icon}} @@ -125,7 +124,6 @@ {{include file="field_input.tpl" field=$optimize_fragmentation}} {{include file="field_input.tpl" field=$abandon_days}} {{include file="field_input.tpl" field=$temppath}} - {{include file="field_input.tpl" field=$basepath}} {{include file="field_checkbox.tpl" field=$suppress_tags}} {{include file="field_checkbox.tpl" field=$nodeinfo}} {{include file="field_select.tpl" field=$check_new_version_url}} diff --git a/view/templates/local.config.tpl b/view/templates/local.config.tpl index c4c4afba4d..3201a99854 100644 --- a/view/templates/local.config.tpl +++ b/view/templates/local.config.tpl @@ -29,6 +29,7 @@ return [ ], 'system' => [ 'urlpath' => '{{$urlpath}}', + 'basepath' => '{{$basepath}}', 'default_timezone' => '{{$timezone}}', 'language' => '{{$language}}', ],