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