Subversion Repositories oidplus

Rev

Rev 1183 | Rev 1206 | 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
 
1116 daniel-mar 28
        /**
29
         * @param bool $html
30
         * @return void
31
         */
32
        public function init(bool $html=true) {
635 daniel-mar 33
        }
34
 
1116 daniel-mar 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);
635 daniel-mar 43
        }
44
 
1116 daniel-mar 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) {
1183 daniel-mar 53
                $parts = explode('$',$id,2);
635 daniel-mar 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');
801 daniel-mar 58
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 59
 
60
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 61
                                $out['icon'] = 'img/error.png';
635 daniel-mar 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
 
645 daniel-mar 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>';
635 daniel-mar 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
 
1183 daniel-mar 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);
635 daniel-mar 75
 
1183 daniel-mar 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-'));
635 daniel-mar 82
 
83
 
1183 daniel-mar 84
                                $out['text'] .= '<h2>'._L('Result (comparison with SVN revision %1)', $ver).'</h2>';
635 daniel-mar 85
 
1183 daniel-mar 86
                                $out['text'] .= '<pre>';
635 daniel-mar 87
 
1183 daniel-mar 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
                                        }
635 daniel-mar 93
 
1183 daniel-mar 94
                                        $mine = self::getDirContents(OIDplus::localpath());
645 daniel-mar 95
 
1183 daniel-mar 96
                                        $num = 0;
645 daniel-mar 97
 
1183 daniel-mar 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
                                                                }
646 daniel-mar 125
                                                        }
635 daniel-mar 126
                                                }
127
                                        }
128
 
1183 daniel-mar 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
                                                }
635 daniel-mar 134
                                        }
135
 
1183 daniel-mar 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
                                                        }
635 daniel-mar 144
                                                }
145
                                        }
1183 daniel-mar 146
 
147
                                        if ($num == 0) {
148
                                                $out['text'] .= _L('Everything OK!');
149
                                        }
150
                                } catch (\Exception $e) {
1201 daniel-mar 151
                                        $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
152
                                        $out['text'] .= mb_strtoupper(_L('Error')).': '.$htmlmsg;
635 daniel-mar 153
                                }
154
 
1183 daniel-mar 155
                                $out['text'] .= '</pre>';
635 daniel-mar 156
                        }
157
                } else {
158
                        $handled = false;
159
                }
160
        }
161
 
1116 daniel-mar 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 {
635 daniel-mar 171
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
172
 
800 daniel-mar 173
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 174
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 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
 
1116 daniel-mar 188
        /**
189
         * @param string $request
190
         * @return array|false
191
         */
192
        public function tree_search(string $request) {
635 daniel-mar 193
                return false;
194
        }
195
 
1116 daniel-mar 196
        /**
1130 daniel-mar 197
         * @param string $dir
198
         * @param string|null $basepath
199
         * @param array $results
200
         * @return array
1116 daniel-mar 201
         */
1130 daniel-mar 202
        private static function getDirContents(string $dir, string $basepath = null, array &$results = array()): array {
635 daniel-mar 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);
1082 daniel-mar 209
                        if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file;
635 daniel-mar 210
                        if (!is_dir($path)) {
211
                                $xpath = substr($path, strlen($basepath));
212
                                $xpath = str_replace('\\', '/', $xpath);
1082 daniel-mar 213
                                $results[$xpath] = @hash_file('sha256', $path);
635 daniel-mar 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
 
1116 daniel-mar 224
        /**
1130 daniel-mar 225
         * @param string $checksumfile
1116 daniel-mar 226
         * @return array|false
227
         */
1130 daniel-mar 228
        private static function checksumFileToArray(string $checksumfile) {
635 daniel-mar 229
                $out = array();
654 daniel-mar 230
 
231
                $cont = url_get_contents($checksumfile);
232
                if ($cont === false) return false;
233
                $lines = explode("\n", $cont);
234
 
635 daniel-mar 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
 
661 daniel-mar 246
}