134aae6dbSAndreas Gohr<?php 28dab6045SAndreas Gohr 334aae6dbSAndreas Gohr/** 4d391262fSAndreas Gohr * DokuWiki Plugin upgrade (Admin Component) 534aae6dbSAndreas Gohr * 634aae6dbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 734aae6dbSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 834aae6dbSAndreas Gohr */ 934aae6dbSAndreas Gohr 10*ab8e5256SAndreas Gohrrequire_once __DIR__ . '/myvendor/autoload.php'; 1134aae6dbSAndreas Gohr 124e3e87e4SAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin 134e3e87e4SAndreas Gohr{ 144ed3677eSAndreas Gohr protected $haderrors = false; 154ed3677eSAndreas Gohr 164e3e87e4SAndreas Gohr /** @var helper_plugin_upgrade */ 174e3e87e4SAndreas Gohr protected $helper; 1834aae6dbSAndreas Gohr 194e3e87e4SAndreas Gohr /** 204e3e87e4SAndreas Gohr * initialize helper 214e3e87e4SAndreas Gohr */ 224e3e87e4SAndreas Gohr public function __construct() 234e3e87e4SAndreas Gohr { 244e3e87e4SAndreas Gohr $this->helper = plugin_load('helper', 'upgrade'); 254e3e87e4SAndreas Gohr $this->helper->setLogger($this); 2634aae6dbSAndreas Gohr } 2734aae6dbSAndreas Gohr 284e3e87e4SAndreas Gohr /** @inheritDoc */ 294e3e87e4SAndreas Gohr public function getMenuSort() 304e3e87e4SAndreas Gohr { 31bd08ebd1SAndreas Gohr return 555; 32bd08ebd1SAndreas Gohr } 3334aae6dbSAndreas Gohr 344e3e87e4SAndreas Gohr /** @inheritDoc */ 354e3e87e4SAndreas Gohr public function handle() 364e3e87e4SAndreas Gohr { 3733bed757SAndreas Gohr if (!empty($_REQUEST['step']) && !checkSecurityToken()) { 3841163f44SAndreas Gohr unset($_REQUEST['step']); 3941163f44SAndreas Gohr } 4034aae6dbSAndreas Gohr } 4134aae6dbSAndreas Gohr 424e3e87e4SAndreas Gohr public function html() 434e3e87e4SAndreas Gohr { 4434aae6dbSAndreas Gohr $abrt = false; 4534aae6dbSAndreas Gohr $next = false; 464e3e87e4SAndreas Gohr $ok = true; 4734aae6dbSAndreas Gohr 4834aae6dbSAndreas Gohr echo '<h1>' . $this->getLang('menu') . '</h1>'; 4934aae6dbSAndreas Gohr 504e3e87e4SAndreas Gohr echo '<div id="plugin__upgrade">'; 5175e9d164SAndreas Gohr // enable auto scroll 5275e9d164SAndreas Gohr ?> 534e3e87e4SAndreas Gohr <script type="text/javascript"> 54d391262fSAndreas Gohr var plugin_upgrade = window.setInterval(function () { 55d4376367SAndreas Gohr var obj = document.getElementById('plugin__upgrade'); 5675e9d164SAndreas Gohr if (obj) obj.scrollTop = obj.scrollHeight; 5775e9d164SAndreas Gohr }, 25); 5875e9d164SAndreas Gohr </script> 5975e9d164SAndreas Gohr <?php 6075e9d164SAndreas Gohr 6175e9d164SAndreas Gohr // handle current step 624e3e87e4SAndreas Gohr $this->nextStep($abrt, $next, $ok); 6334aae6dbSAndreas Gohr 6475e9d164SAndreas Gohr // disable auto scroll 6575e9d164SAndreas Gohr ?> 664e3e87e4SAndreas Gohr <script type="text/javascript"> 6775e9d164SAndreas Gohr window.setTimeout(function () { 68d391262fSAndreas Gohr window.clearInterval(plugin_upgrade); 6975e9d164SAndreas Gohr }, 50); 7075e9d164SAndreas Gohr </script> 7175e9d164SAndreas Gohr <?php 724e3e87e4SAndreas Gohr echo '</div>'; 7375e9d164SAndreas Gohr 744ed3677eSAndreas Gohr $careful = ''; 754e3e87e4SAndreas Gohr if (!$ok) { 764ed3677eSAndreas Gohr echo '<div id="plugin__upgrade_careful">' . $this->getLang('careful') . '</div>'; 774ed3677eSAndreas Gohr $careful = 'careful'; 784ed3677eSAndreas Gohr } 794ed3677eSAndreas Gohr 809f8d4355SAndreas Gohr $action = script(); 819f8d4355SAndreas Gohr echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">'; 8234aae6dbSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 83d391262fSAndreas Gohr echo '<input type="hidden" name="page" value="upgrade" />'; 8441163f44SAndreas Gohr echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 858dab6045SAndreas Gohr if ($next) { 868dab6045SAndreas Gohr echo '<button type="submit" 878dab6045SAndreas Gohr name="step[' . $next . ']" 888dab6045SAndreas Gohr value="1" 898dab6045SAndreas Gohr class="button continue ' . $careful . '">' . 908dab6045SAndreas Gohr $this->getLang('btn_continue') . 918dab6045SAndreas Gohr ' ➡</button>'; 928dab6045SAndreas Gohr } 938dab6045SAndreas Gohr if ($abrt) { 948dab6045SAndreas Gohr echo '<button type="submit" 958dab6045SAndreas Gohr name="step[cancel]" 968dab6045SAndreas Gohr value="1" 978dab6045SAndreas Gohr class="button abort">✖ ' . 988dab6045SAndreas Gohr $this->getLang('btn_abort') . 998dab6045SAndreas Gohr '</button>'; 1008dab6045SAndreas Gohr } 10134aae6dbSAndreas Gohr echo '</form>'; 102eb52c46aSAndreas Gohr 1034e3e87e4SAndreas Gohr $this->displayProgressBar($next); 104eb52c46aSAndreas Gohr } 105eb52c46aSAndreas Gohr 106eb52c46aSAndreas Gohr /** 107eb52c46aSAndreas Gohr * Display a progress bar of all steps 108eb52c46aSAndreas Gohr * 109eb52c46aSAndreas Gohr * @param string $next the next step 110eb52c46aSAndreas Gohr */ 1114e3e87e4SAndreas Gohr private function displayProgressBar($next) 1124e3e87e4SAndreas Gohr { 1134e3e87e4SAndreas Gohr $steps = ['version', 'download', 'unpack', 'check', 'upgrade']; 114eb52c46aSAndreas Gohr $active = true; 115eb52c46aSAndreas Gohr $count = 0; 116eb52c46aSAndreas Gohr 117eb52c46aSAndreas Gohr echo '<div id="plugin__upgrade_meter"><ol>'; 118eb52c46aSAndreas Gohr foreach ($steps as $step) { 119eb52c46aSAndreas Gohr $count++; 120eb52c46aSAndreas Gohr if ($step == $next) $active = false; 121eb52c46aSAndreas Gohr if ($active) { 122eb52c46aSAndreas Gohr echo '<li class="active">'; 123eb52c46aSAndreas Gohr echo '<span class="step">✔</span>'; 124eb52c46aSAndreas Gohr } else { 125eb52c46aSAndreas Gohr echo '<li>'; 126eb52c46aSAndreas Gohr echo '<span class="step">' . $count . '</span>'; 127eb52c46aSAndreas Gohr } 128eb52c46aSAndreas Gohr 129eb52c46aSAndreas Gohr echo '<span class="stage">' . $this->getLang('step_' . $step) . '</span>'; 130eb52c46aSAndreas Gohr echo '</li>'; 131eb52c46aSAndreas Gohr } 132eb52c46aSAndreas Gohr echo '</ol></div>'; 13334aae6dbSAndreas Gohr } 13434aae6dbSAndreas Gohr 135bd08ebd1SAndreas Gohr /** 136bd08ebd1SAndreas Gohr * Decides the current step and executes it 137bd08ebd1SAndreas Gohr * 138bd08ebd1SAndreas Gohr * @param bool $abrt 139bd08ebd1SAndreas Gohr * @param bool $next 1404e3e87e4SAndreas Gohr * @param bool $ok 141bd08ebd1SAndreas Gohr */ 1424e3e87e4SAndreas Gohr private function nextStep(&$abrt, &$next, &$ok) 1434e3e87e4SAndreas Gohr { 1444bed5591SAndreas Gohr 14534aae6dbSAndreas Gohr if (isset($_REQUEST['step']) && is_array($_REQUEST['step'])) { 1469e48b5c0SAndreas Gohr $keys = array_keys($_REQUEST['step']); 1479e48b5c0SAndreas Gohr $step = array_shift($keys); 14834aae6dbSAndreas Gohr } else { 14934aae6dbSAndreas Gohr $step = ''; 15034aae6dbSAndreas Gohr } 15134aae6dbSAndreas Gohr 152fb8e77caSAndreas Gohr if ($step == 'cancel' || $step == 'done') { 1534e3e87e4SAndreas Gohr $ok = $this->helper->cleanup(); 154fb8e77caSAndreas Gohr if ($step == 'cancel') $step = ''; 15534aae6dbSAndreas Gohr } 15634aae6dbSAndreas Gohr 15734aae6dbSAndreas Gohr if ($step) { 15834aae6dbSAndreas Gohr $abrt = true; 15934aae6dbSAndreas Gohr $next = false; 160f378cb7eSAndreas Gohr if ($step == 'version') { 1614e3e87e4SAndreas Gohr $ok = $this->helper->checkVersions(); 162f378cb7eSAndreas Gohr $next = 'download'; 163fb8e77caSAndreas Gohr } elseif ($step == 'done') { 1644e3e87e4SAndreas Gohr echo $this->locale_xhtml('final'); 165fb8e77caSAndreas Gohr $next = ''; 166fb8e77caSAndreas Gohr $abrt = ''; 1674e3e87e4SAndreas Gohr } elseif (!file_exists($this->helper->tgzfile)) { 1684e3e87e4SAndreas Gohr if ($ok = $this->helper->downloadTarball()) $next = 'unpack'; 1694e3e87e4SAndreas Gohr } elseif (!is_dir($this->helper->tgzdir)) { 1704e3e87e4SAndreas Gohr if ($ok = $this->helper->extractTarball()) $next = 'check'; 17134aae6dbSAndreas Gohr } elseif ($step != 'upgrade') { 1724e3e87e4SAndreas Gohr if ($ok = $this->helper->checkPermissions()) $next = 'upgrade'; 17334aae6dbSAndreas Gohr } elseif ($step == 'upgrade') { 1744e3e87e4SAndreas Gohr if ($ok = $this->helper->copyFiles()) { 1754e3e87e4SAndreas Gohr $ok = $this->helper->deleteObsoleteFiles(); 1764e3e87e4SAndreas Gohr $this->helper->cleanup(); 177fb8e77caSAndreas Gohr $next = 'done'; 178fb8e77caSAndreas Gohr $abrt = ''; 179fb8e77caSAndreas Gohr } 18034aae6dbSAndreas Gohr } else { 18175e9d164SAndreas Gohr echo 'uhm. what happened? where am I? This should not happen'; 18234aae6dbSAndreas Gohr } 18334aae6dbSAndreas Gohr } else { 18434aae6dbSAndreas Gohr # first time run, show intro 18534aae6dbSAndreas Gohr echo $this->locale_xhtml('step0'); 1860aa25059SAndreas Gohr $this->helper->cleanup(); // make sure we start clean 18734aae6dbSAndreas Gohr $abrt = false; 188f378cb7eSAndreas Gohr $next = 'version'; 18934aae6dbSAndreas Gohr } 19034aae6dbSAndreas Gohr } 19134aae6dbSAndreas Gohr 192bd08ebd1SAndreas Gohr /** 1934e3e87e4SAndreas Gohr * Print the given message and flush buffers 1944e3e87e4SAndreas Gohr * 1954e3e87e4SAndreas Gohr * @param string $level 1964e3e87e4SAndreas Gohr * @param string $message 197bd08ebd1SAndreas Gohr */ 1988dab6045SAndreas Gohr public function log($level, $message) 1998dab6045SAndreas Gohr { 2004e3e87e4SAndreas Gohr echo '<div class="log-' . $level . '">' . $message . '</div>'; 2016e7afd39SAndreas Gohr flush(); 2026e7afd39SAndreas Gohr ob_flush(); 2036e7afd39SAndreas Gohr } 20434aae6dbSAndreas Gohr} 205