<?php
/*
* VNag - Nagios Framework for PHP
* Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
* Licensed under the terms of the Apache 2.0 license
*
* Revision 2023-10-13
*/
declare(ticks=1);
class PhpMyAdminVersionCheck extends VNag {
protected $argSystemDir = null;
public function __construct() {
parent::__construct();
$this->registerExpectedStandardArguments('Vht');
$this->getHelpManager()->setPluginName('check_phpmyadmin_version');
$this->getHelpManager()->setVersion('2023-10-13');
$this->getHelpManager()->setShortDescription('This plugin checks if a local phpMyAdmin system has the latest version installed.');
$this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
$this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
$this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
// Individual (non-standard) arguments:
$this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'phpmyadminPath', 'The local directory where your phpMyAdmin installation is located.'));
}
protected function get_local_version($path) {
if (file_exists($file = "$path/libraries/classes/Version.php")) { // Variant 3 (5.1.0+)
$regex = '@VERSION = \'(.*)\'@ismU';
} else if (file_exists($file = "$path/libraries/classes/Config.php")) { // Variant 2 (5.0.x)
$regex = '@\$this->set\(\'PMA_VERSION\', \'(.*)\'\);@ismU';
} else if (file_exists($file = "$path/libraries/Config.class.php")) { // Variant 1 (very old)
$regex = '@\$this->set\(\'PMA_VERSION\', \'(.*)\'\);@ismU';
} else {
throw new VNagException("Cannot find the phpMyAdmin version information at $path");
}
if ($cont === false) {
throw new VNagException("Cannot parse the phpMyAdmin version information at $path (cannot open file $file)");
}
throw new VNagException("Cannot parse the phpMyAdmin version information at $path (cannot find version using regex)");
}
return $m[1];
}
protected function get_latest_version() {
$cont = $this->url_get_contents('https://www.phpmyadmin.net/home_page/version.json'); // alternatively version.php
if ($cont === false) {
throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Cannot access phpmyadmin.net)');
}
if ($json === false) {
throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Invalid JSON data downloaded from phpmyadmin.net)');
}
return $json['version'];
}
protected function cbRun
($optional_args=array()) {
$system_dir = $this->argSystemDir->getValue();
if (empty($system_dir)) {
throw new VNagException("Please specify the directory of the phpMyAdmin installation.");
}
$system_dir = realpath($system_dir) === false ?
$system_dir : realpath($system_dir);
throw new VNagException('Directory "'.$system_dir.'" not found.');
}
$version = $this->get_local_version($system_dir);
$latest_version = $this->get_latest_version();
$this->setStatus(VNag::STATUS_OK);
if ($version == $latest_version) {
$this->setHeadline("Version $version (Latest version) at $system_dir", true);
} else {
$this->setHeadline("Version $version (Latest version $latest_version) at $system_dir", true);
}
} else {
$this->setStatus(VNag::STATUS_WARNING);
$this->setHeadline("Version $version is outdated (Latest version is $latest_version) at $system_dir", true);
}
}
}