Subversion Repositories oidplus

Rev

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