Subversion Repositories vnag

Rev

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
// TODO: Also check extensions
12
 
13
declare(ticks=1);
14
 
15
class JoomlaVersionCheck extends VNag {
16
        protected $argSystemDir = null;
17
 
18
        public function __construct() {
19
                parent::__construct();
20
 
21
                $this->registerExpectedStandardArguments('Vht');
22
 
23
                $this->getHelpManager()->setPluginName('check_joomla_version');
79 daniel-mar 24
                $this->getHelpManager()->setVersion('2023-10-13');
2 daniel-mar 25
                $this->getHelpManager()->setShortDescription('This plugin checks if a local Joomla system has the latest version installed.');
26
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
27
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
28
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
29
 
30
                // Individual (non-standard) arguments:
31
                $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'joomlaPath', 'The local directory where your Joomla installation is located.'));
32
        }
33
 
76 daniel-mar 34
        protected function get_versions($path) {
2 daniel-mar 35
                $path = realpath($path) === false ? $path : realpath($path);
36
 
37
                $manifest_file = $path.'/administrator/manifests/files/joomla.xml';
38
 
39
                if (!file_exists($manifest_file)) {
77 daniel-mar 40
                        throw new VNagException("Manifest file $manifest_file not found");
2 daniel-mar 41
                }
42
 
77 daniel-mar 43
                $cont = @file_get_contents($manifest_file);
44
                if ($cont === false) {
45
                        throw new VNagException("Cannot read $manifest_file");
46
                }
47
 
2 daniel-mar 48
                $manifest = new SimpleXMLElement($cont);
49
 
50
                $version = (string)$manifest->version;
51
 
52
                $count = 0;
53
                foreach ($manifest->updateservers->server as $updateserver) {
76 daniel-mar 54
                        $cont = $this->url_get_contents($updateserver);
55
                        if ($cont !== false) {
2 daniel-mar 56
                                $extensions = new SimpleXMLElement($cont);
57
                                foreach ($extensions as $candidate) {
58
                                        $count++;
59
                                        if ($version == (string)$candidate['version']) {
60
                                                $detailsurl = (string)$candidate['detailsurl'];
61
                                                if ($detailsurl == 'https://update.joomla.org/core/extension.xml') $type = 1;
62
                                                else if ($detailsurl == 'https://update.joomla.org/core/sts/extension_sts.xml') $type = 2;
28 daniel-mar 63
                                                else $type = 0;
2 daniel-mar 64
                                                return array($type, $version);
65
                                        }
66
                                }
67
                        }
68
                }
69
 
70
                if ($count == 0) {
77 daniel-mar 71
                        throw new VNagException("Error checking update servers");
2 daniel-mar 72
                }
73
 
74
                return array(-1, $version);
75
        }
76
 
77
        protected function cbRun($optional_args=array()) {
78
                $system_dir = $this->argSystemDir->getValue();
79
                if (empty($system_dir)) {
77 daniel-mar 80
                        throw new VNagException("Please specify the directory of the Joomla installation.");
2 daniel-mar 81
                }
82
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
83
 
84
                if (!is_dir($system_dir)) {
77 daniel-mar 85
                        throw new VNagException('Directory "'.$system_dir.'" not found.');
2 daniel-mar 86
                }
87
 
76 daniel-mar 88
                list($type, $version) = $this->get_versions($system_dir);
2 daniel-mar 89
 
90
                if ($type == 1) {
91
                        $this->setStatus(VNag::STATUS_OK);
92
                        $this->setHeadline("Version $version (Long-Term-Support) found at $system_dir", true);
93
                } else if ($type == 2) {
94
                        $this->setStatus(VNag::STATUS_OK);
95
                        $this->setHeadline("Version $version (Short-Term-Support) found at $system_dir", true);
96
                } else if ($type == 0) {
97
                        // This should not happen
98
                        $this->setStatus(VNag::STATUS_OK);
99
                        $this->setHeadline("Version $version (supported) found at $system_dir", true);
100
                } else if ($type == -1) {
101
                        $this->setStatus(VNag::STATUS_WARNING);
102
                        $this->setHeadline("Version $version is outdated at $system_dir", true);
103
                } else {
104
                        assert(false);
105
                }
106
        }
107
}
108