Subversion Repositories oidplus

Rev

Rev 654 | Rev 662 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusPageAdminSoftwareUpdate extends OIDplusPagePluginAdmin {
23
 
24
        public function init($html=true) {
25
        }
26
 
27
        public function action($actionID, $params) {
28
                if ($actionID == 'update_now') {
647 daniel-mar 29
                        @set_time_limit(0);
635 daniel-mar 30
 
31
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
32
                                throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
33
                        }
34
 
653 daniel-mar 35
                        if (OIDplus::getInstallType() !== 'svn-snapshot') {
36
                                throw new OIDplusException(_L('The web-update can only be applied on a SVN-Snapshot installation.'));
37
                        }
38
 
647 daniel-mar 39
                        $rev = $params['rev'];
635 daniel-mar 40
 
651 daniel-mar 41
                        // Download and unzip
42
 
650 daniel-mar 43
                        if (function_exists('gzdecode')) {
654 daniel-mar 44
                                $url = sprintf(parse_ini_file(__DIR__.'/consts.ini')['update_package_gz'], $rev-1, $rev);
653 daniel-mar 45
                                $cont = url_get_contents($url);
650 daniel-mar 46
                                if ($cont !== false) $cont = @gzdecode($cont);
47
                        } else {
654 daniel-mar 48
                                $url = sprintf(parse_ini_file(__DIR__.'/consts.ini')['update_package'], $rev-1, $rev);
653 daniel-mar 49
                                $cont = url_get_contents($url);
650 daniel-mar 50
                        }
635 daniel-mar 51
 
651 daniel-mar 52
                        if ($cont === false) throw new OIDplusException(_L("Update %1 could not be downloaded from ViaThinkSoft server. Please try again later.",$rev));
650 daniel-mar 53
 
651 daniel-mar 54
                        // Check signature...
55
 
56
                        if (function_exists('openssl_verify')) {
57
 
652 daniel-mar 58
                                $m = array();
651 daniel-mar 59
                                if (!preg_match('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', $cont, $m)) {
60
                                        throw new OIDplusException(_L("Update package file of revision %1 not digitally signed",$rev));
61
                                }
62
                                $signature = base64_decode($m[1]);
63
 
64
                                $naked = preg_replace('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', '', $cont);
65
                                $hash = hash("sha256", $naked."update_".($rev-1)."_to_".($rev).".txt");
66
 
67
                                $public_key = file_get_contents(__DIR__.'/public.pem');
68
                                if (!openssl_verify($hash, $signature, $public_key, OPENSSL_ALGO_SHA256)) {
69
                                        throw new OIDplusException(_L("Update package file of revision %1: Signature invalid",$rev));
70
                                }
71
 
72
                        }
73
 
653 daniel-mar 74
                        // All OK! Now write file
651 daniel-mar 75
 
653 daniel-mar 76
                        $tmp_filename = 'update_'.generateRandomString(10).'.tmp.php';
77
                        $local_file = OIDplus::localpath().$tmp_filename;
78
                        $web_file = OIDplus::webpath().$tmp_filename;
635 daniel-mar 79
 
653 daniel-mar 80
                        @file_put_contents($local_file, $cont);
647 daniel-mar 81
 
653 daniel-mar 82
                        if (!file_exists($local_file) || (@file_get_contents($local_file) !== $cont)) {
83
                                throw new OIDplusException(_L('Update file could not written. Probably there are no write-permissions to the root folder.'));
84
                        }
85
 
86
                        // Now call the written file
87
                        // Note: we may not use eval($cont) because script uses die()
88
 
89
                        $res = url_get_contents($web_file);
90
                        if ($res === false) {
91
                                throw new OIDplusException(_L('Communication with ViaThinkSoft server failed'));
92
                        }
93
 
94
                        return array("status" => 0, "content" => $res);
635 daniel-mar 95
                }
96
        }
97
 
98
        public function gui($id, &$out, &$handled) {
99
                $parts = explode('.',$id,2);
100
                if (!isset($parts[1])) $parts[1] = '';
101
                if ($parts[0] == 'oidplus:software_update') {
102
                        @set_time_limit(0);
103
 
104
                        $handled = true;
105
                        $out['title'] = _L('Software update');
106
                        $out['icon']  = OIDplus::webpath(__DIR__).'icon_big.png';
107
 
108
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
109
                                $out['icon'] = 'img/error_big.png';
110
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
111
                                return;
112
                        }
113
 
114
                        $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>';
115
 
116
                        $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. Highly recommended if you have a Shell/SSH access to your webspace!',htmlentities(parse_ini_file(__DIR__.'/consts.ini')['svn']).'/trunk').'</p>';
117
 
118
                        $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. Highly recommended if you have a Shell/SSH access to your webspace!','https://github.com/danielmarschall/oidplus.git').'</p>';
119
 
661 daniel-mar 120
                        $out['text'] .= '<p><b>'._L('Method C').'</b>: '._L('Install OIDplus by downloading a TAR.GZ file from www.viathinksoft.com, 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.').'</p>';
635 daniel-mar 121
 
122
                        $out['text'] .= '<hr>';
123
 
124
                        $installType = OIDplus::getInstallType();
125
 
126
                        if ($installType === 'ambigous') {
661 daniel-mar 127
                                $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('Multiple version files/directories (oidplus_version.txt, .version.php, .git and .svn) are existing! Therefore, the version is ambiguous!').'</font>';
635 daniel-mar 128
                        } else if ($installType === 'unknown') {
129
                                $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('The version cannot be determined, and the update needs to be applied manually!').'</font>';
130
                        } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
131
                                if ($installType === 'svn-wc') {
132
                                        $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>';
133
                                } else {
134
                                        $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>';
135
                                }
136
 
137
                                $local_installation = OIDplus::getVersion();
648 daniel-mar 138
                                $newest_version = $this->getLatestRevision();
635 daniel-mar 139
 
140
                                $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
141
                                $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
142
 
143
                                $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client');
144
                                $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
145
 
146
                                if (!$newest_version) {
147
                                        $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
654 daniel-mar 148
                                } else if (!$local_installation) {
635 daniel-mar 149
                                        $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>';
654 daniel-mar 150
                                } else if (substr($local_installation,4) >= substr($newest_version,4)) {
635 daniel-mar 151
                                        $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
152
                                } else {
153
                                        $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>';
154
 
155
                                        $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
156
 
157
                                        ob_start();
158
                                        try {
648 daniel-mar 159
                                                $cont = $this->showChangelog($local_installation);
635 daniel-mar 160
                                        } catch (Exception $e) {
161
                                                $cont = _L('Error: %1',$e->getMessage());
162
                                        }
163
                                        ob_end_clean();
164
 
165
                                        $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
166
 
167
                                        $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
168
                                }
169
                        } else if ($installType === 'svn-snapshot') {
647 daniel-mar 170
                                $out['text'] .= '<div id="update_versioninfo">';
171
 
661 daniel-mar 172
                                $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot TAR.GZ file with .version.php file).').'</p>';
635 daniel-mar 173
 
174
                                $local_installation = OIDplus::getVersion();
648 daniel-mar 175
                                $newest_version = $this->getLatestRevision();
635 daniel-mar 176
 
177
                                $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
178
                                $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
179
 
180
                                if (!$newest_version) {
181
                                        $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
647 daniel-mar 182
                                        $out['text'] .= '</div>';
661 daniel-mar 183
                                } else if (!$local_installation) {
184
                                        $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version.').'</font></p>';
654 daniel-mar 185
                                } else if (substr($local_installation,4) >= substr($newest_version,4)) {
635 daniel-mar 186
                                        $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
647 daniel-mar 187
                                        $out['text'] .= '</div>';
635 daniel-mar 188
                                } else {
189
                                        $out['text'] .= '<p><font color="red">'.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! In case the update fails, you can download and extract the complete <a href="https://www.viathinksoft.com/projects/oidplus">SVN-Snapshot TAR.GZ file</a> again. Since all your data should lay inside the folder "userdata" and "userdata_pub", this should be safe.').'</font></p>';
190
                                        $out['text'] .= '<form method="POST" action="index.php">';
191
 
648 daniel-mar 192
                                        $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 193
 
647 daniel-mar 194
                                        $out['text'] .= '</div>';
195
 
635 daniel-mar 196
                                        $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
197
 
198
                                        ob_start();
199
                                        try {
648 daniel-mar 200
                                                $cont = $this->showChangelog($local_installation);
635 daniel-mar 201
                                        } catch (Exception $e) {
202
                                                $cont = _L('Error: %1',$e->getMessage());
203
                                        }
204
                                        ob_end_clean();
205
 
206
                                        $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
207
 
208
                                        $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
209
                                }
210
                        }
211
                } else {
212
                        $handled = false;
213
                }
214
        }
215
 
216
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
217
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
218
 
219
                if (file_exists(__DIR__.'/treeicon.png')) {
220
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
221
                } else {
222
                        $tree_icon = null; // default icon (folder)
223
                }
224
 
225
                $json[] = array(
226
                        'id' => 'oidplus:software_update',
227
                        'icon' => $tree_icon,
228
                        'text' => _L('Software update')
229
                );
230
 
231
                return true;
232
        }
233
 
234
        public function tree_search($request) {
235
                return false;
236
        }
648 daniel-mar 237
 
238
        private $releases_ser = null;
239
 
240
        private function showChangelog($local_ver) {
241
 
242
                try {
243
                        if (is_null($this->releases_ser)) {
654 daniel-mar 244
                                $url = parse_ini_file(__DIR__.'/consts.ini')['revisionlog'];
653 daniel-mar 245
                                $cont = url_get_contents($url);
648 daniel-mar 246
                                if ($cont === false) return false;
247
                                $this->releases_ser = $cont;
248
                        } else {
249
                                $cont = $this->releases_ser;
250
                        }
251
                        $content = '';
252
                        $ary = @unserialize($cont);
253
                        if ($ary === false) return false;
254
                        krsort($ary);
255
                        foreach ($ary as $rev => $data) {
256
                                if ($rev <= substr($local_ver,4)) continue;
257
                                $comment = empty($data['msg']) ? _L('No comment') : $data['msg'];
258
                                $tex = _L("New revision %1 by %2",$rev,$data['author'])." (".$data['date'].") ";
259
                                $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
260
                                $content .= "\n";
261
                        }
262
                        return $content;
263
                } catch (Exception $e) {
264
                        return false;
265
                }
266
 
267
        }
268
 
269
        private function getLatestRevision() {
270
                try {
271
                        if (is_null($this->releases_ser)) {
654 daniel-mar 272
                                $url = parse_ini_file(__DIR__.'/consts.ini')['revisionlog'];
653 daniel-mar 273
                                $cont = url_get_contents($url);
648 daniel-mar 274
                                if ($cont === false) return false;
275
                                $this->releases_ser = $cont;
276
                        } else {
277
                                $cont = $this->releases_ser;
278
                        }
279
                        $ary = @unserialize($cont);
280
                        if ($ary === false) return false;
281
                        krsort($ary);
282
                        $max_rev = array_keys($ary)[0];
283
                        $newest_version = 'svn-' . $max_rev;
284
                        return $newest_version;
285
                } catch (Exception $e) {
286
                        return false;
287
                }
288
        }
661 daniel-mar 289
}