Subversion Repositories oidplus

Rev

Rev 1200 | Rev 1421 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. use ViaThinkSoft\OIDplus\OIDplus;
  21. use ViaThinkSoft\OIDplus\OIDplusException;
  22. use ViaThinkSoft\OIDplus\OIDplusGui;
  23. use ViaThinkSoft\OIDplus\OIDplusPageAdminVNagVersionCheck;
  24.  
  25. include __DIR__ . '/../../../../vendor/danielmarschall/vnag/framework/vnag_framework.inc.php';
  26. include __DIR__ . '/../../../../includes/oidplus.inc.php';
  27.  
  28. const OIDPLUS_VNAG_MAX_CACHE_AGE = 60; // seconds (TODO: in base config?)
  29.  
  30. set_exception_handler(array(OIDplusGui::class, 'html_exception_handler'));
  31.  
  32. OIDplus::init(true);
  33.  
  34. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminVNagVersionCheck', false)) {
  35.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  36. }
  37.  
  38. class VNagMonitorDummy extends VNag {
  39.  
  40.         private $status;
  41.  
  42.         private $content;
  43.  
  44.         public function __construct($status, $content) {
  45.                 parent::__construct();
  46.                 $this->status = $status;
  47.                 $this->content = $content;
  48.         }
  49.  
  50.         protected function cbRun($optional_args = array()) {
  51.                 $this->setStatus($this->status);
  52.                 $this->setHeadline($this->content);
  53.         }
  54. }
  55.  
  56. $cache_file = OIDplus::localpath() . 'userdata/cache/vnag_version_check.ser';
  57.  
  58. if ((file_exists($cache_file)) && (time()-filemtime($cache_file) <= OIDPLUS_VNAG_MAX_CACHE_AGE)) {
  59.         // Anti DoS
  60.  
  61.         // TODO: There is a small flaw: If the admin fails to secure the "cache/" folder
  62.         //       from the public, then people can read the version file, even if the
  63.         //       VNag script is intended to be protected by a vnag_password.
  64.         list($out_stat, $out_msg) = unserialize(file_get_contents($cache_file));
  65.  
  66. } else {
  67.  
  68.         $installType = OIDplus::getInstallType();
  69.  
  70.         if ($installType === 'ambigous') {
  71.                 $out_stat = VNag::STATUS_UNKNOWN;
  72.                 $out_msg  = 'Multiple version files/directories (oidplus_version.txt, .version.php, .git, or .svn) are existing! Therefore, the version is ambiguous!'; // do not translate
  73.         } else if ($installType === 'unknown') {
  74.                 $out_stat = VNag::STATUS_UNKNOWN;
  75.                 $out_msg  = 'The version cannot be determined, and the update needs to be applied manually!'; // do not translate
  76.         } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
  77.                 $local_installation = OIDplus::getVersion();
  78.                 $newest_version = getLatestRevision();
  79.  
  80.                 $requireInfo = ($installType === 'svn-wc') ? 'shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension' : 'shell access with Git client'; // do not translate
  81.                 $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
  82.  
  83.                 if (!$newest_version) {
  84.                         $out_stat = VNag::STATUS_UNKNOWN;
  85.                         $out_msg  = 'OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.'; // do not translate
  86.                 } else if (!$local_installation) {
  87.                         $out_stat = VNag::STATUS_UNKNOWN;
  88.                         $out_msg  = 'OIDplus could not determine its version (Required: ' . $requireInfo . '). Please update your system manually via the "' . $updateCommand . '" command regularly.'; // do not translate
  89.                 } else if (version_compare($local_installation, $newest_version) >= 0) {
  90.                         $out_stat = VNag::STATUS_OK;
  91.                         $out_msg  = 'You are using the latest version of OIDplus (' . $local_installation . ' local / ' . $newest_version . ' remote)'; // do not translate
  92.                 } else {
  93.                         $out_stat = VNag::STATUS_WARNING;
  94.                         $out_msg  = 'OIDplus is outdated. (' . $local_installation . ' local / ' . $newest_version . ' remote)'; // do not translate
  95.                 }
  96.         } else if ($installType === 'svn-snapshot') {
  97.                 $local_installation = OIDplus::getVersion();
  98.                 $newest_version = getLatestRevision();
  99.  
  100.                 if (!$newest_version) {
  101.                         $out_stat = VNag::STATUS_UNKNOWN;
  102.                         $out_msg  = 'OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.'; // do not translate
  103.                 } else if (!$local_installation) {
  104.                         $out_stat = 'WARN';
  105.                         $out_msg  = 'OIDplus could not determine its version. Please update your system manually by downloading the latest archive file from oidplus.com.'; // do not translate
  106.                 } else if (version_compare($local_installation, $newest_version) >= 0) {
  107.                         $out_stat = VNag::STATUS_OK;
  108.                         $out_msg  = 'You are using the latest version of OIDplus (' . $local_installation . ' local / ' . $newest_version . ' remote)'; // do not translate
  109.                 } else {
  110.                         $out_stat = VNag::STATUS_WARNING;
  111.                         $out_msg  = 'OIDplus is outdated. (' . $local_installation . ' local / ' . $newest_version . ' remote)'; // do not translate
  112.                 }
  113.         } else {
  114.                 assert(false);
  115.                 die();
  116.         }
  117.  
  118.         @file_put_contents($cache_file, serialize(array($out_stat, $out_msg)));
  119. }
  120.  
  121. $job = new VNagMonitorDummy($out_stat, $out_msg);
  122. if (OIDplus::config()->getValue('vnag_version_check_password_protected','1') == '1') {
  123.         $job->http_visual_output = VNag::OUTPUT_NEVER;
  124.         $job->password_out = OIDplusPageAdminVNagVersionCheck::vnag_password();
  125.         $job->outputHTML(_L('This page contains an encrypted VNag machine-readable status.'));
  126. } else {
  127.         $job->http_visual_output = VNag::OUTPUT_ALWAYS;
  128. }
  129. if (OIDplus::getPkiStatus()) {
  130.         $job->privkey = OIDplus::getSystemPrivateKey();
  131. }
  132. $job->run();
  133. unset($job);
  134.  
  135. OIDplus::invoke_shutdown();
  136.  
  137. # ---
  138.  
  139. function getLatestRevision() {
  140.         try {
  141.                 if (function_exists('gzdecode')) {
  142.                         $url = OIDplus::getEditionInfo()['revisionlog_gz'];
  143.                         $cont = url_get_contents($url);
  144.                         if ($cont !== false) $cont = @gzdecode($cont);
  145.                 } else {
  146.                         $url = OIDplus::getEditionInfo()['revisionlog'];
  147.                         $cont = url_get_contents($url);
  148.                 }
  149.                 if ($cont === false) return false;
  150.                 $ary = @unserialize($cont);
  151.                 if ($ary === false) return false;
  152.                 krsort($ary);
  153.                 $max_rev = array_keys($ary)[0];
  154.                 return 'svn-' . $max_rev;
  155.         } catch (\Exception $e) {
  156.                 return false;
  157.         }
  158. }
  159.  
  160.