| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\Diagnostics\Diagnostic; |
|
|
| use Piwik\Common; |
| use Piwik\Config; |
| use Piwik\Http; |
| use Piwik\Piwik; |
| use Piwik\SettingsPiwik; |
| use Piwik\Translation\Translator; |
|
|
| abstract class AbstractPrivateDirectories implements Diagnostic |
| { |
| protected $privatePaths = []; |
| protected $accessiblePaths = []; |
|
|
| protected $labelKey = 'Diagnostics_RequiredPrivateDirectories'; |
|
|
| |
| |
| |
| protected $translator; |
|
|
| public function __construct(Translator $translator) |
| { |
| $this->translator = $translator; |
| } |
|
|
| public function execute() |
| { |
| if (!SettingsPiwik::isMatomoInstalled()) { |
| return []; |
| } |
|
|
| $label = $this->translator->translate($this->labelKey); |
|
|
| $baseUrl = SettingsPiwik::getPiwikUrl(); |
| if (!Common::stringEndsWith($baseUrl, '/')) { |
| $baseUrl .= '/'; |
| } |
|
|
| $manualCheck = $this->translator->translate('Diagnostics_PrivateDirectoryManualCheck'); |
|
|
| $testUrls = []; |
| foreach ($this->privatePaths as $path) { |
| if (!file_exists($path)) { |
| continue; |
| } |
| $testUrls[$path] = $baseUrl . $path; |
| } |
|
|
| $isInternetEnabled = SettingsPiwik::isInternetEnabled(); |
| if (!$isInternetEnabled) { |
| $testUrlsList = $this->getUrlList($testUrls); |
|
|
| $unknown = $this->translator->translate('Diagnostics_PrivateDirectoryInternetDisabled') . ' ' . $manualCheck |
| . $testUrlsList; |
| $results[] = DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_WARNING, $unknown); |
| return $results; |
| } |
|
|
| $result = new DiagnosticResult($label); |
| if (Config::getInstance()->General['enable_required_directories_diagnostic'] == 0) { |
| $result->addItem( |
| new DiagnosticResultItem( |
| DiagnosticResult::STATUS_WARNING, |
| $this->translator->translate('Diagnostics_EnableRequiredDirectoriesDiagnostic') |
| ) |
| ); |
| return [$result]; |
| } |
|
|
| $atLeastOneIsAccessible = $this->computeAccessiblePaths($result, $baseUrl, $testUrls); |
|
|
| if ($atLeastOneIsAccessible) { |
| $this->addError($result); |
| } else { |
| $result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_OK, $this->translator->translate('Diagnostics_AllPrivateDirectoriesAreInaccessible'))); |
| } |
|
|
| return [$result]; |
| } |
|
|
| private function getUrlList(array $testUrls) |
| { |
| $testUrlsList = ''; |
| foreach ($testUrls as $testUrl) { |
| $testUrlsList .= '<br/>' . Common::sanitizeInputValue($testUrl); |
| } |
| return $testUrlsList; |
| } |
|
|
| protected function isAccessible(DiagnosticResult $result, $testUrl, $publicIfResponseEquals, $publicIfResponseContains) |
| { |
| try { |
| $response = Http::sendHttpRequest($testUrl, $timeout = 2, null, null, null, false, false, true); |
| $status = $response['status']; |
|
|
| if ($status >= 400 && $status < 500) { |
| return false; |
| } elseif ($status >= 300 && $status < 400) { |
| |
| $response = Http::sendHttpRequest($testUrl, $timeout = 5, null, null, 5, false, false, true); |
| $isResolvedRedirectProtected = $response['status'] >= 400 && $response['status'] < 500; |
| if ($isResolvedRedirectProtected) { |
| |
| return false; |
| } |
|
|
| |
| if (!$publicIfResponseContains || !$publicIfResponseEquals) { |
| |
| |
| $result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_WARNING, $testUrl)); |
| return true; |
| } |
|
|
| if (trim($response['data']) === $publicIfResponseEquals) { |
| |
| $result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_ERROR, $testUrl)); |
| return true; |
| } elseif (strpos($response['data'], $publicIfResponseContains) !== false) { |
| |
| $result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_ERROR, $testUrl)); |
| return true; |
| } |
| |
| |
| } else { |
| |
| $result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_ERROR, $testUrl)); |
| return true; |
| } |
| } catch (\Exception $e) { |
| $error = $e->getMessage(); |
| $result->addItem( |
| new DiagnosticResultItem( |
| DiagnosticResult::STATUS_WARNING, |
| Piwik::translate( |
| 'Diagnostics_PrivateDirectoryCantCheckUrl', |
| [Common::sanitizeInputValue($testUrl), Common::sanitizeInputValue($error)] |
| ) |
| ) |
| ); |
| } |
| return false; |
| } |
|
|
| protected function computeAccessiblePaths(DiagnosticResult &$result, $baseUrl, array $testUrls): bool |
| { |
| $atLeastOneIsAccessible = false; |
| foreach ($testUrls as $path => $testUrl) { |
| if ($this->isAccessible($result, $testUrl, '', '')) { |
| $atLeastOneIsAccessible = true; |
| } |
| } |
| return $atLeastOneIsAccessible; |
| } |
|
|
| abstract protected function addError(DiagnosticResult &$result); |
| } |
|
|