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
32 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
32 daniel-mar 9
 */
10
 
11
declare(ticks=1);
12
 
13
class OwnCloudVersionCheck extends VNag {
14
        protected $argSystemDir = null;
15
 
16
        public function __construct() {
17
                parent::__construct();
18
 
19
                $this->registerExpectedStandardArguments('Vvht');
20
 
21
                $this->getHelpManager()->setPluginName('check_owncloud_version');
79 daniel-mar 22
                $this->getHelpManager()->setVersion('2023-10-13');
32 daniel-mar 23
                $this->getHelpManager()->setShortDescription('This plugin checks if a local ownCloud 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, 'ownCloudPath', 'The local directory where your ownCloud installation is located.'));
30
        }
31
 
76 daniel-mar 32
        protected function get_versions($local_path) {
32 daniel-mar 33
                $local_path = realpath($local_path) === false ? $local_path : realpath($local_path);
34
 
35
                if (!file_exists($local_path . '/version.php')) {
77 daniel-mar 36
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'".');
32 daniel-mar 37
                }
38
 
33 daniel-mar 39
                // We don't include version.php because it would be a security vulnerability
40
                // code injection if somebody controls version.php
77 daniel-mar 41
                $cont = @file_get_contents($local_path . '/version.php');
42
                if ($cont === false) {
43
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing version.php file.');
44
                }
33 daniel-mar 45
                if (preg_match('@\\$(OC_Version)\\s*=\\s*array\\(\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*\\)\\s*;@ismU', $cont, $m)) {
46
                        $OC_Version = array($m[2],$m[3],$m[4],$m[5]);
47
                } else {
77 daniel-mar 48
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "OC_Version".');
32 daniel-mar 49
                }
33 daniel-mar 50
                if (preg_match('@\\$(OC_VersionString)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
51
                        $OC_VersionString = $m[3];
52
                } else {
77 daniel-mar 53
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "OC_VersionString".');
33 daniel-mar 54
                }
55
                if (preg_match('@\\$(OC_Edition)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
56
                        $OC_Edition = $m[3];
57
                } else {
77 daniel-mar 58
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "OC_Edition".');
33 daniel-mar 59
                }
60
                if (preg_match('@\\$(OC_Channel)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
61
                        $OC_Channel = $m[3];
62
                } else {
77 daniel-mar 63
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "OC_Channel".');
33 daniel-mar 64
                }
65
                if (preg_match('@\\$(OC_Build)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
66
                        $OC_Build = $m[3];
67
                } else {
77 daniel-mar 68
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "OC_Build".');
33 daniel-mar 69
                }
70
                if (preg_match('@\\$(vendor)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
71
                        $vendor = $m[3];
72
                        if ($vendor != 'owncloud') {
77 daniel-mar 73
                                throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". It is "'.$vendor.'".');
33 daniel-mar 74
                        }
75
                } else {
77 daniel-mar 76
                        throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "vendor".');
33 daniel-mar 77
                }
32 daniel-mar 78
 
79
                // Owncloud\Updater\Utils\Fetcher::DEFAULT_BASE_URL
80
                $baseUrl = 'https://updates.owncloud.com/server/';
81
 
82
                $update_url = $baseUrl . '?version='.
83
                              implode('x', $OC_Version).'x'.
84
                              'installedatx'.
85
                              'lastupdatedatx'.
86
                              $OC_Channel.'x'.
87
                              $OC_Edition.'x'.
88
                              urlencode($OC_Build);
89
 
76 daniel-mar 90
                $cont = $this->url_get_contents($update_url);
32 daniel-mar 91
                if ($cont === false) {
77 daniel-mar 92
                        throw new VNagException('Could not determinate current ownCloud version in "'.$local_path.'". (Cannot access '.$update_url.')');
32 daniel-mar 93
                }
94
 
95
                if ($cont === '') {
96
                        return array($OC_VersionString, $OC_VersionString, $OC_Channel);
97
                } else {
98
                        $xml = simplexml_load_string($cont);
76 daniel-mar 99
                        if ($xml === false) {
77 daniel-mar 100
                                throw new VNagException('Could not determinate current ownCloud version in "'.$local_path.'". (Invalid XML downloaded from update-server)');
76 daniel-mar 101
                        }
32 daniel-mar 102
                        $new_ver = (string)$xml->version;
103
                        return array($OC_VersionString, $new_ver, $OC_Channel);
104
                }
105
        }
106
 
107
        protected function cbRun($optional_args=array()) {
108
                $system_dir = $this->argSystemDir->getValue();
109
                if (empty($system_dir)) {
77 daniel-mar 110
                        throw new VNagException("Please specify the directory of the ownCloud installation.");
32 daniel-mar 111
                }
112
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
113
 
114
                if (!is_dir($system_dir)) {
77 daniel-mar 115
                        throw new VNagException('Directory "'.$system_dir.'" not found.');
32 daniel-mar 116
                }
117
 
76 daniel-mar 118
                list($cur_ver, $new_ver, $channel) = $this->get_versions($system_dir);
32 daniel-mar 119
 
57 daniel-mar 120
                if (version_compare($cur_ver,$new_ver) >= 0) {
32 daniel-mar 121
                        $this->setStatus(VNag::STATUS_OK);
122
                        $this->setHeadline("ownCloud version $cur_ver [$channel] is the latest available version for your ownCloud installation at $system_dir", true);
123
                } else {
124
                        $this->setStatus(VNag::STATUS_WARNING);
125
                        $this->setHeadline("ownCloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
126
                }
127
        }
128
}