Rev 1430 | 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') { |
1430 | daniel-mar | 72 | $out['text'] .= '<p><input type="button" '.OIDplus::gui()->link('oidplus:system_file_check$go').' value="'._L('Start scan').'"> ('._L('This process might be slow on some systems').')</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') && |
1438 | daniel-mar | 112 | ($filename_old !== 'phpstan.phar') && |
113 | ($filename_old !== 'phpstan.neon') && |
||
114 | ($filename_old !== 'phpstan.bat') && |
||
115 | ($filename_old !== 'phpstan.sh') && |
||
1426 | daniel-mar | 116 | ($filename_old !== 'plugins/viathinksoft/adminPages/902_systemfile_check/checksums.json') |
1183 | daniel-mar | 117 | ){ |
118 | $num++; |
||
119 | if (is_dir(OIDplus::localpath().$filename_old)) { |
||
120 | $out['text'] .= "<b>"._L('Additional directory').":</b> $filename_old\n"; |
||
121 | } else { |
||
122 | $out['text'] .= "<b>"._L('Additional file').":</b> $filename_old\n"; |
||
123 | } |
||
646 | daniel-mar | 124 | } |
635 | daniel-mar | 125 | } |
126 | } |
||
127 | |||
1183 | daniel-mar | 128 | foreach ($theirs as $filename_new => $hash_new) { |
129 | if (!isset($mine[$filename_new])) { |
||
130 | $num++; |
||
131 | $out['text'] .= "<b>"._L('Missing file').":</b> $filename_new\n"; |
||
132 | } |
||
635 | daniel-mar | 133 | } |
134 | |||
1183 | daniel-mar | 135 | foreach ($mine as $filename_old => $hash_old) { |
136 | if (isset($theirs[$filename_old])) { |
||
137 | $hash_new = $theirs[$filename_old]; |
||
138 | if ($hash_old != $hash_new) { |
||
139 | $num++; |
||
1426 | daniel-mar | 140 | $out['text'] .= "<b>"._L('Checksum mismatch').":</b> $filename_old\n"; |
1183 | daniel-mar | 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 |
||
1426 | daniel-mar | 197 | * @param array $exclude |
1130 | daniel-mar | 198 | * @param array $results |
199 | * @return array |
||
1116 | daniel-mar | 200 | */ |
1426 | daniel-mar | 201 | public static function getDirContents(string $dir, string $basepath = null, array $exclude=[], array &$results=[]): array { |
635 | daniel-mar | 202 | if (is_null($basepath)) $basepath = $dir; |
203 | $basepath = realpath($basepath) . DIRECTORY_SEPARATOR; |
||
204 | $dir = realpath($dir) . DIRECTORY_SEPARATOR; |
||
205 | $files = scandir($dir); |
||
206 | foreach ($files as $file) { |
||
207 | $path = realpath($dir . DIRECTORY_SEPARATOR . $file); |
||
1426 | daniel-mar | 208 | if (in_array($path,$exclude)) continue; |
1082 | daniel-mar | 209 | if (empty($path)) $path = $dir . DIRECTORY_SEPARATOR . $file; |
1426 | daniel-mar | 210 | if ($file == 'composer.lock') continue; |
1438 | daniel-mar | 211 | if ($file == 'phpstan.phar') continue; |
212 | if ($file == 'phpstan.neon') continue; |
||
213 | if ($file == 'phpstan.bat') continue; |
||
214 | if ($file == 'phpstan.sh') continue; |
||
635 | daniel-mar | 215 | if (!is_dir($path)) { |
216 | $xpath = substr($path, strlen($basepath)); |
||
217 | $xpath = str_replace('\\', '/', $xpath); |
||
1082 | daniel-mar | 218 | $results[$xpath] = @hash_file('sha256', $path); |
635 | daniel-mar | 219 | } else if ($file != "." && $file != ".." && $file != ".svn" && $file != ".git") { |
1426 | daniel-mar | 220 | self::getDirContents($path, $basepath, $exclude, $results); |
635 | daniel-mar | 221 | $xpath = substr($path, strlen($basepath)); |
222 | $xpath = str_replace('\\', '/', $xpath); |
||
223 | $results[$xpath] = hash('sha256', ''); |
||
224 | } |
||
225 | } |
||
226 | return $results; |
||
227 | } |
||
228 | |||
661 | daniel-mar | 229 | } |