Subversion Repositories vnag

Rev

Rev 32 | 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 /* <ViaThinkSoftSignature>
33 daniel-mar 2
iHvcQ5LzNAer/Ap18FaL5bM2842CWMdX5Gald3rv3jnW8Dl69p7W0T7cUWEPrM39v
3
ZUoVTv+XcWGY4v0L1rTx1UOq68z/vGW9dzCLuPEytVjT3SRojx0qhBOZZHkhGgXKA
4
EEsEKomsdteMo0aMMq6EiM9HDYLVJXnjkzD9QavXNcXP0B52ChK/DL3zhAVan83p9
5
P8zkkRY399qTfoM8IP2vEadUafsYtJewWvxgi/L6U3ogBPTMEF9Ba9iEWBmZCfbRH
6
upSkg1aAetrtAs03HFVpKNb2gC1LUCcwvRT8E06yjU3+9vFSfl4YhXiFuMeSIOaFB
7
H/ZW8kB+sRwkZQoohBrikbXkgj4mJANfqNx7lRvWBLWoJ349vvh+iJdFIIIoOurc7
8
Vlc7MKSRSZ2CNMcTLHLT8pZ5RVdzO2If5iU5diHGWSlX9beliwuZpfJQ86nFOSdIY
9
V65Z0U2hlamxeXbAVWYZbAZ4QlYyl7JlJ32QBcKAMgK5DVc83ZRInPXS/0prvrCDf
10
h17iTLL27hdx8mXSEgmQgFMBS2fyV6+vJxQpdbwUAZTfIcGXyLPKUgXn3jypSzBt4
11
n4a63YcSJWL3iBjQpj1SUYhTCrZ3HlR8OUZ5NRpcUcTwt7TTkNvV3+NAj+LvrAUyv
12
iWK4y7U86l2cLO0b/NHHE7BYZkQhnPCjTjR2tKeRAI63A4+2sor8jtaHGCXCGas59
13
VHYgwbpOJWe3igTv4n0wshGdeO9VgM0EbpwrgIq3zEzbrl1QbfxO3k8TBRWb8wB/F
14
c0zbXJOZqYXdd9eZ6hjxLu/AFjpPJC43PqcVuKHX6Vp3Qwod3zebQxbYl4ajmV3EO
15
Q0nAHJKQbrZ/edxOeYA9RhRP9LUp49fbf8e+GjdEcFqkiAPDjtPhPVPi+fBsx+MfQ
16
UxF+ZmDgjrxaDeTWGuCjv2BJ+q1uGIBkRN0eo+70cRNaaSppxxPED6DxXEA3sw/us
17
OZyvMER1xvTPkLauaNmxZ9zj7nBiyHEO8Gp58XNIkKrNyh1YBYckRmJLCG+x3qP4M
18
WD0tZHuUvLWiPDhUYSVKuVq0qjZOsn9qcTgkV/BXSmnPEPutXyZYShnA5/0RlPaq4
19
XvrbTSHCvviXvJr/rgDtE+7llx6l+PLujtgm1v049sp+CHNy2312NIC73/HSKWU1h
20
GYzKuWt8WOWmVPGNlaQSFEeIkYfZwEp/Og2+QN9TgQsYNcVYBTmo+E7gfgDfj1Lcf
21
YDmXLnZJFOAqVMFlHbiX9/rn3VzSwVHNiIOU1qS+9uYezXbKzjnWTvODfv0gS7Neg
22
/MbDtsRQWG7UMpTPT96oGjGKxPtuBvNg6YwDxNopSG3AfKwLzVI6imvcMhCug9znS
32 daniel-mar 23
w==
24
</ViaThinkSoftSignature> */ ?>
25
<?php
26
 
27
/*
28
 * VNag - Nagios Framework for PHP
29
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
30
 * Licensed under the terms of the Apache 2.0 license
31
 *
32
 * Revision 2021-06-26
33
 */
34
 
35
declare(ticks=1);
36
 
37
class NextCloudVersionCheck extends VNag {
38
        protected $argSystemDir = null;
39
 
40
        public function __construct() {
41
                parent::__construct();
42
 
43
                $this->registerExpectedStandardArguments('Vvht');
44
 
45
                $this->getHelpManager()->setPluginName('check_nextcloud_version');
46
                $this->getHelpManager()->setVersion('1.0');
47
                $this->getHelpManager()->setShortDescription('This plugin checks if a local Nextcloud system has the latest version installed.');
48
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
49
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
50
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
51
 
52
                // Individual (non-standard) arguments:
53
                $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'nextCloudPath', 'The local directory where your Nextcloud installation is located.'));
54
        }
55
 
56
        protected function get_nextcloud_version($local_path) {
57
                $local_path = realpath($local_path) === false ? $local_path : realpath($local_path);
58
 
59
                if (!file_exists($local_path . '/version.php')) {
60
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'".');
61
                }
62
 
33 daniel-mar 63
                // We don't include version.php because it would be a security vulnerability
64
                // code injection if somebody controls version.php
65
                $cont = file_get_contents($local_path . '/version.php');
66
                if (preg_match('@\\$(OC_Version)\\s*=\\s*array\\(\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*\\)\\s*;@ismU', $cont, $m)) {
67
                        $OC_Version = array($m[2],$m[3],$m[4],$m[5]);
68
                } else {
69
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Version".');
32 daniel-mar 70
                }
33 daniel-mar 71
                if (preg_match('@\\$(OC_VersionString)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
72
                        $OC_VersionString = $m[3];
73
                } else {
74
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_VersionString".');
75
                }
76
                if (preg_match('@\\$(OC_Edition)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
77
                        $OC_Edition = $m[3];
78
                } else {
79
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Edition".');
80
                }
81
                if (preg_match('@\\$(OC_Channel)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
82
                        $OC_Channel = $m[3];
83
                } else {
84
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Channel".');
85
                }
86
                if (preg_match('@\\$(OC_Build)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
87
                        $OC_Build = $m[3];
88
                } else {
89
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Build".');
90
                }
91
                if (preg_match('@\\$(vendor)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
92
                        $vendor = $m[3];
93
                        if ($vendor != 'nextcloud') {
94
                                throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". It is "'.$vendor.'".');
95
                        }
96
                } else {
97
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "vendor".');
98
                }
32 daniel-mar 99
 
100
                $baseUrl = 'https://updates.nextcloud.org/updater_server/';
101
 
102
                // More information about the paramters, see https://github.com/nextcloud/updater_server/blob/master/src/Request.php
103
                $php_version = explode('.', PHP_VERSION);
104
                $update_url = $baseUrl . '?version='.
105
                              implode('x', $OC_Version).'x'.
106
                              'x'. // installationMtime
107
                              'x'. // lastCheck
108
                              $OC_Channel.'x'.
109
                              $OC_Edition.'x'.
110
                              urlencode($OC_Build).'x'.
111
                              $php_version[0].'x'.
112
                              $php_version[1].'x'.
113
                              intval($php_version[2]); // Last part could be something like "28-2+0~20210604.85+debian9~1.gbp219f11"
114
 
115
                $cont = file_get_contents($update_url);
116
                if ($cont === false) {
117
                        throw new Exception('Could not determinate current Nextcloud version in "'.$local_path.'".');
118
                }
119
 
120
                if ($cont === '') {
121
                        return array($OC_VersionString, $OC_VersionString, $OC_Channel);
122
                } else {
123
                        $xml = simplexml_load_string($cont);
124
                        $new_ver = (string)$xml->version;
125
                        return array($OC_VersionString, $new_ver, $OC_Channel);
126
                }
127
        }
128
 
129
        protected function cbRun($optional_args=array()) {
130
                $system_dir = $this->argSystemDir->getValue();
131
                if (empty($system_dir)) {
132
                        throw new Exception("Please specify the directory of the Nextcloud installation.");
133
                }
134
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
135
 
136
                if (!is_dir($system_dir)) {
137
                        throw new Exception('Directory "'.$system_dir.'" not found.');
138
                }
139
 
140
                list($cur_ver, $new_ver, $channel) = $this->get_nextcloud_version($system_dir);
141
 
142
                if ($cur_ver === $new_ver) {
143
                        $this->setStatus(VNag::STATUS_OK);
144
                        $this->setHeadline("Nextcloud version $cur_ver [$channel] is the latest available version for your Nextcloud installation at $system_dir", true);
145
                } else {
146
                        $this->setStatus(VNag::STATUS_WARNING);
147
                        $this->setHeadline("Nextcloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
148
                }
149
        }
150
}