Rev 1086 | Rev 1130 | 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 | |||
635 | daniel-mar | 26 | class OIDplusPageAdminSoftwareUpdate extends OIDplusPagePluginAdmin { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @param bool $html |
||
30 | * @return void |
||
31 | */ |
||
32 | public function init(bool $html=true) { |
||
635 | daniel-mar | 33 | } |
34 | |||
1116 | daniel-mar | 35 | /** |
36 | * @return string |
||
37 | */ |
||
38 | private function getGitCommand(): string { |
||
698 | daniel-mar | 39 | return 'git --git-dir='.escapeshellarg(OIDplus::findGitFolder().'/').' --work-tree='.escapeshellarg(OIDplus::localpath()).' -C "" pull origin master -s recursive -X theirs'; |
697 | daniel-mar | 40 | } |
41 | |||
1116 | daniel-mar | 42 | /** |
43 | * @return string |
||
44 | */ |
||
45 | private function getSvnCommand(): string { |
||
697 | daniel-mar | 46 | return 'svn update --accept theirs-full'; |
47 | } |
||
48 | |||
1116 | daniel-mar | 49 | /** |
50 | * @param string $actionID |
||
51 | * @param array $params |
||
52 | * @return array |
||
53 | * @throws OIDplusException |
||
54 | */ |
||
55 | public function action(string $actionID, array $params): array { |
||
635 | daniel-mar | 56 | if ($actionID == 'update_now') { |
647 | daniel-mar | 57 | @set_time_limit(0); |
635 | daniel-mar | 58 | |
59 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
60 | throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin'))); |
||
61 | } |
||
62 | |||
662 | daniel-mar | 63 | if (OIDplus::getInstallType() === 'git-wc') { |
697 | daniel-mar | 64 | $cmd = $this->getGitCommand().' 2>&1'; |
662 | daniel-mar | 65 | |
66 | $ec = -1; |
||
67 | $out = array(); |
||
68 | exec($cmd, $out, $ec); |
||
69 | |||
70 | $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out)); |
||
71 | if ($ec === 0) { |
||
72 | $rev = 'HEAD'; // do not translate |
||
73 | return array("status" => 0, "content" => $res, "rev" => $rev); |
||
74 | } else { |
||
75 | return array("status" => -1, "error" => $res, "content" => ""); |
||
76 | } |
||
653 | daniel-mar | 77 | } |
662 | daniel-mar | 78 | else if (OIDplus::getInstallType() === 'svn-wc') { |
697 | daniel-mar | 79 | $cmd = $this->getSvnCommand().' 2>&1'; |
653 | daniel-mar | 80 | |
662 | daniel-mar | 81 | $ec = -1; |
82 | $out = array(); |
||
83 | exec($cmd, $out, $ec); |
||
635 | daniel-mar | 84 | |
662 | daniel-mar | 85 | $res = _L('Execute command:').' '.$cmd."\n\n".trim(implode("\n",$out)); |
86 | if ($ec === 0) { |
||
87 | $rev = 'HEAD'; // do not translate |
||
88 | return array("status" => 0, "content" => $res, "rev" => $rev); |
||
89 | } else { |
||
90 | return array("status" => -1, "error" => $res, "content" => ""); |
||
91 | } |
||
650 | daniel-mar | 92 | } |
662 | daniel-mar | 93 | else if (OIDplus::getInstallType() === 'svn-snapshot') { |
635 | daniel-mar | 94 | |
662 | daniel-mar | 95 | $rev = $params['rev']; |
650 | daniel-mar | 96 | |
807 | daniel-mar | 97 | $update_version = isset($params['update_version']) ? $params['update_version'] : 1; |
98 | if (($update_version != 1) && ($update_version != 2)) { |
||
99 | throw new OIDplusException(_L('Unknown update version')); |
||
100 | } |
||
101 | |||
662 | daniel-mar | 102 | // Download and unzip |
651 | daniel-mar | 103 | |
716 | daniel-mar | 104 | $cont = false; |
105 | for ($retry=1; $retry<=3; $retry++) { |
||
106 | if (function_exists('gzdecode')) { |
||
107 | $url = sprintf(OIDplus::getEditionInfo()['update_package_gz'], $rev-1, $rev); |
||
108 | $cont = url_get_contents($url); |
||
109 | if ($cont !== false) $cont = @gzdecode($cont); |
||
110 | } else { |
||
111 | $url = sprintf(OIDplus::getEditionInfo()['update_package'], $rev-1, $rev); |
||
112 | $cont = url_get_contents($url); |
||
113 | } |
||
114 | if ($cont !== false) { |
||
115 | break; |
||
116 | } else { |
||
117 | sleep(1); |
||
118 | } |
||
651 | daniel-mar | 119 | } |
662 | daniel-mar | 120 | if ($cont === false) throw new OIDplusException(_L("Update %1 could not be downloaded from ViaThinkSoft server. Please try again later.",$rev)); |
651 | daniel-mar | 121 | |
662 | daniel-mar | 122 | // Check signature... |
123 | |||
124 | if (function_exists('openssl_verify')) { |
||
125 | |||
126 | $m = array(); |
||
127 | if (!preg_match('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', $cont, $m)) { |
||
128 | throw new OIDplusException(_L("Update package file of revision %1 not digitally signed",$rev)); |
||
129 | } |
||
130 | $signature = base64_decode($m[1]); |
||
131 | |||
132 | $naked = preg_replace('@<\?php /\* <ViaThinkSoftSignature>(.+)</ViaThinkSoftSignature> \*/ \?>\n@ismU', '', $cont); |
||
133 | $hash = hash("sha256", $naked."update_".($rev-1)."_to_".($rev).".txt"); |
||
134 | |||
135 | $public_key = file_get_contents(__DIR__.'/public.pem'); |
||
136 | if (!openssl_verify($hash, $signature, $public_key, OPENSSL_ALGO_SHA256)) { |
||
137 | throw new OIDplusException(_L("Update package file of revision %1: Signature invalid",$rev)); |
||
138 | } |
||
139 | |||
651 | daniel-mar | 140 | } |
141 | |||
662 | daniel-mar | 142 | // All OK! Now write file |
651 | daniel-mar | 143 | |
662 | daniel-mar | 144 | $tmp_filename = 'update_'.generateRandomString(10).'.tmp.php'; |
145 | $local_file = OIDplus::localpath().$tmp_filename; |
||
651 | daniel-mar | 146 | |
662 | daniel-mar | 147 | @file_put_contents($local_file, $cont); |
635 | daniel-mar | 148 | |
662 | daniel-mar | 149 | if (!file_exists($local_file) || (@file_get_contents($local_file) !== $cont)) { |
150 | throw new OIDplusException(_L('Update file could not written. Probably there are no write-permissions to the root folder.')); |
||
151 | } |
||
647 | daniel-mar | 152 | |
807 | daniel-mar | 153 | if ($update_version == 1) { |
154 | // Now call the written file |
||
155 | // Note: we may not use eval($cont) because the script uses die(), |
||
156 | // and things in the script might collide with currently (un)loaded source code files, shutdown procedues, etc. |
||
157 | $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 |
||
158 | $res = url_get_contents($web_file); |
||
159 | if ($res === false) { |
||
160 | throw new OIDplusException(_L('Update-script %1 could not be executed',$web_file)); |
||
161 | } |
||
162 | return array("status" => 0, "content" => $res, "rev" => $rev); |
||
163 | } else if ($update_version == 2) { |
||
164 | // In this version, the client will call the web-update file. |
||
165 | // This has the advantage that it will also work if the system is htpasswd protected |
||
166 | return array("status" => 0, "update_file" => $tmp_filename, "rev" => $rev); |
||
1116 | daniel-mar | 167 | } else { |
168 | throw new OIDplusException(_L("Unexpected update version")); |
||
662 | daniel-mar | 169 | } |
653 | daniel-mar | 170 | } |
662 | daniel-mar | 171 | else { |
716 | daniel-mar | 172 | 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 | 173 | } |
1116 | daniel-mar | 174 | } else { |
175 | return parent::action($actionID, $params); |
||
635 | daniel-mar | 176 | } |
177 | } |
||
178 | |||
1116 | daniel-mar | 179 | /** |
180 | * @param string $id |
||
181 | * @param array $out |
||
182 | * @param bool $handled |
||
183 | * @return void |
||
184 | * @throws OIDplusException |
||
185 | */ |
||
186 | public function gui(string $id, array &$out, bool &$handled) { |
||
635 | daniel-mar | 187 | $parts = explode('.',$id,2); |
188 | if (!isset($parts[1])) $parts[1] = ''; |
||
189 | if ($parts[0] == 'oidplus:software_update') { |
||
190 | @set_time_limit(0); |
||
191 | |||
192 | $handled = true; |
||
193 | $out['title'] = _L('Software update'); |
||
801 | daniel-mar | 194 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png'; |
635 | daniel-mar | 195 | |
196 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 197 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 198 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>'; |
199 | return; |
||
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 |
||
635 | daniel-mar | 256 | } |
257 | |||
258 | $local_installation = OIDplus::getVersion(); |
||
648 | daniel-mar | 259 | $newest_version = $this->getLatestRevision(); |
635 | daniel-mar | 260 | |
261 | $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>'; |
||
662 | daniel-mar | 262 | $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br><br>'; |
635 | daniel-mar | 263 | |
264 | if (!$newest_version) { |
||
1061 | daniel-mar | 265 | if (!function_exists('curl_init')) { |
266 | $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version.').'<br>'._L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl').'</font></p>'; |
||
267 | } else { |
||
268 | $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>'; |
||
269 | } |
||
662 | daniel-mar | 270 | $out['text'] .= '</div>'; |
654 | daniel-mar | 271 | } else if (!$local_installation) { |
662 | daniel-mar | 272 | if ($installType === 'svn-snapshot') { |
273 | $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine its version.').'</font></p>'; |
||
274 | } else { |
||
275 | $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 | 276 | } |
647 | daniel-mar | 277 | $out['text'] .= '</div>'; |
997 | daniel-mar | 278 | } else if (version_compare($local_installation, $newest_version) >= 0) { |
635 | daniel-mar | 279 | $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>'; |
647 | daniel-mar | 280 | $out['text'] .= '</div>'; |
635 | daniel-mar | 281 | } else { |
662 | daniel-mar | 282 | if (($installType === 'svn-wc') || ($installType === 'git-wc')) { |
283 | $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>'; |
||
284 | $out['text'] .= '<p>'._L('Alternatively, click this button to execute the command through the web-interface (command execution and write permissions required).').'</p>'; |
||
285 | } |
||
635 | daniel-mar | 286 | |
648 | daniel-mar | 287 | $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 | 288 | |
662 | daniel-mar | 289 | // TODO: Open "system_file_check" without page reload. |
290 | // TODO: Only show link if the plugin is installed |
||
906 | daniel-mar | 291 | $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 | 292 | |
647 | daniel-mar | 293 | $out['text'] .= '</div>'; |
294 | |||
662 | daniel-mar | 295 | $out['text'] .= $this->showPreview($local_installation, $newest_version); |
635 | daniel-mar | 296 | } |
297 | } |
||
298 | } else { |
||
299 | $handled = false; |
||
300 | } |
||
301 | } |
||
302 | |||
1116 | daniel-mar | 303 | /** |
304 | * @param array $json |
||
305 | * @param string|null $ra_email |
||
306 | * @param bool $nonjs |
||
307 | * @param string $req_goto |
||
308 | * @return bool |
||
309 | * @throws OIDplusException |
||
310 | */ |
||
311 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
635 | daniel-mar | 312 | if (!OIDplus::authUtils()->isAdminLoggedIn()) return false; |
313 | |||
800 | daniel-mar | 314 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 315 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 316 | } else { |
317 | $tree_icon = null; // default icon (folder) |
||
318 | } |
||
319 | |||
320 | $json[] = array( |
||
321 | 'id' => 'oidplus:software_update', |
||
322 | 'icon' => $tree_icon, |
||
323 | 'text' => _L('Software update') |
||
324 | ); |
||
325 | |||
326 | return true; |
||
327 | } |
||
328 | |||
1116 | daniel-mar | 329 | /** |
330 | * @param string $request |
||
331 | * @return array|false |
||
332 | */ |
||
333 | public function tree_search(string $request) { |
||
635 | daniel-mar | 334 | return false; |
335 | } |
||
648 | daniel-mar | 336 | |
1116 | daniel-mar | 337 | /** |
338 | * @var null |
||
339 | */ |
||
648 | daniel-mar | 340 | private $releases_ser = null; |
341 | |||
1116 | daniel-mar | 342 | /** |
343 | * @param $local_ver |
||
344 | * @return false|string |
||
345 | */ |
||
648 | daniel-mar | 346 | private function showChangelog($local_ver) { |
347 | |||
348 | try { |
||
349 | if (is_null($this->releases_ser)) { |
||
716 | daniel-mar | 350 | if (function_exists('gzdecode')) { |
351 | $url = OIDplus::getEditionInfo()['revisionlog_gz']; |
||
352 | $cont = url_get_contents($url); |
||
353 | if ($cont !== false) $cont = @gzdecode($cont); |
||
354 | } else { |
||
355 | $url = OIDplus::getEditionInfo()['revisionlog']; |
||
356 | $cont = url_get_contents($url); |
||
357 | } |
||
648 | daniel-mar | 358 | if ($cont === false) return false; |
359 | $this->releases_ser = $cont; |
||
360 | } else { |
||
361 | $cont = $this->releases_ser; |
||
362 | } |
||
363 | $content = ''; |
||
364 | $ary = @unserialize($cont); |
||
365 | if ($ary === false) return false; |
||
366 | krsort($ary); |
||
367 | foreach ($ary as $rev => $data) { |
||
997 | daniel-mar | 368 | if (version_compare("svn-$rev", $local_ver) <= 0) continue; |
648 | daniel-mar | 369 | $comment = empty($data['msg']) ? _L('No comment') : $data['msg']; |
370 | $tex = _L("New revision %1 by %2",$rev,$data['author'])." (".$data['date'].") "; |
||
371 | $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment)); |
||
372 | $content .= "\n"; |
||
373 | } |
||
374 | return $content; |
||
1050 | daniel-mar | 375 | } catch (\Exception $e) { |
648 | daniel-mar | 376 | return false; |
377 | } |
||
378 | |||
379 | } |
||
380 | |||
1116 | daniel-mar | 381 | /** |
382 | * @return false|string |
||
383 | */ |
||
648 | daniel-mar | 384 | private function getLatestRevision() { |
385 | try { |
||
386 | if (is_null($this->releases_ser)) { |
||
716 | daniel-mar | 387 | if (function_exists('gzdecode')) { |
388 | $url = OIDplus::getEditionInfo()['revisionlog_gz']; |
||
389 | $cont = url_get_contents($url); |
||
390 | if ($cont !== false) $cont = @gzdecode($cont); |
||
391 | } else { |
||
392 | $url = OIDplus::getEditionInfo()['revisionlog']; |
||
393 | $cont = url_get_contents($url); |
||
394 | } |
||
648 | daniel-mar | 395 | if ($cont === false) return false; |
396 | $this->releases_ser = $cont; |
||
397 | } else { |
||
398 | $cont = $this->releases_ser; |
||
399 | } |
||
400 | $ary = @unserialize($cont); |
||
401 | if ($ary === false) return false; |
||
402 | krsort($ary); |
||
403 | $max_rev = array_keys($ary)[0]; |
||
404 | $newest_version = 'svn-' . $max_rev; |
||
405 | return $newest_version; |
||
1050 | daniel-mar | 406 | } catch (\Exception $e) { |
648 | daniel-mar | 407 | return false; |
408 | } |
||
409 | } |
||
662 | daniel-mar | 410 | |
1116 | daniel-mar | 411 | /** |
412 | * @param $local_installation |
||
413 | * @param $newest_version |
||
414 | * @return string |
||
415 | */ |
||
662 | daniel-mar | 416 | private function showPreview($local_installation, $newest_version) { |
417 | $out = '<h2 id="update_header">'._L('Preview of update %1 → %2',$local_installation,$newest_version).'</h2>'; |
||
418 | |||
419 | ob_start(); |
||
420 | try { |
||
421 | $cont = $this->showChangelog($local_installation); |
||
1050 | daniel-mar | 422 | } catch (\Exception $e) { |
662 | daniel-mar | 423 | $cont = _L('Error: %1',$e->getMessage()); |
424 | } |
||
425 | ob_end_clean(); |
||
426 | |||
831 | daniel-mar | 427 | $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", "$cont\n"); |
662 | daniel-mar | 428 | |
429 | $out .= '<pre id="update_infobox">'.$cont.'</pre>'; |
||
430 | |||
431 | return $out; |
||
432 | } |
||
1000 | daniel-mar | 433 | |
1116 | daniel-mar | 434 | /** |
435 | * @param string $id |
||
436 | * @return bool |
||
437 | */ |
||
438 | public function implementsFeature(string $id): bool { |
||
1000 | daniel-mar | 439 | if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications() |
440 | return false; |
||
441 | } |
||
442 | |||
1116 | daniel-mar | 443 | /** |
444 | * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.8 |
||
445 | * @param $user |
||
446 | * @return array |
||
447 | * @throws OIDplusException |
||
448 | */ |
||
1000 | daniel-mar | 449 | public function getNotifications($user=null): array { |
450 | $notifications = array(); |
||
451 | if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) { |
||
452 | |||
453 | // Following code is based on the VNag plugin (admin 901) code |
||
454 | |||
455 | $installType = OIDplus::getInstallType(); |
||
456 | |||
457 | if ($installType === 'ambigous') { |
||
458 | $out_stat = 'WARN'; |
||
459 | $out_msg = _L('Multiple version files/directories (oidplus_version.txt, .version.php, .git, or .svn) are existing! Therefore, the version is ambiguous!'); |
||
460 | } else if ($installType === 'unknown') { |
||
461 | $out_stat = 'WARN'; |
||
462 | $out_msg = _L('The version cannot be determined, and the update needs to be applied manually!'); |
||
463 | } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) { |
||
464 | $local_installation = OIDplus::getVersion(); |
||
465 | $newest_version = $this->getLatestRevision(); |
||
466 | |||
467 | $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client'); |
||
468 | $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull'; |
||
469 | |||
470 | if (!$newest_version) { |
||
471 | $out_stat = 'WARN'; |
||
1061 | daniel-mar | 472 | if (!function_exists('curl_init')) { |
473 | $out_msg = _L('OIDplus could not determine the latest version.').' '._L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'); |
||
474 | } else { |
||
475 | $out_msg = _L('OIDplus could not determine the latest version.').' '._L('Probably the ViaThinkSoft server could not be reached.'); |
||
476 | } |
||
1000 | daniel-mar | 477 | } else if (!$local_installation) { |
478 | $out_stat = 'WARN'; |
||
479 | $out_msg = _L('OIDplus could not determine its version (Required: %1). Please update your system manually via the "%2" command regularly.', $requireInfo, $updateCommand); |
||
480 | } else if (version_compare($local_installation, $newest_version) >= 0) { |
||
481 | $out_stat = 'INFO'; |
||
482 | $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version); |
||
483 | } else { |
||
484 | $out_stat = 'WARN'; |
||
485 | $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version); |
||
486 | } |
||
487 | } else if ($installType === 'svn-snapshot') { |
||
488 | $local_installation = OIDplus::getVersion(); |
||
489 | $newest_version = $this->getLatestRevision(); |
||
490 | |||
491 | if (!$newest_version) { |
||
492 | $out_stat = 'WARN'; |
||
1061 | daniel-mar | 493 | if (!function_exists('curl_init')) { |
494 | $out_msg = _L('OIDplus could not determine the latest version.').' '._L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'); |
||
495 | } else { |
||
496 | $out_msg = _L('OIDplus could not determine the latest version.').' '._L('Probably the ViaThinkSoft server could not be reached.'); |
||
497 | } |
||
1000 | daniel-mar | 498 | } else if (version_compare($local_installation, $newest_version) >= 0) { |
499 | $out_stat = 'INFO'; |
||
500 | $out_msg = _L('You are using the latest version of OIDplus (%1 local / %2 remote)', $local_installation, $newest_version); |
||
501 | } else { |
||
502 | $out_stat = 'WARN'; |
||
503 | $out_msg = _L('OIDplus is outdated. (%1 local / %2 remote)', $local_installation, $newest_version); |
||
504 | } |
||
505 | } else { |
||
506 | assert(false); |
||
507 | return $notifications; |
||
508 | } |
||
509 | |||
510 | if ($out_stat != 'INFO') { |
||
511 | $out_msg = '<a '.OIDplus::gui()->link('oidplus:software_update').'>'._L('Software update').'</a>: ' . $out_msg; |
||
512 | |||
513 | $notifications[] = array($out_stat, $out_msg); |
||
514 | } |
||
515 | |||
516 | } |
||
517 | return $notifications; |
||
518 | } |
||
519 | |||
661 | daniel-mar | 520 | } |