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