1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Diagnostics;
use Piwik\Development;
use Matomo\Ini\IniReader;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Settings as PiwikSettings;
/**
* A diagnostic report contains all the results of all the diagnostics.
*/
class ConfigReader
{
/**
* @var GlobalSettingsProvider
*/
private $settings;
/**
* @var IniReader
*/
private $iniReader;
public function __construct(GlobalSettingsProvider $settings, IniReader $iniReader)
{
$this->settings = $settings;
$this->iniReader = $iniReader;
}
public function getConfigValuesFromFiles()
{
$ini = $this->settings->getIniFileChain();
$descriptions = $this->iniReader->readComments($this->settings->getPathGlobal());
$copy = array();
foreach ($ini->getAll() as $category => $values) {
if ($this->shouldSkipCategory($category)) {
continue;
}
$local = $this->getFromLocalConfig($category);
if (empty($local)) {
$local = array();
}
$global = $this->getFromGlobalConfig($category);
if (empty($global)) {
$global = array();
}
$copy[$category] = array();
foreach ($values as $key => $value) {
$newValue = $value;
if ($this->isKeyAPassword($key)) {
$newValue = $this->getMaskedPassword();
}
$defaultValue = null;
if (array_key_exists($key, $global)) {
$defaultValue = $global[$key];
}
$description = '';
if (!empty($descriptions[$category][$key])) {
$description = trim($descriptions[$category][$key]);
}
$copy[$category][$key] = array(
'value' => $newValue,
'description' => $description,
'isCustomValue' => array_key_exists($key, $local),
'defaultValue' => $defaultValue,
);
}
}
return $copy;
}
private function shouldSkipCategory($category)
{
$category = strtolower($category);
if ($category === 'database' || $category === 'database_reader') {
return true;
}
$developmentOnlySections = array('database_tests', 'tests', 'debugtests');
return !Development::isEnabled() && in_array($category, $developmentOnlySections);
}
public function getFromGlobalConfig($name)
{
return $this->settings->getIniFileChain()->getFrom($this->settings->getPathGlobal(), $name);
}
public function getFromLocalConfig($name)
{
return $this->settings->getIniFileChain()->getFrom($this->settings->getPathLocal(), $name);
}
private function getMaskedPassword()
{
return '******';
}
private function isKeyAPassword($key)
{
$key = strtolower($key);
$passwordFields = array(
'password', 'secret', 'apikey', 'privatekey', 'admin_pass', 'md5', 'sha1',
);
foreach ($passwordFields as $value) {
if (strpos($key, $value) !== false) {
return true;
}
}
if ($key === 'salt') {
return true;
}
return false;
}
/**
* Adds config values that can be used to overwrite a plugin system setting and adds a description + default value
* for already existing configured config values that overwrite a plugin system setting.
*
* @param array $configValues
* @param \Piwik\Settings\Plugin\SystemSettings[] $systemSettings
* @return array
*/
public function addConfigValuesFromSystemSettings($configValues, $systemSettings)
{
foreach ($systemSettings as $pluginSetting) {
$pluginName = $pluginSetting->getPluginName();
if (empty($pluginName)) {
continue;
}
if (!array_key_exists($pluginName, $configValues)) {
$configValues[$pluginName] = array();
}
foreach ($pluginSetting->getSettingsWritableByCurrentUser() as $setting) {
$name = $setting->getName();
$configSection = $pluginName;
if ($setting instanceof PiwikSettings\Plugin\SystemConfigSetting) {
$configSection = $setting->getConfigSectionName();
if ($this->shouldSkipCategory($configSection)) {
continue;
}
}
$config = $setting->configureField();
$description = '';
if (!empty($config->description)) {
$description .= $config->description . ' ';
}
if (!empty($config->inlineHelp)) {
$description .= $config->inlineHelp;
}
if (isset($configValues[$configSection][$name])) {
$configValues[$configSection][$name]['defaultValue'] = $setting->getDefaultValue();
$configValues[$configSection][$name]['description'] = trim($description);
} else {
$configValues[$configSection][$name] = array(
'value' => $setting->getValue() != $setting->getDefaultValue() ? $setting->getValue() : null,
'description' => trim($description),
'isCustomValue' => $setting->getValue() != $setting->getDefaultValue(),
'defaultValue' => $setting->getDefaultValue(),
);
}
if (
!empty($configValues[$configSection][$name]['value'])
&& $config->uiControl === PiwikSettings\FieldConfig::UI_CONTROL_PASSWORD
) {
$configValues[$configSection][$name]['value'] = $this->getMaskedPassword();
}
}
if (empty($configValues[$pluginName])) {
unset($configValues[$pluginName]);
}
}
return $configValues;
}
}
|