PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Application / Kernel / EnvironmentValidator.php
matomo / app / core / Application / Kernel Last commit date
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