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