Subversion Repositories oidplus

Rev

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