Subversion Repositories vnag

Rev

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

Rev Author Line No. Line
2 daniel-mar 1
<?php /* <ViaThinkSoftSignature>XTWdCCK0FEM0wejpeajPPdpwhBNotRfIAK4cUBRtt32pcQoxsNdP1sU8Zw38IEq2BP1pK8ELVVyb20dUZGQouY42DJQaTE0RRDGSfKZVxsYWAdLyiRc1gWPVsj3fHFJHJOjey16hbqsdiKLpwT8fHgqaCz0y87oeksLQOmGw1VXfbQECYz3XU1JGie+Mxbx/dZNgMb3l3juVwrs9veDBU2Q0HyeVAsV1xebfm+AfKngIV0dBEFW+kkBAcFuwzfaLIlhTC5Vt1mvs7ZVbrgFxybkTuPV0J7aHwqt9qrRUiJLCOT6Vd2JuMQisCjVecjKD6XSNq2+ZQ/YOuzuQ8HtPm3zKZeyk+PjQ+LqcoRY0i8MEXyxkHwZbBIgwko00Nw9hzSbtE0iQfDi/KtAYBr8bb3U8ufMgbRWWf3ayrDOAJ2wNVgt6CqZACtPncvdpEc0fTAMm5n7oinbC81zCpOCza9+Wka1Rl3w6forL9vezAG10K+9tn7PyUFKTxJKEUY2agFtumywKDh2tLoODZyZ8mNF3/pUeCgZFzoYgRvdRHBqc4E3Bnqb3ffB1JSaHl0FizufFv1UdHrG0EYXL7VARPHjDAXNi9EiPsudZP3XREcQpDSk01zqajMrA4s7PphLKigGcvW9nw5BTlo8i10RWzQcSsIqQNP7wibr5TZUrkSVjRZZNOOXepdGiAHapKn9h2tTcYbQb8yG5j4bGTzFQsD/9wJql+acM/sdwDR3Gn/j0765eioW3DUCp0QeTmHewerq85aL+T+GJA3cjFkk1yemgOVI9QlDFBrCe1iYnep+XxuN8vEDM/AXy1e122FCDAHACWFTE6qrbDSF5Gr92vMoQKyKtTTDJRDjrAIkPtQzSSHeyHUqKAFN2xapEiyq6rbuSQvbBnQPp+zDPkJUTV55U84JpRWmVEDe2TdUJcrJKRRGGY0kXZk75M+9L3UUDJljSzTemvMSD/kLFz0tGFHLzLguUZFlKMMQwsV/+WqQ13jl9FO8r7zmEG+9f3S2kqhQEyF+ZjmqBeSh5vb6zOzQ5ZSyPMdVcdYtOnPhKAARA3/oj9T9IB0Ybji4bYs7u2cD00yxHJ/f9yR5CRY6T6CJHuY6qduzacD6t8wrigfLQtv2/K27hpdoTOQi6oqPCy1zbF9UY6N4JpBVHnsMDv7akwPTMNTB0tuYTIHH8rcQudrJfqXUXquTgL35k8KaB+IGtGlrGq0w9OtCA199or1heFAvPzi6lGkjF5rftF/V4HpO4JZQ2lKY41Z2ZXGK75oryv2DL79h4cvdFdhR7O88AHlwlcodGTMfaKa2o51scaVjjDirWe4lZb0uXXLHDr19dDeajObnBFnukl1FGaQ==</ViaThinkSoftSignature> */ ?>
2
<?php
3
 
4
/*
5
 * VNag - Nagios Framework for PHP
6
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
7
 * Licensed under the terms of the Apache 2.0 license
8
 *
9
 * Revision 2018-07-15
10
 */
11
 
12
// TODO: Should we also warn if a newer major version is released?
13
 
14
declare(ticks=1);
15
 
16
class PhpBbVersionCheck extends VNag {
17
        protected $argSystemDir = null;
18
 
19
        public function __construct() {
20
                parent::__construct();
21
 
22
                $this->registerExpectedStandardArguments('Vvht');
23
 
24
                $this->getHelpManager()->setPluginName('check_phpbb_version');
25
                $this->getHelpManager()->setVersion('1.0');
26
                $this->getHelpManager()->setShortDescription('This plugin checks if a local phpBB system has the latest version installed.');
27
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
28
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
29
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
30
 
31
                // Individual (non-standard) arguments:
32
                $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'phpBBPath', 'The local directory where your phpBB installation is located.'));
33
        }
34
 
35
        protected function get_phpbb_version($path) {
36
                $path = realpath($path) === false ? $path : realpath($path);
37
 
38
                // Version 3 support
39
                $cont = @file_get_contents("$path/includes/constants.php");
40
                preg_match("@define\('PHPBB_VERSION', '(.*)'\);@ismU", $cont, $m);
41
 
42
                if (isset($m[1])) {
43
                        return $m[1];
44
                } else {
45
                        // Version 2 support
46
                        $cont = @file_get_contents("$path/docs/INSTALL.html");
47
                        preg_match("@phpBB-(.*)_to_(.*)\.(zip|patch|tar)@ismU", $cont, $m);
48
 
49
                        if (isset($m[2])) {
50
                                return $m[2];
51
                        } else {
52
                                return false;
53
                        }
54
                }
55
        }
56
 
57
        protected function isVersionCurrentStable($json, $version) {
58
                foreach ($json['stable'] as $major => $data) {
59
                        if ($data['current'] == $version) return true;
60
                }
61
                return false;
62
        }
63
 
64
        protected function isVersionCurrentUnstable($json, $version) {
65
                foreach ($json['unstable'] as $major => $data) {
66
                        if ($data['current'] == $version) return true;
67
                }
68
                return false;
69
        }
70
 
71
        protected function cbRun($optional_args=array()) {
72
                $system_dir = $this->argSystemDir->getValue();
73
                if (empty($system_dir)) {
74
                        throw new Exception("Please specify the directory of the phpBB installation.");
75
                }
76
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
77
 
78
                if (!is_dir($system_dir)) {
79
                        throw new Exception('Directory "'.$system_dir.'" not found.', false);
80
                }
81
 
82
                // 1. Checking the main system
83
 
84
                // See also the original version checking code at "phpbb/version_helper.php"
85
                // More information about the JSON structure: https://area51.phpbb.com/docs/dev/extensions/tutorial_advanced.html#extension-version-checking
86
                // (Note: We should check regularly if the fields 'eol' and 'security' will be officially implemented/described)
87
                $versionCheckUrl = "https://version.phpbb.com/phpbb/versions.json";
88
                $cont = @file_get_contents($versionCheckUrl);
89
                if ($cont === false) {
90
                        throw new Exception('Could not determinate latest phpBB version');
91
                }
92
                $json = @json_decode($cont,true);
93
                if ($json === false) {
94
                        throw new Exception('Could not determinate latest phpBB version');
95
                }
96
 
97
                $version = $this->get_phpbb_version($system_dir);
98
 
99
                if ($version === false) {
100
                        throw new Exception('Could not determinate current phpBB version in "'.$system_dir.'".', false);
101
                        return false;
102
                }
103
 
104
                if ($this->isVersionCurrentStable($json, $version)) {
105
                        $this->setStatus(VNag::STATUS_OK);
106
                        $this->setHeadline("Version $version (Latest Stable version) at $system_dir", true);
107
                } else if ($this->isVersionCurrentUnstable($json, $version)) {
108
                        $this->setStatus(VNag::STATUS_OK);
109
                        $this->setHeadline("Version $version (Latest Unstable version) at $system_dir", true);
110
                } else {
111
                        $this->setStatus(VNag::STATUS_WARNING);
112
                        $this->setHeadline("Version $version (Old version!) at $system_dir", true);
113
                }
114
 
115
                // 2. Checking extensions
116
 
117
                $total_extensions = 0;
118
                $unknown_extensions = 0;
119
                $old_extensions = 0;
120
                $current_extensions = 0;
121
                $check_errors = 0;
122
 
123
                $ext_json_files = glob($system_dir.'/ext/*/*/composer.json');
124
                foreach ($ext_json_files as $ext_json_file) {
125
                        $ext_json_client = json_decode(file_get_contents($ext_json_file),true);
126
                        $extname = $ext_json_client['name'];
127
                        $version = $ext_json_client['version'];
128
                        $total_extensions++;
129
                        if (isset($ext_json_client['extra']) && isset($ext_json_client['extra']['version-check'])) {
130
                                if (!isset($ext_json_client['extra']['version-check']['ssl'])) $ext_json_client['extra']['version-check']['ssl'] = false;
131
                                $versionCheckUrl  = $ext_json_client['extra']['version-check']['ssl'] ? 'https://' : 'http://';
132
                                $versionCheckUrl .= $ext_json_client['extra']['version-check']['host'];
133
                                $versionCheckUrl .= $ext_json_client['extra']['version-check']['directory'].'/';
134
                                $versionCheckUrl .= $ext_json_client['extra']['version-check']['filename'];
135
                                $cont = @file_get_contents($versionCheckUrl);
136
                                if ($cont === false) {
137
                                        $this->setStatus(VNag::STATUS_WARNING);
138
                                        $this->addVerboseMessage("Extension $extname : Cannot reach update-server (Version $version)!", VNag::VERBOSITY_SUMMARY);
139
                                        $check_errors++;
140
                                        continue;
141
                                }
142
                                $json = @json_decode($cont,true);
143
                                if ($json === false) {
144
                                        $this->setStatus(VNag::STATUS_WARNING);
145
                                        $this->addVerboseMessage("Extension $extname : Cannot reach update-server (Version $version)!", VNag::VERBOSITY_SUMMARY);
146
                                        $check_errors++;
147
                                        continue;
148
                                }
149
 
150
                                if ($this->isVersionCurrentStable($json, $version)) {
151
                                        $this->setStatus(VNag::STATUS_OK);
152
                                        $this->addVerboseMessage("Extension $extname : Version $version is latest stable.", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
153
                                        $current_extensions++;
154
                                } else if ($this->isVersionCurrentUnstable($json, $version)) {
155
                                        $this->setStatus(VNag::STATUS_OK);
156
                                        $this->addVerboseMessage("Extension $extname : Version $version is latest unstable.", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
157
                                        $current_extensions++;
158
                                } else {
159
                                        $this->setStatus(VNag::STATUS_WARNING);
160
                                        $this->addVerboseMessage("Extension $extname : Version $version is outdated!", VNag::VERBOSITY_SUMMARY);
161
                                        $old_extensions++;
162
                                }
163
                        } else {
164
                                $this->addVerboseMessage("Extension $extname (version $version) does not have any update server information.", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
165
                                $unknown_extensions++;
166
                        }
167
                }
168
 
169
                if ($old_extensions > 0) {
170
                        $this->setHeadline("$old_extensions extensions require an update", true);
171
                }
172
                if ($check_errors > 0) {
173
                        $this->setHeadline("$old_extensions extensions can't be checked (update server error)", true);
174
                }
175
                $this->addVerboseMessage("$total_extensions extensions total, $current_extensions up-to-date, $old_extensions outdated, $unknown_extensions without update information, $check_errors update server errors", VNag::VERBOSITY_SUMMARY);
176
        }
177
}