Subversion Repositories oidplus

Rev

Rev 1375 | Rev 1426 | 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
 
1131 daniel-mar 26
class OIDplusPageAdminSoftwareUpdate extends OIDplusPagePluginAdmin
27
        implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8 /* getNotifications */
28
{
635 daniel-mar 29
 
1116 daniel-mar 30
        /**
31
         * @param bool $html
32
         * @return void
33
         */
34
        public function init(bool $html=true) {
635 daniel-mar 35
        }
36
 
1116 daniel-mar 37
        /**
38
         * @return string
39
         */
40
        private function getGitCommand(): string {
1195 daniel-mar 41
                return 'git --git-dir='.escapeshellarg(OIDplus::findGitFolder()).' --work-tree='.escapeshellarg(OIDplus::localpath()).' -C "" pull origin master -s recursive -X theirs';
697 daniel-mar 42
        }
43
 
1116 daniel-mar 44
        /**
45
         * @return string
46
         */
47
        private function getSvnCommand(): string {
697 daniel-mar 48
                return 'svn update --accept theirs-full';
49
        }
50
 
1116 daniel-mar 51
        /**
52
         * @param array $params
53
         * @return array
54
         * @throws OIDplusException
55
         */
1293 daniel-mar 56
        private function action_Update(array $params): array {
57
                @set_time_limit(0);
635 daniel-mar 58
 
1293 daniel-mar 59
                if (!OIDplus::authUtils()->isAdminLoggedIn()) {
60
                        throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
61
                }
635 daniel-mar 62
 
1293 daniel-mar 63
                if (OIDplus::getInstallType() === 'git-wc') {
64
                        $cmd = $this->getGitCommand().' 2>&1';
662 daniel-mar 65
 
1293 daniel-mar 66
                        $ec = -1;
67
                        $out = array();
68
                        exec($cmd, $out, $ec);
662 daniel-mar 69
 
1293 daniel-mar 70
                        $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out));
71
                        if ($ec === 0) {
72
                                $rev = 'HEAD'; // do not translate
73
                                return array("status" => 0, "content" => $res, "rev" => $rev);
74
                        } else {
75
                                return array("status" => -1, "error" => $res, "content" => "");
653 daniel-mar 76
                        }
1293 daniel-mar 77
                }
78
                else if (OIDplus::getInstallType() === 'svn-wc') {
79
                        $cmd = $this->getSvnCommand().' 2>&1';
653 daniel-mar 80
 
1293 daniel-mar 81
                        $ec = -1;
82
                        $out = array();
83
                        exec($cmd, $out, $ec);
635 daniel-mar 84
 
1293 daniel-mar 85
                        $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out));
86
                        if ($ec === 0) {
87
                                $rev = 'HEAD'; // do not translate
88
                                return array("status" => 0, "content" => $res, "rev" => $rev);
89
                        } else {
90
                                return array("status" => -1, "error" => $res, "content" => "");
650 daniel-mar 91
                        }
1293 daniel-mar 92
                }
93
                else if (OIDplus::getInstallType() === 'svn-snapshot') {
635 daniel-mar 94
 
1293 daniel-mar 95
                        $rev = $params['rev'];
650 daniel-mar 96
 
1293 daniel-mar 97
                        $update_version = $params['update_version'] ?? 1;
98
                        if (($update_version != 1) && ($update_version != 2)) {
99
                                throw new OIDplusException(_L('Unknown update version'));
100
                        }
807 daniel-mar 101
 
1293 daniel-mar 102
                        // Download and unzip
651 daniel-mar 103
 
1293 daniel-mar 104
                        $cont = false;
105
                        for ($retry=1; $retry<=3; $retry++) {
106
                                if (function_exists('gzdecode')) {
107
                                        $url = sprintf(OIDplus::getEditionInfo()['update_package_gz'], $rev-1, $rev);
108
                                        $cont = url_get_contents($url);
109
                                        if ($cont !== false) $cont = @gzdecode($cont);
110
                                } else {
111
                                        $url = sprintf(OIDplus::getEditionInfo()['update_package'], $rev-1, $rev);
112
                                        $cont = url_get_contents($url);
651 daniel-mar 113
                                }
1293 daniel-mar 114
                                if ($cont !== false) {
115
                                        break;
116
                                } else {
117
                                        sleep(1);
118
                                }
119
                        }
120
                        if ($cont === false) throw new OIDplusException(_L("Update %1 could not be downloaded from ViaThinkSoft server. Please try again later.",$rev));
651 daniel-mar 121
 
1293 daniel-mar 122
                        // Check signature...
662 daniel-mar 123
 
1293 daniel-mar 124
                        if (function_exists('openssl_verify')) {
662 daniel-mar 125
 
1293 daniel-mar 126
                                $m = array();
127
                                if (!preg_match('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', $cont, $m)) {
128
                                        throw new OIDplusException(_L("Update package file of revision %1 not digitally signed",$rev));
129
                                }
130
                                $signature = base64_decode($m[1]);
662 daniel-mar 131
 
1293 daniel-mar 132
                                $naked = preg_replace('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', '', $cont);
133
                                $hash = hash("sha256", $naked."update_".($rev-1)."_to_".($rev).".txt");
662 daniel-mar 134
 
1293 daniel-mar 135
                                $public_key = file_get_contents(__DIR__.'/public.pem');
136
                                if (!openssl_verify($hash, $signature, $public_key, OPENSSL_ALGO_SHA256)) {
137
                                        throw new OIDplusException(_L("Update package file of revision %1: Signature invalid",$rev));
651 daniel-mar 138
                                }
139
 
1293 daniel-mar 140
                        }
651 daniel-mar 141
 
1293 daniel-mar 142
                        // All OK! Now write the file
651 daniel-mar 143
 
1293 daniel-mar 144
                        $tmp_filename = 'update_'.generateRandomString(10).'.tmp.php';
145
                        $local_file = OIDplus::localpath().$tmp_filename;
635 daniel-mar 146
 
1293 daniel-mar 147
                        @file_put_contents($local_file, $cont);
647 daniel-mar 148
 
1293 daniel-mar 149
                        if (!file_exists($local_file) || (@file_get_contents($local_file) !== $cont)) {
150
                                throw new OIDplusException(_L('Update file could not written. Probably there are no write-permissions to the root folder.'));
151
                        }
152
 
153
                        if ($update_version == 1) {
154
                                // Now call the written file
155
                                // Note: we may not use eval($cont) because the script uses die(),
156
                                // and things in the script might collide with currently (un)loaded source code files, shutdown procedues, etc.
1375 daniel-mar 157
                                $web_file = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$tmp_filename;
1293 daniel-mar 158
                                $res = url_get_contents($web_file);
159
                                if ($res === false) {
1375 daniel-mar 160
                                        $web_file = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$tmp_filename;
161
                                        $res = url_get_contents($web_file);
162
                                        if ($res === false) {
163
                                                throw new OIDplusException(_L('Update-script %1 could not be executed',$web_file));
164
                                        }
662 daniel-mar 165
                                }
1293 daniel-mar 166
                                return array("status" => 0, "content" => $res, "rev" => $rev);
167
                        } else if ($update_version == 2) {
168
                                // In this version, the client will call the web-update file.
169
                                // This has the advantage that it will also work if the system is htpasswd protected
170
                                return array("status" => 0, "update_file" => $tmp_filename, "rev" => $rev);
171
                        } else {
172
                                throw new OIDplusException(_L("Unexpected update version"));
653 daniel-mar 173
                        }
1293 daniel-mar 174
                }
175
                else {
176
                        throw new OIDplusException(_L('Multiple version files/directories (oidplus_version.txt, .version.php, .git, or .svn) are existing! Therefore, the version is ambiguous!'));
177
                }
178
        }
179
 
180
        /**
181
         * @param string $actionID
182
         * @param array $params
183
         * @return array
184
         * @throws OIDplusException
185
         */
186
        public function action(string $actionID, array $params): array {
187
                if ($actionID == 'update_now') {
188
                        return $this->action_Update($params);
1116 daniel-mar 189
                } else {
190
                        return parent::action($actionID, $params);
635 daniel-mar 191
                }
192
        }
193
 
1116 daniel-mar 194
        /**
195
         * @param string $id
196
         * @param array $out
197
         * @param bool $handled
198
         * @return void
199
         * @throws OIDplusException
200
         */
201
        public function gui(string $id, array &$out, bool &$handled) {
1278 daniel-mar 202
                if ($id == 'oidplus:software_update') {
635 daniel-mar 203
                        @set_time_limit(0);
204
 
205
                        $handled = true;
206
                        $out['title'] = _L('Software update');
801 daniel-mar 207
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 208
 
209
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 210
                                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 211
                        }
212
 
665 daniel-mar 213
                        $out['text'] .= '<div id="update_versioninfo">';
214
 
635 daniel-mar 215
                        $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>';
216
 
840 daniel-mar 217
                        if (isset(OIDplus::getEditionInfo()['svnrepo']) && (OIDplus::getEditionInfo()['svnrepo'] != '')) {
218
                                $out['text'] .= '<p><b>'._L('Method A').'</b>: '._L('Install OIDplus using the subversion tool in your SSH/Linux shell using the command <code>svn co %1</code> and update it regularly with the command <code>svn update</code> . This will automatically download the latest version and check for conflicts.',htmlentities(OIDplus::getEditionInfo()['svnrepo']).'/trunk/');
219
                                if (!str_starts_with(PHP_OS, 'WIN')) {
220
                                        $out['text'] .= ' '._L('Make sure that you invoke the <code>%1</code> command as the user who runs PHP or that you <code>chown -R</code> the files after invoking <code>%1</code>','svn update');
221
                                }
222
                                $out['text'] .= '</p>';
699 daniel-mar 223
                        } else {
224
                                $out['text'] .= '<p><b>'._L('Method A').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','GIT').'</p>';
225
                        }
635 daniel-mar 226
 
840 daniel-mar 227
                        if (isset(OIDplus::getEditionInfo()['gitrepo']) && (OIDplus::getEditionInfo()['gitrepo'] != '')) {
228
                                $out['text'] .= '<p><b>'._L('Method B').'</b>: '._L('Install OIDplus using the Git client in your SSH/Linux shell using the command <code>git clone %1</code> and update it regularly with the command <code>git pull</code> . This will automatically download the latest version and check for conflicts.',htmlentities(OIDplus::getEditionInfo()['gitrepo'].'.git'));
229
                                if (!str_starts_with(PHP_OS, 'WIN')) {
230
                                        $out['text'] .= ' '._L('Make sure that you invoke the <code>%1</code> command as the user who runs PHP or that you <code>chown -R</code> the files after invoking <code>%1</code>','git pull');
231
                                }
232
                                $out['text'] .= '</p>';
699 daniel-mar 233
                        } else {
234
                                $out['text'] .= '<p><b>'._L('Method B').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','SVN').'</p>';
235
                        }
635 daniel-mar 236
 
699 daniel-mar 237
                        if (isset(OIDplus::getEditionInfo()['downloadpage']) && (OIDplus::getEditionInfo()['downloadpage'] != '')) {
238
                                $out['text'] .= '<p><b>'._L('Method C').'</b>: '._L('Install OIDplus by downloading a TAR.GZ file from %1, which contains an SVN snapshot, and extract it to your webspace. The TAR.GZ file contains a file named ".version.php" which contains the SVN revision of the snapshot. This update-tool will then try to update your files on-the-fly by downloading them from the ViaThinkSoft SVN repository directly into your webspace directory. A change conflict detection is NOT implemented. It is required that the files on your webspace have create/write/delete permissions. Only recommended if you have no access to the SSH/Linux shell.','<a href="'.OIDplus::getEditionInfo()['downloadpage'].'">'.parse_url(OIDplus::getEditionInfo()['downloadpage'])['host'].'</a>').'</p>';
239
                        } else {
240
                                $out['text'] .= '<p><b>'._L('Method C').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','Snapshot').'</p>';
241
                        }
635 daniel-mar 242
 
699 daniel-mar 243
 
635 daniel-mar 244
                        $out['text'] .= '<hr>';
245
 
246
                        $installType = OIDplus::getInstallType();
247
 
248
                        if ($installType === 'ambigous') {
906 daniel-mar 249
                                $out['text'] .= '<font color="red">'.mb_strtoupper(_L('Error')).': '._L('Multiple version files/directories (oidplus_version.txt, .version.php, .git, or .svn) are existing! Therefore, the version is ambiguous!').'</font>';
665 daniel-mar 250
                                $out['text'] .= '</div>';
635 daniel-mar 251
                        } else if ($installType === 'unknown') {
906 daniel-mar 252
                                $out['text'] .= '<font color="red">'.mb_strtoupper(_L('Error')).': '._L('The version cannot be determined, and the update needs to be applied manually!').'</font>';
665 daniel-mar 253
                                $out['text'] .= '</div>';
662 daniel-mar 254
                        } else if (($installType === 'svn-wc') || ($installType === 'git-wc') || ($installType === 'svn-snapshot')) {
635 daniel-mar 255
                                if ($installType === 'svn-wc') {
256
                                        $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>';
662 daniel-mar 257
                                        $requireInfo = _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension');
697 daniel-mar 258
                                        $updateCommand = $this->getSvnCommand();
662 daniel-mar 259
                                } else if ($installType === 'git-wc') {
635 daniel-mar 260
                                        $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>';
662 daniel-mar 261
                                        $requireInfo = _L('shell access with Git client');
697 daniel-mar 262
                                        $updateCommand = $this->getGitCommand();
662 daniel-mar 263
                                } else if ($installType === 'svn-snapshot') {
264
                                        $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot TAR.GZ file with .version.php file).').'</p>';
265
                                        $requireInfo = ''; // unused
266
                                        $updateCommand = ''; // unused
1132 daniel-mar 267
                                } else {
268
                                        assert(false);
635 daniel-mar 269
                                }
270
 
271
                                $local_installation = OIDplus::getVersion();
648 daniel-mar 272
                                $newest_version = $this->getLatestRevision();
635 daniel-mar 273
 
1130 daniel-mar 274
                                $out['text'] .= _L('Local installation: %1',($local_installation ?: _L('unknown'))).'<br>';
275
                                $out['text'] .= _L('Latest published version: %1',($newest_version ?: _L('unknown'))).'<br><br>';
635 daniel-mar 276
 
277
                                if (!$newest_version) {
1181 daniel-mar 278
                                        if (!url_get_contents_available(true, $reason)) {
279
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version.').'<br>'.$reason.'</p>';
1061 daniel-mar 280
                                        } else {
281
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version.').'<br>'._L('Probably the ViaThinkSoft server could not be reached.').'</font></p>';
282
                                        }
662 daniel-mar 283
                                        $out['text'] .= '</div>';
654 daniel-mar 284
                                } else if (!$local_installation) {
662 daniel-mar 285
                                        if ($installType === 'svn-snapshot') {
286
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version.').'</font></p>';
287
                                        } else {
288
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version. (Required: %1). Please update your system manually via the "%2" command regularly.',$requireInfo,$updateCommand).'</font></p>';
635 daniel-mar 289
                                        }
647 daniel-mar 290
                                        $out['text'] .= '</div>';
997 daniel-mar 291
                                } else if (version_compare($local_installation, $newest_version) >= 0) {
635 daniel-mar 292
                                        $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
647 daniel-mar 293
                                        $out['text'] .= '</div>';
635 daniel-mar 294
                                } else {
662 daniel-mar 295
                                        if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
1422 daniel-mar 296
                                                if ($installType === 'svn-wc') {
297
                                                        $shell_diff_cmd = 'svn stat';
298
                                                } else if ($installType === 'git-wc') {
299
                                                        $shell_diff_cmd = 'git status -s';
300
                                                } else {
301
                                                        $shell_diff_cmd = '';
302
                                                }
303
 
304
                                                $can_access_shell = true;
305
                                                if ($shell_diff_cmd) {
306
                                                        $cout = [];
307
                                                        exec("svn stat", $cout, $ec);
308
                                                        if ($ec === 0) {
309
                                                                // TODO: should this also be shown when there is no update available?
310
                                                                if (trim(implode('',$cout)) !== '') {
311
                                                                        $out['text'] .= '<p><font color="red">'._L('WARNING: There are changes in your working copy which WILL be reverted if you continue!').'</font></p>';
312
                                                                        $out['text'] .= '<p><font color="red">'._L('Detected changes:').'</font></p>';
313
                                                                        $out['text'] .= '<p><font color="red"><pre>'.htmlentities(implode("\n",$cout)).'</pre></font></p>';
314
                                                                } else {
315
                                                                        $out['text'] .= '<p><font color="green">'._L('Working copy is clean.').'</font></p>';
316
                                                                }
317
                                                        } else {
318
                                                                $can_access_shell = false;
319
                                                        }
320
                                                }
321
 
662 daniel-mar 322
                                                $out['text'] .= '<p><font color="blue">'._L('Please enter %1 into the SSH shell to update OIDplus to the latest version.','<code>'.$updateCommand.'</code>').'</font></p>';
1422 daniel-mar 323
                                                if ($can_access_shell) {
324
                                                        $out['text'] .= '<p>'._L('Alternatively, click this button to execute the command through the web-interface (command execution and write permissions required).').'</p>';
325
                                                }
662 daniel-mar 326
                                        }
635 daniel-mar 327
 
648 daniel-mar 328
                                        $out['text'] .= '<p><input type="button" onclick="OIDplusPageAdminSoftwareUpdate.doUpdateOIDplus('.((int)substr($local_installation,4)+1).', '.substr($newest_version,4).')" value="'._L('Update NOW').'"></p>';
635 daniel-mar 329
 
662 daniel-mar 330
                                        // TODO: Open "system_file_check" without page reload.
331
                                        // TODO: Only show link if the plugin is installed
906 daniel-mar 332
                                        $out['text'] .= '<p><font color="red">'.mb_strtoupper(_L('Warning')).': '._L('Please make a backup of your files before updating. In case of an error, the OIDplus system (including this update-assistant) might become unavailable. Also, since the web-update does not contain collision-detection, changes you have applied (like adding, removing or modified files) might get reverted/lost! (<a href="%1">Click here to check which files have been modified</a>) In case the update fails, you can download and extract the complete <a href="%s">SVN-Snapshot TAR.GZ file</a> again. Since all your data should lay inside the folder "userdata" and "userdata_pub", this should be safe.','?goto='.urlencode('oidplus:system_file_check'),OIDplus::getEditionInfo()['downloadpage']).'</font></p>';
662 daniel-mar 333
 
647 daniel-mar 334
                                        $out['text'] .= '</div>';
335
 
662 daniel-mar 336
                                        $out['text'] .= $this->showPreview($local_installation, $newest_version);
635 daniel-mar 337
                                }
338
                        }
339
                } else {
340
                        $handled = false;
341
                }
342
        }
343
 
1116 daniel-mar 344
        /**
345
         * @param array $json
346
         * @param string|null $ra_email
347
         * @param bool $nonjs
348
         * @param string $req_goto
349
         * @return bool
350
         * @throws OIDplusException
351
         */
352
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 353
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
354
 
800 daniel-mar 355
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 356
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 357
                } else {
358
                        $tree_icon = null; // default icon (folder)
359
                }
360
 
361
                $json[] = array(
362
                        'id' => 'oidplus:software_update',
363
                        'icon' => $tree_icon,
364
                        'text' => _L('Software update')
365
                );
366
 
367
                return true;
368
        }
369
 
1116 daniel-mar 370
        /**
371
         * @param string $request
372
         * @return array|false
373
         */
374
        public function tree_search(string $request) {
635 daniel-mar 375
                return false;
376
        }
648 daniel-mar 377
 
1116 daniel-mar 378
        /**
1130 daniel-mar 379
         * @var string|null
1116 daniel-mar 380
         */
648 daniel-mar 381
        private $releases_ser = null;
382
 
1116 daniel-mar 383
        /**
1130 daniel-mar 384
         * @param string $local_ver
1116 daniel-mar 385
         * @return false|string
386
         */
1130 daniel-mar 387
        private function showChangelog(string $local_ver) {
648 daniel-mar 388
 
389
                try {
390
                        if (is_null($this->releases_ser)) {
716 daniel-mar 391
                                if (function_exists('gzdecode')) {
392
                                        $url = OIDplus::getEditionInfo()['revisionlog_gz'];
393
                                        $cont = url_get_contents($url);
394
                                        if ($cont !== false) $cont = @gzdecode($cont);
395
                                } else {
396
                                        $url = OIDplus::getEditionInfo()['revisionlog'];
397
                                        $cont = url_get_contents($url);
398
                                }
648 daniel-mar 399
                                if ($cont === false) return false;
400
                                $this->releases_ser = $cont;
401
                        } else {
402
                                $cont = $this->releases_ser;
403
                        }
404
                        $content = '';
405
                        $ary = @unserialize($cont);
406
                        if ($ary === false) return false;
407
                        krsort($ary);
408
                        foreach ($ary as $rev => $data) {
997 daniel-mar 409
                                if (version_compare("svn-$rev", $local_ver) <= 0) continue;
648 daniel-mar 410
                                $comment = empty($data['msg']) ? _L('No comment') : $data['msg'];
411
                                $tex = _L("New revision %1 by %2",$rev,$data['author'])." (".$data['date'].") ";
412
                                $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
413
                                $content .= "\n";
414
                        }
415
                        return $content;
1050 daniel-mar 416
                } catch (\Exception $e) {
648 daniel-mar 417
                        return false;
418
                }
419
 
420
        }
421
 
1116 daniel-mar 422
        /**
423
         * @return false|string
424
         */
648 daniel-mar 425
        private function getLatestRevision() {
426
                try {
427
                        if (is_null($this->releases_ser)) {
716 daniel-mar 428
                                if (function_exists('gzdecode')) {
429
                                        $url = OIDplus::getEditionInfo()['revisionlog_gz'];
430
                                        $cont = url_get_contents($url);
431
                                        if ($cont !== false) $cont = @gzdecode($cont);
432
                                } else {
433
                                        $url = OIDplus::getEditionInfo()['revisionlog'];
434
                                        $cont = url_get_contents($url);
435
                                }
648 daniel-mar 436
                                if ($cont === false) return false;
437
                                $this->releases_ser = $cont;
438
                        } else {
439
                                $cont = $this->releases_ser;
440
                        }
441
                        $ary = @unserialize($cont);
442
                        if ($ary === false) return false;
443
                        krsort($ary);
444
                        $max_rev = array_keys($ary)[0];
1130 daniel-mar 445
                        return 'svn-' . $max_rev;
1050 daniel-mar 446
                } catch (\Exception $e) {
648 daniel-mar 447
                        return false;
448
                }
449
        }
662 daniel-mar 450
 
1116 daniel-mar 451
        /**
1130 daniel-mar 452
         * @param string $local_installation
453
         * @param string $newest_version
1116 daniel-mar 454
         * @return string
455
         */
1130 daniel-mar 456
        private function showPreview(string $local_installation, string $newest_version): string {
662 daniel-mar 457
                $out = '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
458
 
459
                ob_start();
460
                try {
461
                        $cont = $this->showChangelog($local_installation);
1050 daniel-mar 462
                } catch (\Exception $e) {
1201 daniel-mar 463
                        $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
464
                        $cont = _L('Error: %1',$htmlmsg);
662 daniel-mar 465
                }
466
                ob_end_clean();
467
 
831 daniel-mar 468
                $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", "$cont\n");
1274 daniel-mar 469
                $cont = preg_replace('@\\*\\*\\*(.+)\\n@', '<strong>!!!\\1</strong>'."\n", "$cont\n");
662 daniel-mar 470
 
471
                $out .= '<pre id="update_infobox">'.$cont.'</pre>';
472
 
473
                return $out;
474
        }
1000 daniel-mar 475
 
1116 daniel-mar 476
        /**
1131 daniel-mar 477
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8
1130 daniel-mar 478
         * @param string|null $user
1116 daniel-mar 479
         * @return array
480
         * @throws OIDplusException
481
         */
1130 daniel-mar 482
        public function getNotifications(string $user=null): array {
1000 daniel-mar 483
                $notifications = array();
484
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
485
 
486
                        // Following code is based on the VNag plugin (admin 901) code
487
 
488
                        $installType = OIDplus::getInstallType();
489
 
490
                        if ($installType === 'ambigous') {
491
                                $out_stat = 'WARN';
492
                                $out_msg  = _L('Multiple version files/directories (oidplus_version.txt, .version.php, .git, or .svn) are existing! Therefore, the version is ambiguous!');
493
                        } else if ($installType === 'unknown') {
494
                                $out_stat = 'WARN';
495
                                $out_msg  = _L('The version cannot be determined, and the update needs to be applied manually!');
496
                        } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
1181 daniel-mar 497
                                if (!url_get_contents_available(true, $reason)) {
498
                                        $out_stat = 'WARN';
499
                                        $out_msg  = _L('OIDplus could not determine the latest version.').' '.$reason;
500
                                } else {
501
                                        $local_installation = OIDplus::getVersion();
502
                                        $newest_version = $this->getLatestRevision();
1000 daniel-mar 503
 
1181 daniel-mar 504
                                        $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client');
505
                                        $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
1000 daniel-mar 506
 
1181 daniel-mar 507
                                        if (!$newest_version) {
508
                                                $out_stat = 'WARN';
509
                                                $out_msg = _L('OIDplus could not determine the latest version.') . ' ' . _L('Probably the ViaThinkSoft server could not be reached.');
510
                                        } else if (!$local_installation) {
511
                                                $out_stat = 'WARN';
512
                                                $out_msg = _L('OIDplus could not determine its version (Required: %1). Please update your system manually via the "%2" command regularly.', $requireInfo, $updateCommand);
513
                                        } else if (version_compare($local_installation, $newest_version) >= 0) {
514
                                                $out_stat = 'INFO';
515
                                                $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 516
                                        } else {
1181 daniel-mar 517
                                                $out_stat = 'WARN';
518
                                                $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 519
                                        }
1181 daniel-mar 520
                                }
521
                        } else if ($installType === 'svn-snapshot') {
522
                                if (!url_get_contents_available(true, $reason)) {
1000 daniel-mar 523
                                        $out_stat = 'WARN';
1181 daniel-mar 524
                                        $out_msg  = _L('OIDplus could not determine the latest version.').' '.$reason;
1000 daniel-mar 525
                                } else {
1181 daniel-mar 526
                                        $local_installation = OIDplus::getVersion();
527
                                        $newest_version = $this->getLatestRevision();
1000 daniel-mar 528
 
1181 daniel-mar 529
                                        if (!$newest_version) {
530
                                                $out_stat = 'WARN';
531
                                                $out_msg = _L('OIDplus could not determine the latest version.') . ' ' . _L('Probably the ViaThinkSoft server could not be reached.');
532
                                        } else if (!$local_installation) {
533
                                                $out_stat = 'WARN';
534
                                                $out_msg = _L('OIDplus could not determine its version. Please update your system manually by downloading the latest archive file from oidplus.com.');
535
                                        } else if (version_compare($local_installation, $newest_version) >= 0) {
536
                                                $out_stat = 'INFO';
537
                                                $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 538
                                        } else {
1181 daniel-mar 539
                                                $out_stat = 'WARN';
540
                                                $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 541
                                        }
1000 daniel-mar 542
                                }
543
                        } else {
544
                                assert(false);
545
                                return $notifications;
546
                        }
547
 
548
                        if ($out_stat != 'INFO') {
549
                                $out_msg = '<a '.OIDplus::gui()->link('oidplus:software_update').'>'._L('Software update').'</a>: ' . $out_msg;
550
 
1189 daniel-mar 551
                                $notifications[] = new OIDplusNotification($out_stat, $out_msg);
1000 daniel-mar 552
                        }
553
 
554
                }
555
                return $notifications;
556
        }
557
 
661 daniel-mar 558
}