Subversion Repositories oidplus

Rev

Rev 1183 | Rev 1206 | 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.                         $handled = true;
  57.                         $out['title'] = _L('System file check');
  58.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  59.  
  60.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  61.                                 $out['icon'] = 'img/error.png';
  62.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  63.                                 return;
  64.                         }
  65.  
  66.                         $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>';
  67.                         $out['text'] .= _L('Differences could have various reasons, for example, a hotfix you have applied.').'<br>';
  68.                         $out['text'] .= _L('The folders "userdata" and "userdata_pub" as well as third-party-plugins (folder "plugins" excluding "viathinksoft") are not listed.').'</p>';
  69.                         $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>';
  70.  
  71.                         if ($parts[1] !== 'go') {
  72.                                 $out['text'] .= '<p><input type="button" '.OIDplus::gui()->link('oidplus:system_file_check$go').' value="'._L('Start scan').'"> ('._L('Attention: Takes a long time!').')</p>';
  73.                         } else {
  74.                                 @set_time_limit(0);
  75.  
  76.                                 $ver = OIDplus::getVersion();
  77.                                 if (substr($ver,0,4) !== 'svn-') {
  78.                                         $out['text'] = '<p><font color="red">'.mb_strtoupper(_L('Error')).': '._L('Cannot determine version of the system').'</font></p>';
  79.                                         return;
  80.                                 }
  81.                                 $ver = substr($ver,strlen('svn-'));
  82.  
  83.  
  84.                                 $out['text'] .= '<h2>'._L('Result (comparison with SVN revision %1)', $ver).'</h2>';
  85.  
  86.                                 $out['text'] .= '<pre>';
  87.  
  88.                                 try {
  89.                                         $theirs = self::checksumFileToArray(sprintf(OIDplus::getEditionInfo()['checksum_file'],$ver));
  90.                                         if ($theirs === false) {
  91.                                                 throw new OIDplusException(_L("Cannot download checksum file. Please try again later."));
  92.                                         }
  93.  
  94.                                         $mine = self::getDirContents(OIDplus::localpath());
  95.  
  96.                                         $num = 0;
  97.  
  98.                                         foreach ($mine as $filename_old => $hash_old) {
  99.                                                 $filename_old = str_replace('\\', '/', $filename_old);
  100.                                                 if (!isset($theirs[$filename_old])) {
  101.                                                         if (
  102.                                                                 (substr($filename_old, 0, strlen('userdata/')) !== 'userdata/') &&
  103.                                                                 (substr($filename_old, 0, strlen('userdata_pub/')) !== 'userdata_pub/') &&
  104.  
  105.                                                                 // Don't list third-party plugins
  106.                                                                 ((
  107.                                                                                 (substr($filename_old, 0, strlen('plugins/')) === 'plugins/') &&
  108.                                                                                 (
  109.                                                                                         (explode('/',$filename_old)[1] === 'viathinksoft') ||
  110.                                                                                         (explode('/',$filename_old)[1] === 'index.html') ||
  111.                                                                                         (substr(explode('/',$filename_old)[1],-4) === '.xsd')
  112.                                                                                 )
  113.                                                                         ) || (substr($filename_old, 0, strlen('plugins/')) !== 'plugins/')) &&
  114.  
  115.                                                                 ($filename_old !== 'oidplus_version.txt') &&
  116.                                                                 ($filename_old !== '.version.php') &&
  117.                                                                 ($filename_old !== 'composer.lock')
  118.                                                         ){
  119.                                                                 $num++;
  120.                                                                 if (is_dir(OIDplus::localpath().$filename_old)) {
  121.                                                                         $out['text'] .= "<b>"._L('Additional directory').":</b> $filename_old\n";
  122.                                                                 } else {
  123.                                                                         $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
  124.                                                                 }
  125.                                                         }
  126.                                                 }
  127.                                         }
  128.  
  129.                                         foreach ($theirs as $filename_new => $hash_new) {
  130.                                                 if (!isset($mine[$filename_new])) {
  131.                                                         $num++;
  132.                                                         $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
  133.                                                 }
  134.                                         }
  135.  
  136.                                         foreach ($mine as $filename_old => $hash_old) {
  137.                                                 if (isset($theirs[$filename_old])) {
  138.                                                         $hash_new = $theirs[$filename_old];
  139.                                                         if ($hash_old != $hash_new) {
  140.                                                                 $num++;
  141.                                                                 $svn_url = sprintf(OIDplus::getEditionInfo()['svn_original'],urlencode($filename_old),urlencode($ver));
  142.                                                                 $out['text'] .= "<b>"._L('Checksum mismatch').":</b> $filename_old (<a target=\"_blank\" href=\"$svn_url\">"._L('Expected file contents')."</a>)\n";
  143.                                                         }
  144.                                                 }
  145.                                         }
  146.  
  147.                                         if ($num == 0) {
  148.                                                 $out['text'] .= _L('Everything OK!');
  149.                                         }
  150.                                 } catch (\Exception $e) {
  151.                                         $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  152.                                         $out['text'] .= mb_strtoupper(_L('Error')).': '.$htmlmsg;
  153.                                 }
  154.  
  155.                                 $out['text'] .= '</pre>';
  156.                         }
  157.                 } else {
  158.                         $handled = false;
  159.                 }
  160.         }
  161.  
  162.         /**
  163.          * @param array $json
  164.          * @param string|null $ra_email
  165.          * @param bool $nonjs
  166.          * @param string $req_goto
  167.          * @return bool
  168.          * @throws OIDplusException
  169.          */
  170.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  171.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  172.  
  173.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  174.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  175.                 } else {
  176.                         $tree_icon = null; // default icon (folder)
  177.                 }
  178.  
  179.                 $json[] = array(
  180.                         'id' => 'oidplus:system_file_check',
  181.                         'icon' => $tree_icon,
  182.                         'text' => _L('System file check')
  183.                 );
  184.  
  185.                 return true;
  186.         }
  187.  
  188.         /**
  189.          * @param string $request
  190.          * @return array|false
  191.          */
  192.         public function tree_search(string $request) {
  193.                 return false;
  194.         }
  195.  
  196.         /**
  197.          * @param string $dir
  198.          * @param string|null $basepath
  199.          * @param array $results
  200.          * @return array
  201.          */
  202.         private static function getDirContents(string $dir, string $basepath = null, array &$results = array()): array {
  203.                 if (is_null($basepath)) $basepath = $dir;
  204.                 $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
  205.                 $dir = realpath($dir) . DIRECTORY_SEPARATOR;
  206.                 $files = scandir($dir);
  207.                 foreach ($files as $file) {
  208.                         $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
  209.                         if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file;
  210.                         if (!is_dir($path)) {
  211.                                 $xpath = substr($path, strlen($basepath));
  212.                                 $xpath = str_replace('\\', '/', $xpath);
  213.                                 $results[$xpath] = @hash_file('sha256', $path);
  214.                         } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
  215.                                 self::getDirContents($path, $basepath, $results);
  216.                                 $xpath = substr($path, strlen($basepath));
  217.                                 $xpath = str_replace('\\', '/', $xpath);
  218.                                 $results[$xpath] = hash('sha256', '');
  219.                         }
  220.                 }
  221.                 return $results;
  222.         }
  223.  
  224.         /**
  225.          * @param string $checksumfile
  226.          * @return array|false
  227.          */
  228.         private static function checksumFileToArray(string $checksumfile) {
  229.                 $out = array();
  230.  
  231.                 $cont = url_get_contents($checksumfile);
  232.                 if ($cont === false) return false;
  233.                 $lines = explode("\n", $cont);
  234.  
  235.                 foreach ($lines as $line) {
  236.                         $line = trim($line);
  237.                         if ($line == '') continue;
  238.                         list($hash, $filename) = explode("\t",$line);
  239.                         if (substr($filename,0,strlen('trunk/')) == 'trunk/') {
  240.                                 $out[substr($filename,strlen('trunk/'))] = $hash;
  241.                         }
  242.                 }
  243.                 return $out;
  244.         }
  245.  
  246. }
  247.