Rev 661 | Rev 665 | 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 | |||
662 | daniel-mar | 35 | if (OIDplus::getInstallType() === 'git-wc') { |
36 | $cmd = 'git pull -s recursive -X theirs 2>&1'; |
||
37 | |||
38 | $ec = -1; |
||
39 | $out = array(); |
||
40 | exec($cmd, $out, $ec); |
||
41 | |||
42 | $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out)); |
||
43 | if ($ec === 0) { |
||
44 | $rev = 'HEAD'; // do not translate |
||
45 | return array("status" => 0, "content" => $res, "rev" => $rev); |
||
46 | } else { |
||
47 | return array("status" => -1, "error" => $res, "content" => ""); |
||
48 | } |
||
653 | daniel-mar | 49 | } |
662 | daniel-mar | 50 | else if (OIDplus::getInstallType() === 'svn-wc') { |
51 | $cmd = 'svn update --accept theirs-full 2>&1'; |
||
653 | daniel-mar | 52 | |
662 | daniel-mar | 53 | $ec = -1; |
54 | $out = array(); |
||
55 | exec($cmd, $out, $ec); |
||
635 | daniel-mar | 56 | |
662 | daniel-mar | 57 | $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out)); |
58 | if ($ec === 0) { |
||
59 | $rev = 'HEAD'; // do not translate |
||
60 | return array("status" => 0, "content" => $res, "rev" => $rev); |
||
61 | } else { |
||
62 | return array("status" => -1, "error" => $res, "content" => ""); |
||
63 | } |
||
650 | daniel-mar | 64 | } |
662 | daniel-mar | 65 | else if (OIDplus::getInstallType() === 'svn-snapshot') { |
635 | daniel-mar | 66 | |
662 | daniel-mar | 67 | $rev = $params['rev']; |
650 | daniel-mar | 68 | |
662 | daniel-mar | 69 | // Download and unzip |
651 | daniel-mar | 70 | |
662 | daniel-mar | 71 | if (function_exists('gzdecode')) { |
72 | $url = sprintf(parse_ini_file(__DIR__.'/consts.ini')['update_package_gz'], $rev-1, $rev); |
||
73 | $cont = url_get_contents($url); |
||
74 | if ($cont !== false) $cont = @gzdecode($cont); |
||
75 | } else { |
||
76 | $url = sprintf(parse_ini_file(__DIR__.'/consts.ini')['update_package'], $rev-1, $rev); |
||
77 | $cont = url_get_contents($url); |
||
651 | daniel-mar | 78 | } |
79 | |||
662 | daniel-mar | 80 | if ($cont === false) throw new OIDplusException(_L("Update %1 could not be downloaded from ViaThinkSoft server. Please try again later.",$rev)); |
651 | daniel-mar | 81 | |
662 | daniel-mar | 82 | // Check signature... |
83 | |||
84 | if (function_exists('openssl_verify')) { |
||
85 | |||
86 | $m = array(); |
||
87 | if (!preg_match('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', $cont, $m)) { |
||
88 | throw new OIDplusException(_L("Update package file of revision %1 not digitally signed",$rev)); |
||
89 | } |
||
90 | $signature = base64_decode($m[1]); |
||
91 | |||
92 | $naked = preg_replace('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', '', $cont); |
||
93 | $hash = hash("sha256", $naked."update_".($rev-1)."_to_".($rev).".txt"); |
||
94 | |||
95 | $public_key = file_get_contents(__DIR__.'/public.pem'); |
||
96 | if (!openssl_verify($hash, $signature, $public_key, OPENSSL_ALGO_SHA256)) { |
||
97 | throw new OIDplusException(_L("Update package file of revision %1: Signature invalid",$rev)); |
||
98 | } |
||
99 | |||
651 | daniel-mar | 100 | } |
101 | |||
662 | daniel-mar | 102 | // All OK! Now write file |
651 | daniel-mar | 103 | |
662 | daniel-mar | 104 | $tmp_filename = 'update_'.generateRandomString(10).'.tmp.php'; |
105 | $local_file = OIDplus::localpath().$tmp_filename; |
||
106 | $web_file = OIDplus::webpath().$tmp_filename; |
||
651 | daniel-mar | 107 | |
662 | daniel-mar | 108 | @file_put_contents($local_file, $cont); |
635 | daniel-mar | 109 | |
662 | daniel-mar | 110 | if (!file_exists($local_file) || (@file_get_contents($local_file) !== $cont)) { |
111 | throw new OIDplusException(_L('Update file could not written. Probably there are no write-permissions to the root folder.')); |
||
112 | } |
||
647 | daniel-mar | 113 | |
662 | daniel-mar | 114 | // Now call the written file |
115 | // Note: we may not use eval($cont) because script uses die() |
||
653 | daniel-mar | 116 | |
662 | daniel-mar | 117 | $res = url_get_contents($web_file); |
118 | if ($res === false) { |
||
119 | throw new OIDplusException(_L('Communication with ViaThinkSoft server failed')); |
||
120 | } |
||
653 | daniel-mar | 121 | |
662 | daniel-mar | 122 | return array("status" => 0, "content" => $res, "rev" => $rev); |
653 | daniel-mar | 123 | } |
662 | daniel-mar | 124 | else { |
125 | throw new OIDplusException(_L('Multiple version files/directories (oidplus_version.txt, .version.php, .git and .svn) are existing! Therefore, the version is ambiguous!')); |
||
126 | } |
||
635 | daniel-mar | 127 | } |
128 | } |
||
129 | |||
130 | public function gui($id, &$out, &$handled) { |
||
131 | $parts = explode('.',$id,2); |
||
132 | if (!isset($parts[1])) $parts[1] = ''; |
||
133 | if ($parts[0] == 'oidplus:software_update') { |
||
134 | @set_time_limit(0); |
||
135 | |||
136 | $handled = true; |
||
137 | $out['title'] = _L('Software update'); |
||
138 | $out['icon'] = OIDplus::webpath(__DIR__).'icon_big.png'; |
||
139 | |||
140 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
141 | $out['icon'] = 'img/error_big.png'; |
||
142 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>'; |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>'; |
||
147 | |||
148 | $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>'; |
||
149 | |||
150 | $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>'; |
||
151 | |||
661 | daniel-mar | 152 | $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 | 153 | |
154 | $out['text'] .= '<hr>'; |
||
155 | |||
156 | $installType = OIDplus::getInstallType(); |
||
157 | |||
158 | if ($installType === 'ambigous') { |
||
661 | daniel-mar | 159 | $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 | 160 | } else if ($installType === 'unknown') { |
161 | $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('The version cannot be determined, and the update needs to be applied manually!').'</font>'; |
||
662 | daniel-mar | 162 | } else if (($installType === 'svn-wc') || ($installType === 'git-wc') || ($installType === 'svn-snapshot')) { |
163 | $out['text'] .= '<div id="update_versioninfo">'; |
||
164 | |||
635 | daniel-mar | 165 | if ($installType === 'svn-wc') { |
166 | $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>'; |
||
662 | daniel-mar | 167 | $requireInfo = _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension'); |
168 | $updateCommand = 'svn update'; |
||
169 | } else if ($installType === 'git-wc') { |
||
635 | daniel-mar | 170 | $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>'; |
662 | daniel-mar | 171 | $requireInfo = _L('shell access with Git client'); |
172 | $updateCommand = 'git pull'; |
||
173 | } else if ($installType === 'svn-snapshot') { |
||
174 | $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot TAR.GZ file with .version.php file).').'</p>'; |
||
175 | $requireInfo = ''; // unused |
||
176 | $updateCommand = ''; // unused |
||
635 | daniel-mar | 177 | } |
178 | |||
179 | $local_installation = OIDplus::getVersion(); |
||
648 | daniel-mar | 180 | $newest_version = $this->getLatestRevision(); |
635 | daniel-mar | 181 | |
182 | $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>'; |
||
662 | daniel-mar | 183 | $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br><br>'; |
635 | daniel-mar | 184 | |
185 | if (!$newest_version) { |
||
186 | $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>'; |
||
662 | daniel-mar | 187 | $out['text'] .= '</div>'; |
654 | daniel-mar | 188 | } else if (!$local_installation) { |
662 | daniel-mar | 189 | if ($installType === 'svn-snapshot') { |
190 | $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version.').'</font></p>'; |
||
191 | } else { |
||
192 | $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 | 193 | } |
647 | daniel-mar | 194 | $out['text'] .= '</div>'; |
654 | daniel-mar | 195 | } else if (substr($local_installation,4) >= substr($newest_version,4)) { |
635 | daniel-mar | 196 | $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>'; |
647 | daniel-mar | 197 | $out['text'] .= '</div>'; |
635 | daniel-mar | 198 | } else { |
662 | daniel-mar | 199 | if (($installType === 'svn-wc') || ($installType === 'git-wc')) { |
200 | $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>'; |
||
201 | $out['text'] .= '<p>'._L('Alternatively, click this button to execute the command through the web-interface (command execution and write permissions required).').'</p>'; |
||
202 | } |
||
635 | daniel-mar | 203 | |
648 | daniel-mar | 204 | $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 | 205 | |
662 | daniel-mar | 206 | // TODO: Open "system_file_check" without page reload. |
207 | // TODO: Only show link if the plugin is installed |
||
208 | $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! (<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="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.','?goto='.urlencode('oidplus:system_file_check')).'</font></p>'; |
||
209 | |||
647 | daniel-mar | 210 | $out['text'] .= '</div>'; |
211 | |||
662 | daniel-mar | 212 | $out['text'] .= $this->showPreview($local_installation, $newest_version); |
635 | daniel-mar | 213 | } |
214 | } |
||
215 | } else { |
||
216 | $handled = false; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
||
221 | if (!OIDplus::authUtils()->isAdminLoggedIn()) return false; |
||
222 | |||
223 | if (file_exists(__DIR__.'/treeicon.png')) { |
||
224 | $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png'; |
||
225 | } else { |
||
226 | $tree_icon = null; // default icon (folder) |
||
227 | } |
||
228 | |||
229 | $json[] = array( |
||
230 | 'id' => 'oidplus:software_update', |
||
231 | 'icon' => $tree_icon, |
||
232 | 'text' => _L('Software update') |
||
233 | ); |
||
234 | |||
235 | return true; |
||
236 | } |
||
237 | |||
238 | public function tree_search($request) { |
||
239 | return false; |
||
240 | } |
||
648 | daniel-mar | 241 | |
242 | private $releases_ser = null; |
||
243 | |||
244 | private function showChangelog($local_ver) { |
||
245 | |||
246 | try { |
||
247 | if (is_null($this->releases_ser)) { |
||
654 | daniel-mar | 248 | $url = parse_ini_file(__DIR__.'/consts.ini')['revisionlog']; |
653 | daniel-mar | 249 | $cont = url_get_contents($url); |
648 | daniel-mar | 250 | if ($cont === false) return false; |
251 | $this->releases_ser = $cont; |
||
252 | } else { |
||
253 | $cont = $this->releases_ser; |
||
254 | } |
||
255 | $content = ''; |
||
256 | $ary = @unserialize($cont); |
||
257 | if ($ary === false) return false; |
||
258 | krsort($ary); |
||
259 | foreach ($ary as $rev => $data) { |
||
260 | if ($rev <= substr($local_ver,4)) continue; |
||
261 | $comment = empty($data['msg']) ? _L('No comment') : $data['msg']; |
||
262 | $tex = _L("New revision %1 by %2",$rev,$data['author'])." (".$data['date'].") "; |
||
263 | $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment)); |
||
264 | $content .= "\n"; |
||
265 | } |
||
266 | return $content; |
||
267 | } catch (Exception $e) { |
||
268 | return false; |
||
269 | } |
||
270 | |||
271 | } |
||
272 | |||
273 | private function getLatestRevision() { |
||
274 | try { |
||
275 | if (is_null($this->releases_ser)) { |
||
654 | daniel-mar | 276 | $url = parse_ini_file(__DIR__.'/consts.ini')['revisionlog']; |
653 | daniel-mar | 277 | $cont = url_get_contents($url); |
648 | daniel-mar | 278 | if ($cont === false) return false; |
279 | $this->releases_ser = $cont; |
||
280 | } else { |
||
281 | $cont = $this->releases_ser; |
||
282 | } |
||
283 | $ary = @unserialize($cont); |
||
284 | if ($ary === false) return false; |
||
285 | krsort($ary); |
||
286 | $max_rev = array_keys($ary)[0]; |
||
287 | $newest_version = 'svn-' . $max_rev; |
||
288 | return $newest_version; |
||
289 | } catch (Exception $e) { |
||
290 | return false; |
||
291 | } |
||
292 | } |
||
662 | daniel-mar | 293 | |
294 | private function showPreview($local_installation, $newest_version) { |
||
295 | $out = '<h2 id="update_header">'._L('Preview of update %1 → %2',$local_installation,$newest_version).'</h2>'; |
||
296 | |||
297 | ob_start(); |
||
298 | try { |
||
299 | $cont = $this->showChangelog($local_installation); |
||
300 | } catch (Exception $e) { |
||
301 | $cont = _L('Error: %1',$e->getMessage()); |
||
302 | } |
||
303 | ob_end_clean(); |
||
304 | |||
305 | $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont); |
||
306 | |||
307 | $out .= '<pre id="update_infobox">'.$cont.'</pre>'; |
||
308 | |||
309 | return $out; |
||
310 | } |
||
661 | daniel-mar | 311 | } |