134aae6dbSAndreas Gohr<?php 234aae6dbSAndreas Gohr/** 3d391262fSAndreas Gohr * DokuWiki Plugin upgrade (Admin Component) 434aae6dbSAndreas Gohr * 534aae6dbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 634aae6dbSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 734aae6dbSAndreas Gohr */ 834aae6dbSAndreas Gohr 9*4e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 1034aae6dbSAndreas Gohr 11*4e3e87e4SAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin 12*4e3e87e4SAndreas Gohr{ 134ed3677eSAndreas Gohr protected $haderrors = false; 144ed3677eSAndreas Gohr 15*4e3e87e4SAndreas Gohr /** @var helper_plugin_upgrade */ 16*4e3e87e4SAndreas Gohr protected $helper; 1734aae6dbSAndreas Gohr 18*4e3e87e4SAndreas Gohr /** 19*4e3e87e4SAndreas Gohr * initialize helper 20*4e3e87e4SAndreas Gohr */ 21*4e3e87e4SAndreas Gohr public function __construct() 22*4e3e87e4SAndreas Gohr { 23*4e3e87e4SAndreas Gohr $this->helper = plugin_load('helper', 'upgrade'); 24*4e3e87e4SAndreas Gohr $this->helper->setLogger($this); 2534aae6dbSAndreas Gohr } 2634aae6dbSAndreas Gohr 27*4e3e87e4SAndreas Gohr /** @inheritDoc */ 28*4e3e87e4SAndreas Gohr public function getMenuSort() 29*4e3e87e4SAndreas Gohr { 30bd08ebd1SAndreas Gohr return 555; 31bd08ebd1SAndreas Gohr } 3234aae6dbSAndreas Gohr 33*4e3e87e4SAndreas Gohr /** @inheritDoc */ 34*4e3e87e4SAndreas Gohr public function handle() 35*4e3e87e4SAndreas Gohr { 3633bed757SAndreas Gohr if (!empty($_REQUEST['step']) && !checkSecurityToken()) { 3741163f44SAndreas Gohr unset($_REQUEST['step']); 3841163f44SAndreas Gohr } 3934aae6dbSAndreas Gohr } 4034aae6dbSAndreas Gohr 41*4e3e87e4SAndreas Gohr public function html() 42*4e3e87e4SAndreas Gohr { 4334aae6dbSAndreas Gohr $abrt = false; 4434aae6dbSAndreas Gohr $next = false; 45*4e3e87e4SAndreas Gohr $ok = true; 4634aae6dbSAndreas Gohr 4734aae6dbSAndreas Gohr echo '<h1>' . $this->getLang('menu') . '</h1>'; 4834aae6dbSAndreas Gohr 49*4e3e87e4SAndreas Gohr echo '<div id="plugin__upgrade">'; 5075e9d164SAndreas Gohr // enable auto scroll 5175e9d164SAndreas Gohr ?> 52*4e3e87e4SAndreas Gohr <script type="text/javascript"> 53d391262fSAndreas Gohr var plugin_upgrade = window.setInterval(function () { 54d4376367SAndreas Gohr var obj = document.getElementById('plugin__upgrade'); 5575e9d164SAndreas Gohr if (obj) obj.scrollTop = obj.scrollHeight; 5675e9d164SAndreas Gohr }, 25); 5775e9d164SAndreas Gohr </script> 5875e9d164SAndreas Gohr <?php 5975e9d164SAndreas Gohr 6075e9d164SAndreas Gohr // handle current step 61*4e3e87e4SAndreas Gohr $this->nextStep($abrt, $next, $ok); 6234aae6dbSAndreas Gohr 6375e9d164SAndreas Gohr // disable auto scroll 6475e9d164SAndreas Gohr ?> 65*4e3e87e4SAndreas Gohr <script type="text/javascript"> 6675e9d164SAndreas Gohr window.setTimeout(function () { 67d391262fSAndreas Gohr window.clearInterval(plugin_upgrade); 6875e9d164SAndreas Gohr }, 50); 6975e9d164SAndreas Gohr </script> 7075e9d164SAndreas Gohr <?php 71*4e3e87e4SAndreas Gohr echo '</div>'; 7275e9d164SAndreas Gohr 734ed3677eSAndreas Gohr $careful = ''; 74*4e3e87e4SAndreas Gohr if (!$ok) { 754ed3677eSAndreas Gohr echo '<div id="plugin__upgrade_careful">' . $this->getLang('careful') . '</div>'; 764ed3677eSAndreas Gohr $careful = 'careful'; 774ed3677eSAndreas Gohr } 784ed3677eSAndreas Gohr 799f8d4355SAndreas Gohr $action = script(); 809f8d4355SAndreas Gohr echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">'; 8134aae6dbSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 82d391262fSAndreas Gohr echo '<input type="hidden" name="page" value="upgrade" />'; 8341163f44SAndreas Gohr echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 8438665ab9SAndreas Gohr if ($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue ' . $careful . '">' . $this->getLang('btn_continue') . ' ➡</button>'; 859f8d4355SAndreas Gohr if ($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>'; 8634aae6dbSAndreas Gohr echo '</form>'; 87eb52c46aSAndreas Gohr 88*4e3e87e4SAndreas Gohr $this->displayProgressBar($next); 89eb52c46aSAndreas Gohr } 90eb52c46aSAndreas Gohr 91eb52c46aSAndreas Gohr /** 92eb52c46aSAndreas Gohr * Display a progress bar of all steps 93eb52c46aSAndreas Gohr * 94eb52c46aSAndreas Gohr * @param string $next the next step 95eb52c46aSAndreas Gohr */ 96*4e3e87e4SAndreas Gohr private function displayProgressBar($next) 97*4e3e87e4SAndreas Gohr { 98*4e3e87e4SAndreas Gohr $steps = ['version', 'download', 'unpack', 'check', 'upgrade']; 99eb52c46aSAndreas Gohr $active = true; 100eb52c46aSAndreas Gohr $count = 0; 101eb52c46aSAndreas Gohr 102eb52c46aSAndreas Gohr echo '<div id="plugin__upgrade_meter"><ol>'; 103eb52c46aSAndreas Gohr foreach ($steps as $step) { 104eb52c46aSAndreas Gohr $count++; 105eb52c46aSAndreas Gohr if ($step == $next) $active = false; 106eb52c46aSAndreas Gohr if ($active) { 107eb52c46aSAndreas Gohr echo '<li class="active">'; 108eb52c46aSAndreas Gohr echo '<span class="step">✔</span>'; 109eb52c46aSAndreas Gohr } else { 110eb52c46aSAndreas Gohr echo '<li>'; 111eb52c46aSAndreas Gohr echo '<span class="step">' . $count . '</span>'; 112eb52c46aSAndreas Gohr } 113eb52c46aSAndreas Gohr 114eb52c46aSAndreas Gohr echo '<span class="stage">' . $this->getLang('step_' . $step) . '</span>'; 115eb52c46aSAndreas Gohr echo '</li>'; 116eb52c46aSAndreas Gohr } 117eb52c46aSAndreas Gohr echo '</ol></div>'; 11834aae6dbSAndreas Gohr } 11934aae6dbSAndreas Gohr 120bd08ebd1SAndreas Gohr /** 121bd08ebd1SAndreas Gohr * Decides the current step and executes it 122bd08ebd1SAndreas Gohr * 123bd08ebd1SAndreas Gohr * @param bool $abrt 124bd08ebd1SAndreas Gohr * @param bool $next 125*4e3e87e4SAndreas Gohr * @param bool $ok 126bd08ebd1SAndreas Gohr */ 127*4e3e87e4SAndreas Gohr private function nextStep(&$abrt, &$next, &$ok) 128*4e3e87e4SAndreas Gohr { 1294bed5591SAndreas Gohr 13034aae6dbSAndreas Gohr if (isset($_REQUEST['step']) && is_array($_REQUEST['step'])) { 1319e48b5c0SAndreas Gohr $keys = array_keys($_REQUEST['step']); 1329e48b5c0SAndreas Gohr $step = array_shift($keys); 13334aae6dbSAndreas Gohr } else { 13434aae6dbSAndreas Gohr $step = ''; 13534aae6dbSAndreas Gohr } 13634aae6dbSAndreas Gohr 137fb8e77caSAndreas Gohr if ($step == 'cancel' || $step == 'done') { 138*4e3e87e4SAndreas Gohr $ok = $this->helper->cleanup(); 139fb8e77caSAndreas Gohr if ($step == 'cancel') $step = ''; 14034aae6dbSAndreas Gohr } 14134aae6dbSAndreas Gohr 14234aae6dbSAndreas Gohr if ($step) { 14334aae6dbSAndreas Gohr $abrt = true; 14434aae6dbSAndreas Gohr $next = false; 145f378cb7eSAndreas Gohr if ($step == 'version') { 146*4e3e87e4SAndreas Gohr $ok = $this->helper->checkVersions(); 147f378cb7eSAndreas Gohr $next = 'download'; 148fb8e77caSAndreas Gohr } elseif ($step == 'done') { 149*4e3e87e4SAndreas Gohr echo $this->locale_xhtml('final'); 150fb8e77caSAndreas Gohr $next = ''; 151fb8e77caSAndreas Gohr $abrt = ''; 152*4e3e87e4SAndreas Gohr } elseif (!file_exists($this->helper->tgzfile)) { 153*4e3e87e4SAndreas Gohr if ($ok = $this->helper->downloadTarball()) $next = 'unpack'; 154*4e3e87e4SAndreas Gohr } elseif (!is_dir($this->helper->tgzdir)) { 155*4e3e87e4SAndreas Gohr if ($ok = $this->helper->extractTarball()) $next = 'check'; 15634aae6dbSAndreas Gohr } elseif ($step != 'upgrade') { 157*4e3e87e4SAndreas Gohr if ($ok = $this->helper->checkPermissions()) $next = 'upgrade'; 15834aae6dbSAndreas Gohr } elseif ($step == 'upgrade') { 159*4e3e87e4SAndreas Gohr if ($ok = $this->helper->copyFiles()) { 160*4e3e87e4SAndreas Gohr $ok = $this->helper->deleteObsoleteFiles(); 161*4e3e87e4SAndreas Gohr $this->helper->cleanup(); 162fb8e77caSAndreas Gohr $next = 'done'; 163fb8e77caSAndreas Gohr $abrt = ''; 164fb8e77caSAndreas Gohr } 16534aae6dbSAndreas Gohr } else { 16675e9d164SAndreas Gohr echo 'uhm. what happened? where am I? This should not happen'; 16734aae6dbSAndreas Gohr } 16834aae6dbSAndreas Gohr } else { 16934aae6dbSAndreas Gohr # first time run, show intro 17034aae6dbSAndreas Gohr echo $this->locale_xhtml('step0'); 17134aae6dbSAndreas Gohr $abrt = false; 172f378cb7eSAndreas Gohr $next = 'version'; 17334aae6dbSAndreas Gohr } 17434aae6dbSAndreas Gohr } 17534aae6dbSAndreas Gohr 176bd08ebd1SAndreas Gohr /** 177*4e3e87e4SAndreas Gohr * Print the given message and flush buffers 178*4e3e87e4SAndreas Gohr * 179*4e3e87e4SAndreas Gohr * @param string $level 180*4e3e87e4SAndreas Gohr * @param string $message 181bd08ebd1SAndreas Gohr */ 182*4e3e87e4SAndreas Gohr public function log($level, $message) { 183*4e3e87e4SAndreas Gohr echo '<div class="log-' . $level . '">' . $message . '</div>'; 1846e7afd39SAndreas Gohr flush(); 1856e7afd39SAndreas Gohr ob_flush(); 1866e7afd39SAndreas Gohr } 18734aae6dbSAndreas Gohr} 188