Subversion Repositories oidplus

Rev

Rev 1278 | Rev 1430 | 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] = '';
1278 daniel-mar 55
 
635 daniel-mar 56
                if ($parts[0] == 'oidplus:system_file_check') {
57
                        $handled = true;
1278 daniel-mar 58
 
635 daniel-mar 59
                        $out['title'] = _L('System file check');
801 daniel-mar 60
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 61
 
62
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 63
                                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 64
                        }
65
 
1426 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 version.').'<br>';
645 daniel-mar 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') {
1426 daniel-mar 72
                                $out['text'] .= '<p><input type="button" '.OIDplus::gui()->link('oidplus:system_file_check$go').' value="'._L('Start scan').'"></p>';
1183 daniel-mar 73
                        } else {
74
                                @set_time_limit(0);
635 daniel-mar 75
 
1426 daniel-mar 76
                                $out['text'] .= '<h2>'._L('Result').'</h2>';
635 daniel-mar 77
 
1183 daniel-mar 78
                                $out['text'] .= '<pre>';
635 daniel-mar 79
 
1183 daniel-mar 80
                                try {
1426 daniel-mar 81
                                        $theirs = json_decode(file_get_contents(__DIR__.'/checksums.json'),true);
635 daniel-mar 82
 
1426 daniel-mar 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);
645 daniel-mar 90
 
1183 daniel-mar 91
                                        $num = 0;
645 daniel-mar 92
 
1183 daniel-mar 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
1426 daniel-mar 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/')) &&
1183 daniel-mar 110
 
1426 daniel-mar 111
                                                                ($filename_old !== 'composer.lock') &&
112
                                                                ($filename_old !== 'plugins/viathinksoft/adminPages/902_systemfile_check/checksums.json')
1183 daniel-mar 113
                                                        ){
114
                                                                $num++;
115
                                                                if (is_dir(OIDplus::localpath().$filename_old)) {
116
                                                                        $out['text'] .= "<b>"._L('Additional directory').":</b> $filename_old\n";
117
                                                                } else {
118
                                                                        $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
119
                                                                }
646 daniel-mar 120
                                                        }
635 daniel-mar 121
                                                }
122
                                        }
123
 
1183 daniel-mar 124
                                        foreach ($theirs as $filename_new => $hash_new) {
125
                                                if (!isset($mine[$filename_new])) {
126
                                                        $num++;
127
                                                        $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
128
                                                }
635 daniel-mar 129
                                        }
130
 
1183 daniel-mar 131
                                        foreach ($mine as $filename_old => $hash_old) {
132
                                                if (isset($theirs[$filename_old])) {
133
                                                        $hash_new = $theirs[$filename_old];
134
                                                        if ($hash_old != $hash_new) {
135
                                                                $num++;
1426 daniel-mar 136
                                                                $out['text'] .= "<b>"._L('Checksum mismatch').":</b> $filename_old\n";
1183 daniel-mar 137
                                                        }
635 daniel-mar 138
                                                }
139
                                        }
1183 daniel-mar 140
 
141
                                        if ($num == 0) {
142
                                                $out['text'] .= _L('Everything OK!');
143
                                        }
144
                                } catch (\Exception $e) {
1201 daniel-mar 145
                                        $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
146
                                        $out['text'] .= mb_strtoupper(_L('Error')).': '.$htmlmsg;
635 daniel-mar 147
                                }
148
 
1183 daniel-mar 149
                                $out['text'] .= '</pre>';
635 daniel-mar 150
                        }
151
                } else {
152
                        $handled = false;
153
                }
154
        }
155
 
1116 daniel-mar 156
        /**
157
         * @param array $json
158
         * @param string|null $ra_email
159
         * @param bool $nonjs
160
         * @param string $req_goto
161
         * @return bool
162
         * @throws OIDplusException
163
         */
164
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 165
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
166
 
800 daniel-mar 167
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 168
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 169
                } else {
170
                        $tree_icon = null; // default icon (folder)
171
                }
172
 
173
                $json[] = array(
174
                        'id' => 'oidplus:system_file_check',
175
                        'icon' => $tree_icon,
176
                        'text' => _L('System file check')
177
                );
178
 
179
                return true;
180
        }
181
 
1116 daniel-mar 182
        /**
183
         * @param string $request
184
         * @return array|false
185
         */
186
        public function tree_search(string $request) {
635 daniel-mar 187
                return false;
188
        }
189
 
1116 daniel-mar 190
        /**
1130 daniel-mar 191
         * @param string $dir
192
         * @param string|null $basepath
1426 daniel-mar 193
         * @param array $exclude
1130 daniel-mar 194
         * @param array $results
195
         * @return array
1116 daniel-mar 196
         */
1426 daniel-mar 197
        public static function getDirContents(string $dir, string $basepath = null, array $exclude=[], array &$results=[]): array {
635 daniel-mar 198
                if (is_null($basepath)) $basepath = $dir;
199
                $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
200
                $dir = realpath($dir) . DIRECTORY_SEPARATOR;
201
                $files = scandir($dir);
202
                foreach ($files as $file) {
203
                        $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
1426 daniel-mar 204
                        if (in_array($path,$exclude)) continue;
1082 daniel-mar 205
                        if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file;
1426 daniel-mar 206
                        if ($file == 'composer.lock') continue;
635 daniel-mar 207
                        if (!is_dir($path)) {
208
                                $xpath = substr($path, strlen($basepath));
209
                                $xpath = str_replace('\\', '/', $xpath);
1082 daniel-mar 210
                                $results[$xpath] = @hash_file('sha256', $path);
635 daniel-mar 211
                        } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
1426 daniel-mar 212
                                self::getDirContents($path, $basepath, $exclude, $results);
635 daniel-mar 213
                                $xpath = substr($path, strlen($basepath));
214
                                $xpath = str_replace('\\', '/', $xpath);
215
                                $results[$xpath] = hash('sha256', '');
216
                        }
217
                }
218
                return $results;
219
        }
220
 
661 daniel-mar 221
}