Subversion Repositories oidplus

Rev

Rev 1430 | 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.  
  56.                 if ($parts[0] == 'oidplus:system_file_check') {
  57.                         $handled = true;
  58.  
  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.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
  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 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('This process might be slow on some systems').')</p>';
  73.                         } else {
  74.                                 @set_time_limit(0);
  75.  
  76.                                 $out['text'] .= '<h2>'._L('Result').'</h2>';
  77.  
  78.                                 $out['text'] .= '<pre>';
  79.  
  80.                                 try {
  81.                                         $theirs = json_decode(file_get_contents(__DIR__.'/checksums.json'),true);
  82.  
  83.                                         $exclude = [
  84.                                                 // Please keep in-sync with private/gen_serverside_v3
  85.                                                 realpath(__DIR__.'/checksums.json'),
  86.                                                 realpath(OIDplus::localpath().'/userdata'),
  87.                                                 realpath(OIDplus::localpath().'/userdata_pub')
  88.                                         ];
  89.                                         $mine = self::getDirContents(OIDplus::localpath(), null, $exclude);
  90.  
  91.                                         $num = 0;
  92.  
  93.                                         foreach ($mine as $filename_old => $hash_old) {
  94.                                                 $filename_old = str_replace('\\', '/', $filename_old);
  95.                                                 if (!isset($theirs[$filename_old])) {
  96.                                                         if (
  97.                                                                 (substr($filename_old, 0, strlen('userdata/')) !== 'userdata/') &&
  98.                                                                 (substr($filename_old, 0, strlen('userdata_pub/')) !== 'userdata_pub/') &&
  99.  
  100.                                                                 // Don't list third-party plugins
  101.                                                                 // TODO: but bundled ones should be listed!
  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 !== 'composer.lock') &&
  112.                                                                 ($filename_old !== 'phpstan.phar') &&
  113.                                                                 ($filename_old !== 'phpstan.neon') &&
  114.                                                                 ($filename_old !== 'phpstan.bat') &&
  115.                                                                 ($filename_old !== 'phpstan.sh') &&
  116.                                                                 ($filename_old !== 'plugins/viathinksoft/adminPages/902_systemfile_check/checksums.json')
  117.                                                         ){
  118.                                                                 $num++;
  119.                                                                 if (is_dir(OIDplus::localpath().$filename_old)) {
  120.                                                                         $out['text'] .= "<b>"._L('Additional directory').":</b> $filename_old\n";
  121.                                                                 } else {
  122.                                                                         $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
  123.                                                                 }
  124.                                                         }
  125.                                                 }
  126.                                         }
  127.  
  128.                                         foreach ($theirs as $filename_new => $hash_new) {
  129.                                                 if (!isset($mine[$filename_new])) {
  130.                                                         $num++;
  131.                                                         $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
  132.                                                 }
  133.                                         }
  134.  
  135.                                         foreach ($mine as $filename_old => $hash_old) {
  136.                                                 if (isset($theirs[$filename_old])) {
  137.                                                         $hash_new = $theirs[$filename_old];
  138.                                                         if ($hash_old != $hash_new) {
  139.                                                                 $num++;
  140.                                                                 $out['text'] .= "<b>"._L('Checksum mismatch').":</b> $filename_old\n";
  141.                                                         }
  142.                                                 }
  143.                                         }
  144.  
  145.                                         if ($num == 0) {
  146.                                                 $out['text'] .= _L('Everything OK!');
  147.                                         }
  148.                                 } catch (\Exception $e) {
  149.                                         $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  150.                                         $out['text'] .= mb_strtoupper(_L('Error')).': '.$htmlmsg;
  151.                                 }
  152.  
  153.                                 $out['text'] .= '</pre>';
  154.                         }
  155.                 } else {
  156.                         $handled = false;
  157.                 }
  158.         }
  159.  
  160.         /**
  161.          * @param array $json
  162.          * @param string|null $ra_email
  163.          * @param bool $nonjs
  164.          * @param string $req_goto
  165.          * @return bool
  166.          * @throws OIDplusException
  167.          */
  168.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  169.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  170.  
  171.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  172.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  173.                 } else {
  174.                         $tree_icon = null; // default icon (folder)
  175.                 }
  176.  
  177.                 $json[] = array(
  178.                         'id' => 'oidplus:system_file_check',
  179.                         'icon' => $tree_icon,
  180.                         'text' => _L('System file check')
  181.                 );
  182.  
  183.                 return true;
  184.         }
  185.  
  186.         /**
  187.          * @param string $request
  188.          * @return array|false
  189.          */
  190.         public function tree_search(string $request) {
  191.                 return false;
  192.         }
  193.  
  194.         /**
  195.          * @param string $dir
  196.          * @param string|null $basepath
  197.          * @param array $exclude
  198.          * @param array $results
  199.          * @return array
  200.          */
  201.         public static function getDirContents(string $dir, string $basepath = null, array $exclude=[], array &$results=[]): array {
  202.                 if (is_null($basepath)) $basepath = $dir;
  203.                 $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
  204.                 $dir = realpath($dir) . DIRECTORY_SEPARATOR;
  205.                 $files = scandir($dir);
  206.                 foreach ($files as $file) {
  207.                         $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
  208.                         if (in_array($path,$exclude)) continue;
  209.                         if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file;
  210.                         if ($file == 'composer.lock') continue;
  211.                         if ($file == 'phpstan.phar') continue;
  212.                         if ($file == 'phpstan.neon') continue;
  213.                         if ($file == 'phpstan.bat') continue;
  214.                         if ($file == 'phpstan.sh') continue;
  215.                         if (!is_dir($path)) {
  216.                                 $xpath = substr($path, strlen($basepath));
  217.                                 $xpath = str_replace('\\', '/', $xpath);
  218.                                 $results[$xpath] = @hash_file('sha256', $path);
  219.                         } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
  220.                                 self::getDirContents($path, $basepath, $exclude, $results);
  221.                                 $xpath = substr($path, strlen($basepath));
  222.                                 $xpath = str_replace('\\', '/', $xpath);
  223.                                 $results[$xpath] = hash('sha256', '');
  224.                         }
  225.                 }
  226.                 return $results;
  227.         }
  228.  
  229. }
  230.