Subversion Repositories oidplus

Rev

Rev 1442 | 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) {
1426 daniel-mar 72
                                $next_version = 'HEAD'; // do not translate
73
                                return array("status" => 0, "content" => $res, "rev" => $next_version);
1293 daniel-mar 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) {
1426 daniel-mar 87
                                $next_version = 'HEAD'; // do not translate
88
                                return array("status" => 0, "content" => $res, "rev" => $next_version);
1293 daniel-mar 89
                        } else {
90
                                return array("status" => -1, "error" => $res, "content" => "");
650 daniel-mar 91
                        }
1293 daniel-mar 92
                }
1426 daniel-mar 93
                else if (OIDplus::getInstallType() === 'manual') {
635 daniel-mar 94
 
1293 daniel-mar 95
                        $update_version = $params['update_version'] ?? 1;
1426 daniel-mar 96
                        if (($update_version != 1) && ($update_version != 2) && ($update_version != 3)) {
1293 daniel-mar 97
                                throw new OIDplusException(_L('Unknown update version'));
98
                        }
807 daniel-mar 99
 
1426 daniel-mar 100
                        if ($update_version >= 3) {
101
                                $next_version = $params['next_version'];
102
                                $max_version = $params['max_version'] ?? null;
103
                        } else {
104
                                $next_version = $params['rev'];
105
                                $max_version = null;
106
                        }
651 daniel-mar 107
 
1426 daniel-mar 108
                        // Prepare update for all next versions
109
 
110
                        $downloaded_changescripts = [];
111
 
112
                        $ver = $next_version;
113
                        do {
114
                                // Download and unzip
115
 
116
                                $cont = false;
117
                                $basename = 'changescript_'.$ver.'.txt';
1446 daniel-mar 118
                                $url = '';
1426 daniel-mar 119
                                for ($retry=1; $retry<=3; $retry++) {
1446 daniel-mar 120
                                        $update_packages_candidates = OIDplus::getEditionInfo()['update_packages'];
121
                                        if (!is_array($update_packages_candidates)) $update_packages_candidates = [ $update_packages_candidates ];
122
 
123
                                        foreach ($update_packages_candidates as $update_packages_candidate) {
124
                                                if (function_exists('gzdecode')) {
125
                                                        $url = $update_packages_candidate.$basename.'.gz';
126
                                                        $cont = url_get_contents($url);
127
                                                        if ($cont !== false) $cont = @gzdecode($cont);
128
                                                } else {
129
                                                        $url = $update_packages_candidate.$basename;
130
                                                        $cont = url_get_contents($url);
131
                                                }
132
 
133
                                                if ($cont !== false) {
134
                                                        break 2;
135
                                                }
1426 daniel-mar 136
                                        }
1446 daniel-mar 137
 
138
                                        sleep(1);
651 daniel-mar 139
                                }
1426 daniel-mar 140
                                if ($cont === false) throw new OIDplusException(_L("Update %1 could not be downloaded from the remote server (%2). Please try again later.",$ver,$url));
651 daniel-mar 141
 
1426 daniel-mar 142
                                // Check signature...
662 daniel-mar 143
 
1426 daniel-mar 144
                                if (function_exists('openssl_verify')) {
145
                                        $m = array();
146
                                        if (!preg_match('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', $cont, $m)) {
147
                                                throw new OIDplusException(_L("Update package file of revision %1 not digitally signed",$ver));
148
                                        }
149
                                        $signature = base64_decode($m[1]);
662 daniel-mar 150
 
1426 daniel-mar 151
                                        $naked = preg_replace('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', '', $cont);
152
                                        $hash = hash("sha256", $naked.$basename);
153
 
1441 daniel-mar 154
                                        $public_key = "-----BEGIN PUBLIC KEY-----\r\n".wordwrap(OIDplus::getEditionInfo()['update_public_key'], 64, "\r\n", true)."\r\n-----END PUBLIC KEY-----\r\n";
1426 daniel-mar 155
                                        if (!openssl_verify($hash, $signature, $public_key, OPENSSL_ALGO_SHA256)) {
156
                                                throw new OIDplusException(_L("Update package file of revision %1: Signature invalid",$ver));
157
                                        }
158
 
1293 daniel-mar 159
                                }
662 daniel-mar 160
 
1426 daniel-mar 161
                                // All OK! Now write the file
662 daniel-mar 162
 
1426 daniel-mar 163
                                $tmp_filename = 'update_'.generateRandomString(10).'.tmp.php';
164
                                $local_file = OIDplus::localpath().$tmp_filename;
165
 
166
                                @file_put_contents($local_file, $cont);
167
 
168
                                if (!file_exists($local_file) || (@file_get_contents($local_file) !== $cont)) {
169
                                        throw new OIDplusException(_L('Update file could not written. Probably there are no write-permissions to the root folder.'));
651 daniel-mar 170
                                }
171
 
1426 daniel-mar 172
                                $downloaded_changescripts[] = [ $ver, $tmp_filename ];
651 daniel-mar 173
 
1426 daniel-mar 174
                        } while (($update_version>=3)&&($ver!=$max_version)&&($ver=$this->getNextVersionFrom($ver,false)));
651 daniel-mar 175
 
1426 daniel-mar 176
                        # ---
635 daniel-mar 177
 
647 daniel-mar 178
 
1293 daniel-mar 179
                        if ($update_version == 1) {
180
                                // Now call the written file
181
                                // Note: we may not use eval($cont) because the script uses die(),
182
                                // and things in the script might collide with currently (un)loaded source code files, shutdown procedues, etc.
1375 daniel-mar 183
                                $web_file = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$tmp_filename;
1293 daniel-mar 184
                                $res = url_get_contents($web_file);
185
                                if ($res === false) {
1375 daniel-mar 186
                                        $web_file = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$tmp_filename;
187
                                        $res = url_get_contents($web_file);
188
                                        if ($res === false) {
189
                                                throw new OIDplusException(_L('Update-script %1 could not be executed',$web_file));
190
                                        }
662 daniel-mar 191
                                }
1426 daniel-mar 192
                                return array("status" => 0, "content" => $res, "rev" => $next_version);
1293 daniel-mar 193
                        } else if ($update_version == 2) {
194
                                // In this version, the client will call the web-update file.
195
                                // This has the advantage that it will also work if the system is htpasswd protected
1426 daniel-mar 196
                                return array("status" => 0, "update_file" => $tmp_filename, "rev" => $next_version);
197
                        } else if ($update_version == 3) {
198
                                // Version 3:
199
                                // - All changescripts are downloaded at once and then processed purely in JS (reduces risk of bricking if an intermediate version is broken)
200
                                // - We return the next version(s) in the list of changescripts
201
                                // - Versions are not SVN revisions anymore, but version strings
202
                                return array("status" => 0, "update_files" => $downloaded_changescripts);
1293 daniel-mar 203
                        } else {
204
                                throw new OIDplusException(_L("Unexpected update version"));
653 daniel-mar 205
                        }
1293 daniel-mar 206
                }
207
                else {
1426 daniel-mar 208
                        throw new OIDplusException(_L('Multiple version files/directories (.git and .svn) are existing! Therefore, the distribution channel is ambiguous!'));
1293 daniel-mar 209
                }
210
        }
211
 
212
        /**
213
         * @param string $actionID
214
         * @param array $params
215
         * @return array
216
         * @throws OIDplusException
217
         */
218
        public function action(string $actionID, array $params): array {
219
                if ($actionID == 'update_now') {
220
                        return $this->action_Update($params);
1116 daniel-mar 221
                } else {
222
                        return parent::action($actionID, $params);
635 daniel-mar 223
                }
224
        }
225
 
1116 daniel-mar 226
        /**
227
         * @param string $id
228
         * @param array $out
229
         * @param bool $handled
230
         * @return void
231
         * @throws OIDplusException
232
         */
233
        public function gui(string $id, array &$out, bool &$handled) {
1278 daniel-mar 234
                if ($id == 'oidplus:software_update') {
635 daniel-mar 235
                        @set_time_limit(0);
236
 
237
                        $handled = true;
238
                        $out['title'] = _L('Software update');
801 daniel-mar 239
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 240
 
241
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 242
                                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 243
                        }
244
 
665 daniel-mar 245
                        $out['text'] .= '<div id="update_versioninfo">';
246
 
635 daniel-mar 247
                        $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>';
248
 
840 daniel-mar 249
                        if (isset(OIDplus::getEditionInfo()['svnrepo']) && (OIDplus::getEditionInfo()['svnrepo'] != '')) {
250
                                $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/');
251
                                if (!str_starts_with(PHP_OS, 'WIN')) {
1430 daniel-mar 252
                                        $out['text'] .= ' '._L('Make sure that you invoke the <code>%1</code> command as the user who runs PHP or that you <code>%1</code> the files after invoking <code>%2</code>','chown -R ...','svn update');
840 daniel-mar 253
                                }
254
                                $out['text'] .= '</p>';
699 daniel-mar 255
                        } else {
256
                                $out['text'] .= '<p><b>'._L('Method A').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','GIT').'</p>';
257
                        }
635 daniel-mar 258
 
840 daniel-mar 259
                        if (isset(OIDplus::getEditionInfo()['gitrepo']) && (OIDplus::getEditionInfo()['gitrepo'] != '')) {
260
                                $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'));
261
                                if (!str_starts_with(PHP_OS, 'WIN')) {
1430 daniel-mar 262
                                        $out['text'] .= ' '._L('Make sure that you invoke the <code>%1</code> command as the user who runs PHP or that you <code>%1</code> the files after invoking <code>%2</code>','chown -R ...','git pull');
840 daniel-mar 263
                                }
264
                                $out['text'] .= '</p>';
699 daniel-mar 265
                        } else {
266
                                $out['text'] .= '<p><b>'._L('Method B').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','SVN').'</p>';
267
                        }
635 daniel-mar 268
 
699 daniel-mar 269
                        if (isset(OIDplus::getEditionInfo()['downloadpage']) && (OIDplus::getEditionInfo()['downloadpage'] != '')) {
1430 daniel-mar 270
                                $out['text'] .= '<p><b>'._L('Method C').'</b>: '._L('Install OIDplus by downloading an archive file from %1, which contains the latest development version, and extract it to your webspace. This update-tool will then update the files using change-scripts from the remote update server. It is required that the files on your webspace have create/write/delete permissions.','<a href="'.OIDplus::getEditionInfo()['downloadpage'].'">'.parse_url(OIDplus::getEditionInfo()['downloadpage'])['host'].'</a>').'</p>';
699 daniel-mar 271
                        } else {
272
                                $out['text'] .= '<p><b>'._L('Method C').'</b>: '._L('Distribution via %1 is not possible with this edition of OIDplus','Snapshot').'</p>';
273
                        }
635 daniel-mar 274
 
699 daniel-mar 275
 
635 daniel-mar 276
                        $out['text'] .= '<hr>';
277
 
278
                        $installType = OIDplus::getInstallType();
279
 
280
                        if ($installType === 'ambigous') {
1426 daniel-mar 281
                                $out['text'] .= '<font color="red">'.mb_strtoupper(_L('Error')).': '._L('Multiple version files/directories (.git and .svn) are existing! Therefore, the distribution channel is ambiguous!').'</font>';
665 daniel-mar 282
                                $out['text'] .= '</div>';
635 daniel-mar 283
                        } else if ($installType === 'unknown') {
906 daniel-mar 284
                                $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 285
                                $out['text'] .= '</div>';
1426 daniel-mar 286
                        } else if (($installType === 'svn-wc') || ($installType === 'git-wc') || ($installType === 'manual')) {
635 daniel-mar 287
                                if ($installType === 'svn-wc') {
288
                                        $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>';
662 daniel-mar 289
                                        $requireInfo = _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension');
697 daniel-mar 290
                                        $updateCommand = $this->getSvnCommand();
662 daniel-mar 291
                                } else if ($installType === 'git-wc') {
635 daniel-mar 292
                                        $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>';
662 daniel-mar 293
                                        $requireInfo = _L('shell access with Git client');
697 daniel-mar 294
                                        $updateCommand = $this->getGitCommand();
1426 daniel-mar 295
                                } else if ($installType === 'manual') {
1430 daniel-mar 296
                                        $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot file).').'</p>';
662 daniel-mar 297
                                        $requireInfo = ''; // unused
298
                                        $updateCommand = ''; // unused
1132 daniel-mar 299
                                } else {
300
                                        assert(false);
635 daniel-mar 301
                                }
302
 
303
                                $local_installation = OIDplus::getVersion();
1436 daniel-mar 304
                                $newest_version = $this->getLatestVersion(false);
635 daniel-mar 305
 
1130 daniel-mar 306
                                $out['text'] .= _L('Local installation: %1',($local_installation ?: _L('unknown'))).'<br>';
307
                                $out['text'] .= _L('Latest published version: %1',($newest_version ?: _L('unknown'))).'<br><br>';
635 daniel-mar 308
 
309
                                if (!$newest_version) {
1181 daniel-mar 310
                                        if (!url_get_contents_available(true, $reason)) {
311
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version.').'<br>'.$reason.'</p>';
1061 daniel-mar 312
                                        } else {
1426 daniel-mar 313
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version.').'<br>'._L('Probably the remote server could not be reached.').'</font></p>';
1061 daniel-mar 314
                                        }
662 daniel-mar 315
                                        $out['text'] .= '</div>';
654 daniel-mar 316
                                } else if (!$local_installation) {
1426 daniel-mar 317
                                        if ($installType === 'manual') {
662 daniel-mar 318
                                                $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version.').'</font></p>';
319
                                        } else {
320
                                                $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 321
                                        }
647 daniel-mar 322
                                        $out['text'] .= '</div>';
997 daniel-mar 323
                                } else if (version_compare($local_installation, $newest_version) >= 0) {
635 daniel-mar 324
                                        $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
647 daniel-mar 325
                                        $out['text'] .= '</div>';
635 daniel-mar 326
                                } else {
662 daniel-mar 327
                                        if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
1422 daniel-mar 328
                                                if ($installType === 'svn-wc') {
329
                                                        $shell_diff_cmd = 'svn stat';
330
                                                } else if ($installType === 'git-wc') {
331
                                                        $shell_diff_cmd = 'git status -s';
332
                                                } else {
333
                                                        $shell_diff_cmd = '';
334
                                                }
335
 
336
                                                $can_access_shell = true;
337
                                                if ($shell_diff_cmd) {
338
                                                        $cout = [];
339
                                                        exec("svn stat", $cout, $ec);
340
                                                        if ($ec === 0) {
341
                                                                // TODO: should this also be shown when there is no update available?
342
                                                                if (trim(implode('',$cout)) !== '') {
343
                                                                        $out['text'] .= '<p><font color="red">'._L('WARNING: There are changes in your working copy which WILL be reverted if you continue!').'</font></p>';
344
                                                                        $out['text'] .= '<p><font color="red">'._L('Detected changes:').'</font></p>';
345
                                                                        $out['text'] .= '<p><font color="red"><pre>'.htmlentities(implode("\n",$cout)).'</pre></font></p>';
346
                                                                } else {
347
                                                                        $out['text'] .= '<p><font color="green">'._L('Working copy is clean.').'</font></p>';
348
                                                                }
349
                                                        } else {
350
                                                                $can_access_shell = false;
351
                                                        }
352
                                                }
353
 
662 daniel-mar 354
                                                $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 355
                                                if ($can_access_shell) {
356
                                                        $out['text'] .= '<p>'._L('Alternatively, click this button to execute the command through the web-interface (command execution and write permissions required).').'</p>';
357
                                                }
662 daniel-mar 358
                                        }
635 daniel-mar 359
 
1426 daniel-mar 360
                                        $next_version = $this->getNextVersionFrom($local_installation,false);
361
                                        if ($next_version) {
1434 daniel-mar 362
                                                $out['text'] .= '<p><input type="button" onclick="OIDplusPageAdminSoftwareUpdate.doUpdateOIDplus('.js_escape($local_installation).', '.js_escape($next_version).', '.js_escape($newest_version).')" value="'._L('Update NOW').'"></p>';
1426 daniel-mar 363
                                        } else {
364
                                                $out['text'] .= '<p><font color="red">'._L('Could not determine next version. Please try again later.').'</font></p>';
365
                                        }
635 daniel-mar 366
 
662 daniel-mar 367
                                        // TODO: Open "system_file_check" without page reload.
368
                                        // TODO: Only show link if the plugin is installed
1430 daniel-mar 369
                                        $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">archive 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 370
 
647 daniel-mar 371
                                        $out['text'] .= '</div>';
372
 
662 daniel-mar 373
                                        $out['text'] .= $this->showPreview($local_installation, $newest_version);
635 daniel-mar 374
                                }
375
                        }
376
                } else {
377
                        $handled = false;
378
                }
379
        }
380
 
1116 daniel-mar 381
        /**
382
         * @param array $json
383
         * @param string|null $ra_email
384
         * @param bool $nonjs
385
         * @param string $req_goto
386
         * @return bool
387
         * @throws OIDplusException
388
         */
389
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 390
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
391
 
800 daniel-mar 392
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 393
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 394
                } else {
395
                        $tree_icon = null; // default icon (folder)
396
                }
397
 
398
                $json[] = array(
399
                        'id' => 'oidplus:software_update',
400
                        'icon' => $tree_icon,
401
                        'text' => _L('Software update')
402
                );
403
 
404
                return true;
405
        }
406
 
1116 daniel-mar 407
        /**
408
         * @param string $request
409
         * @return array|false
410
         */
411
        public function tree_search(string $request) {
635 daniel-mar 412
                return false;
413
        }
648 daniel-mar 414
 
1426 daniel-mar 415
 
416
        /*
417
         * @return array|false
1116 daniel-mar 418
         */
1426 daniel-mar 419
        private function changeLogJson() {
420
                static $cache = null;
421
                if (!is_null($cache)) return $cache;
648 daniel-mar 422
 
1426 daniel-mar 423
                $cache_file = OIDplus::localpath() . 'userdata/cache/master_changelog.json';
424
                if ((file_exists($cache_file)) && (time()-filemtime($cache_file) <= 10*60/*10 Minutes*/)) {
425
                        $changelog = file_get_contents($cache_file);
426
                } else {
1442 daniel-mar 427
                        $changelog_candidates = OIDplus::getEditionInfo()['master_changelog'];
428
                        if (!is_array($changelog_candidates)) $changelog_candidates = [ $changelog_candidates ];
429
 
430
                        $changelog = false;
431
                        foreach ($changelog_candidates as $master_changelog) {
432
                                if ((stripos($master_changelog,'http://')===0) || (stripos($master_changelog,'https://')===0)) {
433
                                        $changelog = @url_get_contents($master_changelog);
434
                                } else {
435
                                        $changelog = @file_get_contents($master_changelog);
436
                                }
437
                                if ($changelog) break;
1426 daniel-mar 438
                        }
1442 daniel-mar 439
 
1426 daniel-mar 440
                        if (!$changelog) return false;
441
                        file_put_contents($cache_file, $changelog);
442
                }
443
 
444
                $json = @json_decode($changelog, true);
445
                if (!$json) return false;
446
 
447
                $cache = $json;
448
                return $json;
449
        }
450
 
1116 daniel-mar 451
        /**
1130 daniel-mar 452
         * @param string $local_ver
1116 daniel-mar 453
         * @return false|string
454
         */
1130 daniel-mar 455
        private function showChangelog(string $local_ver) {
1426 daniel-mar 456
                try {
457
                        $json = $this->changeLogJson();
458
                        if (!$json) return false;
648 daniel-mar 459
 
460
                        $content = '';
1426 daniel-mar 461
 
462
                        foreach ($json as $data) {
463
                                if (!isset($data['version'])) continue;
464
                                if (version_compare($data['version'], $local_ver) <= 0) continue;
465
 
466
                                $data['msg'] = implode("\n", $data['changes']);
648 daniel-mar 467
                                $comment = empty($data['msg']) ? _L('No comment') : $data['msg'];
1426 daniel-mar 468
                                $tex = _L("Version %1",$data['version'])." (".$data['date'].") ";
469
                                $tex = str_pad($tex, 48, ' ', STR_PAD_RIGHT);
648 daniel-mar 470
                                $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
471
                                $content .= "\n";
472
                        }
1426 daniel-mar 473
 
648 daniel-mar 474
                        return $content;
1050 daniel-mar 475
                } catch (\Exception $e) {
648 daniel-mar 476
                        return false;
477
                }
478
        }
479
 
1116 daniel-mar 480
        /**
1426 daniel-mar 481
         * @param bool $allow_dev_version
1116 daniel-mar 482
         * @return false|string
483
         */
1436 daniel-mar 484
        private function getLatestVersion(bool $allow_dev_version=true) {
648 daniel-mar 485
                try {
1436 daniel-mar 486
                        $json = $this->changeLogJson();
487
                        return OIDplus::getVersion($json, $allow_dev_version);
1050 daniel-mar 488
                } catch (\Exception $e) {
648 daniel-mar 489
                        return false;
490
                }
491
        }
662 daniel-mar 492
 
1116 daniel-mar 493
        /**
1426 daniel-mar 494
         * @param string $prev_version
495
         * @param bool $allow_dev_version
496
         * @return false|string
497
         */
498
        private function getNextVersionFrom(string $prev_version, bool $allow_dev_version=true) {
499
                $json = $this->changeLogJson();
500
                if (!$json) return false;
501
                $next_version = false;
502
                foreach ($json as $v) {
503
                        if (!isset($v['version'])) continue;
504
                        if (!$allow_dev_version && str_ends_with($v['version'],'-dev')) continue;
505
                        if ($v['version'] == $prev_version) {
506
                                break;
507
                        }
508
                        $next_version = $v['version']; // the order of $json is critical: the version in front of our current version is the next available version
509
                }
510
                return $next_version;
511
        }
512
 
513
        /**
1130 daniel-mar 514
         * @param string $local_installation
515
         * @param string $newest_version
1116 daniel-mar 516
         * @return string
517
         */
1130 daniel-mar 518
        private function showPreview(string $local_installation, string $newest_version): string {
662 daniel-mar 519
                $out = '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
520
 
521
                ob_start();
522
                try {
523
                        $cont = $this->showChangelog($local_installation);
1050 daniel-mar 524
                } catch (\Exception $e) {
1201 daniel-mar 525
                        $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
526
                        $cont = _L('Error: %1',$htmlmsg);
662 daniel-mar 527
                }
528
                ob_end_clean();
529
 
831 daniel-mar 530
                $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", "$cont\n");
1274 daniel-mar 531
                $cont = preg_replace('@\\*\\*\\*(.+)\\n@', '<strong>!!!\\1</strong>'."\n", "$cont\n");
662 daniel-mar 532
 
533
                $out .= '<pre id="update_infobox">'.$cont.'</pre>';
534
 
535
                return $out;
536
        }
1000 daniel-mar 537
 
1116 daniel-mar 538
        /**
1131 daniel-mar 539
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8
1130 daniel-mar 540
         * @param string|null $user
1116 daniel-mar 541
         * @return array
542
         * @throws OIDplusException
543
         */
1130 daniel-mar 544
        public function getNotifications(string $user=null): array {
1000 daniel-mar 545
                $notifications = array();
546
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
547
 
548
                        // Following code is based on the VNag plugin (admin 901) code
549
 
550
                        $installType = OIDplus::getInstallType();
551
 
552
                        if ($installType === 'ambigous') {
553
                                $out_stat = 'WARN';
1426 daniel-mar 554
                                $out_msg  = _L('Multiple version files/directories (.git and .svn) are existing! Therefore, the distribution channel is ambiguous!');
1000 daniel-mar 555
                        } else if ($installType === 'unknown') {
556
                                $out_stat = 'WARN';
557
                                $out_msg  = _L('The version cannot be determined, and the update needs to be applied manually!');
558
                        } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
1181 daniel-mar 559
                                if (!url_get_contents_available(true, $reason)) {
560
                                        $out_stat = 'WARN';
561
                                        $out_msg  = _L('OIDplus could not determine the latest version.').' '.$reason;
562
                                } else {
563
                                        $local_installation = OIDplus::getVersion();
1436 daniel-mar 564
                                        $newest_version = $this->getLatestVersion(false);
1000 daniel-mar 565
 
1181 daniel-mar 566
                                        $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client');
567
                                        $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
1000 daniel-mar 568
 
1181 daniel-mar 569
                                        if (!$newest_version) {
570
                                                $out_stat = 'WARN';
1426 daniel-mar 571
                                                $out_msg = _L('OIDplus could not determine the latest version.') . ' ' . _L('Probably the remote server could not be reached.');
1181 daniel-mar 572
                                        } else if (!$local_installation) {
573
                                                $out_stat = 'WARN';
574
                                                $out_msg = _L('OIDplus could not determine its version (Required: %1). Please update your system manually via the "%2" command regularly.', $requireInfo, $updateCommand);
575
                                        } else if (version_compare($local_installation, $newest_version) >= 0) {
576
                                                $out_stat = 'INFO';
577
                                                $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 578
                                        } else {
1181 daniel-mar 579
                                                $out_stat = 'WARN';
580
                                                $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 581
                                        }
1181 daniel-mar 582
                                }
1426 daniel-mar 583
                        } else if ($installType === 'manual') {
1181 daniel-mar 584
                                if (!url_get_contents_available(true, $reason)) {
1000 daniel-mar 585
                                        $out_stat = 'WARN';
1181 daniel-mar 586
                                        $out_msg  = _L('OIDplus could not determine the latest version.').' '.$reason;
1000 daniel-mar 587
                                } else {
1181 daniel-mar 588
                                        $local_installation = OIDplus::getVersion();
1436 daniel-mar 589
                                        $newest_version = $this->getLatestVersion(false);
1000 daniel-mar 590
 
1181 daniel-mar 591
                                        if (!$newest_version) {
592
                                                $out_stat = 'WARN';
1426 daniel-mar 593
                                                $out_msg = _L('OIDplus could not determine the latest version.') . ' ' . _L('Probably the remote server could not be reached.');
1181 daniel-mar 594
                                        } else if (!$local_installation) {
595
                                                $out_stat = 'WARN';
596
                                                $out_msg = _L('OIDplus could not determine its version. Please update your system manually by downloading the latest archive file from oidplus.com.');
597
                                        } else if (version_compare($local_installation, $newest_version) >= 0) {
598
                                                $out_stat = 'INFO';
599
                                                $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 600
                                        } else {
1181 daniel-mar 601
                                                $out_stat = 'WARN';
602
                                                $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version);
1061 daniel-mar 603
                                        }
1000 daniel-mar 604
                                }
605
                        } else {
606
                                assert(false);
607
                                return $notifications;
608
                        }
609
 
610
                        if ($out_stat != 'INFO') {
611
                                $out_msg = '<a '.OIDplus::gui()->link('oidplus:software_update').'>'._L('Software update').'</a>: ' . $out_msg;
612
 
1189 daniel-mar 613
                                $notifications[] = new OIDplusNotification($out_stat, $out_msg);
1000 daniel-mar 614
                        }
615
 
616
                }
617
                return $notifications;
618
        }
619
 
661 daniel-mar 620
}