Subversion Repositories vnag

Rev

Rev 77 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * VNag - Nagios Framework for PHP
  5.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  6.  * Licensed under the terms of the Apache 2.0 license
  7.  *
  8.  * Revision 2023-10-13
  9.  */
  10.  
  11. declare(ticks=1);
  12.  
  13. class PhpMyAdminVersionCheck extends VNag {
  14.         protected $argSystemDir = null;
  15.  
  16.         public function __construct() {
  17.                 parent::__construct();
  18.  
  19.                 $this->registerExpectedStandardArguments('Vht');
  20.  
  21.                 $this->getHelpManager()->setPluginName('check_phpmyadmin_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local phpMyAdmin system has the latest version installed.');
  24.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  25.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  26.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  27.  
  28.                 // Individual (non-standard) arguments:
  29.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'phpmyadminPath', 'The local directory where your phpMyAdmin installation is located.'));
  30.         }
  31.  
  32.         protected function get_local_version($path) {
  33.                 $path = realpath($path) === false ? $path : realpath($path);
  34.  
  35.                 if (file_exists($file = "$path/libraries/classes/Version.php")) { // Variant 3 (5.1.0+)
  36.                         $regex = '@VERSION = \'(.*)\'@ismU';
  37.                 } else if (file_exists($file = "$path/libraries/classes/Config.php")) { // Variant 2 (5.0.x)
  38.                         $regex = '@\$this->set\(\'PMA_VERSION\', \'(.*)\'\);@ismU';
  39.                 } else if (file_exists($file = "$path/libraries/Config.class.php")) { // Variant 1 (very old)
  40.                         $regex = '@\$this->set\(\'PMA_VERSION\', \'(.*)\'\);@ismU';
  41.                 } else {
  42.                         throw new VNagException("Cannot find the phpMyAdmin version information at $path");
  43.                 }
  44.  
  45.                 $cont = @file_get_contents($file);
  46.                 if ($cont === false) {
  47.                         throw new VNagException("Cannot parse the phpMyAdmin version information at $path (cannot open file $file)");
  48.                 }
  49.                 if (!preg_match($regex, $cont, $m)) {
  50.                         throw new VNagException("Cannot parse the phpMyAdmin version information at $path (cannot find version using regex)");
  51.                 }
  52.  
  53.                 return $m[1];
  54.         }
  55.  
  56.         protected function get_latest_version() {
  57.                 $cont = $this->url_get_contents('https://www.phpmyadmin.net/home_page/version.json'); // alternatively version.php
  58.                 if ($cont === false) {
  59.                         throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Cannot access phpmyadmin.net)');
  60.                 }
  61.  
  62.                 $json = @json_decode($cont, true);
  63.                 if ($json === false) {
  64.                         throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Invalid JSON data downloaded from phpmyadmin.net)');
  65.                 }
  66.                 return $json['version'];
  67.         }
  68.  
  69.         protected function cbRun($optional_args=array()) {
  70.                 $system_dir = $this->argSystemDir->getValue();
  71.                 if (empty($system_dir)) {
  72.                         throw new VNagException("Please specify the directory of the phpMyAdmin installation.");
  73.                 }
  74.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  75.  
  76.                 if (!is_dir($system_dir)) {
  77.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  78.                 }
  79.  
  80.                 $version = $this->get_local_version($system_dir);
  81.  
  82.                 $latest_version = $this->get_latest_version();
  83.  
  84.                 if (version_compare($version,$latest_version) >= 0) {
  85.                         $this->setStatus(VNag::STATUS_OK);
  86.                         if ($version == $latest_version) {
  87.                                 $this->setHeadline("Version $version (Latest version) at $system_dir", true);
  88.                         } else {
  89.                                 $this->setHeadline("Version $version (Latest version $latest_version) at $system_dir", true);
  90.                         }
  91.                 } else {
  92.                         $this->setStatus(VNag::STATUS_WARNING);
  93.                         $this->setHeadline("Version $version is outdated (Latest version is $latest_version) at $system_dir", true);
  94.                 }
  95.         }
  96. }
  97.