Subversion Repositories oidplus

Rev

Rev 635 | Rev 646 | 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());
65
                                $theirs = self::checksumFileToArray('https://www.oidplus.com/checksums/svn-rev'.$ver.'.txt');
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++;
90
                                                        $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
91
                                                }
92
                                        }
93
                                }
94
 
95
                                foreach ($theirs as $filename_new => $hash_new) {
96
                                        if (!isset($mine[$filename_new])) {
97
                                                $num++;
98
                                                $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
99
                                        }
100
                                }
101
 
102
                                foreach ($mine as $filename_old => $hash_old) {
103
                                        if (isset($theirs[$filename_old])) {
104
                                                $hash_new = $theirs[$filename_old];
105
                                                if ($hash_old != $hash_new) {
106
                                                        $num++;
107
                                                        $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";
108
                                                }
109
                                        }
110
                                }
111
 
112
                                if ($num == 0) {
113
                                        $out['text'] .= _L('Everything OK!');
114
                                }
115
                        } catch (Exception $e) {
116
                                $out['text'] .= strtoupper(_L('Error')).': '.htmlentities($e->getMessage());
117
                        }
118
 
119
                        $out['text'] .= '</pre>';
120
                } else {
121
                        $handled = false;
122
                }
123
        }
124
 
125
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
126
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
127
 
128
                if (file_exists(__DIR__.'/treeicon.png')) {
129
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
130
                } else {
131
                        $tree_icon = null; // default icon (folder)
132
                }
133
 
134
                $json[] = array(
135
                        'id' => 'oidplus:system_file_check',
136
                        'icon' => $tree_icon,
137
                        'text' => _L('System file check')
138
                );
139
 
140
                return true;
141
        }
142
 
143
        public function tree_search($request) {
144
                return false;
145
        }
146
 
147
        private static function getDirContents($dir, $basepath = null, &$results = array()) {
148
                if (is_null($basepath)) $basepath = $dir;
149
                $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
150
                $dir = realpath($dir) . DIRECTORY_SEPARATOR;
151
                $files = scandir($dir);
152
                foreach ($files as $file) {
153
                        $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
154
                        if (!is_dir($path)) {
155
                                $xpath = substr($path, strlen($basepath));
156
                                $xpath = str_replace('\\', '/', $xpath);
157
                                $results[$xpath] = hash_file('sha256', $path);
158
                        } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
159
                                self::getDirContents($path, $basepath, $results);
160
                                $xpath = substr($path, strlen($basepath));
161
                                $xpath = str_replace('\\', '/', $xpath);
162
                                $results[$xpath] = hash('sha256', '');
163
                        }
164
                }
165
                return $results;
166
        }
167
 
168
        private static function checksumFileToArray($checksumfile) {
169
                $out = array();
170
                $lines = @file($checksumfile);
171
                if (!$lines) {
172
                        throw new OIDplusException(_L("Cannot download checksum file. Please try again later."));
173
                }
174
                foreach ($lines as $line) {
175
                        $line = trim($line);
176
                        if ($line == '') continue;
177
                        list($hash, $filename) = explode("\t",$line);
178
                        if (substr($filename,0,strlen('trunk/')) == 'trunk/') {
179
                                $out[substr($filename,strlen('trunk/'))] = $hash;
180
                        }
181
                }
182
                return $out;
183
        }
184
 
185
}