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