Subversion Repositories oidplus

Rev

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