API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Config.php
479 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik; |
| 11 | |
| 12 | use Exception; |
| 13 | use Piwik\Application\Kernel\GlobalSettingsProvider; |
| 14 | use Piwik\Config\Cache; |
| 15 | use Piwik\Config\IniFileChain; |
| 16 | use Piwik\Container\StaticContainer; |
| 17 | use Piwik\Exception\MissingFilePermissionException; |
| 18 | use Piwik\ProfessionalServices\Advertising; |
| 19 | |
| 20 | /** |
| 21 | * Singleton that provides read & write access to Piwik's INI configuration. |
| 22 | * |
| 23 | * This class reads and writes to the `config/config.ini.php` file. If config |
| 24 | * options are missing from that file, this class will look for their default |
| 25 | * values in `config/global.ini.php`. |
| 26 | * |
| 27 | * ### Examples |
| 28 | * |
| 29 | * **Getting a value:** |
| 30 | * |
| 31 | * // read the minimum_memory_limit option under the [General] section |
| 32 | * $minValue = Config::getInstance()->General['minimum_memory_limit']; |
| 33 | * |
| 34 | * **Setting a value:** |
| 35 | * |
| 36 | * // set the minimum_memory_limit option |
| 37 | * Config::getInstance()->General['minimum_memory_limit'] = 256; |
| 38 | * Config::getInstance()->forceSave(); |
| 39 | * |
| 40 | * **Setting an entire section:** |
| 41 | * |
| 42 | * Config::getInstance()->MySection = array('myoption' => 1); |
| 43 | * Config::getInstance()->forceSave(); |
| 44 | */ |
| 45 | class Config |
| 46 | { |
| 47 | const DEFAULT_LOCAL_CONFIG_PATH = '/config/config.ini.php'; |
| 48 | const DEFAULT_COMMON_CONFIG_PATH = '/config/common.config.ini.php'; |
| 49 | const DEFAULT_GLOBAL_CONFIG_PATH = '/config/global.ini.php'; |
| 50 | |
| 51 | /** |
| 52 | * @var boolean |
| 53 | */ |
| 54 | protected $doNotWriteConfigInTests = false; |
| 55 | |
| 56 | /** |
| 57 | * @var GlobalSettingsProvider |
| 58 | */ |
| 59 | protected $settings; |
| 60 | |
| 61 | /** |
| 62 | * @return Config |
| 63 | */ |
| 64 | public static function getInstance() |
| 65 | { |
| 66 | return StaticContainer::get('Piwik\Config'); |
| 67 | } |
| 68 | |
| 69 | public function __construct(GlobalSettingsProvider $settings) |
| 70 | { |
| 71 | $this->settings = $settings; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the path to the local config file used by this instance. |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function getLocalPath() |
| 80 | { |
| 81 | return $this->settings->getPathLocal(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the path to the global config file used by this instance. |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function getGlobalPath() |
| 90 | { |
| 91 | return $this->settings->getPathGlobal(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the path to the common config file used by this instance. |
| 96 | * |
| 97 | * @return string |
| 98 | */ |
| 99 | public function getCommonPath() |
| 100 | { |
| 101 | return $this->settings->getPathCommon(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns absolute path to the global configuration file |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public static function getGlobalConfigPath() |
| 110 | { |
| 111 | return PIWIK_DOCUMENT_ROOT . self::DEFAULT_GLOBAL_CONFIG_PATH; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns absolute path to the common configuration file. |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | public static function getCommonConfigPath() |
| 120 | { |
| 121 | return PIWIK_USER_PATH . self::DEFAULT_COMMON_CONFIG_PATH; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns default absolute path to the local configuration file. |
| 126 | * |
| 127 | * @return string |
| 128 | */ |
| 129 | public static function getDefaultLocalConfigPath() |
| 130 | { |
| 131 | return PIWIK_USER_PATH . self::DEFAULT_LOCAL_CONFIG_PATH; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns absolute path to the local configuration file |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | public static function getLocalConfigPath() |
| 140 | { |
| 141 | if (!empty($GLOBALS['CONFIG_INI_PATH_RESOLVER']) && is_callable($GLOBALS['CONFIG_INI_PATH_RESOLVER'])) { |
| 142 | return call_user_func($GLOBALS['CONFIG_INI_PATH_RESOLVER']); |
| 143 | } |
| 144 | |
| 145 | $path = self::getByDomainConfigPath(); |
| 146 | if ($path) { |
| 147 | return $path; |
| 148 | } |
| 149 | return self::getDefaultLocalConfigPath(); |
| 150 | } |
| 151 | |
| 152 | private static function getLocalConfigInfoForHostname($hostname) |
| 153 | { |
| 154 | if (!$hostname) { |
| 155 | return array(); |
| 156 | } |
| 157 | |
| 158 | // Remove any port number to get actual hostname |
| 159 | $hostname = Url::getHostSanitized($hostname); |
| 160 | $standardConfigName = 'config.ini.php'; |
| 161 | $perHostFilename = $hostname . '.' . $standardConfigName; |
| 162 | $pathDomainConfig = PIWIK_USER_PATH . '/config/' . $perHostFilename; |
| 163 | $pathDomainMiscUser = PIWIK_USER_PATH . '/misc/user/' . $hostname . '/' . $standardConfigName; |
| 164 | |
| 165 | $locations = array( |
| 166 | array('file' => $perHostFilename, 'path' => $pathDomainConfig), |
| 167 | array('file' => $standardConfigName, 'path' => $pathDomainMiscUser) |
| 168 | ); |
| 169 | |
| 170 | return $locations; |
| 171 | } |
| 172 | |
| 173 | public function getConfigHostnameIfSet() |
| 174 | { |
| 175 | if ($this->getByDomainConfigPath() === false) { |
| 176 | return false; |
| 177 | } |
| 178 | return $this->getHostname(); |
| 179 | } |
| 180 | |
| 181 | public function getClientSideOptions() |
| 182 | { |
| 183 | $general = $this->General; |
| 184 | |
| 185 | return array( |
| 186 | 'action_url_category_delimiter' => $general['action_url_category_delimiter'], |
| 187 | 'action_title_category_delimiter' => $general['action_title_category_delimiter'], |
| 188 | 'autocomplete_min_sites' => $general['autocomplete_min_sites'], |
| 189 | 'datatable_export_range_as_day' => $general['datatable_export_range_as_day'], |
| 190 | 'datatable_row_limits' => $this->getDatatableRowLimits(), |
| 191 | 'are_ads_enabled' => Advertising::isAdsEnabledInConfig($general) |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param $general |
| 197 | * @return mixed |
| 198 | */ |
| 199 | private function getDatatableRowLimits() |
| 200 | { |
| 201 | $limits = $this->General['datatable_row_limits']; |
| 202 | $limits = explode(",", $limits); |
| 203 | $limits = array_map('trim', $limits); |
| 204 | return $limits; |
| 205 | } |
| 206 | |
| 207 | public static function getByDomainConfigPath() |
| 208 | { |
| 209 | $host = self::getHostname(); |
| 210 | $hostConfigs = self::getLocalConfigInfoForHostname($host); |
| 211 | |
| 212 | foreach ($hostConfigs as $hostConfig) { |
| 213 | if (Filesystem::isValidFilename($hostConfig['file']) |
| 214 | && file_exists($hostConfig['path']) |
| 215 | ) { |
| 216 | return $hostConfig['path']; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the hostname of the current request (without port number) |
| 225 | * |
| 226 | * @return string |
| 227 | */ |
| 228 | public static function getHostname() |
| 229 | { |
| 230 | // Check trusted requires config file which is not ready yet |
| 231 | $host = Url::getHost($checkIfTrusted = false); |
| 232 | |
| 233 | // Remove any port number to get actual hostname |
| 234 | $host = Url::getHostSanitized($host); |
| 235 | |
| 236 | return $host; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * If set, Piwik will use the hostname config no matter if it exists or not. Useful for instance if you want to |
| 241 | * create a new hostname config: |
| 242 | * |
| 243 | * $config = Config::getInstance(); |
| 244 | * $config->forceUsageOfHostnameConfig('piwik.example.com'); |
| 245 | * $config->save(); |
| 246 | * |
| 247 | * @param string $hostname eg piwik.example.com |
| 248 | * @param string $preferredPath If there are different paths for the config that can be used, eg /config/* and /misc/user/*, |
| 249 | * and a preferred path is given, then the config path must contain the preferred path. |
| 250 | * @return string |
| 251 | * @throws \Exception In case the domain contains not allowed characters |
| 252 | * @internal |
| 253 | */ |
| 254 | public function forceUsageOfLocalHostnameConfig($hostname, $preferredPath = null) |
| 255 | { |
| 256 | $hostConfigs = self::getLocalConfigInfoForHostname($hostname); |
| 257 | $fileNames = ''; |
| 258 | |
| 259 | foreach ($hostConfigs as $hostConfig) { |
| 260 | if (count($hostConfigs) > 1 |
| 261 | && $preferredPath |
| 262 | && strpos($hostConfig['path'], $preferredPath) === false) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | $filename = $hostConfig['file']; |
| 267 | $fileNames .= $filename . ' '; |
| 268 | |
| 269 | if (Filesystem::isValidFilename($filename)) { |
| 270 | $pathLocal = $hostConfig['path']; |
| 271 | |
| 272 | try { |
| 273 | $this->reload($pathLocal); |
| 274 | } catch (Exception $ex) { |
| 275 | // pass (not required for local file to exist at this point) |
| 276 | } |
| 277 | |
| 278 | return $pathLocal; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | throw new Exception('Matomo domain is not a valid looking hostname (' . trim($fileNames) . ').'); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns `true` if the local configuration file is writable. |
| 287 | * |
| 288 | * @return bool |
| 289 | */ |
| 290 | public function isFileWritable() |
| 291 | { |
| 292 | return is_writable($this->settings->getPathLocal()); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Clear in-memory configuration so it can be reloaded |
| 297 | * @deprecated since v2.12.0 |
| 298 | */ |
| 299 | public function clear() |
| 300 | { |
| 301 | $this->reload(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Read configuration from files into memory |
| 306 | * |
| 307 | * @throws Exception if local config file is not readable; exits for other errors |
| 308 | * @deprecated since v2.12.0 |
| 309 | */ |
| 310 | public function init() |
| 311 | { |
| 312 | $this->reload(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Reloads config data from disk. |
| 317 | * |
| 318 | * @throws \Exception if the global config file is not found and this is a tracker request, or |
| 319 | * if the local config file is not found and this is NOT a tracker request. |
| 320 | */ |
| 321 | protected function reload($pathLocal = null, $pathGlobal = null, $pathCommon = null) |
| 322 | { |
| 323 | $this->settings->reload($pathGlobal, $pathLocal, $pathCommon); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * @deprecated |
| 328 | */ |
| 329 | public function existsLocalConfig() |
| 330 | { |
| 331 | return is_readable($this->getLocalPath()); |
| 332 | } |
| 333 | |
| 334 | public function deleteLocalConfig() |
| 335 | { |
| 336 | $configLocal = $this->getLocalPath(); |
| 337 | |
| 338 | if(file_exists($configLocal)){ |
| 339 | @unlink($configLocal); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Returns a configuration value or section by name. |
| 345 | * |
| 346 | * @param string $name The value or section name. |
| 347 | * @return string|array The requested value requested. Returned by reference. |
| 348 | * @throws Exception If the value requested not found in either `config.ini.php` or |
| 349 | * `global.ini.php`. |
| 350 | * @api |
| 351 | */ |
| 352 | public function &__get($name) |
| 353 | { |
| 354 | $section =& $this->settings->getIniFileChain()->get($name); |
| 355 | return $section; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @api |
| 360 | */ |
| 361 | public function getFromGlobalConfig($name) |
| 362 | { |
| 363 | return $this->settings->getIniFileChain()->getFrom($this->getGlobalPath(), $name); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @api |
| 368 | */ |
| 369 | public function getFromCommonConfig($name) |
| 370 | { |
| 371 | return $this->settings->getIniFileChain()->getFrom($this->getCommonPath(), $name); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @api |
| 376 | */ |
| 377 | public function getFromLocalConfig($name) |
| 378 | { |
| 379 | return $this->settings->getIniFileChain()->getFrom($this->getLocalPath(), $name); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Sets a configuration value or section. |
| 384 | * |
| 385 | * @param string $name This section name or value name to set. |
| 386 | * @param mixed $value |
| 387 | * @api |
| 388 | */ |
| 389 | public function __set($name, $value) |
| 390 | { |
| 391 | $this->settings->getIniFileChain()->set($name, $value); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Dump config |
| 396 | * |
| 397 | * @return string|null |
| 398 | * @throws \Exception |
| 399 | */ |
| 400 | public function dumpConfig() |
| 401 | { |
| 402 | $chain = $this->settings->getIniFileChain(); |
| 403 | |
| 404 | $header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n"; |
| 405 | $header .= "; file automatically generated or modified by Matomo; you can manually override the default values in global.ini.php by redefining them in this file.\n"; |
| 406 | return $chain->dumpChanges($header); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Write user configuration file |
| 411 | * |
| 412 | * @throws \Exception if config file not writable |
| 413 | */ |
| 414 | protected function writeConfig() |
| 415 | { |
| 416 | $output = $this->dumpConfig(); |
| 417 | if ($output !== null |
| 418 | && $output !== false |
| 419 | ) { |
| 420 | $localPath = $this->getLocalPath(); |
| 421 | |
| 422 | if ($this->doNotWriteConfigInTests) { |
| 423 | // simulate whether it would be successful |
| 424 | $success = is_writable($localPath); |
| 425 | } else { |
| 426 | $success = @file_put_contents($localPath, $output, LOCK_EX); |
| 427 | } |
| 428 | |
| 429 | if ($success === false) { |
| 430 | throw $this->getConfigNotWritableException(); |
| 431 | } |
| 432 | |
| 433 | $this->settings->getIniFileChain()->deleteConfigCache(); |
| 434 | |
| 435 | /** |
| 436 | * Triggered when a INI config file is changed on disk. |
| 437 | * |
| 438 | * @param string $localPath Absolute path to the changed file on the server. |
| 439 | */ |
| 440 | Piwik::postEvent('Core.configFileChanged', [$localPath]); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Writes the current configuration to the **config.ini.php** file. Only writes options whose |
| 446 | * values are different from the default. |
| 447 | * |
| 448 | * @api |
| 449 | */ |
| 450 | public function forceSave() |
| 451 | { |
| 452 | $this->writeConfig(); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * @throws \Exception |
| 457 | */ |
| 458 | public function getConfigNotWritableException() |
| 459 | { |
| 460 | $path = "config/" . basename($this->getLocalPath()); |
| 461 | return new MissingFilePermissionException(Piwik::translate('General_ConfigFileIsNotWritable', array("(" . $path . ")", ""))); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Convenience method for setting settings in a single section. Will set them in a new array first |
| 466 | * to be compatible with certain PHP versions. |
| 467 | * |
| 468 | * @param string $sectionName Section name. |
| 469 | * @param string $name The setting name. |
| 470 | * @param mixed $value The setting value to set. |
| 471 | */ |
| 472 | public static function setSetting($sectionName, $name, $value) |
| 473 | { |
| 474 | $section = self::getInstance()->$sectionName; |
| 475 | $section[$name] = $value; |
| 476 | self::getInstance()->$sectionName = $section; |
| 477 | } |
| 478 | } |
| 479 |