Subversion Repositories vnag

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
32 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
2
gX9e3ZsTr/5gCndjoXkh5Y65OgXPOPRob9TUwW2ssq+YdiQl3g/A8t35kb23dX6WN
3
fU0aZTP1S2DBLLaf0I6CiKrli99a9KsHjA7kH4rmjed5RFwqBoRbUxJTFvt4X0EkX
4
8k/sIUmdES8kVO+JTfIlUB4ZFjVlc89bTWQaeagXkE58f8Fhr+Up9GvQJ2eJDEUpv
5
Zfw7kHZ+odkkiK5L+aCP+Mwl+A044R/e8SnBMf3ng7PKwLRoGu9d2iRAEdAIUzZWz
6
RAxmnyVqsjtUUxSPrCp+3WOmzdR1hWLZm+BJvpGOTjuICKXxOatyIsBR76mh2ndri
7
PrD9y8NLdI/azsEiJWZgCJr8h1R2/SWF1ndiC5nshT2sZBAQLdSQ/zvcbIdHtnibq
8
63KEDcR/m3V657gVmT1UAAM7JtcTh2x9GDz5ZWAg7Vcb+q54s9A6NNEXaHOc3XMHq
9
aCGuf1EYZyaQeTiyPkgJHwtH4rawwu4K+keDm+loS/KQ9btuC0tDLcupwJZn9qREo
10
2YJ2IwKaNzRj7DNkIG7ASwqwcbWQSx2RqUWm9zG0D5NIx+Jc97A19v3p3HaplGH5K
11
+1anSj87rtxENA+n1L+8ZbvI4SZ0txLxS2sJwYr/0Pv2gWUxUdoVhmNPRWAKnQM8w
12
12TqndyF4ls5KamJemkz3RSiYLWH67sW1jBi+QYu/VGcf15vOmlHMQemf+TEly1sA
13
zn7BQsxGS5Wosux4fToyLNKNvXlaWiuo6Q1SACRTns9rKnX9atFrnydO1NHaA1WkA
14
KgEQolI7fL2ggz+6veuyAbHnpMvp26essnXApwC604HOFW7IB4lrnZCyk8UHtGixD
15
xy7bHKXV1jPGXkN/AUADY3Avq1/6N65GAAPNoHnKrlGVjDJyI7H/KMiTPASzJzJ0e
16
XFpmgWSHmQfPMNv0dFwjE8siQyF2FwGz4bYb4FbfLheUhEZRcGtZniAZMHT4zPAK4
17
+Mp61QsX/79a1NBg3gmPtk1cziK6UyhuD2BubUm3y4MXANDxZhe0mw0UxKQtW0XDL
18
yXBDmGL8D3yPdmafx4UublOjvfAJLZDjYENPoaDhH8ZCowcdSbe7NZKsCtNWsRh+a
19
7kEUZ2wEtCWiajyOq4piR9fRO4uVb+4Umdw+UaG6kKlCbuV4jkC48EQUUt56c19zK
20
WopXwCSa2GZioCz5vUzNoeNtdjJDMXgW+Af/PbV4qBRZkv3vYJ84Z8fFDZrGqUnpU
21
5mLlK8KuaGLg3nZLXsNYgepTXPqvtTvC9Xps/HNsUBiHBcKNcff8iqOl6cWZlBCl1
22
hOWTlTqhzBLCLnXKxnvX+yG9c2chKna94ZAPgX7w95k5vX8+xOg9nfwAIfjZkm83C
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
 
63
                // TODO: this is a security vulnerability if an non-root user can control the input of version.php !
64
                $vendor = 'unknown';
65
                $OC_Version = array(0,0,0,0);
66
                $OC_VersionString = 'unknown';
67
                $OC_Edition = 'unknown';
68
                $OC_Channel = 'unknown';
69
                $OC_Build = 'unknown';
70
                include $local_path . '/version.php';
71
 
72
                if ($vendor != 'nextcloud') {
73
                        throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". It is "$vendor".');
74
                }
75
 
76
                $baseUrl = 'https://updates.nextcloud.org/updater_server/';
77
 
78
                // More information about the paramters, see https://github.com/nextcloud/updater_server/blob/master/src/Request.php
79
                $php_version = explode('.', PHP_VERSION);
80
                $update_url = $baseUrl . '?version='.
81
                              implode('x', $OC_Version).'x'.
82
                              'x'. // installationMtime
83
                              'x'. // lastCheck
84
                              $OC_Channel.'x'.
85
                              $OC_Edition.'x'.
86
                              urlencode($OC_Build).'x'.
87
                              $php_version[0].'x'.
88
                              $php_version[1].'x'.
89
                              intval($php_version[2]); // Last part could be something like "28-2+0~20210604.85+debian9~1.gbp219f11"
90
 
91
                $cont = file_get_contents($update_url);
92
                if ($cont === false) {
93
                        throw new Exception('Could not determinate current Nextcloud version in "'.$local_path.'".');
94
                }
95
 
96
                if ($cont === '') {
97
                        return array($OC_VersionString, $OC_VersionString, $OC_Channel);
98
                } else {
99
                        $xml = simplexml_load_string($cont);
100
                        $new_ver = (string)$xml->version;
101
                        return array($OC_VersionString, $new_ver, $OC_Channel);
102
                }
103
        }
104
 
105
        protected function cbRun($optional_args=array()) {
106
                $system_dir = $this->argSystemDir->getValue();
107
                if (empty($system_dir)) {
108
                        throw new Exception("Please specify the directory of the Nextcloud installation.");
109
                }
110
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
111
 
112
                if (!is_dir($system_dir)) {
113
                        throw new Exception('Directory "'.$system_dir.'" not found.');
114
                }
115
 
116
                list($cur_ver, $new_ver, $channel) = $this->get_nextcloud_version($system_dir);
117
 
118
                if ($cur_ver === $new_ver) {
119
                        $this->setStatus(VNag::STATUS_OK);
120
                        $this->setHeadline("Nextcloud version $cur_ver [$channel] is the latest available version for your Nextcloud installation at $system_dir", true);
121
                } else {
122
                        $this->setStatus(VNag::STATUS_WARNING);
123
                        $this->setHeadline("Nextcloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
124
                }
125
        }
126
}