Subversion Repositories vnag

Rev

Rev 33 | 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>
57 daniel-mar 2
xYW/6C/wvjTZL6NWDg36CNbd8VUdQ+nizmR3DZoL1h5ilQel2IFUN5YWMqw4Tigvf
3
ksHsVSaLyt0z5J3jEtgSQmg5odNKw74JH5npY2xfnfeoHbFMQlDdFY4xsZh51JW9t
4
xmQWV0B0qASrYDJjZdejxG92UWf+ZrsuOa+3D5t9qZnQsQrIb7dm0daBEY2+bVdx0
5
elCVFpdQ/MUcAZI7MKJoPRxbmQpZQiBJRAq1aR6G09srBOZCkzR62x8pVOiFDE/4B
6
Y4eo/ZTzcolD97seWeqGODnRFdL7rzMUY29iPZlvk0I6s5KWoWJm1bGMeRqX06vRC
7
mZp6bDpaTAoVe1Rh1laJn2+KjLeupsnKCcQncdyy1Y5m9rs+PEia3+ZFRgDmz8p2B
8
ClpX1Ko969jtAe2oYQn82rhnbv+w1cSUJyPnn76FmvShXNC7DuFPWl3rPdEjFUXRe
9
jZD6HAKumND2IAokTU8P/Mn0Xoqj3gpu23RVByOAU9PHrD4M4vfTPLxmiYvyrqBVs
10
rJSvaURUJs4RFvZGlBGjmsRWjOtn6J/1iXiTHk3197IruG/laaBzoBZhOzWv2MV7I
11
xddtrr9blabIHkngnwD5XtO40x8P5kKH9qc4l8vBl1MGNvN1BJ2BJPV6X4IrVSmJ5
12
sz3SCgXJkEVFth/KZCc+6DtD/bze4gj9Ad212vTkPMsxS0NTALDd6nLdExjj5TewQ
13
B0YLmZsn+hltPTbxod6ukis3UTE92GUsN35K/DNyJcfJ/cebbQlcMhcAojLRkUNTq
14
1W6M2m24l6CGwkUPM1w0e3RlyQA5TS2PjX8TRX91spRXY8hD8fpNHL9+GOzbEIyIy
15
73yeoOGHeB1hSnD4H2LSs3dwl1189PyuNl2jJNVa6cLPy3R1Yixz4q29XkS60Oxpg
16
lGme9nlW2S4yJJLB/VWStEXNeCOfTvKIHmTZSEQ1RvxttRBkcjMoJqZbaKpwW8rVB
17
PVaw07SCi7IKXWiosY9hMvHbnLU0GkbgkxW09bXw4LkTLwYHEfbGGsBe1qS1IGF7/
18
JzfPxj7RX0tDZX5wuolJEZb+rer76VJD1wNdzcpbTAAdu/bf3Ptwblx6eIFQw6+YN
19
/eOs1YC4VAUedKGh1FruSU5wZiJJDizP/+33OapudvTaRlK6/AEdSc5JnP5GsFQah
20
1CzAU0WekFhFP51SmSMsefVjC3AzWSDJUTa4E+e8SsyYOkHCDAZmcI8wL+KMsbNLs
21
0QMHW9GwY+9BP+iGMFBMklBWEFkKkPx/QfbqGW0JB4bjcgB3UVHMAHbJ5+DlN9SZ7
22
EUqh5dIkoaQ8FwKIIIa8qw7GCN6F96IKZ+3O80RVSm6ZPDSdd/Z+qgPUg06La0Xme
23
g==
32 daniel-mar 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
 *
57 daniel-mar 32
 * Revision 2022-09-12
32 daniel-mar 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
 
57 daniel-mar 142
                if (version_compare($cur_ver,$new_ver) >= 0) {
32 daniel-mar 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
}