Subversion Repositories oidplus

Rev

Rev 647 | Rev 650 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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') {
  29.                         @set_time_limit(0);
  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.  
  35.                         $rev = $params['rev'];
  36.  
  37.                         $url = "https://www.oidplus.com/updates/update_".($rev-1)."_to_".($rev).".txt"; // TODO: in consts.ini
  38.                         $cont = @file_get_contents($url);
  39.  
  40.                         if ($cont === false) throw new OIDplusException(_L("Update could not be downloaded from ViaThinkSoft server. Please try again later."));
  41.                         file_put_contents(OIDplus::localpath().'update.tmp.php', $cont);
  42.  
  43.                         # TODO: instead use cURL?
  44.                         // Note: we may not use eval() because script uses die()
  45.                         $cont = @file_get_contents(OIDplus::webpath().'update.tmp.php');
  46.                         if ($cont === false) throw new OIDplusException(_L("Failed to execute update-script. Probably file_get_contents() may not open URLs!"));
  47.  
  48.                         return array("status" => 0, "content" => $cont);
  49.                 }
  50.         }
  51.  
  52.         public function gui($id, &$out, &$handled) {
  53.                 $parts = explode('.',$id,2);
  54.                 if (!isset($parts[1])) $parts[1] = '';
  55.                 if ($parts[0] == 'oidplus:software_update') {
  56.                         @set_time_limit(0);
  57.  
  58.                         $handled = true;
  59.                         $out['title'] = _L('Software update');
  60.                         $out['icon']  = OIDplus::webpath(__DIR__).'icon_big.png';
  61.  
  62.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  63.                                 $out['icon'] = 'img/error_big.png';
  64.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  65.                                 return;
  66.                         }
  67.  
  68.                         $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>';
  69.  
  70.                         $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>';
  71.  
  72.                         $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>';
  73.  
  74.                         $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 "oidplus_version.txt" 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>';
  75.  
  76.                         $out['text'] .= '<hr>';
  77.  
  78.                         $installType = OIDplus::getInstallType();
  79.  
  80.                         if ($installType === 'ambigous') {
  81.                                 $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('Multiple version files/directories (oidplus_version.txt, .git and .svn) are existing! Therefore, the version is ambiguous!').'</font>';
  82.                         } else if ($installType === 'unknown') {
  83.                                 $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('The version cannot be determined, and the update needs to be applied manually!').'</font>';
  84.                         } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
  85.                                 if ($installType === 'svn-wc') {
  86.                                         $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>';
  87.                                 } else {
  88.                                         $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>';
  89.                                 }
  90.  
  91.                                 $local_installation = OIDplus::getVersion();
  92.                                 $newest_version = $this->getLatestRevision();
  93.  
  94.                                 $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
  95.                                 $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
  96.  
  97.                                 $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client');
  98.                                 $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
  99.  
  100.                                 if (!$newest_version) {
  101.                                         $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
  102.                                 }
  103.                                 else if (!$local_installation) {
  104.                                         $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>';
  105.                                 } else if ($local_installation == $newest_version) {
  106.                                         $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
  107.                                 } else {
  108.                                         $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>';
  109.  
  110.                                         $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
  111.  
  112.                                         ob_start();
  113.                                         try {
  114.                                                 $cont = $this->showChangelog($local_installation);
  115.                                         } catch (Exception $e) {
  116.                                                 $cont = _L('Error: %1',$e->getMessage());
  117.                                         }
  118.                                         ob_end_clean();
  119.  
  120.                                         $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
  121.  
  122.                                         $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
  123.                                 }
  124.                         } else if ($installType === 'svn-snapshot') {
  125.                                 $out['text'] .= '<div id="update_versioninfo">';
  126.  
  127.                                 $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot TAR.GZ file with oidplus_version.txt file).').'</p>';
  128.  
  129.                                 $local_installation = OIDplus::getVersion();
  130.                                 $newest_version = $this->getLatestRevision();
  131.  
  132.                                 $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
  133.                                 $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
  134.  
  135.                                 if (!$newest_version) {
  136.                                         $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
  137.                                         $out['text'] .= '</div>';
  138.                                 }
  139.                                 else if ($local_installation == $newest_version) {
  140.                                         $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
  141.                                         $out['text'] .= '</div>';
  142.                                 } else {
  143.                                         $out['text'] .= '<p><font color="red">'.strtoupper(_L('Warning')).': '._L('Please make a backup of your files before updating. In case of an error, the OIDplus system (including this update-assistant) might become unavailable. Also, since the web-update does not contain collision-detection, changes you have applied (like adding, removing or modified files) might get reverted/lost! In case the update fails, you can download and extract the complete <a href="https://www.viathinksoft.com/projects/oidplus">SVN-Snapshot TAR.GZ file</a> again. Since all your data should lay inside the folder "userdata" and "userdata_pub", this should be safe.').'</font></p>';
  144.                                         $out['text'] .= '<form method="POST" action="index.php">';
  145.  
  146.                                         $out['text'] .= '<p><input type="button" onclick="OIDplusPageAdminSoftwareUpdate.doUpdateOIDplus('.((int)substr($local_installation,4)+1).', '.substr($newest_version,4).')" value="'._L('Update NOW').'"></p>';
  147.  
  148.                                         $out['text'] .= '</div>';
  149.  
  150.                                         $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
  151.  
  152.                                         ob_start();
  153.                                         try {
  154.                                                 $cont = $this->showChangelog($local_installation);
  155.                                         } catch (Exception $e) {
  156.                                                 $cont = _L('Error: %1',$e->getMessage());
  157.                                         }
  158.                                         ob_end_clean();
  159.  
  160.                                         $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
  161.  
  162.                                         $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
  163.                                 }
  164.                         }
  165.                 } else {
  166.                         $handled = false;
  167.                 }
  168.         }
  169.  
  170.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  171.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  172.  
  173.                 if (file_exists(__DIR__.'/treeicon.png')) {
  174.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  175.                 } else {
  176.                         $tree_icon = null; // default icon (folder)
  177.                 }
  178.  
  179.                 $json[] = array(
  180.                         'id' => 'oidplus:software_update',
  181.                         'icon' => $tree_icon,
  182.                         'text' => _L('Software update')
  183.                 );
  184.  
  185.                 return true;
  186.         }
  187.  
  188.         public function tree_search($request) {
  189.                 return false;
  190.         }
  191.  
  192.         private $releases_ser = null;
  193.  
  194.         private function showChangelog($local_ver) {
  195.  
  196.                 try {
  197.                         if (is_null($this->releases_ser)) {
  198.                                 $url = "https://www.oidplus.com/updates/releases.ser"; // TODO: in consts.ini
  199.                                 $cont = @file_get_contents($url);
  200.                                 if ($cont === false) return false;
  201.                                 $this->releases_ser = $cont;
  202.                         } else {
  203.                                 $cont = $this->releases_ser;
  204.                         }
  205.                         $content = '';
  206.                         $ary = @unserialize($cont);
  207.                         if ($ary === false) return false;
  208.                         krsort($ary);
  209.                         foreach ($ary as $rev => $data) {
  210.                                 if ($rev <= substr($local_ver,4)) continue;
  211.                                 $comment = empty($data['msg']) ? _L('No comment') : $data['msg'];
  212.                                 $tex = _L("New revision %1 by %2",$rev,$data['author'])." (".$data['date'].") ";
  213.                                 $content .= trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
  214.                                 $content .= "\n";
  215.                         }
  216.                         return $content;
  217.                 } catch (Exception $e) {
  218.                         return false;
  219.                 }
  220.  
  221.         }
  222.  
  223.         private function getLatestRevision() {
  224.                 try {
  225.                         if (is_null($this->releases_ser)) {
  226.                                 $url = "https://www.oidplus.com/updates/releases.ser"; // TODO: in consts.ini
  227.                                 $cont = @file_get_contents($url);
  228.                                 if ($cont === false) return false;
  229.                                 $this->releases_ser = $cont;
  230.                         } else {
  231.                                 $cont = $this->releases_ser;
  232.                         }
  233.                         $ary = @unserialize($cont);
  234.                         if ($ary === false) return false;
  235.                         krsort($ary);
  236.                         $max_rev = array_keys($ary)[0];
  237.                         $newest_version = 'svn-' . $max_rev;
  238.                         return $newest_version;
  239.                 } catch (Exception $e) {
  240.                         return false;
  241.                 }
  242.         }
  243. }
  244.