Skip to content

Commit

Permalink
Get default config values from Config\Settings classes
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Sep 30, 2021
1 parent d29b99c commit 67e96cf
Show file tree
Hide file tree
Showing 83 changed files with 116 additions and 505 deletions.
6 changes: 1 addition & 5 deletions doc/setup.rst
Expand Up @@ -519,7 +519,7 @@ To manually create the file, simply use your text editor to create the
file :file:`config.inc.php` (you can copy :file:`config.sample.inc.php` to get
a minimal configuration file) in the main (top-level) phpMyAdmin
directory (the one that contains :file:`index.php`). phpMyAdmin first
loads :file:`libraries/config.default.php` and then overrides those values
loads the default configuration values and then overrides those values
with anything found in :file:`config.inc.php`. If the default value is
okay for a particular setting, there is no need to include it in
:file:`config.inc.php`. You'll probably need only a few directives to get going; a
Expand Down Expand Up @@ -852,10 +852,6 @@ For compatibility with PHP 5.3 and later, remove a
``set_magic_quotes_runtime(0);`` statement that you might find near
the end of your configuration file.

You should **not** copy :file:`libraries/config.default.php` over
:file:`config.inc.php` because the default configuration file is version-
specific.

The complete upgrade can be performed in a few simple steps:

1. Download the latest phpMyAdmin version from <https://www.phpmyadmin.net/downloads/>.
Expand Down
78 changes: 12 additions & 66 deletions libraries/classes/Config.php
@@ -1,12 +1,11 @@
<?php
/**
* Configuration handling.
*/

declare(strict_types=1);

namespace PhpMyAdmin;

use PhpMyAdmin\Config\Settings;

use function __;
use function array_filter;
use function array_merge;
Expand All @@ -25,6 +24,7 @@
use function fread;
use function function_exists;
use function gd_info;
use function get_object_vars;
use function implode;
use function ini_get;
use function intval;
Expand All @@ -34,7 +34,6 @@
use function is_readable;
use function is_string;
use function is_writable;
use function max;
use function mb_strstr;
use function mb_strtolower;
use function md5;
Expand Down Expand Up @@ -69,13 +68,10 @@
use const PHP_VERSION_ID;

/**
* Configuration class
* Configuration handling
*/
class Config
{
/** @var string default config source */
public $defaultSource = ROOT_PATH . 'libraries/config.default.php';

/** @var array default configuration settings */
public $default = [];

Expand All @@ -91,18 +87,12 @@ class Config
/** @var int source modification time */
public $sourceMtime = 0;

/** @var int */
public $defaultSourceMtime = 0;

/** @var int */
public $setMtime = 0;

/** @var bool */
public $errorConfigFile = false;

/** @var bool */
public $errorConfigDefaultFile = false;

/** @var array */
public $defaultServer = [];

Expand Down Expand Up @@ -333,51 +323,17 @@ public function checkWebServerOs(): void
/**
* loads default values from default source
*/
public function loadDefaults(): bool
public function loadDefaults(): void
{
global $isConfigLoading;

/** @var array<string,mixed> $cfg */
$cfg = [];
if (! @file_exists($this->defaultSource)) {
$this->errorConfigDefaultFile = true;

return false;
}

$canUseErrorReporting = Util::isErrorReportingAvailable();
$oldErrorReporting = null;
if ($canUseErrorReporting) {
$oldErrorReporting = error_reporting(0);
}
$settings = new Settings([]);
$cfg = $settings->toArray();

ob_start();
$isConfigLoading = true;
$eval_result = include $this->defaultSource;
$isConfigLoading = false;
ob_end_clean();

if ($canUseErrorReporting) {
error_reporting($oldErrorReporting);
}

if ($eval_result === false) {
$this->errorConfigDefaultFile = true;

return false;
}

$this->defaultSourceMtime = filemtime($this->defaultSource);

$this->defaultServer = $cfg['Servers'][1];
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->defaultServer = get_object_vars($settings->Servers[1]);
unset($cfg['Servers']);

$this->default = $cfg;
$this->settings = array_replace_recursive($this->settings, $cfg);

$this->errorConfigDefaultFile = false;

return true;
}

/**
Expand Down Expand Up @@ -427,7 +383,7 @@ public function load(?string $source = null): bool
$this->errorConfigFile = true;
} else {
$this->errorConfigFile = false;
$this->sourceMtime = filemtime($this->getSource());
$this->sourceMtime = (int) filemtime($this->getSource());
}

/**
Expand Down Expand Up @@ -485,17 +441,16 @@ public function loadUserPreferences(): void
: 0);
$cache_key = 'server_' . $server;
if ($server > 0 && ! isset($isMinimumCommon)) {
$config_mtime = max($this->defaultSourceMtime, $this->sourceMtime);
// cache user preferences, use database only when needed
if (
! isset($_SESSION['cache'][$cache_key]['userprefs'])
|| $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
|| $_SESSION['cache'][$cache_key]['config_mtime'] < $this->sourceMtime
) {
$prefs = $userPreferences->load();
$_SESSION['cache'][$cache_key]['userprefs'] = $userPreferences->apply($prefs['config_data']);
$_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
$_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
$_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
$_SESSION['cache'][$cache_key]['config_mtime'] = $this->sourceMtime;
}
} elseif ($server == 0 || ! isset($_SESSION['cache'][$cache_key]['userprefs'])) {
$this->set('user_preferences', false);
Expand Down Expand Up @@ -738,15 +693,6 @@ public function checkPermissions(): void
*/
public function checkErrors(): void
{
if ($this->errorConfigDefaultFile) {
Core::fatalError(
sprintf(
__('Could not load default configuration from: %1$s'),
$this->defaultSource
)
);
}

if (! $this->errorConfigFile) {
return;
}
Expand Down
14 changes: 8 additions & 6 deletions libraries/classes/Config/ConfigFile.php
Expand Up @@ -24,7 +24,9 @@
class ConfigFile
{
/**
* Stores default PMA config from config.default.php
* Stores default phpMyAdmin config
*
* @see Settings
*
* @var array
*/
Expand Down Expand Up @@ -96,16 +98,16 @@ class ConfigFile
public function __construct($baseConfig = null)
{
// load default config values
$cfg = &$this->defaultCfg;
include ROOT_PATH . 'libraries/config.default.php';
$settings = new Settings([]);
$this->defaultCfg = $settings->toArray();

// load additional config information
$this->cfgDb = include ROOT_PATH . 'libraries/config.values.php';

// apply default values overrides
if (count($this->cfgDb['_overrides'])) {
foreach ($this->cfgDb['_overrides'] as $path => $value) {
Core::arrayWrite($path, $cfg, $value);
Core::arrayWrite($path, $this->defaultCfg, $value);
}
}

Expand Down Expand Up @@ -291,7 +293,7 @@ function ($value, $key, $prefix): void {

/**
* Updates config with values read from given array
* (config will contain differences to defaults from config.defaults.php).
* (config will contain differences to defaults from {@see \PhpMyAdmin\Config\Settings}).
*
* @param array $cfg Configuration
*/
Expand Down Expand Up @@ -336,7 +338,7 @@ public function get($path, $default = null)

/**
* Returns default config value or $default it it's not set ie. it doesn't
* exist in config.default.php ($cfg) and config.values.php
* exist in {@see \PhpMyAdmin\Config\Settings} ($cfg) and config.values.php
* ($_cfg_db['_overrides'])
*
* @param string $canonicalPath Canonical path
Expand Down

0 comments on commit 67e96cf

Please sign in to comment.