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. declare(ticks=1);
  12.  
  13. class RoundcubeVersionCheck extends VNag {
  14.         protected $argSystemDir = null;
  15.  
  16.         public function __construct() {
  17.                 parent::__construct();
  18.  
  19.                 $this->registerExpectedStandardArguments('Vht');
  20.  
  21.                 $this->getHelpManager()->setPluginName('check_roundcube_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local Roundcube Webmail system has the latest version installed.');
  24.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  25.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  26.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  27.  
  28.                 // Individual (non-standard) arguments:
  29.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'roundcubePath', 'The local directory where your Roundcube installation is located.'));
  30.         }
  31.  
  32.         protected function get_local_version($path) {
  33.                 $path = realpath($path) === false ? $path : realpath($path);
  34.  
  35.                 $cont = @file_get_contents("$path/program/lib/Roundcube/bootstrap.php");
  36.                 if ($cont === false) {
  37.                         throw new VNagException("Cannot find version information at $path (cannot find bootstrap.php)");
  38.                 }
  39.                 if (!preg_match("@define\('RCUBE_VERSION', '(.*)'\);@ismU", $cont, $m)) {
  40.                         throw new VNagException("Cannot find version information at $path (cannot find RCUBE_VERSION)");
  41.                 }
  42.  
  43.                 return $m[1];
  44.         }
  45.  
  46.         protected function get_latest_version() {
  47.                 $cont = $this->url_get_contents('https://api.github.com/repos/roundcube/roundcubemail/releases/latest');
  48.                 if ($cont === false) {
  49.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Cannot access api.github.com)');
  50.                 }
  51.  
  52.                 $data = @json_decode($cont, true);
  53.                 if ($data === false) {
  54.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Invalid JSON at api.github.com)');
  55.                 }
  56.  
  57.                 return $data['tag_name']; // e.g. "1.6.3"
  58.         }
  59.  
  60.         protected function get_latest_versions_with_lts() {
  61.                 $cont = $this->url_get_contents('https://roundcube.net/download/');
  62.                 if ($cont === false) {
  63.                         throw new VNagException('Cannot parse version from Roundcube website. The plugin probably needs to be updated. (Cannot access roundcube.net)');
  64.                 }
  65.  
  66.                 if (!preg_match_all('@https://github.com/roundcube/roundcubemail/releases/download/([^/]+)/@ismU', $cont, $m)) {
  67.                         throw new VNagException('Cannot parse version from Roundcube website. The plugin probably needs to be updated. (Regex mismatch at roundcube.net)');
  68.                 }
  69.  
  70.                 return $m[1];
  71.         }
  72.  
  73.         protected function cbRun($optional_args=array()) {
  74.                 $system_dir = $this->argSystemDir->getValue();
  75.                 if (empty($system_dir)) {
  76.                         throw new VNagException("Please specify the directory of the Roundcube installation.");
  77.                 }
  78.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  79.  
  80.                 if (!is_dir($system_dir)) {
  81.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  82.                 }
  83.  
  84.                 $version = $this->get_local_version($system_dir);
  85.  
  86.                 try {
  87.                         $stable_versions = $this->get_latest_versions_with_lts();
  88.                         $latest_version = $stable_versions[0];
  89.                 } catch (Exception $e) {
  90.                         // roundcube.net blocks HTTPS connections from the ViaThinkSoft server since 13 Oct 2023. WHY?!
  91.                         // Access GitHub instead (but we do not get the LTS information there...)
  92.                         $latest_version = $this->get_latest_version();
  93.                         $stable_versions = [ $latest_version ];
  94.                 }
  95.  
  96.                 if (in_array($version, $stable_versions)) {
  97.                         if ($version === $latest_version) {
  98.                                 $this->setStatus(VNag::STATUS_OK);
  99.                                 $this->setHeadline("Version $version (Latest Stable version) at $system_dir", true);
  100.                         } else {
  101.                                 $this->setStatus(VNag::STATUS_OK);
  102.                                 $this->setHeadline("Version $version (Old Stable / LTS version; latest version is $latest_version) at $system_dir", true);
  103.                         }
  104.                 } else {
  105.                         $this->setStatus(VNag::STATUS_WARNING);
  106.                         $this->setHeadline("Version $version is outdated (Latest version is $latest_version) at $system_dir", true);
  107.                 }
  108.         }
  109. }
  110.  
  111.