Subversion Repositories oidplus

Rev

Rev 1083 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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