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 NextCloudVersionCheck extends VNag {
  14.         protected $argSystemDir = null;
  15.  
  16.         public function __construct() {
  17.                 parent::__construct();
  18.  
  19.                 $this->registerExpectedStandardArguments('Vvht');
  20.  
  21.                 $this->getHelpManager()->setPluginName('check_nextcloud_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local Nextcloud 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, 'nextCloudPath', 'The local directory where your Nextcloud installation is located.'));
  30.         }
  31.  
  32.         protected function get_versions($local_path) {
  33.                 $local_path = realpath($local_path) === false ? $local_path : realpath($local_path);
  34.  
  35.                 if (!file_exists($local_path . '/version.php')) {
  36.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'".');
  37.                 }
  38.  
  39.                 // We don't include version.php because it would be a security vulnerability
  40.                 // code injection if somebody controls version.php
  41.                 $cont = @file_get_contents($local_path . '/version.php');
  42.                 if ($cont === false) {
  43.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing file version.php.');
  44.                 }
  45.                 if (preg_match('@\\$(OC_Version)\\s*=\\s*array\\(\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*\\)\\s*;@ismU', $cont, $m)) {
  46.                         $OC_Version = array($m[2],$m[3],$m[4],$m[5]);
  47.                 } else {
  48.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Version".');
  49.                 }
  50.                 if (preg_match('@\\$(OC_VersionString)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  51.                         $OC_VersionString = $m[3];
  52.                 } else {
  53.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_VersionString".');
  54.                 }
  55.                 if (preg_match('@\\$(OC_Edition)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  56.                         $OC_Edition = $m[3];
  57.                 } else {
  58.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Edition".');
  59.                 }
  60.                 if (preg_match('@\\$(OC_Channel)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  61.                         $OC_Channel = $m[3];
  62.                 } else {
  63.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Channel".');
  64.                 }
  65.                 if (preg_match('@\\$(OC_Build)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  66.                         $OC_Build = $m[3];
  67.                 } else {
  68.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Build".');
  69.                 }
  70.                 if (preg_match('@\\$(vendor)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  71.                         $vendor = $m[3];
  72.                         if ($vendor != 'nextcloud') {
  73.                                 throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". It is "'.$vendor.'".');
  74.                         }
  75.                 } else {
  76.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "vendor".');
  77.                 }
  78.  
  79.                 $baseUrl = 'https://updates.nextcloud.org/updater_server/';
  80.  
  81.                 // More information about the paramters, see https://github.com/nextcloud/updater_server/blob/master/src/Request.php
  82.                 $php_version = explode('.', PHP_VERSION);
  83.                 $update_url = $baseUrl . '?version='.
  84.                               implode('x', $OC_Version).'x'.
  85.                               'x'. // installationMtime
  86.                               'x'. // lastCheck
  87.                               $OC_Channel.'x'.
  88.                               $OC_Edition.'x'.
  89.                               urlencode($OC_Build).'x'.
  90.                               $php_version[0].'x'.
  91.                               $php_version[1].'x'.
  92.                               intval($php_version[2]); // Last part could be something like "28-2+0~20210604.85+debian9~1.gbp219f11"
  93.  
  94.                 $cont = $this->url_get_contents($update_url);
  95.                 if ($cont === false) {
  96.                         throw new VNagException('Could not determinate current Nextcloud version in "'.$local_path.'". (Cannot access '.$update_url.')');
  97.                 }
  98.  
  99.                 if ($cont === '') {
  100.                         return array($OC_VersionString, $OC_VersionString, $OC_Channel);
  101.                 } else {
  102.                         $xml = simplexml_load_string($cont);
  103.                         if ($xml === false) {
  104.                                 throw new VNagException('Could not determinate current Nextcloud version in "'.$local_path.'". (Invalid XML downloaded from update server)');
  105.                         }
  106.                         $new_ver = (string)$xml->version;
  107.                         return array($OC_VersionString, $new_ver, $OC_Channel);
  108.                 }
  109.         }
  110.  
  111.         protected function cbRun($optional_args=array()) {
  112.                 $system_dir = $this->argSystemDir->getValue();
  113.                 if (empty($system_dir)) {
  114.                         throw new VNagException("Please specify the directory of the Nextcloud installation.");
  115.                 }
  116.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  117.  
  118.                 if (!is_dir($system_dir)) {
  119.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  120.                 }
  121.  
  122.                 list($cur_ver, $new_ver, $channel) = $this->get_versions($system_dir);
  123.  
  124.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  125.                         $this->setStatus(VNag::STATUS_OK);
  126.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is the latest available version for your Nextcloud installation at $system_dir", true);
  127.                 } else {
  128.                         $this->setStatus(VNag::STATUS_WARNING);
  129.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
  130.                 }
  131.         }
  132. }
  133.