Subversion Repositories oidplus

Rev

Rev 645 | Go to most recent revision | Details | 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
 
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.').'</p>';
47
                        $out['text'] .= '<p>'._L('Differences could have various reasons, for example, a hotfix you have applied or additional plugins you have installed.').'</p>';
48
                        $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>';
49
 
50
                        $ver = OIDplus::getVersion();
51
                        if (substr($ver,0,4) !== 'svn-') {
52
                                $out['text'] = '<p><font color="red">'.strtoupper(_L('Error')).': '._L('Cannot determine version of the system').'</font></p>';
53
                                return;
54
                        }
55
                        $ver = substr($ver,strlen('svn-'));
56
 
57
 
58
                        $out['text'] .= '<h2>'._L('Result (comparison with SVN revision %1)', $ver).'</h2>';
59
 
60
                        $out['text'] .= '<pre>';
61
 
62
                        try {
63
                                $mine = self::getDirContents(OIDplus::localpath());
64
                                $theirs = self::checksumFileToArray('https://www.oidplus.com/checksums/svn-rev'.$ver.'.txt');
65
 
66
                                $num = 0;
67
 
68
                                foreach ($mine as $filename_old => $hash_old) {
69
                                        if (!isset($theirs[$filename_old])) {
70
                                                if (
71
                                                  (substr($filename_old, 0, strlen('userdata/')) !== 'userdata/') &&
72
                                                  (substr($filename_old, 0, strlen('userdata_pub/')) !== 'userdata_pub/') &&
73
                                                  ($filename_old !== 'oidplus_version.txt')
74
                                                ){
75
                                                        $num++;
76
                                                        $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n";
77
                                                }
78
                                        }
79
                                }
80
 
81
                                foreach ($theirs as $filename_new => $hash_new) {
82
                                        if (!isset($mine[$filename_new])) {
83
                                                $num++;
84
                                                $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n";
85
                                        }
86
                                }
87
 
88
                                foreach ($mine as $filename_old => $hash_old) {
89
                                        if (isset($theirs[$filename_old])) {
90
                                                $hash_new = $theirs[$filename_old];
91
                                                if ($hash_old != $hash_new) {
92
                                                        $num++;
93
                                                        $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";
94
                                                }
95
                                        }
96
                                }
97
 
98
                                if ($num == 0) {
99
                                        $out['text'] .= _L('Everything OK!');
100
                                }
101
                        } catch (Exception $e) {
102
                                $out['text'] .= strtoupper(_L('Error')).': '.htmlentities($e->getMessage());
103
                        }
104
 
105
                        $out['text'] .= '</pre>';
106
                } else {
107
                        $handled = false;
108
                }
109
        }
110
 
111
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
112
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
113
 
114
                if (file_exists(__DIR__.'/treeicon.png')) {
115
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
116
                } else {
117
                        $tree_icon = null; // default icon (folder)
118
                }
119
 
120
                $json[] = array(
121
                        'id' => 'oidplus:system_file_check',
122
                        'icon' => $tree_icon,
123
                        'text' => _L('System file check')
124
                );
125
 
126
                return true;
127
        }
128
 
129
        public function tree_search($request) {
130
                return false;
131
        }
132
 
133
        private static function getDirContents($dir, $basepath = null, &$results = array()) {
134
                if (is_null($basepath)) $basepath = $dir;
135
                $basepath = realpath($basepath) . DIRECTORY_SEPARATOR;
136
                $dir = realpath($dir) . DIRECTORY_SEPARATOR;
137
                $files = scandir($dir);
138
                foreach ($files as $file) {
139
                        $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
140
                        if (!is_dir($path)) {
141
                                $xpath = substr($path, strlen($basepath));
142
                                $xpath = str_replace('\\', '/', $xpath);
143
                                $results[$xpath] = hash_file('sha256', $path);
144
                        } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") {
145
                                self::getDirContents($path, $basepath, $results);
146
                                $xpath = substr($path, strlen($basepath));
147
                                $xpath = str_replace('\\', '/', $xpath);
148
                                $results[$xpath] = hash('sha256', '');
149
                        }
150
                }
151
                return $results;
152
        }
153
 
154
        private static function checksumFileToArray($checksumfile) {
155
                $out = array();
156
                $lines = @file($checksumfile);
157
                if (!$lines) {
158
                        throw new OIDplusException(_L("Cannot download checksum file. Please try again later."));
159
                }
160
                foreach ($lines as $line) {
161
                        $line = trim($line);
162
                        if ($line == '') continue;
163
                        list($hash, $filename) = explode("\t",$line);
164
                        if (substr($filename,0,strlen('trunk/')) == 'trunk/') {
165
                                $out[substr($filename,strlen('trunk/'))] = $hash;
166
                        }
167
                }
168
                return $out;
169
        }
170
 
171
}