1
0
Fork 0
mirror of https://github.com/Findus23/matomo-DiagnosticsExtended.git synced 2024-09-18 14:53:45 +02:00

first version

This commit is contained in:
Lukas Winkler 2021-03-22 22:55:14 +01:00
commit d6e785684a
Signed by: lukas
GPG key ID: 54DE4D798D244853
19 changed files with 667 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
TODO.md

3
CHANGELOG.md Normal file
View file

@ -0,0 +1,3 @@
## Changelog
Here goes the changelog text.

View file

@ -0,0 +1,141 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic;
use Piwik\Db;
use Piwik\Http;
use Piwik\Date;
use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem;
use Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\IniSetting;
use Psr\Log\LoggerInterface;
use function DI\factory;
class DatabaseVersionCheck implements Diagnostic
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var string
*/
private $label;
const SOCKET_TIMEOUT = 2;
/**
* @var \Matomo\Cache\Lazy
*/
private $lazyCache;
public function __construct(LoggerInterface $logger, \Matomo\Cache\Lazy $lazyCache)
{
$this->logger = $logger;
$this->label = "Database version check";
$this->lazyCache = $lazyCache;
}
private function getDatabaseVersion()
{
$db = Db::get();
return $db->getServerVersion();
}
/**
* @return DiagnosticResult[]
*/
public function execute()
{
$version = $this->getDatabaseVersion();
$versionParts = explode(".", explode("-", $version)[0]);
$minorVersion = $versionParts[0] . "." . $versionParts[1];
$currentVersion = $minorVersion . "." . $versionParts[2];
if (strpos(strtolower($version), "mariadb") !== false) {
# check for MariaDB
$cacheId = 'DiagnosticsExtended_MariaDBVersion_' . $minorVersion;
$url = "https://endoflife.date/api/mariadb/$minorVersion.json";
$timeout = self::SOCKET_TIMEOUT;
try {
$response = $this->lazyCache->fetch($cacheId);
if (!$response) {
$response = Http::sendHttpRequest($url, $timeout);
$this->lazyCache->save($cacheId, $response, 60 * 60 * 24);
}
$versionInfo = json_decode($response, true);
$latestVersion = $versionInfo["latest"];
$results = new DiagnosticResult($this->label);
if (version_compare($currentVersion, $latestVersion, ">=")) {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
"You are using the latest version of MariaDB " . $minorVersion
));
} else {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"There is a newer MariaDB patch version ($latestVersion) available (you are using $version/$currentVersion).
You should update to it as soon as possible
(unless the distributor of your MariaDB binary is backporting security patches)."
));
}
if (new \DateTime() > new \DateTime($versionInfo["eol"])) {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"Your MariaDB version ($currentVersion) does not recieve security support by the MariaDB
team anymore. You should update to a newer version
(unless the distributor of your PHP binary is backporting security patches)."
));
} else {
$formattedDate = (Date::factory($versionInfo["eol"]))->getLocalized(Date::DATE_FORMAT_LONG);
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
"Your MariaDB version ($minorVersion) receives security support by the MariaDB
team until $formattedDate."
));
}
return [$results];
} catch (\Exception $e) {
return [DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_INFORMATIONAL,
"Matomo could not check if your Database version is up-to-date"
)];
}
} else {
# MySQL check
# 5.7 is latest non-eol version
if (((int)$versionParts[0] < 5) || ((int)$versionParts[0] == 5 && (int)$versionParts[1] >= 7)) {
return [DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_WARNING,
"Your MySQL version might be not receiving security updates anymore
(unless the distributor of your PHP binary is backporting security patches).
Please check if your MySQL version is still secure."
)];
} else {
return [DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_OK,
"Your MySQL version is probably up-to-date
(assuming you are using the latest patch version)."
)];
}
}
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem;
use Psr\Log\LoggerInterface;
class ExampleCheck implements Diagnostic
{
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function execute()
{
$result=new DiagnosticResult("label");
$result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_ERROR,"a"));
$result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_OK,"b"));
return array($result);
}
}

View file

@ -0,0 +1,109 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic;
use Piwik\Db;
use Piwik\Http;
use Piwik\Date;
use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem;
use Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\IniSetting;
use Piwik\SettingsPiwik;
use Piwik\Tracker\TrackerCodeGenerator;
use Psr\Log\LoggerInterface;
use function DI\factory;
class GzipMatomoJsCheck implements Diagnostic
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var string
*/
private $label;
const SOCKET_TIMEOUT = 2;
/**
* @var \Matomo\Cache\Lazy
*/
private $lazyCache;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->label = "matomo.js gzip";
}
/**
* @return DiagnosticResult[]
*/
public function execute()
{
$matomoUrl = SettingsPiwik::getPiwikUrl();
$generator = new TrackerCodeGenerator();
$matomoJs = $generator->getJsTrackerEndpoint();
$checkURL = "$matomoUrl$matomoJs"; # something like https://example.com/matomo.js
$timeout = self::SOCKET_TIMEOUT;
try {
$response = Http::sendHttpRequest($checkURL, $timeout, $userAgent = null,
$destinationPath = null,
$followDepth = 0,
$acceptLanguage = false,
$byteRange = false,
$getExtendedInfo = true);
$status = $response["status"];
$headers = $response["headers"];
$data = $response["data"];
if ($status != 200 || strpos($data, "c80d50af7d3db9be66a4d0a86db0286e4fd33292") === false) {
return [DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_INFORMATIONAL,
"It seems like matomo.js can't be fetched properly"
)];
}
$results = new DiagnosticResult($this->label);
$contentType = $headers["content-type"];
if ($contentType !== "application/javascript") {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"matomo.js should be delivered with an 'application/javascript' Content-Type. You are using '$contentType'."
));
}
$contentEncoding = $headers["content-encoding"];
if ($contentEncoding === "gzip") {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
"matomo.js is delivered gzipped."
));
} else {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"matomo.js is not delivered gzipped.
You might want to set up gzip for .js files as it can reduce the size of the file by up to 60 %."
));
}
return [$results];
} catch (\Exception $e) {
return [DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_INFORMATIONAL,
"Matomo could not check if your matomo.js can be fetched properly"
)];
}
}
}

View file

@ -0,0 +1,19 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings;
class AllowUrlInclude extends IniSetting
{
static public $key = "allow_url_include";
public static $targetValue = false;
public static $url = "https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-include";
}

View file

@ -0,0 +1,19 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings;
class DisplayErrors extends IniSetting
{
static public $key = "display_errors";
public static $targetValue = false;
public static $url = "https://www.php.net/manual/de/errorfunc.configuration.php#ini.display-errors";
}

View file

@ -0,0 +1,21 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings;
class ExposePhp extends IniSetting
{
static public $key = "expose_php";
public static $targetValue = false;
public static $severe = false;
public static $url = "https://www.php.net/manual/de/security.hiding.php";
}

View file

@ -0,0 +1,26 @@
<?php
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings;
abstract class IniSetting
{
/**
* @var string
*/
static public $key;
/**
* @var boolean
*/
static public $targetValue;
/**
* @var bool
*/
static public $severe = true;
/**
* @var string
*/
static public $url;
}

View file

@ -0,0 +1,83 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem;
use Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\IniSetting;
use Psr\Log\LoggerInterface;
class PhpIniCheck implements Diagnostic
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var IniSetting[]
*/
private $iniSettings;
public function __construct(array $iniSettings, LoggerInterface $logger)
{
$this->logger = $logger;
$this->iniSettings = $iniSettings;
}
public function execute()
{
$result = new DiagnosticResult("php.ini checks");
foreach ($this->iniSettings as $setting) {
$key = $setting::$key;
if ($this->booleanIni($key) === $setting::$targetValue) {
$item = new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
$setting::$targetValue ? "$key is enabled" : "$key is disabled"
);
} else {
$status = $setting::$severe ? DiagnosticResult::STATUS_ERROR : DiagnosticResult::STATUS_WARNING;
$item = new DiagnosticResultItem(
$status,
$setting::$targetValue ? "$key should be enabled" : "$key should be disabled"
);
}
$result->addItem($item);
}
return array($result);
}
private function booleanIni(string $key): bool
{
return $this->IniValueToBoolean(ini_get($key));
}
private function IniValueToBoolean(string $iniValue): bool
{
switch (strtolower($iniValue)) {
case "on":
case "true":
case "yes":
case "1":
return true;
case "off":
case "false":
case "no":
case "0":
case "":
return false;
default:
return $iniValue;
}
}
}

View file

@ -0,0 +1,132 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended\Diagnostic;
use Piwik\Http;
use Piwik\Date;
use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult;
use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem;
use Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\IniSetting;
use Psr\Log\LoggerInterface;
use function DI\factory;
class PhpVersionCheck implements Diagnostic
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var string
*/
private $label;
const SOCKET_TIMEOUT = 2;
/**
* @var \Matomo\Cache\Lazy
*/
private $lazyCache;
public function __construct(LoggerInterface $logger, \Matomo\Cache\Lazy $lazyCache)
{
$this->logger = $logger;
$this->label = "php version check";
$this->lazyCache = $lazyCache;
}
/**
* from
* https://www.php.net/supported-versions
* and
* https://www.php.net/eol.php
* @var string[]
*/
private $eolDates = [
"7.2" => "2020-11-30",
"7.3" => "2021-12-06",
"7.4" => "2022-11-28",
"8.0" => "2023-11-26"
];
/**
* @return DiagnosticResult[]
*/
public function execute()
{
$minorVersion = PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;
$currentVersion = $minorVersion . "." . PHP_RELEASE_VERSION;
$cacheId = 'DiagnosticsExtended_PhpVersion_' . $minorVersion;
$url = "https://php.net/releases/?json=1&version=" . $minorVersion;
$timeout = self::SOCKET_TIMEOUT;
try {
$response = $this->lazyCache->fetch($cacheId);
if (!$response) {
$response = Http::sendHttpRequest($url, $timeout);
$this->lazyCache->save($cacheId, $response, 60 * 60 * 24);
}
$versionInfo = json_decode($response, true);
if (empty($versionInfo["version"])) {
return [$this->testCouldNotRunResult()];
}
$latestVersion = $versionInfo["version"];
$results = new DiagnosticResult($this->label);
if (version_compare($currentVersion, $latestVersion, ">=")) {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
"You are using the latest version of PHP " . $minorVersion
));
} else {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"There is a newer PHP patch version ($latestVersion) available (you are using $currentVersion).
You should update to it as soon as possible
(unless the distributor of your PHP binary is backporting security patches)."
));
}
if (empty($this->eolDates[$minorVersion])) {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_INFORMATIONAL,
"No information is know about your PHP version ($currentVersion)."
));
} elseif (new \DateTime() > new \DateTime($this->eolDates[$minorVersion])) {
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_WARNING,
"Your PHP version ($currentVersion) does not recieve security support by the PHP
team anymore. You should update to a newer version
(unless the distributor of your PHP binary is backporting security patches)."
));
} else {
$formattedDate = (Date::factory($this->eolDates[$minorVersion]))->getLocalized(Date::DATE_FORMAT_LONG);
$results->addItem(new DiagnosticResultItem(
DiagnosticResult::STATUS_OK,
"Your PHP version ($minorVersion) receives security support by the PHP
team until $formattedDate."
));
}
} catch (\Exception $e) {
$this->logger->warning($e);
return [$this->testCouldNotRunResult()];
}
return [$results];
}
private function testCouldNotRunResult()
{
return DiagnosticResult::singleResult(
$this->label,
DiagnosticResult::STATUS_INFORMATIONAL,
"Matomo could not check if your PHP version is up-to-date"
);
}
}

13
DiagnosticsExtended.php Normal file
View file

@ -0,0 +1,13 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DiagnosticsExtended;
class DiagnosticsExtended extends \Piwik\Plugin
{
}

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# Matomo DiagnosticExtended Plugin
## Description
Add your plugin description here.

20
config/config.php Normal file
View file

@ -0,0 +1,20 @@
<?php
return [
'diagnosticsExtended.inisettings' => DI\add([
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\AllowUrlInclude'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\DisplayErrors'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\ExposePhp'),
]),
'\Piwik\Plugins\DiagnosticsExtended\Diagnostic\PhpIniCheck' => DI\autowire()
->constructor(
DI\get('diagnosticsExtended.inisettings')
),
'diagnostics.optional' => DI\add([
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\ExampleCheck'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\PhpIniCheck'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\PhpVersionCheck'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\DatabaseVersionCheck'),
DI\get('\Piwik\Plugins\DiagnosticsExtended\Diagnostic\GzipMatomoJsCheck'),
]),
];

2
config/tracker.php Normal file
View file

@ -0,0 +1,2 @@
<?php
return array();

5
docs/faq.md Normal file
View file

@ -0,0 +1,5 @@
## FAQ
__My question?__
My answer

1
docs/index.md Normal file
View file

@ -0,0 +1 @@
## Documentation

29
plugin.json Normal file
View file

@ -0,0 +1,29 @@
{
"name": "DiagnosticsExtended",
"description": "Additional checks for the System Check",
"version": "0.1.0",
"theme": false,
"require": {
"matomo": ">=4.2.1-stable,<5.0.0-b1"
},
"authors": [
{
"name": "Matomo",
"email": "",
"homepage": ""
}
],
"support": {
"email": "",
"issues": "",
"forum": "",
"irc": "",
"wiki": "",
"source": "",
"docs": "",
"rss": ""
},
"homepage": "",
"license": "GPL v3+",
"keywords": []
}

0
screenshots/.gitkeep Normal file
View file