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