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 934aae6dbSAndreas Gohr// must be run within Dokuwiki 1034aae6dbSAndreas Gohrif(!defined('DOKU_INC')) die(); 1134aae6dbSAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 1234aae6dbSAndreas Gohrrequire_once DOKU_PLUGIN.'admin.php'; 13d391262fSAndreas Gohrrequire_once DOKU_PLUGIN.'upgrade/VerboseTarLib.class.php'; 14e90f5147SAndreas Gohrrequire_once DOKU_PLUGIN.'upgrade/HTTPClient.php'; 1534aae6dbSAndreas Gohr 1668cf950eSAndreas Gohruse dokuwiki\plugin\upgrade\DokuHTTPClient; 1768cf950eSAndreas Gohr 18ff284f1fSAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin { 1934aae6dbSAndreas Gohr private $tgzurl; 2034aae6dbSAndreas Gohr private $tgzfile; 2134aae6dbSAndreas Gohr private $tgzdir; 22f378cb7eSAndreas Gohr private $tgzversion; 23f378cb7eSAndreas Gohr private $pluginversion; 2434aae6dbSAndreas Gohr 254ed3677eSAndreas Gohr protected $haderrors = false; 264ed3677eSAndreas Gohr 2734aae6dbSAndreas Gohr public function __construct() { 2834aae6dbSAndreas Gohr global $conf; 2934aae6dbSAndreas Gohr 3034aae6dbSAndreas Gohr $branch = 'stable'; 3134aae6dbSAndreas Gohr 32bd08ebd1SAndreas Gohr $this->tgzurl = "https://github.com/splitbrain/dokuwiki/archive/$branch.tar.gz"; 33d391262fSAndreas Gohr $this->tgzfile = $conf['tmpdir'].'/dokuwiki-upgrade.tgz'; 34d391262fSAndreas Gohr $this->tgzdir = $conf['tmpdir'].'/dokuwiki-upgrade/'; 35f378cb7eSAndreas Gohr $this->tgzversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki/$branch/VERSION"; 36f378cb7eSAndreas Gohr $this->pluginversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki-plugin-upgrade/master/plugin.info.txt"; 3734aae6dbSAndreas Gohr } 3834aae6dbSAndreas Gohr 39bd08ebd1SAndreas Gohr public function getMenuSort() { 40bd08ebd1SAndreas Gohr return 555; 41bd08ebd1SAndreas Gohr } 4234aae6dbSAndreas Gohr 4341163f44SAndreas Gohr public function handle() { 4441163f44SAndreas Gohr if($_REQUEST['step'] && !checkSecurityToken()) { 4541163f44SAndreas Gohr unset($_REQUEST['step']); 4641163f44SAndreas Gohr } 4734aae6dbSAndreas Gohr } 4834aae6dbSAndreas Gohr 4934aae6dbSAndreas Gohr public function html() { 5034aae6dbSAndreas Gohr $abrt = false; 5134aae6dbSAndreas Gohr $next = false; 5234aae6dbSAndreas Gohr 5334aae6dbSAndreas Gohr echo '<h1>'.$this->getLang('menu').'</h1>'; 5434aae6dbSAndreas Gohr 5506dc93a8SAndreas Gohr global $conf; 5606dc93a8SAndreas Gohr if($conf['safemodehack']) { 5706dc93a8SAndreas Gohr $abrt = false; 5806dc93a8SAndreas Gohr $next = false; 5906dc93a8SAndreas Gohr echo $this->locale_xhtml('safemode'); 6006dc93a8SAndreas Gohr return; 6106dc93a8SAndreas Gohr } 6206dc93a8SAndreas Gohr 63d391262fSAndreas Gohr $this->_say('<div id="plugin__upgrade">'); 6475e9d164SAndreas Gohr // enable auto scroll 6575e9d164SAndreas Gohr ?> 6675e9d164SAndreas Gohr <script language="javascript" type="text/javascript"> 67d391262fSAndreas Gohr var plugin_upgrade = window.setInterval(function () { 68d4376367SAndreas Gohr var obj = document.getElementById('plugin__upgrade'); 6975e9d164SAndreas Gohr if (obj) obj.scrollTop = obj.scrollHeight; 7075e9d164SAndreas Gohr }, 25); 7175e9d164SAndreas Gohr </script> 7275e9d164SAndreas Gohr <?php 7375e9d164SAndreas Gohr 7475e9d164SAndreas Gohr // handle current step 7522c39b33SAndreas Gohr $this->_stepit($abrt, $next); 7634aae6dbSAndreas Gohr 7775e9d164SAndreas Gohr // disable auto scroll 7875e9d164SAndreas Gohr ?> 7975e9d164SAndreas Gohr <script language="javascript" type="text/javascript"> 8075e9d164SAndreas Gohr window.setTimeout(function () { 81d391262fSAndreas Gohr window.clearInterval(plugin_upgrade); 8275e9d164SAndreas Gohr }, 50); 8375e9d164SAndreas Gohr </script> 8475e9d164SAndreas Gohr <?php 8575e9d164SAndreas Gohr $this->_say('</div>'); 8675e9d164SAndreas Gohr 874ed3677eSAndreas Gohr $careful = ''; 884ed3677eSAndreas Gohr if($this->haderrors) { 894ed3677eSAndreas Gohr echo '<div id="plugin__upgrade_careful">'.$this->getLang('careful').'</div>'; 904ed3677eSAndreas Gohr $careful = 'careful'; 914ed3677eSAndreas Gohr } 924ed3677eSAndreas Gohr 939f8d4355SAndreas Gohr $action = script(); 949f8d4355SAndreas Gohr echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">'; 9534aae6dbSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 96d391262fSAndreas Gohr echo '<input type="hidden" name="page" value="upgrade" />'; 9741163f44SAndreas Gohr echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 9838665ab9SAndreas Gohr if($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue '.$careful.'">' . $this->getLang('btn_continue') . ' ➡</button>'; 999f8d4355SAndreas Gohr if($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>'; 10034aae6dbSAndreas Gohr echo '</form>'; 101eb52c46aSAndreas Gohr 102eb52c46aSAndreas Gohr $this->_progress($next); 103eb52c46aSAndreas Gohr } 104eb52c46aSAndreas Gohr 105eb52c46aSAndreas Gohr /** 106eb52c46aSAndreas Gohr * Display a progress bar of all steps 107eb52c46aSAndreas Gohr * 108eb52c46aSAndreas Gohr * @param string $next the next step 109eb52c46aSAndreas Gohr */ 110eb52c46aSAndreas Gohr private function _progress($next) { 111eb52c46aSAndreas Gohr $steps = array('version', 'download', 'unpack', 'check', 'upgrade'); 112eb52c46aSAndreas Gohr $active = true; 113eb52c46aSAndreas Gohr $count = 0; 114eb52c46aSAndreas Gohr 115eb52c46aSAndreas Gohr echo '<div id="plugin__upgrade_meter"><ol>'; 116eb52c46aSAndreas Gohr foreach($steps as $step) { 117eb52c46aSAndreas Gohr $count++; 118eb52c46aSAndreas Gohr if($step == $next) $active = false; 119eb52c46aSAndreas Gohr if($active) { 120eb52c46aSAndreas Gohr echo '<li class="active">'; 121eb52c46aSAndreas Gohr echo '<span class="step">✔</span>'; 122eb52c46aSAndreas Gohr } else { 123eb52c46aSAndreas Gohr echo '<li>'; 124eb52c46aSAndreas Gohr echo '<span class="step">'.$count.'</span>'; 125eb52c46aSAndreas Gohr } 126eb52c46aSAndreas Gohr 127eb52c46aSAndreas Gohr echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>'; 128eb52c46aSAndreas Gohr echo '</li>'; 129eb52c46aSAndreas Gohr } 130eb52c46aSAndreas Gohr echo '</ol></div>'; 13134aae6dbSAndreas Gohr } 13234aae6dbSAndreas Gohr 133bd08ebd1SAndreas Gohr /** 134bd08ebd1SAndreas Gohr * Decides the current step and executes it 135bd08ebd1SAndreas Gohr * 136bd08ebd1SAndreas Gohr * @param bool $abrt 137bd08ebd1SAndreas Gohr * @param bool $next 138bd08ebd1SAndreas Gohr */ 13934aae6dbSAndreas Gohr private function _stepit(&$abrt, &$next) { 1404bed5591SAndreas Gohr 14134aae6dbSAndreas Gohr if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) { 14234aae6dbSAndreas Gohr $step = array_shift(array_keys($_REQUEST['step'])); 14334aae6dbSAndreas Gohr } else { 14434aae6dbSAndreas Gohr $step = ''; 14534aae6dbSAndreas Gohr } 14634aae6dbSAndreas Gohr 147fb8e77caSAndreas Gohr if($step == 'cancel' || $step == 'done') { 14834aae6dbSAndreas Gohr # cleanup 14934aae6dbSAndreas Gohr @unlink($this->tgzfile); 1506b2d1b30SAndreas Gohr $this->_rdel($this->tgzdir); 151fb8e77caSAndreas Gohr if($step == 'cancel') $step = ''; 15234aae6dbSAndreas Gohr } 15334aae6dbSAndreas Gohr 15434aae6dbSAndreas Gohr if($step) { 15534aae6dbSAndreas Gohr $abrt = true; 15634aae6dbSAndreas Gohr $next = false; 157f378cb7eSAndreas Gohr if($step == 'version') { 158f378cb7eSAndreas Gohr $this->_step_version(); 159f378cb7eSAndreas Gohr $next = 'download'; 160fb8e77caSAndreas Gohr } elseif ($step == 'done') { 161fb8e77caSAndreas Gohr $this->_step_done(); 162fb8e77caSAndreas Gohr $next = ''; 163fb8e77caSAndreas Gohr $abrt = ''; 164f378cb7eSAndreas Gohr } elseif(!file_exists($this->tgzfile)) { 16534aae6dbSAndreas Gohr if($this->_step_download()) $next = 'unpack'; 16634aae6dbSAndreas Gohr } elseif(!is_dir($this->tgzdir)) { 16734aae6dbSAndreas Gohr if($this->_step_unpack()) $next = 'check'; 16834aae6dbSAndreas Gohr } elseif($step != 'upgrade') { 1693c38de15SAndreas Gohr if($this->_step_check()) $next = 'upgrade'; 17034aae6dbSAndreas Gohr } elseif($step == 'upgrade') { 171fb8e77caSAndreas Gohr if($this->_step_copy()) { 172fb8e77caSAndreas Gohr $next = 'done'; 173fb8e77caSAndreas Gohr $abrt = ''; 174fb8e77caSAndreas Gohr } 17534aae6dbSAndreas Gohr } else { 17675e9d164SAndreas Gohr echo 'uhm. what happened? where am I? This should not happen'; 17734aae6dbSAndreas Gohr } 17834aae6dbSAndreas Gohr } else { 17934aae6dbSAndreas Gohr # first time run, show intro 18034aae6dbSAndreas Gohr echo $this->locale_xhtml('step0'); 18134aae6dbSAndreas Gohr $abrt = false; 182f378cb7eSAndreas Gohr $next = 'version'; 18334aae6dbSAndreas Gohr } 18434aae6dbSAndreas Gohr } 18534aae6dbSAndreas Gohr 186bd08ebd1SAndreas Gohr /** 187bd08ebd1SAndreas Gohr * Output the given arguments using vsprintf and flush buffers 188bd08ebd1SAndreas Gohr */ 1894ed3677eSAndreas Gohr public function _say() { 19034aae6dbSAndreas Gohr $args = func_get_args(); 1916e7afd39SAndreas Gohr echo '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="16" height="16" alt="" /> '; 1926e7afd39SAndreas Gohr echo vsprintf(array_shift($args)."<br />\n", $args); 1936e7afd39SAndreas Gohr flush(); 1946e7afd39SAndreas Gohr ob_flush(); 1956e7afd39SAndreas Gohr } 1966e7afd39SAndreas Gohr 1976e7afd39SAndreas Gohr /** 1986e7afd39SAndreas Gohr * Print a warning using the given arguments with vsprintf and flush buffers 1996e7afd39SAndreas Gohr */ 2004ed3677eSAndreas Gohr public function _warn() { 2014ed3677eSAndreas Gohr $this->haderrors = true; 2024ed3677eSAndreas Gohr 2036e7afd39SAndreas Gohr $args = func_get_args(); 2046e7afd39SAndreas Gohr echo '<img src="'.DOKU_BASE.'lib/images/error.png" width="16" height="16" alt="!" /> '; 20534aae6dbSAndreas Gohr echo vsprintf(array_shift($args)."<br />\n", $args); 20634aae6dbSAndreas Gohr flush(); 20734aae6dbSAndreas Gohr ob_flush(); 20834aae6dbSAndreas Gohr } 20934aae6dbSAndreas Gohr 2106b2d1b30SAndreas Gohr /** 2116b2d1b30SAndreas Gohr * Recursive delete 2126b2d1b30SAndreas Gohr * 2139285faa5SAndreas Gohr * @author Jon Hassall 2149285faa5SAndreas Gohr * @link http://de.php.net/manual/en/function.unlink.php#87045 2156b2d1b30SAndreas Gohr */ 2169285faa5SAndreas Gohr private function _rdel($dir) { 2179285faa5SAndreas Gohr if(!$dh = @opendir($dir)) { 218bd08ebd1SAndreas Gohr return false; 2199285faa5SAndreas Gohr } 2209285faa5SAndreas Gohr while(false !== ($obj = readdir($dh))) { 2219285faa5SAndreas Gohr if($obj == '.' || $obj == '..') continue; 2229285faa5SAndreas Gohr 2239285faa5SAndreas Gohr if(!@unlink($dir.'/'.$obj)) { 2249285faa5SAndreas Gohr $this->_rdel($dir.'/'.$obj); 2259285faa5SAndreas Gohr } 2269285faa5SAndreas Gohr } 2279285faa5SAndreas Gohr closedir($dh); 228e32047e3SAndreas Gohr return @rmdir($dir); 2296b2d1b30SAndreas Gohr } 2306b2d1b30SAndreas Gohr 231bd08ebd1SAndreas Gohr /** 232f378cb7eSAndreas Gohr * Check various versions 233f378cb7eSAndreas Gohr * 234f378cb7eSAndreas Gohr * @return bool 235f378cb7eSAndreas Gohr */ 236f378cb7eSAndreas Gohr private function _step_version() { 237f378cb7eSAndreas Gohr $ok = true; 238f378cb7eSAndreas Gohr 239c37aa5dcSAndreas Gohr // we need SSL - only newer HTTPClients check that themselves 240c37aa5dcSAndreas Gohr if(!in_array('ssl', stream_get_transports())) { 241c37aa5dcSAndreas Gohr $this->_warn($this->getLang('vs_ssl')); 242c37aa5dcSAndreas Gohr $ok = false; 243c37aa5dcSAndreas Gohr } 244c37aa5dcSAndreas Gohr 245f378cb7eSAndreas Gohr // get the available version 246f378cb7eSAndreas Gohr $http = new DokuHTTPClient(); 247f378cb7eSAndreas Gohr $tgzversion = $http->get($this->tgzversion); 248f378cb7eSAndreas Gohr if(!$tgzversion) { 2496e7afd39SAndreas Gohr $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error)); 250f378cb7eSAndreas Gohr $ok = false; 251f378cb7eSAndreas Gohr } 252f378cb7eSAndreas Gohr if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) { 2536e7afd39SAndreas Gohr $this->_warn($this->getLang('vs_tgzno')); 254f378cb7eSAndreas Gohr $ok = false; 255f378cb7eSAndreas Gohr $tgzversionnum = 0; 256f378cb7eSAndreas Gohr } else { 257f378cb7eSAndreas Gohr $tgzversionnum = $m[2]; 258f378cb7eSAndreas Gohr $this->_say($this->getLang('vs_tgz'), $tgzversion); 259f378cb7eSAndreas Gohr } 260f378cb7eSAndreas Gohr 261f378cb7eSAndreas Gohr // get the current version 262f378cb7eSAndreas Gohr $version = getVersion(); 263f378cb7eSAndreas Gohr if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) { 264f378cb7eSAndreas Gohr $versionnum = 0; 265f378cb7eSAndreas Gohr } else { 266f378cb7eSAndreas Gohr $versionnum = $m[2]; 267f378cb7eSAndreas Gohr } 268f378cb7eSAndreas Gohr $this->_say($this->getLang('vs_local'), $version); 269f378cb7eSAndreas Gohr 270f378cb7eSAndreas Gohr // compare versions 271f378cb7eSAndreas Gohr if(!$versionnum) { 2726e7afd39SAndreas Gohr $this->_warn($this->getLang('vs_localno')); 273f378cb7eSAndreas Gohr $ok = false; 274f378cb7eSAndreas Gohr } else if($tgzversionnum) { 275f378cb7eSAndreas Gohr if($tgzversionnum < $versionnum) { 2766e7afd39SAndreas Gohr $this->_warn($this->getLang('vs_newer')); 277f378cb7eSAndreas Gohr $ok = false; 278f378cb7eSAndreas Gohr } elseif($tgzversionnum == $versionnum) { 2796e7afd39SAndreas Gohr $this->_warn($this->getLang('vs_same')); 280f378cb7eSAndreas Gohr $ok = false; 281f378cb7eSAndreas Gohr } 282f378cb7eSAndreas Gohr } 283f378cb7eSAndreas Gohr 284f378cb7eSAndreas Gohr // check plugin version 285f378cb7eSAndreas Gohr $pluginversion = $http->get($this->pluginversion); 286f378cb7eSAndreas Gohr if($pluginversion) { 287f378cb7eSAndreas Gohr $plugininfo = linesToHash(explode("\n", $pluginversion)); 288f378cb7eSAndreas Gohr $myinfo = $this->getInfo(); 289f378cb7eSAndreas Gohr if($plugininfo['date'] > $myinfo['date']) { 2907cb119ecSAndreas Gohr $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']); 291f378cb7eSAndreas Gohr $ok = false; 292f378cb7eSAndreas Gohr } 293f378cb7eSAndreas Gohr } 294f378cb7eSAndreas Gohr 2959bbe795aSAndreas Gohr // check if PHP is up to date 296cd074bc5SAndreas Gohr $minphp = '5.6'; 2979bbe795aSAndreas Gohr if(version_compare(phpversion(), $minphp, '<')) { 298a92a10d4SAndreas Gohr $this->_warn($this->getLang('vs_php'), $minphp, phpversion()); 2999bbe795aSAndreas Gohr $ok = false; 3009bbe795aSAndreas Gohr } 3019bbe795aSAndreas Gohr 302f378cb7eSAndreas Gohr return $ok; 303f378cb7eSAndreas Gohr } 304f378cb7eSAndreas Gohr 305f378cb7eSAndreas Gohr /** 306fb8e77caSAndreas Gohr * Redirect to the start page 307fb8e77caSAndreas Gohr */ 308fb8e77caSAndreas Gohr private function _step_done() { 309fb8e77caSAndreas Gohr echo $this->getLang('finish'); 31061f0f3bfSAndreas Gohr echo "<script type='text/javascript'>location.href='".DOKU_URL."';</script>"; 311fb8e77caSAndreas Gohr } 312fb8e77caSAndreas Gohr 313fb8e77caSAndreas Gohr /** 314bd08ebd1SAndreas Gohr * Download the tarball 315bd08ebd1SAndreas Gohr * 316bd08ebd1SAndreas Gohr * @return bool 317bd08ebd1SAndreas Gohr */ 31834aae6dbSAndreas Gohr private function _step_download() { 3193c38de15SAndreas Gohr $this->_say($this->getLang('dl_from'), $this->tgzurl); 32034aae6dbSAndreas Gohr 32158774abaSFrancis GUDIN @set_time_limit(300); 32234aae6dbSAndreas Gohr @ignore_user_abort(); 32334aae6dbSAndreas Gohr 32434aae6dbSAndreas Gohr $http = new DokuHTTPClient(); 32558774abaSFrancis GUDIN $http->timeout = 300; 32634aae6dbSAndreas Gohr $data = $http->get($this->tgzurl); 32734aae6dbSAndreas Gohr 32834aae6dbSAndreas Gohr if(!$data) { 3296e7afd39SAndreas Gohr $this->_warn($http->error); 3306e7afd39SAndreas Gohr $this->_warn($this->getLang('dl_fail')); 33134aae6dbSAndreas Gohr return false; 33234aae6dbSAndreas Gohr } 33334aae6dbSAndreas Gohr 33434aae6dbSAndreas Gohr if(!io_saveFile($this->tgzfile, $data)) { 3356e7afd39SAndreas Gohr $this->_warn($this->getLang('dl_fail')); 33634aae6dbSAndreas Gohr return false; 33734aae6dbSAndreas Gohr } 33834aae6dbSAndreas Gohr 339a7b56078SAndreas Gohr $this->_say($this->getLang('dl_done'), filesize_h(strlen($data))); 3403c38de15SAndreas Gohr 34134aae6dbSAndreas Gohr return true; 34234aae6dbSAndreas Gohr } 34334aae6dbSAndreas Gohr 344bd08ebd1SAndreas Gohr /** 345bd08ebd1SAndreas Gohr * Unpack the tarball 346bd08ebd1SAndreas Gohr * 347bd08ebd1SAndreas Gohr * @return bool 348bd08ebd1SAndreas Gohr */ 34934aae6dbSAndreas Gohr private function _step_unpack() { 3503c38de15SAndreas Gohr $this->_say('<b>'.$this->getLang('pk_extract').'</b>'); 35134aae6dbSAndreas Gohr 35258774abaSFrancis GUDIN @set_time_limit(300); 35334aae6dbSAndreas Gohr @ignore_user_abort(); 35434aae6dbSAndreas Gohr 355d04a99cdSAndreas Gohr try { 356d04a99cdSAndreas Gohr $tar = new VerboseTar(); 357d04a99cdSAndreas Gohr $tar->open($this->tgzfile); 358d04a99cdSAndreas Gohr $tar->extract($this->tgzdir, 1); 359d04a99cdSAndreas Gohr $tar->close(); 360d04a99cdSAndreas Gohr } catch (Exception $e) { 361d04a99cdSAndreas Gohr $this->_warn($e->getMessage()); 3626e7afd39SAndreas Gohr $this->_warn($this->getLang('pk_fail')); 36334aae6dbSAndreas Gohr return false; 36434aae6dbSAndreas Gohr } 36534aae6dbSAndreas Gohr 3663c38de15SAndreas Gohr $this->_say($this->getLang('pk_done')); 367738c0102SAndreas Gohr 368bd08ebd1SAndreas Gohr $this->_say( 369bd08ebd1SAndreas Gohr $this->getLang('pk_version'), 370738c0102SAndreas Gohr hsc(file_get_contents($this->tgzdir.'/VERSION')), 371bd08ebd1SAndreas Gohr getVersion() 372bd08ebd1SAndreas Gohr ); 37334aae6dbSAndreas Gohr return true; 37434aae6dbSAndreas Gohr } 37534aae6dbSAndreas Gohr 376bd08ebd1SAndreas Gohr /** 377bd08ebd1SAndreas Gohr * Check permissions of files to change 378bd08ebd1SAndreas Gohr * 379bd08ebd1SAndreas Gohr * @return bool 380bd08ebd1SAndreas Gohr */ 3813c38de15SAndreas Gohr private function _step_check() { 3823c38de15SAndreas Gohr $this->_say($this->getLang('ck_start')); 3833c38de15SAndreas Gohr $ok = $this->_traverse('', true); 38434aae6dbSAndreas Gohr if($ok) { 3853c38de15SAndreas Gohr $this->_say('<b>'.$this->getLang('ck_done').'</b>'); 38634aae6dbSAndreas Gohr } else { 3876e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('ck_fail').'</b>'); 38834aae6dbSAndreas Gohr } 3893c38de15SAndreas Gohr return $ok; 3903c38de15SAndreas Gohr } 39134aae6dbSAndreas Gohr 392bd08ebd1SAndreas Gohr /** 393bd08ebd1SAndreas Gohr * Copy over new files 394bd08ebd1SAndreas Gohr * 395bd08ebd1SAndreas Gohr * @return bool 396bd08ebd1SAndreas Gohr */ 3973c38de15SAndreas Gohr private function _step_copy() { 3983c38de15SAndreas Gohr $this->_say($this->getLang('cp_start')); 3993c38de15SAndreas Gohr $ok = $this->_traverse('', false); 4003c38de15SAndreas Gohr if($ok) { 4013c38de15SAndreas Gohr $this->_say('<b>'.$this->getLang('cp_done').'</b>'); 40263712694SAndreas Gohr $this->_rmold(); 403e32047e3SAndreas Gohr $this->_say('<b>'.$this->getLang('finish').'</b>'); 40434aae6dbSAndreas Gohr } else { 4056e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('cp_fail').'</b>'); 40634aae6dbSAndreas Gohr } 40734aae6dbSAndreas Gohr return $ok; 40834aae6dbSAndreas Gohr } 40934aae6dbSAndreas Gohr 410bd08ebd1SAndreas Gohr /** 411bd08ebd1SAndreas Gohr * Delete outdated files 412bd08ebd1SAndreas Gohr */ 41363712694SAndreas Gohr private function _rmold() { 414a85c9abbSAndreas Gohr global $conf; 415a85c9abbSAndreas Gohr 41663712694SAndreas Gohr $list = file($this->tgzdir.'data/deleted.files'); 41763712694SAndreas Gohr foreach($list as $line) { 41863712694SAndreas Gohr $line = trim(preg_replace('/#.*$/', '', $line)); 41963712694SAndreas Gohr if(!$line) continue; 42063712694SAndreas Gohr $file = DOKU_INC.$line; 42163712694SAndreas Gohr if(!file_exists($file)) continue; 4227b7da9aaSAndreas Gohr 4237b7da9aaSAndreas Gohr // check that the given file is an case sensitive match 4247b7da9aaSAndreas Gohr if(basename(realpath($file)) != basename($file)) { 425*a6a26032SAndreas Gohr $this->_say($this->getLang('rm_mismatch'), hsc($line)); 426*a6a26032SAndreas Gohr continue; 4277b7da9aaSAndreas Gohr } 4287b7da9aaSAndreas Gohr 42963712694SAndreas Gohr if((is_dir($file) && $this->_rdel($file)) || 430bd08ebd1SAndreas Gohr @unlink($file) 431bd08ebd1SAndreas Gohr ) { 432e32047e3SAndreas Gohr $this->_say($this->getLang('rm_done'), hsc($line)); 43363712694SAndreas Gohr } else { 4346e7afd39SAndreas Gohr $this->_warn($this->getLang('rm_fail'), hsc($line)); 43563712694SAndreas Gohr } 43663712694SAndreas Gohr } 437331439fbSAndreas Gohr // delete install 438331439fbSAndreas Gohr @unlink(DOKU_INC.'install.php'); 439de440c89SAndreas Gohr 440de440c89SAndreas Gohr // make sure update message will be gone 441de440c89SAndreas Gohr @touch(DOKU_INC.'doku.php'); 442a85c9abbSAndreas Gohr @unlink($conf['cachedir'].'/messages.txt'); 44363712694SAndreas Gohr } 44463712694SAndreas Gohr 445bd08ebd1SAndreas Gohr /** 446bd08ebd1SAndreas Gohr * Traverse over the given dir and compare it to the DokuWiki dir 447bd08ebd1SAndreas Gohr * 448bd08ebd1SAndreas Gohr * Checks what files need an update, tests for writability and copies 449bd08ebd1SAndreas Gohr * 450bd08ebd1SAndreas Gohr * @param string $dir 451bd08ebd1SAndreas Gohr * @param bool $dryrun do not copy but only check permissions 452bd08ebd1SAndreas Gohr * @return bool 453bd08ebd1SAndreas Gohr */ 45434aae6dbSAndreas Gohr private function _traverse($dir, $dryrun) { 45534aae6dbSAndreas Gohr $base = $this->tgzdir; 45634aae6dbSAndreas Gohr $ok = true; 45734aae6dbSAndreas Gohr 45834aae6dbSAndreas Gohr $dh = @opendir($base.'/'.$dir); 459bd08ebd1SAndreas Gohr if(!$dh) return false; 46034aae6dbSAndreas Gohr while(($file = readdir($dh)) !== false) { 46134aae6dbSAndreas Gohr if($file == '.' || $file == '..') continue; 46234aae6dbSAndreas Gohr $from = "$base/$dir/$file"; 46334aae6dbSAndreas Gohr $to = DOKU_INC."$dir/$file"; 46434aae6dbSAndreas Gohr 46534aae6dbSAndreas Gohr if(is_dir($from)) { 46634aae6dbSAndreas Gohr if($dryrun) { 46734aae6dbSAndreas Gohr // just check for writability 46834aae6dbSAndreas Gohr if(!is_dir($to)) { 46934aae6dbSAndreas Gohr if(is_dir(dirname($to)) && !is_writable(dirname($to))) { 4706e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 47134aae6dbSAndreas Gohr $ok = false; 47234aae6dbSAndreas Gohr } 47334aae6dbSAndreas Gohr } 47434aae6dbSAndreas Gohr } 47534aae6dbSAndreas Gohr 47634aae6dbSAndreas Gohr // recursion 47734aae6dbSAndreas Gohr if(!$this->_traverse("$dir/$file", $dryrun)) { 47834aae6dbSAndreas Gohr $ok = false; 47934aae6dbSAndreas Gohr } 48034aae6dbSAndreas Gohr } else { 481f2fa6d10SAndreas Gohr $fmd5 = md5(@file_get_contents($from)); 482f2fa6d10SAndreas Gohr $tmd5 = md5(@file_get_contents($to)); 4836deeb3b1SAndreas Gohr if($fmd5 != $tmd5 || !file_exists($to)) { 48434aae6dbSAndreas Gohr if($dryrun) { 48534aae6dbSAndreas Gohr // just check for writability 48634aae6dbSAndreas Gohr if((file_exists($to) && !is_writable($to)) || 487bd08ebd1SAndreas Gohr (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) 488bd08ebd1SAndreas Gohr ) { 48934aae6dbSAndreas Gohr 4906e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 49134aae6dbSAndreas Gohr $ok = false; 49234aae6dbSAndreas Gohr } else { 4933c38de15SAndreas Gohr $this->_say($this->getLang('tv_upd'), hsc("$dir/$file")); 49434aae6dbSAndreas Gohr } 49534aae6dbSAndreas Gohr } else { 49634aae6dbSAndreas Gohr // check dir 49734aae6dbSAndreas Gohr if(io_mkdir_p(dirname($to))) { 4987b7da9aaSAndreas Gohr // remove existing (avoid case sensitivity problems) 4997b7da9aaSAndreas Gohr if(file_exists($to) && !@unlink($to)) { 5007b7da9aaSAndreas Gohr $this->_warn('<b>'.$this->getLang('tv_nodel').'</b>', hsc("$dir/$file")); 5017b7da9aaSAndreas Gohr $ok = false; 5027b7da9aaSAndreas Gohr } 50334aae6dbSAndreas Gohr // copy 50434aae6dbSAndreas Gohr if(!copy($from, $to)) { 5056e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file")); 50634aae6dbSAndreas Gohr $ok = false; 50734aae6dbSAndreas Gohr } else { 5083c38de15SAndreas Gohr $this->_say($this->getLang('tv_done'), hsc("$dir/$file")); 50934aae6dbSAndreas Gohr } 51034aae6dbSAndreas Gohr } else { 5116e7afd39SAndreas Gohr $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir")); 51234aae6dbSAndreas Gohr $ok = false; 51334aae6dbSAndreas Gohr } 51434aae6dbSAndreas Gohr } 51534aae6dbSAndreas Gohr } 51634aae6dbSAndreas Gohr } 51734aae6dbSAndreas Gohr } 51834aae6dbSAndreas Gohr closedir($dh); 51934aae6dbSAndreas Gohr return $ok; 52034aae6dbSAndreas Gohr } 52134aae6dbSAndreas Gohr} 52234aae6dbSAndreas Gohr 52334aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8: 524