| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Application\Kernel; |
|
|
| use Piwik\Common; |
| use Piwik\Exception\NotYetInstalledException; |
| use Piwik\Filechecks; |
| use Piwik\Piwik; |
| use Piwik\SettingsPiwik; |
| use Piwik\SettingsServer; |
| use Piwik\Translation\Translator; |
|
|
| |
| |
| |
| |
| class EnvironmentValidator |
| { |
| |
| |
| |
| protected $settingsProvider; |
|
|
| |
| |
| |
| protected $translator; |
|
|
| public function __construct(GlobalSettingsProvider $settingsProvider, Translator $translator) |
| { |
| $this->settingsProvider = $settingsProvider; |
| $this->translator = $translator; |
| } |
|
|
| public function validate() |
| { |
| $this->checkConfigFileExists($this->settingsProvider->getPathGlobal()); |
|
|
| if (SettingsPiwik::isMatomoInstalled()) { |
| $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller = false); |
| return; |
| } |
|
|
| $startInstaller = true; |
|
|
| if (SettingsServer::isTrackerApiRequest()) { |
| |
| throw new NotYetInstalledException("As Matomo is not installed yet, the Tracking API cannot proceed and will exit without error."); |
| } |
|
|
| if (Common::isPhpCliMode()) { |
| |
| $startInstaller = false; |
| } |
|
|
| |
| $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller); |
| } |
|
|
| |
| |
| |
| |
| |
| private function checkConfigFileExists($path, $startInstaller = false) |
| { |
| if (is_readable($path) && !$startInstaller) { |
| return; |
| } |
|
|
| $general = $this->settingsProvider->getSection('General'); |
|
|
| if ( |
| isset($general['installation_in_progress']) |
| && $general['installation_in_progress'] |
| && $startInstaller |
| ) { |
| return; |
| } |
|
|
| if (isset($general['enable_installer']) && !$general['enable_installer']) { |
| throw new NotYetInstalledException('Matomo is not set up yet'); |
| } |
|
|
| $message = $this->getSpecificMessageWhetherFileExistsOrNot($path); |
|
|
| $exception = new NotYetInstalledException($message); |
|
|
| if ($startInstaller) { |
| $this->startInstallation($exception); |
| } else { |
| throw $exception; |
| } |
| } |
|
|
| |
| |
| |
| private function startInstallation($exception) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('Config.NoConfigurationFile', array($exception), $pending = true); |
| } |
|
|
| |
| |
| |
| |
| private function getMessageWhenFileExistsButNotReadable($path) |
| { |
| $format = " \n<b>» %s </b>"; |
| if (Common::isPhpCliMode()) { |
| $format = "\n » %s \n"; |
| } |
|
|
| return sprintf( |
| $format, |
| $this->translator->translate( |
| 'General_ExceptionConfigurationFilePleaseCheckReadableByUser', |
| array($path, Filechecks::getUser()) |
| ) |
| ); |
| } |
|
|
| |
| |
| |
| |
| private function getSpecificMessageWhetherFileExistsOrNot($path) |
| { |
| if (!file_exists($path)) { |
| $message = $this->translator->translate('General_ExceptionConfigurationFileNotFound', array($path)); |
| if (Common::isPhpCliMode()) { |
| $message .= $this->getMessageWhenFileExistsButNotReadable($path); |
| } |
| } else { |
| $message = $this->translator->translate( |
| 'General_ExceptionConfigurationFileExistsButNotReadable', |
| array($path) |
| ); |
| $message .= $this->getMessageWhenFileExistsButNotReadable($path); |
| } |
|
|
| if (Common::isPhpCliMode()) { |
| $message = "\n" . $message; |
| } |
| return $message; |
| } |
| } |
|
|