Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. cMU48trGvkbMlzRpC9CObsZaTJQLSX+W3WA0s/ipwluSIf5e1oPV/8hsf/xQ8Jdoy
  3. md+onDcvBYBvQRK/eb9NblY+OVHpQyBTmeFabsnWFzAL4cR6IDUh7Wi7Mv8z30tIu
  4. bHIGsE1zP8M02Sm1Cy/LEAHrUSzTum4vCZ0Ur7IgWqVJyvmwyLS3yMUu2ozk1fii6
  5. PjlciD5Oe+DiNsVERv5/YtirDychwrUz4lPOWnZQebZsMEmNHCWZbWree1WYCV4u1
  6. b7JaROxuZRfghe3vxTigLrDG2emmKanw6+W9dfaQEfsQDF0kn2UNYquj+IjmRWkQN
  7. bTLJzV2SJwr+ayyIWjJXuKTS+OtMI2PEFvqC1WdGC7HSvlE68fNXZcZzuS2dxUGeO
  8. IiSfmD6eKfHs5JtPQrY910QgvqGFtd8Rdvfi9KUrXUP2YXuEiL/A3+vXoGg2eseEd
  9. aDjte0ZFJratQXBR3tp/1+lXGwkRv+1016kqprH9jaqubbTz98waLllyxBjG19mZD
  10. S85CFfL6kPiWInCdMSRsytG4h5T4w3CZsPTwnP0MOekO1j0eXncoeCMVr4AO5j6wl
  11. 4jdv4NAO0o2YJrmDaC6gV8N0aGlCArC1RRk49fExUFyHMOuO99ZY4gqyp04JhQzrX
  12. 9Bgj7sa3zsnrSAfCCgBf9o62OZUUypiuRNrfpj+DLYyaeIe2MUBhlFFOWEN1XLbev
  13. 5zvWLFJyfcw+b/iJSViKu8PTyHJgu5ZBCMhyJ2WD7AX+wwPKFjFI7f5CF+eZj/n4Q
  14. LDzc7LvTjQ/01hIGNZNRvGsyJ1YvVkrfZrIpFmE+X77IQRhpRoQv0gyb0sutQ/bAF
  15. qeQyb449/UZk8hXBxlYi6AxUQhFPx1jNCG8q2jCKDPkRo5BrHRsORNFKlQiQKHuWi
  16. zrm26GiJRUhryn4AScMwWBXDYMLV5i+C5zYOnfI4SMQJurgPHr3lhELHTB6C6IeKL
  17. s1kVHLbPqTCNr3dWK46gtGGoMwvjEDTcPyiEOxERZuYZ/BNxl6P+G0E+oNeBQPVbG
  18. 9e7vi7SQeZrEhqKn8gNQx5DQyOdP7PsBmHJG8th6Tr77YktIE+yVRR8rxj0tHvqrY
  19. ppdYUl/rIpj1LyYS3MyOXneekAn/rnEOe0lGM5sIM4/pI4oK03snEY9ngD9X7xNm3
  20. lUWz/1tWfJFYolMVQOdEfHU5rvavSJASlUihm9tcuIM+jAJDSF+UMR7sQ+n46VaaB
  21. 2u/aP50UMrtU1FVoooXeB6uSH/EGesTmYREA13UjmVz3gIB4QT9m/XVZ5zkRVMleM
  22. b9sCS5/IGoiyhR3ge17fVPUmRsdI7maW5t+v5FxM45qv2t0hFGEfoBFY03oi+Mjdr
  23. A==
  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 Exception('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 (preg_match('@\\$(OC_Version)\\s*=\\s*array\\(\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*,\\s*(.+)\\s*\\)\\s*;@ismU', $cont, $m)) {
  67.                         $OC_Version = array($m[2],$m[3],$m[4],$m[5]);
  68.                 } else {
  69.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Version".');
  70.                 }
  71.                 if (preg_match('@\\$(OC_VersionString)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  72.                         $OC_VersionString = $m[3];
  73.                 } else {
  74.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_VersionString".');
  75.                 }
  76.                 if (preg_match('@\\$(OC_Edition)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  77.                         $OC_Edition = $m[3];
  78.                 } else {
  79.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Edition".');
  80.                 }
  81.                 if (preg_match('@\\$(OC_Channel)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  82.                         $OC_Channel = $m[3];
  83.                 } else {
  84.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Channel".');
  85.                 }
  86.                 if (preg_match('@\\$(OC_Build)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  87.                         $OC_Build = $m[3];
  88.                 } else {
  89.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "OC_Build".');
  90.                 }
  91.                 if (preg_match('@\\$(vendor)\\s*=\\s*([\'"])(.*)\\2\\s*;@ismU', $cont, $m)) {
  92.                         $vendor = $m[3];
  93.                         if ($vendor != 'nextcloud') {
  94.                                 throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". It is "'.$vendor.'".');
  95.                         }
  96.                 } else {
  97.                         throw new Exception('This is not a valid Nextcloud installation in "'.$local_path.'". Missing "vendor".');
  98.                 }
  99.  
  100.                 $baseUrl = 'https://updates.nextcloud.org/updater_server/';
  101.  
  102.                 // More information about the paramters, see https://github.com/nextcloud/updater_server/blob/master/src/Request.php
  103.                 $php_version = explode('.', PHP_VERSION);
  104.                 $update_url = $baseUrl . '?version='.
  105.                               implode('x', $OC_Version).'x'.
  106.                               'x'. // installationMtime
  107.                               'x'. // lastCheck
  108.                               $OC_Channel.'x'.
  109.                               $OC_Edition.'x'.
  110.                               urlencode($OC_Build).'x'.
  111.                               $php_version[0].'x'.
  112.                               $php_version[1].'x'.
  113.                               intval($php_version[2]); // Last part could be something like "28-2+0~20210604.85+debian9~1.gbp219f11"
  114.  
  115.                 $cont = $this->url_get_contents($update_url);
  116.                 if ($cont === false) {
  117.                         throw new Exception('Could not determinate current Nextcloud version in "'.$local_path.'". A');
  118.                 }
  119.  
  120.                 if ($cont === '') {
  121.                         return array($OC_VersionString, $OC_VersionString, $OC_Channel);
  122.                 } else {
  123.                         $xml = simplexml_load_string($cont);
  124.                         if ($xml === false) {
  125.                                 throw new Exception('Could not determinate current Nextcloud version in "'.$local_path.'". B');
  126.                         }
  127.                         $new_ver = (string)$xml->version;
  128.                         return array($OC_VersionString, $new_ver, $OC_Channel);
  129.                 }
  130.         }
  131.  
  132.         protected function cbRun($optional_args=array()) {
  133.                 $system_dir = $this->argSystemDir->getValue();
  134.                 if (empty($system_dir)) {
  135.                         throw new Exception("Please specify the directory of the Nextcloud installation.");
  136.                 }
  137.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  138.  
  139.                 if (!is_dir($system_dir)) {
  140.                         throw new Exception('Directory "'.$system_dir.'" not found.');
  141.                 }
  142.  
  143.                 list($cur_ver, $new_ver, $channel) = $this->get_versions($system_dir);
  144.  
  145.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  146.                         $this->setStatus(VNag::STATUS_OK);
  147.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is the latest available version for your Nextcloud installation at $system_dir", true);
  148.                 } else {
  149.                         $this->setStatus(VNag::STATUS_WARNING);
  150.                         $this->setHeadline("Nextcloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
  151.                 }
  152.         }
  153. }
  154.