Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusPageAdminSystemFileCheck extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          */
  32.         public function init(bool $html=true) {
  33.         }
  34.  
  35.         /**
  36.          * @param string $actionID
  37.          * @param array $params
  38.          * @return array
  39.          * @throws OIDplusException
  40.          */
  41.         public function action(string $actionID, array $params): array {
  42.                 return parent::action($actionID, $params);
  43.         }
  44.  
  45.         /**
  46.          * @param string $id
  47.          * @param array $out
  48.          * @param bool $handled
  49.          * @return void
  50.          * @throws OIDplusException
  51.          */
  52.         public function gui(string $id, array &$out, bool &$handled) {
  53.                 $parts = explode('.',$id,2);
  54.                 if (!isset($parts[1])) $parts[1] = '';
  55.                 if ($parts[0] == 'oidplus:system_file_check') {
  56.                         @set_time_limit(0);
  57.  
  58.                         $handled = true;
  59.                         $out['title'] = _L('System file check');
  60.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  61.  
  62.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  63.                                 $out['icon'] = 'img/error.png';
  64.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  65.                                 return;
  66.                         }
  67.  
  68.                         $out['text'] = '<p>'._L('This tool compares the checksums of the files of your OIDplus installation with the checksums of the OIDplus original SVN version.').'<br>';
  69.                         $out['text'] .= _L('Differences could have various reasons, for example, a hotfix you have applied.').'<br>';
  70.                         $out['text'] .= _L('The folders "userdata" and "userdata_pub" as well as third-party-plugins (folder "plugins" excluding "viathinksoft") are not listed.').'</p>';
  71.                         $out['text'] .= '<p>'._L('Please note: If you believe that you were hacked, you should not trust the output of this tool, because it might be compromised, too.').'</p>';
  72.  
  73.                         $ver = OIDplus::getVersion();
  74.                         if (substr($ver,0,4) !== 'svn-') {
  75.                                 $out['text'] = '<p><font color="red">'.mb_strtoupper(_L('Error')).': '._L('Cannot determine version of the system').'</font></p>';
  76.                                 return;
  77.                         }
  78.                         $ver = substr($ver,strlen('svn-'));
  79.  
  80.  
  81.                         $out['text'] .= '<h2>'._L('Result (comparison with SVN revision %1)', $ver).'</h2>';
  82.  
  83.                         $out['text'] .= '<pre>';
  84.  
  85.                         try {
  86.                                 $mine = self::getDirContents(OIDplus::localpath());
  87.                                 $theirs = self::checksumFileToArray(sprintf(OIDplus::getEditionInfo()['checksum_file'],$ver));
  88.                                 if ($theirs === false) {
  89.                                         throw new OIDplusException(_L("Cannot download checksum file. Please try again later."));
  90.                                 }
  91.  
  92.                                 $num = 0;
  93.  
  94.                                 foreach ($mine as $filename_old => $hash_old) {
  95.                                         $filename_old = str_replace('\\', '/', $filename_old);
  96.                                         if (!isset($theirs[$filename_old])) {
  97.                                                 if (
  98.                                                   (substr($filename_old, 0, strlen('userdata/')) !== 'userdata/') &&
  99.                                                   (substr($filename_old, 0, strlen('userdata_pub/')) !== 'userdata_pub/') &&
  100.  
  101.                                                   // Don't list third-party plugins
  102.                                                   ((
  103.                                                     (substr($filename_old, 0, strlen('plugins/')) === 'plugins/') &&
  104.                                                     (
  105.                                                       (explode('/',$filename_old)[1] === 'viathinksoft') ||
  106.                                                       (explode('/',$filename_old)[1] === 'index.html') ||
  107.                                                       (substr(explode('/',$filename_old)[1],-4) === '.xsd')
  108.                                                     )
  109.                                                   ) || (substr($filename_old, 0, strlen('plugins/')) !== 'plugins/')) &&
  110.  
  111.                                                   ($filename_old !== 'oidplus_version.txt') &&
  112.                                                   ($filename_old !== '.version.php') &&
  113.                                                   ($filename_old !== 'composer.lock')
  114.                                                 ){
  115.                                                         $num++;
  116.                                                         if (is_dir(OIDplus::localpath().$filename_old)) {
  117.                                                                 $out['text'] .= "<b>"._L('Additional directory').":</b> $filename_old\n";
  118.                                                         } else {
  119.                                                                 $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
  120.                                                         }
  121.                                                 }
  122.                                         }
  123.                                 }
  124.  
  125.                                 foreach ($theirs as $filename_new => $hash_new) {
  126.                                         if (!isset($mine[$filename_new])) {
  127.                                                 $num++;
  128.                                                 $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
  129.                                         }
  130.                                 }
  131.  
  132.                                 foreach ($mine as $filename_old => $hash_old) {
  133.                                         if (isset($theirs[$filename_old])) {
  134.                                                 $hash_new = $theirs[$filename_old];
  135.                                                 if ($hash_old != $hash_new) {
  136.                                                         $num++;
  137.                                                         $svn_url = sprintf(OIDplus::getEditionInfo()['svn_original'],urlencode($filename_old),urlencode($ver));
  138.                                                         $out['text'] .= "<b>"._L('Checksum mismatch').":</b> $filename_old (<a target=\"_blank\" href=\"$svn_url\">"._L('Expected file contents')."</a>)\n";
  139.                                                 }
  140.                                         }
  141.                                 }
  142.  
  143.                                 if ($num == 0) {
  144.                                         $out['text'] .= _L('Everything OK!');
  145.                                 }
  146.                         } catch (\Exception $e) {
  147.                                 $out['text'] .= mb_strtoupper(_L('Error')).': '.htmlentities($e->getMessage());
  148.                         }
  149.  
  150.                         $out['text'] .= '</pre>';
  151.                 } else {
  152.                         $handled = false;
  153.                 }
  154.         }
  155.  
  156.         /**
  157.          * @param array $json
  158.          * @param string|null $ra_email
  159.          * @param bool $nonjs
  160.          * @param string $req_goto
  161.          * @return bool
  162.          * @throws OIDplusException
  163.          */
  164.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  165.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  166.  
  167.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  168.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  169.                 } else {
  170.                         $tree_icon = null; // default icon (folder)
  171.                 }
  172.  
  173.                 $json[] = array(
  174.                         'id' => 'oidplus:system_file_check',
  175.                         'icon' => $tree_icon,
  176.                         'text' => _L('System file check')
  177.                 );
  178.  
  179.                 return true;
  180.         }
  181.  
  182.         /**
  183.          * @param string $request
  184.          * @return array|false
  185.          */
  186.         public function tree_search(string $request) {
  187.                 return false;
  188.         }
  189.  
  190.         /**
  191.          * @param $dir
  192.          * @param $basepath
  193.          * @param $results
  194.          * @return array|mixed
  195.          */
  196.         private static function getDirContents($dir, $basepath = null, &$results = array()) {
  197.                 if (is_null($basepath)) $basepath = $dir;
  198.                 $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
  199.                 $dir = realpath($dir) . DIRECTORY_SEPARATOR;
  200.                 $files = scandir($dir);
  201.                 foreach ($files as $file) {
  202.                         $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
  203.                         if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file;
  204.                         if (!is_dir($path)) {
  205.                                 $xpath = substr($path, strlen($basepath));
  206.                                 $xpath = str_replace('\\', '/', $xpath);
  207.                                 $results[$xpath] = @hash_file('sha256', $path);
  208.                         } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
  209.                                 self::getDirContents($path, $basepath, $results);
  210.                                 $xpath = substr($path, strlen($basepath));
  211.                                 $xpath = str_replace('\\', '/', $xpath);
  212.                                 $results[$xpath] = hash('sha256', '');
  213.                         }
  214.                 }
  215.                 return $results;
  216.         }
  217.  
  218.         /**
  219.          * @param $checksumfile
  220.          * @return array|false
  221.          */
  222.         private static function checksumFileToArray($checksumfile) {
  223.                 $out = array();
  224.  
  225.                 $cont = url_get_contents($checksumfile);
  226.                 if ($cont === false) return false;
  227.                 $lines = explode("\n", $cont);
  228.  
  229.                 foreach ($lines as $line) {
  230.                         $line = trim($line);
  231.                         if ($line == '') continue;
  232.                         list($hash, $filename) = explode("\t",$line);
  233.                         if (substr($filename,0,strlen('trunk/')) == 'trunk/') {
  234.                                 $out[substr($filename,strlen('trunk/'))] = $hash;
  235.                         }
  236.                 }
  237.                 return $out;
  238.         }
  239.  
  240. }
  241.