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 OwnCloudVersionCheck 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_owncloud_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local ownCloud 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, 'ownCloudPath', 'The local directory where your ownCloud 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 ownCloud 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 ownCloud installation in "'.$local_path.'". Missing version.php file.');
  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 ownCloud 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 ownCloud 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 ownCloud 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 ownCloud 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 ownCloud 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 != 'owncloud') {
  73.                                 throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". It is "'.$vendor.'".');
  74.                         }
  75.                 } else {
  76.                         throw new VNagException('This is not a valid ownCloud installation in "'.$local_path.'". Missing "vendor".');
  77.                 }
  78.  
  79.                 // Owncloud\Updater\Utils\Fetcher::DEFAULT_BASE_URL
  80.                 $baseUrl = 'https://updates.owncloud.com/server/';
  81.  
  82.                 $update_url = $baseUrl . '?version='.
  83.                               implode('x', $OC_Version).'x'.
  84.                               'installedatx'.
  85.                               'lastupdatedatx'.
  86.                               $OC_Channel.'x'.
  87.                               $OC_Edition.'x'.
  88.                               urlencode($OC_Build);
  89.  
  90.                 $cont = $this->url_get_contents($update_url);
  91.                 if ($cont === false) {
  92.                         throw new VNagException('Could not determinate current ownCloud version in "'.$local_path.'". (Cannot access '.$update_url.')');
  93.                 }
  94.  
  95.                 if ($cont === '') {
  96.                         return array($OC_VersionString, $OC_VersionString, $OC_Channel);
  97.                 } else {
  98.                         $xml = simplexml_load_string($cont);
  99.                         if ($xml === false) {
  100.                                 throw new VNagException('Could not determinate current ownCloud version in "'.$local_path.'". (Invalid XML downloaded from update-server)');
  101.                         }
  102.                         $new_ver = (string)$xml->version;
  103.                         return array($OC_VersionString, $new_ver, $OC_Channel);
  104.                 }
  105.         }
  106.  
  107.         protected function cbRun($optional_args=array()) {
  108.                 $system_dir = $this->argSystemDir->getValue();
  109.                 if (empty($system_dir)) {
  110.                         throw new VNagException("Please specify the directory of the ownCloud installation.");
  111.                 }
  112.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  113.  
  114.                 if (!is_dir($system_dir)) {
  115.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  116.                 }
  117.  
  118.                 list($cur_ver, $new_ver, $channel) = $this->get_versions($system_dir);
  119.  
  120.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  121.                         $this->setStatus(VNag::STATUS_OK);
  122.                         $this->setHeadline("ownCloud version $cur_ver [$channel] is the latest available version for your ownCloud installation at $system_dir", true);
  123.                 } else {
  124.                         $this->setStatus(VNag::STATUS_WARNING);
  125.                         $this->setHeadline("ownCloud version $cur_ver [$channel] is outdated. Newer version is $new_ver [$channel] for installation at $system_dir", true);
  126.                 }
  127.         }
  128. }
  129.