Cache.php
1 year ago
ConfigNotFoundException.php
2 years ago
DatabaseConfig.php
1 year ago
GeneralConfig.php
2 years ago
IniFileChain.php
1 month ago
SectionConfig.php
1 month ago
SectionConfig.php
238 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Config; |
| 10 | |
| 11 | use Piwik\Config; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Container\StaticContainer; |
| 14 | use Piwik\Log\LoggerInterface; |
| 15 | abstract class SectionConfig |
| 16 | { |
| 17 | public static abstract function getSectionName() : string; |
| 18 | /** |
| 19 | * Set the value for a setting |
| 20 | * |
| 21 | * @param string $name Setting name |
| 22 | * @param mixed $value Value |
| 23 | */ |
| 24 | public static function setConfigValue(string $name, $value) : void |
| 25 | { |
| 26 | $section = self::getConfig(); |
| 27 | $section[$name] = $value; |
| 28 | Config::getInstance()->{static::getSectionName()} = $section; |
| 29 | } |
| 30 | /** |
| 31 | * Get a setting value |
| 32 | * |
| 33 | * Prefer one of the type-safe getters if a specific type is expected: |
| 34 | * @see getIntegerConfigValue() |
| 35 | * @see getFloatConfigValue() |
| 36 | * @see getBoolConfigValue() |
| 37 | * @see getStringConfigValue() |
| 38 | * @see getArrayConfigValue() |
| 39 | * |
| 40 | * @param string $name Setting name |
| 41 | * @param int|null $idSite Optional site Id |
| 42 | * |
| 43 | * @return mixed|null |
| 44 | */ |
| 45 | public static function getConfigValue(string $name, ?int $idSite = null) |
| 46 | { |
| 47 | return static::getRawConfigValue($name, $idSite); |
| 48 | } |
| 49 | /** |
| 50 | * @phpstan-return ($default is null ? int|null : int) |
| 51 | */ |
| 52 | public static function getIntegerConfigValue(string $name, ?int $default = null, ?int $idSite = null) : ?int |
| 53 | { |
| 54 | return self::castIntConfigValue($name, static::getRawConfigValue($name, $idSite), $default); |
| 55 | } |
| 56 | /** |
| 57 | * @phpstan-return ($default is null ? float|null : float) |
| 58 | */ |
| 59 | public static function getFloatConfigValue(string $name, ?float $default = null, ?int $idSite = null) : ?float |
| 60 | { |
| 61 | return self::castFloatConfigValue($name, static::getRawConfigValue($name, $idSite), $default); |
| 62 | } |
| 63 | /** |
| 64 | * @phpstan-return ($default is null ? bool|null : bool) |
| 65 | */ |
| 66 | public static function getBoolConfigValue(string $name, ?bool $default = null, ?int $idSite = null) : ?bool |
| 67 | { |
| 68 | return self::castBoolConfigValue($name, static::getRawConfigValue($name, $idSite), $default); |
| 69 | } |
| 70 | /** |
| 71 | * @phpstan-return ($default is null ? string|null : string) |
| 72 | */ |
| 73 | public static function getStringConfigValue(string $name, ?string $default = null, ?int $idSite = null) : ?string |
| 74 | { |
| 75 | return self::castStringConfigValue($name, static::getRawConfigValue($name, $idSite), $default); |
| 76 | } |
| 77 | /** |
| 78 | * @return array<mixed>|null |
| 79 | * @phpstan-return ($default is null ? array<mixed>|null : array<mixed>) |
| 80 | */ |
| 81 | public static function getArrayConfigValue(string $name, ?array $default = null, ?int $idSite = null) : ?array |
| 82 | { |
| 83 | return self::castArrayConfigValue($name, static::getRawConfigValue($name, $idSite), $default); |
| 84 | } |
| 85 | /** |
| 86 | * @return mixed|null |
| 87 | */ |
| 88 | protected static function getRawConfigValue(string $name, ?int $idSite = null) |
| 89 | { |
| 90 | $config = self::getMergedConfig($idSite); |
| 91 | return $config[$name] ?? null; |
| 92 | } |
| 93 | /** |
| 94 | * Get the section config as an array |
| 95 | * |
| 96 | * @return array<string, mixed> |
| 97 | */ |
| 98 | private static function getConfig() : array |
| 99 | { |
| 100 | $config = Config::getInstance()->{static::getSectionName()}; |
| 101 | return is_array($config) ? $config : []; |
| 102 | } |
| 103 | /** |
| 104 | * Get the site specific config (if any) as an array |
| 105 | * |
| 106 | * @return array<string, mixed> |
| 107 | */ |
| 108 | private static function getSiteSpecificConfig(int $idSite) : array |
| 109 | { |
| 110 | $key = static::getSectionName() . '_' . $idSite; |
| 111 | $config = Config::getInstance()->{$key}; |
| 112 | return is_array($config) ? $config : []; |
| 113 | } |
| 114 | /** |
| 115 | * @return array<string, mixed> |
| 116 | */ |
| 117 | private static function getMergedConfig(?int $idSite = null) : array |
| 118 | { |
| 119 | $config = self::getConfig(); |
| 120 | if (!empty($idSite)) { |
| 121 | $config = array_merge($config, self::getSiteSpecificConfig($idSite)); |
| 122 | } |
| 123 | return $config; |
| 124 | } |
| 125 | /** |
| 126 | * @param mixed $value |
| 127 | */ |
| 128 | private static function castIntConfigValue(string $name, $value, ?int $default) : ?int |
| 129 | { |
| 130 | if ($value === null) { |
| 131 | return $default; |
| 132 | } |
| 133 | if ((is_string($value) || is_numeric($value)) && (string) $value === (string) (int) $value) { |
| 134 | return (int) $value; |
| 135 | } |
| 136 | self::logTypeCastWarning($name, 'int', $value); |
| 137 | return $default; |
| 138 | } |
| 139 | /** |
| 140 | * @param mixed $value |
| 141 | */ |
| 142 | private static function castFloatConfigValue(string $name, $value, ?float $default) : ?float |
| 143 | { |
| 144 | if ($value === null) { |
| 145 | return $default; |
| 146 | } |
| 147 | $parsedFloat = Common::parseFloat($value); |
| 148 | if ($parsedFloat !== null) { |
| 149 | return $parsedFloat; |
| 150 | } |
| 151 | self::logTypeCastWarning($name, 'float', $value); |
| 152 | return $default; |
| 153 | } |
| 154 | /** |
| 155 | * @param mixed $value |
| 156 | */ |
| 157 | private static function castBoolConfigValue(string $name, $value, ?bool $default) : ?bool |
| 158 | { |
| 159 | if ($value === null) { |
| 160 | return $default; |
| 161 | } |
| 162 | if ($value === \false || $value === \true) { |
| 163 | return $value; |
| 164 | } |
| 165 | if (is_string($value) && strtolower($value) === 'false' || $value === '0' || $value === 0) { |
| 166 | return \false; |
| 167 | } |
| 168 | if (is_string($value) && strtolower($value) === 'true' || $value === '1' || $value === 1) { |
| 169 | return \true; |
| 170 | } |
| 171 | self::logTypeCastWarning($name, 'bool', $value); |
| 172 | return $default; |
| 173 | } |
| 174 | /** |
| 175 | * @param mixed $value |
| 176 | */ |
| 177 | private static function castStringConfigValue(string $name, $value, ?string $default) : ?string |
| 178 | { |
| 179 | if ($value === null) { |
| 180 | return $default; |
| 181 | } |
| 182 | if (is_string($value) || is_numeric($value)) { |
| 183 | return Common::sanitizeNullBytes((string) $value); |
| 184 | } |
| 185 | self::logTypeCastWarning($name, 'string', $value); |
| 186 | return $default; |
| 187 | } |
| 188 | /** |
| 189 | * @param mixed $value |
| 190 | * @return array<mixed>|null |
| 191 | */ |
| 192 | private static function castArrayConfigValue(string $name, $value, ?array $default) : ?array |
| 193 | { |
| 194 | if ($value === null) { |
| 195 | return $default; |
| 196 | } |
| 197 | if (is_array($value)) { |
| 198 | /** @var array<mixed> $sanitizedValue */ |
| 199 | $sanitizedValue = self::filterNullBytes($value); |
| 200 | return $sanitizedValue; |
| 201 | } |
| 202 | self::logTypeCastWarning($name, 'array', $value); |
| 203 | return $default; |
| 204 | } |
| 205 | /** |
| 206 | * @param mixed $value |
| 207 | */ |
| 208 | private static function logTypeCastWarning(string $name, string $type, $value) : void |
| 209 | { |
| 210 | StaticContainer::get(LoggerInterface::class)->warning('Failed to cast config value {section}.{name} to {type}; actual type was {actualType}.', ['section' => static::getSectionName(), 'name' => $name, 'type' => $type, 'actualType' => self::getValueType($value)]); |
| 211 | } |
| 212 | /** |
| 213 | * @param mixed $value |
| 214 | */ |
| 215 | private static function getValueType($value) : string |
| 216 | { |
| 217 | if (is_object($value)) { |
| 218 | return get_class($value); |
| 219 | } |
| 220 | return gettype($value); |
| 221 | } |
| 222 | /** |
| 223 | * @param mixed $value |
| 224 | * @return mixed |
| 225 | */ |
| 226 | private static function filterNullBytes($value) |
| 227 | { |
| 228 | if (is_array($value)) { |
| 229 | $result = []; |
| 230 | foreach ($value as $key => $arrayValue) { |
| 231 | $result[$key] = self::filterNullBytes($arrayValue); |
| 232 | } |
| 233 | return $result; |
| 234 | } |
| 235 | return is_string($value) ? Common::sanitizeNullBytes($value) : $value; |
| 236 | } |
| 237 | } |
| 238 |