Rev 77 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 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 | * |
||
76 | daniel-mar | 8 | * Revision 2023-10-13 |
2 | daniel-mar | 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'); |
||
79 | daniel-mar | 22 | $this->getHelpManager()->setVersion('2023-10-13'); |
2 | daniel-mar | 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 | |||
76 | daniel-mar | 32 | protected function get_local_version($path) { |
2 | daniel-mar | 33 | $path = realpath($path) === false ? $path : realpath($path); |
34 | |||
28 | daniel-mar | 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'; |
||
2 | daniel-mar | 41 | } else { |
77 | daniel-mar | 42 | throw new VNagException("Cannot find the phpMyAdmin version information at $path"); |
2 | daniel-mar | 43 | } |
44 | |||
77 | daniel-mar | 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 | } |
||
28 | daniel-mar | 49 | if (!preg_match($regex, $cont, $m)) { |
77 | daniel-mar | 50 | throw new VNagException("Cannot parse the phpMyAdmin version information at $path (cannot find version using regex)"); |
2 | daniel-mar | 51 | } |
52 | |||
53 | return $m[1]; |
||
54 | } |
||
55 | |||
76 | daniel-mar | 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) { |
||
77 | daniel-mar | 59 | throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Cannot access phpmyadmin.net)'); |
76 | daniel-mar | 60 | } |
61 | |||
62 | $json = @json_decode($cont, true); |
||
63 | if ($json === false) { |
||
77 | daniel-mar | 64 | throw new VNagException('Cannot parse version from phpMyAdmin website. The plugin probably needs to be updated. (Invalid JSON data downloaded from phpmyadmin.net)'); |
76 | daniel-mar | 65 | } |
66 | return $json['version']; |
||
67 | } |
||
68 | |||
2 | daniel-mar | 69 | protected function cbRun($optional_args=array()) { |
70 | $system_dir = $this->argSystemDir->getValue(); |
||
71 | if (empty($system_dir)) { |
||
77 | daniel-mar | 72 | throw new VNagException("Please specify the directory of the phpMyAdmin installation."); |
2 | daniel-mar | 73 | } |
74 | $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir); |
||
75 | |||
76 | if (!is_dir($system_dir)) { |
||
77 | daniel-mar | 77 | throw new VNagException('Directory "'.$system_dir.'" not found.'); |
2 | daniel-mar | 78 | } |
79 | |||
76 | daniel-mar | 80 | $version = $this->get_local_version($system_dir); |
2 | daniel-mar | 81 | |
76 | daniel-mar | 82 | $latest_version = $this->get_latest_version(); |
2 | daniel-mar | 83 | |
57 | daniel-mar | 84 | if (version_compare($version,$latest_version) >= 0) { |
2 | daniel-mar | 85 | $this->setStatus(VNag::STATUS_OK); |
57 | daniel-mar | 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 | } |
||
2 | daniel-mar | 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 | } |