Subversion Repositories vnag

Rev

Rev 77 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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.  *
  8.  * Revision 2023-10-13
  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');
  24.                 $this->getHelpManager()->setVersion('2023-10-13');
  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.  
  34.         protected function get_local_version($path) {
  35.                 $path = realpath($path) === false ? $path : realpath($path);
  36.  
  37.                 // Version 3 support
  38.                 $cont = @file_get_contents("$path/includes/constants.php");
  39.                 if ($cont !== false) {
  40.                         if (preg_match("@define\('PHPBB_VERSION', '(.*)'\);@ismU", $cont, $m)) return $m[1];
  41.                 }
  42.  
  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.                 }
  48.  
  49.                 throw new VNagException('Could not determinate current phpBB version in "'.$path.'".');
  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)) {
  69.                         throw new VNagException("Please specify the directory of the phpBB installation.");
  70.                 }
  71.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  72.  
  73.                 if (!is_dir($system_dir)) {
  74.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  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";
  83.                 $cont = $this->url_get_contents($versionCheckUrl);
  84.                 if ($cont === false) {
  85.                         throw new VNagException('Could not determinate latest phpBB version');
  86.                 }
  87.                 $json = @json_decode($cont,true);
  88.                 if ($json === false) {
  89.                         throw new VNagException('Could not determinate latest phpBB version');
  90.                 }
  91.  
  92.                 $version = $this->get_local_version($system_dir);
  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) {
  115.                         $cont = @file_get_contents($ext_json_file);
  116.                         if ($cont === false) continue; // TODO: throw exception?
  117.                         $ext_json_client = @json_decode($cont,true);
  118.                         if ($ext_json_client === false) continue; // TODO: throw exception?
  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. }
  171.