PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.0 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 2 years ago GlobalSettingsProvider.php 2 years ago PluginList.php 2 years ago
EnvironmentValidator.php
126 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://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)) {
64 return;
65 }
66 $general = $this->settingsProvider->getSection('General');
67 if (isset($general['enable_installer']) && !$general['enable_installer']) {
68 throw new NotYetInstalledException('Matomo is not set up yet');
69 }
70 $message = $this->getSpecificMessageWhetherFileExistsOrNot($path);
71 $exception = new NotYetInstalledException($message);
72 if ($startInstaller) {
73 $this->startInstallation($exception);
74 } else {
75 throw $exception;
76 }
77 }
78 /**
79 * @param $exception
80 */
81 private function startInstallation($exception)
82 {
83 /**
84 * Triggered when the configuration file cannot be found or read, which usually
85 * means Piwik is not installed yet.
86 *
87 * This event can be used to start the installation process or to display a custom error message.
88 *
89 * @param \Exception $exception The exception that was thrown by `Config::getInstance()`.
90 */
91 Piwik::postEvent('Config.NoConfigurationFile', array($exception), $pending = true);
92 }
93 /**
94 * @param $path
95 * @return string
96 */
97 private function getMessageWhenFileExistsButNotReadable($path)
98 {
99 $format = " \n<b>» %s </b>";
100 if (Common::isPhpCliMode()) {
101 $format = "\n » %s \n";
102 }
103 return sprintf($format, $this->translator->translate('General_ExceptionConfigurationFilePleaseCheckReadableByUser', array($path, Filechecks::getUser())));
104 }
105 /**
106 * @param $path
107 * @return string
108 */
109 private function getSpecificMessageWhetherFileExistsOrNot($path)
110 {
111 if (!file_exists($path)) {
112 $message = $this->translator->translate('General_ExceptionConfigurationFileNotFound', array($path));
113 if (Common::isPhpCliMode()) {
114 $message .= $this->getMessageWhenFileExistsButNotReadable($path);
115 }
116 } else {
117 $message = $this->translator->translate('General_ExceptionConfigurationFileExistsButNotReadable', array($path));
118 $message .= $this->getMessageWhenFileExistsButNotReadable($path);
119 }
120 if (Common::isPhpCliMode()) {
121 $message = "\n" . $message;
122 }
123 return $message;
124 }
125 }
126