EnvironmentValidator.php
1 year ago
GlobalSettingsProvider.php
2 years ago
PluginList.php
1 month ago
EnvironmentValidator.php
129 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\Application\Kernel; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | use Piwik\Exception\NotYetInstalledException; |
| 13 | use Piwik\Filechecks; |
| 14 | use Piwik\Piwik; |
| 15 | use Piwik\SettingsPiwik; |
| 16 | use Piwik\SettingsServer; |
| 17 | use Piwik\Translation\Translator; |
| 18 | /** |
| 19 | * Validates the Piwik environment. This includes making sure the required config files |
| 20 | * are present, and triggering the correct behaviour if otherwise. |
| 21 | */ |
| 22 | class EnvironmentValidator |
| 23 | { |
| 24 | /** |
| 25 | * @var GlobalSettingsProvider |
| 26 | */ |
| 27 | protected $settingsProvider; |
| 28 | /** |
| 29 | * @var Translator |
| 30 | */ |
| 31 | protected $translator; |
| 32 | public function __construct(\Piwik\Application\Kernel\GlobalSettingsProvider $settingsProvider, Translator $translator) |
| 33 | { |
| 34 | $this->settingsProvider = $settingsProvider; |
| 35 | $this->translator = $translator; |
| 36 | } |
| 37 | public function validate() |
| 38 | { |
| 39 | $this->checkConfigFileExists($this->settingsProvider->getPathGlobal()); |
| 40 | if (SettingsPiwik::isMatomoInstalled()) { |
| 41 | $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller = \false); |
| 42 | return; |
| 43 | } |
| 44 | $startInstaller = \true; |
| 45 | if (SettingsServer::isTrackerApiRequest()) { |
| 46 | // if Piwik is not installed yet, the piwik.php should do nothing and not return an error |
| 47 | throw new NotYetInstalledException("As Matomo is not installed yet, the Tracking API cannot proceed and will exit without error."); |
| 48 | } |
| 49 | if (Common::isPhpCliMode()) { |
| 50 | // in CLI, do not start/redirect to installer, simply output the exception at the top |
| 51 | $startInstaller = \false; |
| 52 | } |
| 53 | // Start the installation when config file not found |
| 54 | $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller); |
| 55 | } |
| 56 | /** |
| 57 | * @param $path |
| 58 | * @param bool $startInstaller |
| 59 | * @throws \Exception |
| 60 | */ |
| 61 | private function checkConfigFileExists($path, $startInstaller = \false) |
| 62 | { |
| 63 | if (is_readable($path) && !$startInstaller) { |
| 64 | return; |
| 65 | } |
| 66 | $general = $this->settingsProvider->getSection('General'); |
| 67 | if (isset($general['installation_in_progress']) && $general['installation_in_progress'] && $startInstaller) { |
| 68 | return; |
| 69 | } |
| 70 | if (isset($general['enable_installer']) && !$general['enable_installer']) { |
| 71 | throw new NotYetInstalledException('Matomo is not set up yet'); |
| 72 | } |
| 73 | $message = $this->getSpecificMessageWhetherFileExistsOrNot($path); |
| 74 | $exception = new NotYetInstalledException($message); |
| 75 | if ($startInstaller) { |
| 76 | $this->startInstallation($exception); |
| 77 | } else { |
| 78 | throw $exception; |
| 79 | } |
| 80 | } |
| 81 | /** |
| 82 | * @param $exception |
| 83 | */ |
| 84 | private function startInstallation($exception) |
| 85 | { |
| 86 | /** |
| 87 | * Triggered when the configuration file cannot be found or read, which usually |
| 88 | * means Piwik is not installed yet. |
| 89 | * |
| 90 | * This event can be used to start the installation process or to display a custom error message. |
| 91 | * |
| 92 | * @param \Exception $exception The exception that was thrown by `Config::getInstance()`. |
| 93 | */ |
| 94 | Piwik::postEvent('Config.NoConfigurationFile', array($exception), $pending = \true); |
| 95 | } |
| 96 | /** |
| 97 | * @param $path |
| 98 | * @return string |
| 99 | */ |
| 100 | private function getMessageWhenFileExistsButNotReadable($path) |
| 101 | { |
| 102 | $format = " \n<b>» %s </b>"; |
| 103 | if (Common::isPhpCliMode()) { |
| 104 | $format = "\n » %s \n"; |
| 105 | } |
| 106 | return sprintf($format, $this->translator->translate('General_ExceptionConfigurationFilePleaseCheckReadableByUser', array($path, Filechecks::getUser()))); |
| 107 | } |
| 108 | /** |
| 109 | * @param $path |
| 110 | * @return string |
| 111 | */ |
| 112 | private function getSpecificMessageWhetherFileExistsOrNot($path) |
| 113 | { |
| 114 | if (!file_exists($path)) { |
| 115 | $message = $this->translator->translate('General_ExceptionConfigurationFileNotFound', array($path)); |
| 116 | if (Common::isPhpCliMode()) { |
| 117 | $message .= $this->getMessageWhenFileExistsButNotReadable($path); |
| 118 | } |
| 119 | } else { |
| 120 | $message = $this->translator->translate('General_ExceptionConfigurationFileExistsButNotReadable', array($path)); |
| 121 | $message .= $this->getMessageWhenFileExistsButNotReadable($path); |
| 122 | } |
| 123 | if (Common::isPhpCliMode()) { |
| 124 | $message = "\n" . $message; |
| 125 | } |
| 126 | return $message; |
| 127 | } |
| 128 | } |
| 129 |