Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. EnOGgWZfinduNUAQbydaichGVfM683Yilh8mvZzyRozjE2Msy1CX7FR4SAPTmNOb2
  3. FgLa4ReON+Wl2ftGo8eDdPGr2rJFUCVtdnDQkc3kbTAijv6gMNYFmfZ1FS/89+j57
  4. nOsM/EPhcFJO4TL+8DWey7u3i11JHuAeqnn3abpvyxNH4qa4a4ADhX7aOEWXALqXu
  5. TM1A+UUKMMrj7vZ0BVPeNoBvh2i26LepK9pZSUY1DPhNLVNfGFcDiO3ukVffU8BWf
  6. GgNGs/Wl68Agyille2MjjqTFF7W7W5Os+nM4WFMhlKe/AFC9uYbJwxqe41s+mCkIW
  7. jvlUXTVN8K3Q+jsHow+QNT+VCHo9vB9G6upqrvLzH1sdTqR4XgRDarUOiCrbirkD3
  8. kBF5osVIDn8T4lcg+gsx6P/M/YFBxu66Pb2Kwc1hjsbsQ4bm3XV8al92AlohzKN6r
  9. XDduu9BZgoAXH3qIc22e8EE2ADTRDWzSSDrfXx/SJwnWpQVsPkIS1qscp6vXyOMti
  10. e0d9mEzrTQ7rQWM1Cx7WU+pWSGPpyv0XGqKIs2O0xP8cj/26l+Pgo2q6lfcehBz1n
  11. xONlI2T9vQOL1vGHLaF0GdnrR4WPPBM7GqbtoHOcMH5ra7SqK13rhZWYJCHsNcZIj
  12. ucUo0Bm6B857MIxFDr7hddl8cD9D1crBjR3fHHdR5S3U1TI/CAhwrCEz/Fuqhkerb
  13. pb2uKz+3FlQuFooUW1o7ihAVfeLSFCBb/rYDK5OJtH01TOvldKbRUVwALyuRbCisH
  14. X6j+mqP7je6+Bj1Ers5BL4Avz24JQc/jBGTNlgWWGlgW/ZT0f080MuSlzvhVwdEbu
  15. PC2JG7dyBrd4JKtS3DmIrLcjiNiYcRx6HUZ2pxSc9GZzo/hsvzlXIiv6D/66CBS10
  16. 4fcustBJakDkLGQgTC8S3oIxuAaJtgOLZeeXuXVWbehDCCp1QQBi3/ToQE90O/AMT
  17. viEmHyEp7ncg8gbzR4cwBBOEPEt93crlzZjYujk2skDfCMk5xma+QNh4fDp2/TGAG
  18. v+r+PTNJdQpEYocDb/TS2xhzyxboag2tCzvGGqYMMbPSgDNQLJOIjwgUgvJnJFT2b
  19. YMASHuyUX7ACroRkbjPS3zZJ+ZgTPFOAqtZrAOfAWEt2lJSsV0X2Umh0t79Hv97m5
  20. 5nu9PhMp9XohPDndCpc/DDWu3m03+rVgRWN8EiP8Lz3VgoT7NamZZjdTFEA51ITPJ
  21. lzXjz0svhhpNGwLFiMvueXREMlkEV1HoEJtQTZFytQW7Ov3oyhVoX9R/CNOvQUEuT
  22. MdigueiPViNF94keIN7D1C9p5b1PzU/p09Ani4/wBpqPsk1GQ83CBvP22IOhVIDxm
  23. Q==
  24. </ViaThinkSoftSignature> */ ?>
  25. <?php
  26.  
  27. /*
  28.  * VNag - Nagios Framework for PHP
  29.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  30.  * Licensed under the terms of the Apache 2.0 license
  31.  *
  32.  * Revision 2023-10-13
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class NextCloudVersionCheck extends VNag {
  38.         protected $argSystemDir = null;
  39.  
  40.         public function __construct() {
  41.                 parent::__construct();
  42.  
  43.                 $this->registerExpectedStandardArguments('Vvht');
  44.  
  45.                 $this->getHelpManager()->setPluginName('check_nextcloud_version');
  46.                 $this->getHelpManager()->setVersion('1.0');
  47.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local Nextcloud system has the latest version installed.');
  48.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  49.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  50.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  51.  
  52.                 // Individual (non-standard) arguments:
  53.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'nextCloudPath', 'The local directory where your Nextcloud installation is located.'));
  54.         }
  55.  
  56.         protected function get_versions($local_path) {
  57.                 $local_path = realpath($local_path) === false ? $local_path : realpath($local_path);
  58.  
  59.                 if (!file_exists($local_path . '/version.php')) {
  60.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'".');
  61.                 }
  62.  
  63.                 // We don't include version.php because it would be a security vulnerability
  64.                 // code injection if somebody controls version.php
  65.                 $cont = @file_get_contents($local_path . '/version.php');
  66.                 if ($cont === false) {
  67.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing file version.php.');
  68.                 }
  69.                 if (preg_match('@\\$(OC_Version)\\s*=\\s*array\\(\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*\\)\\s*;@ismU', $cont, $m)) {
  70.                         $OC_Version = array($m[2],$m[3],$m[4],$m[5]);
  71.                 } else {
  72.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Version".');
  73.                 }
  74.                 if (preg_match('@\\$(OC_VersionString)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  75.                         $OC_VersionString = $m[3];
  76.                 } else {
  77.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_VersionString".');
  78.                 }
  79.                 if (preg_match('@\\$(OC_Edition)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  80.                         $OC_Edition = $m[3];
  81.                 } else {
  82.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Edition".');
  83.                 }
  84.                 if (preg_match('@\\$(OC_Channel)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  85.                         $OC_Channel = $m[3];
  86.                 } else {
  87.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Channel".');
  88.                 }
  89.                 if (preg_match('@\\$(OC_Build)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  90.                         $OC_Build = $m[3];
  91.                 } else {
  92.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Build".');
  93.                 }
  94.                 if (preg_match('@\\$(vendor)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  95.                         $vendor = $m[3];
  96.                         if ($vendor != 'nextcloud') {
  97.                                 throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". It is "'.$vendor.'".');
  98.                         }
  99.                 } else {
  100.                         throw new VNagException('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "vendor".');
  101.                 }
  102.  
  103.                 $baseUrl = 'https://updates.nextcloud.org/updater_server/';
  104.  
  105.                 // More information about the paramters, see https://github.com/nextcloud/updater_server/blob/master/src/Request.php
  106.                 $php_version = explode('.', PHP_VERSION);
  107.                 $update_url = $baseUrl . '?version='.
  108.                               implode('x', $OC_Version).'x'.
  109.                               'x'. // installationMtime
  110.                               'x'. // lastCheck
  111.                               $OC_Channel.'x'.
  112.                               $OC_Edition.'x'.
  113.                               urlencode($OC_Build).'x'.
  114.                               $php_version[0].'x'.
  115.                               $php_version[1].'x'.
  116.                               intval($php_version[2]); // Last part could be something like "28-2+0~20210604.85+debian9~1.gbp219f11"
  117.  
  118.                 $cont = $this->url_get_contents($update_url);
  119.                 if ($cont === false) {
  120.                         throw new VNagException('Could not determinate current Nextcloud version in "'.$local_path.'". (Cannot access '.$update_url.')');
  121.                 }
  122.  
  123.                 if ($cont === '') {
  124.                         return array($OC_VersionString, $OC_VersionString, $OC_Channel);
  125.                 } else {
  126.                         $xml = simplexml_load_string($cont);
  127.                         if ($xml === false) {
  128.                                 throw new VNagException('Could not determinate current Nextcloud version in "'.$local_path.'". (Invalid XML downloaded from update server)');
  129.                         }
  130.                         $new_ver = (string)$xml->version;
  131.                         return array($OC_VersionString, $new_ver, $OC_Channel);
  132.                 }
  133.         }
  134.  
  135.         protected function cbRun($optional_args=array()) {
  136.                 $system_dir = $this->argSystemDir->getValue();
  137.                 if (empty($system_dir)) {
  138.                         throw new VNagException("Please specify the directory of the Nextcloud installation.");
  139.                 }
  140.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  141.  
  142.                 if (!is_dir($system_dir)) {
  143.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  144.                 }
  145.  
  146.                 list($cur_ver, $new_ver, $channel) = $this->get_versions($system_dir);
  147.  
  148.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  149.                         $this->setStatus(VNag::STATUS_OK);
  150.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is the latest available version for your Nextcloud installation at $system_dir", true);
  151.                 } else {
  152.                         $this->setStatus(VNag::STATUS_WARNING);
  153.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
  154.                 }
  155.         }
  156. }
  157.