Subversion Repositories oidplus

Rev

Rev 647 | 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); // TODO: what to do if the server does not accept it?
  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.                         ob_start();
  36.                         $error = "";
  37.                         try {
  38.                                 $svn = new phpsvnclient(parse_ini_file(__DIR__.'/consts.ini')['svn']);
  39.  
  40.                                 // We are caching the changed file logs here only in the preview mode.
  41.                                 // Reason: We want to avoid that the "update/" page becomes an
  42.                                 // DoS attack vector if there hasn't been an update for a long time,
  43.                                 // and the list is very large.
  44.                                 // But we don't want to use cache in the real update, because
  45.                                 // otherwise it might break the system if an update is made
  46.                                 // while the ViaThinkSoft server is down (because the file list
  47.                                 // is cached, and therefore "delete" actions can be made, while
  48.                                 // adding/downloading does not work)
  49.                                 $svn->use_cache = false;
  50.  
  51.                                 if (!$svn->updateWorkingCopy(OIDplus::localpath().'oidplus_version.txt', '/trunk', OIDplus::localpath(), false)) {
  52.                                         $error = _L("Some updates failed. Please see details in the update protocol.");
  53.                                 }
  54.                         } catch (Exception $e) {
  55.                                 $error = $e->getMessage();
  56.                         }
  57.                         $cont = ob_get_contents();
  58.                         $cont = str_replace(OIDplus::localpath(), '...', $cont);
  59.                         ob_end_clean();
  60.  
  61.                         if ($error != "") {
  62.                                 return array("status" => -1, "error" => $error, "content" => $cont);
  63.                         } else {
  64.                                 return array("status" => 0, "content" => $cont);
  65.                         }
  66.                 }
  67.         }
  68.  
  69.         public function gui($id, &$out, &$handled) {
  70.                 $parts = explode('.',$id,2);
  71.                 if (!isset($parts[1])) $parts[1] = '';
  72.                 if ($parts[0] == 'oidplus:software_update') {
  73.                         @set_time_limit(0);
  74.  
  75.                         $handled = true;
  76.                         $out['title'] = _L('Software update');
  77.                         $out['icon']  = OIDplus::webpath(__DIR__).'icon_big.png';
  78.  
  79.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  80.                                 $out['icon'] = 'img/error_big.png';
  81.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  82.                                 return;
  83.                         }
  84.  
  85.                         $out['text'] .= '<p><u>'._L('There are three possibilities how to keep OIDplus up-to-date').':</u></p>';
  86.  
  87.                         $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>';
  88.  
  89.                         $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>';
  90.  
  91.                         $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>';
  92.  
  93.                         $out['text'] .= '<hr>';
  94.  
  95.                         $installType = OIDplus::getInstallType();
  96.  
  97.                         if ($installType === 'ambigous') {
  98.                                 $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>';
  99.                         } else if ($installType === 'unknown') {
  100.                                 $out['text'] .= '<font color="red">'.strtoupper(_L('Error')).': '._L('The version cannot be determined, and the update needs to be applied manually!').'</font>';
  101.                         } else if (($installType === 'svn-wc') || ($installType === 'git-wc')) {
  102.                                 if ($installType === 'svn-wc') {
  103.                                         $out['text'] .= '<p>'._L('You are using <b>method A</b> (SVN working copy).').'</p>';
  104.                                 } else {
  105.                                         $out['text'] .= '<p>'._L('You are using <b>method B</b> (Git working copy).').'</p>';
  106.                                 }
  107.  
  108.                                 $local_installation = OIDplus::getVersion();
  109.                                 try {
  110.                                         $svn = new phpsvnclient(parse_ini_file(__DIR__.'/consts.ini')['svn']);
  111.                                         $newest_version = 'svn-'.$svn->getVersion();
  112.                                 } catch (Exception $e) {
  113.                                         $newest_version = false;
  114.                                 }
  115.  
  116.                                 $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
  117.                                 $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
  118.  
  119.                                 $requireInfo = ($installType === 'svn-wc') ? _L('shell access with svn/svnversion tool, or PDO/SQLite3 PHP extension') : _L('shell access with Git client');
  120.                                 $updateCommand = ($installType === 'svn-wc') ? 'svn update' : 'git pull';
  121.  
  122.                                 if (!$newest_version) {
  123.                                         $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
  124.                                 }
  125.                                 else if (!$local_installation) {
  126.                                         $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>';
  127.                                 } else if ($local_installation == $newest_version) {
  128.                                         $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
  129.                                 } else {
  130.                                         $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>';
  131.  
  132.                                         $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
  133.  
  134.                                         ob_start();
  135.                                         try {
  136.                                                 $svn = new phpsvnclient(parse_ini_file(__DIR__.'/consts.ini')['svn']);
  137.                                                 $svn->use_cache = true;
  138.                                                 $svn->updateWorkingCopy(str_replace('svn-', '', $local_installation), '/trunk', OIDplus::localpath(), true);
  139.                                                 $cont = ob_get_contents();
  140.                                                 $cont = str_replace(OIDplus::localpath(), '...', $cont);
  141.                                         } catch (Exception $e) {
  142.                                                 $cont = _L('Error: %1',$e->getMessage());
  143.                                         }
  144.                                         ob_end_clean();
  145.  
  146.                                         $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
  147.  
  148.                                         $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
  149.                                 }
  150.                         } else if ($installType === 'svn-snapshot') {
  151.                                 $out['text'] .= '<p>'._L('You are using <b>method C</b> (Snapshot TAR.GZ file with oidplus_version.txt file).').'</p>';
  152.  
  153.                                 $local_installation = OIDplus::getVersion();
  154.                                 try {
  155.                                         $svn = new phpsvnclient(parse_ini_file(__DIR__.'/consts.ini')['svn']);
  156.                                         $newest_version = 'svn-'.$svn->getVersion();
  157.                                 } catch (Exception $e) {
  158.                                         $newest_version = false;
  159.                                 }
  160.  
  161.                                 $out['text'] .= _L('Local installation: %1',($local_installation ? $local_installation : _L('unknown'))).'<br>';
  162.                                 $out['text'] .= _L('Latest published version: %1',($newest_version ? $newest_version : _L('unknown'))).'<br>';
  163.  
  164.                                 if (!$newest_version) {
  165.                                         $out['text'] .= '<p><font color="red">'._L('OIDplus could not determine the latest version. Probably the ViaThinkSoft server could not be reached.').'</font></p>';
  166.                                 }
  167.                                 else if ($local_installation == $newest_version) {
  168.                                         $out['text'] .= '<p><font color="green">'._L('You are already using the latest version of OIDplus.').'</font></p>';
  169.                                 } else {
  170.                                         $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>';
  171.                                         $out['text'] .= '<form method="POST" action="index.php">';
  172.  
  173.                                         $out['text'] .= '<p><input type="button" onclick="OIDplusPageAdminSoftwareUpdate.doUpdateOIDplus()" value="'._L('Update NOW').'"></p>';
  174.  
  175.                                         $out['text'] .= '<h2 id="update_header">'._L('Preview of update %1 &rarr; %2',$local_installation,$newest_version).'</h2>';
  176.  
  177.                                         ob_start();
  178.                                         try {
  179.                                                 $svn = new phpsvnclient(parse_ini_file(__DIR__.'/consts.ini')['svn']);
  180.                                                 $svn->use_cache = true;
  181.                                                 $svn->updateWorkingCopy(OIDplus::localpath().'/oidplus_version.txt', '/trunk', OIDplus::localpath(), true);
  182.                                                 $cont = ob_get_contents();
  183.                                                 $cont = str_replace(OIDplus::localpath(), '...', $cont);
  184.                                         } catch (Exception $e) {
  185.                                                 $cont = _L('Error: %1',$e->getMessage());
  186.                                         }
  187.                                         ob_end_clean();
  188.  
  189.                                         $cont = preg_replace('@!!!(.+)\\n@', '<font color="red">!!!\\1</font>'."\n", $cont);
  190.  
  191.                                         $out['text'] .= '<pre id="update_infobox">'.$cont.'</pre>';
  192.                                 }
  193.                         }
  194.                 } else {
  195.                         $handled = false;
  196.                 }
  197.         }
  198.  
  199.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  200.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  201.  
  202.                 if (file_exists(__DIR__.'/treeicon.png')) {
  203.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  204.                 } else {
  205.                         $tree_icon = null; // default icon (folder)
  206.                 }
  207.  
  208.                 $json[] = array(
  209.                         'id' => 'oidplus:software_update',
  210.                         'icon' => $tree_icon,
  211.                         'text' => _L('Software update')
  212.                 );
  213.  
  214.                 return true;
  215.         }
  216.  
  217.         public function tree_search($request) {
  218.                 return false;
  219.         }
  220. }
  221.