1<?php 2/** 3 * DokuWiki Plugin upgrade (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 12require_once DOKU_PLUGIN.'admin.php'; 13require_once DOKU_PLUGIN.'upgrade/VerboseTarLib.class.php'; 14require_once DOKU_PLUGIN.'upgrade/HTTPClient.php'; 15 16use dokuwiki\plugin\upgrade\DokuHTTPClient; 17 18class admin_plugin_upgrade extends DokuWiki_Admin_Plugin { 19 private $tgzurl; 20 private $tgzfile; 21 private $tgzdir; 22 private $tgzversion; 23 private $pluginversion; 24 25 protected $haderrors = false; 26 27 public function __construct() { 28 global $conf; 29 30 $branch = 'stable'; 31 32 $this->tgzurl = "https://github.com/splitbrain/dokuwiki/archive/$branch.tar.gz"; 33 $this->tgzfile = $conf['tmpdir'].'/dokuwiki-upgrade.tgz'; 34 $this->tgzdir = $conf['tmpdir'].'/dokuwiki-upgrade/'; 35 $this->tgzversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki/$branch/VERSION"; 36 $this->pluginversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki-plugin-upgrade/master/plugin.info.txt"; 37 } 38 39 public function getMenuSort() { 40 return 555; 41 } 42 43 public function handle() { 44 if($_REQUEST['step'] && !checkSecurityToken()) { 45 unset($_REQUEST['step']); 46 } 47 } 48 49 public function html() { 50 $abrt = false; 51 $next = false; 52 53 echo '<h1>'.$this->getLang('menu').'</h1>'; 54 55 self::_say('<div id="plugin__upgrade">'); 56 // enable auto scroll 57 ?> 58 <script language="javascript" type="text/javascript"> 59 var plugin_upgrade = window.setInterval(function () { 60 var obj = document.getElementById('plugin__upgrade'); 61 if (obj) obj.scrollTop = obj.scrollHeight; 62 }, 25); 63 </script> 64 <?php 65 66 // handle current step 67 $this->_stepit($abrt, $next); 68 69 // disable auto scroll 70 ?> 71 <script language="javascript" type="text/javascript"> 72 window.setTimeout(function () { 73 window.clearInterval(plugin_upgrade); 74 }, 50); 75 </script> 76 <?php 77 self::_say('</div>'); 78 79 $careful = ''; 80 if($this->haderrors) { 81 echo '<div id="plugin__upgrade_careful">'.$this->getLang('careful').'</div>'; 82 $careful = 'careful'; 83 } 84 85 $action = script(); 86 echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">'; 87 echo '<input type="hidden" name="do" value="admin" />'; 88 echo '<input type="hidden" name="page" value="upgrade" />'; 89 echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 90 if($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue '.$careful.'">' . $this->getLang('btn_continue') . ' ➡</button>'; 91 if($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>'; 92 echo '</form>'; 93 94 $this->_progress($next); 95 } 96 97 /** 98 * Display a progress bar of all steps 99 * 100 * @param string $next the next step 101 */ 102 private function _progress($next) { 103 $steps = array('version', 'download', 'unpack', 'check', 'upgrade'); 104 $active = true; 105 $count = 0; 106 107 echo '<div id="plugin__upgrade_meter"><ol>'; 108 foreach($steps as $step) { 109 $count++; 110 if($step == $next) $active = false; 111 if($active) { 112 echo '<li class="active">'; 113 echo '<span class="step">✔</span>'; 114 } else { 115 echo '<li>'; 116 echo '<span class="step">'.$count.'</span>'; 117 } 118 119 echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>'; 120 echo '</li>'; 121 } 122 echo '</ol></div>'; 123 } 124 125 /** 126 * Decides the current step and executes it 127 * 128 * @param bool $abrt 129 * @param bool $next 130 */ 131 private function _stepit(&$abrt, &$next) { 132 133 if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) { 134 $step = array_shift(array_keys($_REQUEST['step'])); 135 } else { 136 $step = ''; 137 } 138 139 if($step == 'cancel' || $step == 'done') { 140 # cleanup 141 @unlink($this->tgzfile); 142 $this->_rdel($this->tgzdir); 143 if($step == 'cancel') $step = ''; 144 } 145 146 if($step) { 147 $abrt = true; 148 $next = false; 149 if($step == 'version') { 150 $this->_step_version(); 151 $next = 'download'; 152 } elseif ($step == 'done') { 153 $this->_step_done(); 154 $next = ''; 155 $abrt = ''; 156 } elseif(!file_exists($this->tgzfile)) { 157 if($this->_step_download()) $next = 'unpack'; 158 } elseif(!is_dir($this->tgzdir)) { 159 if($this->_step_unpack()) $next = 'check'; 160 } elseif($step != 'upgrade') { 161 if($this->_step_check()) $next = 'upgrade'; 162 } elseif($step == 'upgrade') { 163 if($this->_step_copy()) { 164 $next = 'done'; 165 $abrt = ''; 166 } 167 } else { 168 echo 'uhm. what happened? where am I? This should not happen'; 169 } 170 } else { 171 # first time run, show intro 172 echo $this->locale_xhtml('step0'); 173 $abrt = false; 174 $next = 'version'; 175 } 176 } 177 178 /** 179 * Output the given arguments using vsprintf and flush buffers 180 */ 181 public static function _say() { 182 $args = func_get_args(); 183 echo '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="16" height="16" alt="" /> '; 184 echo vsprintf(array_shift($args)."<br />\n", $args); 185 flush(); 186 ob_flush(); 187 } 188 189 /** 190 * Print a warning using the given arguments with vsprintf and flush buffers 191 */ 192 public function _warn() { 193 $this->haderrors = true; 194 195 $args = func_get_args(); 196 echo '<img src="'.DOKU_BASE.'lib/images/error.png" width="16" height="16" alt="!" /> '; 197 echo vsprintf(array_shift($args)."<br />\n", $args); 198 flush(); 199 ob_flush(); 200 } 201 202 /** 203 * Recursive delete 204 * 205 * @author Jon Hassall 206 * @link http://de.php.net/manual/en/function.unlink.php#87045 207 */ 208 private function _rdel($dir) { 209 if(!$dh = @opendir($dir)) { 210 return false; 211 } 212 while(false !== ($obj = readdir($dh))) { 213 if($obj == '.' || $obj == '..') continue; 214 215 if(!@unlink($dir.'/'.$obj)) { 216 $this->_rdel($dir.'/'.$obj); 217 } 218 } 219 closedir($dh); 220 return @rmdir($dir); 221 } 222 223 /** 224 * Check various versions 225 * 226 * @return bool 227 */ 228 private function _step_version() { 229 $ok = true; 230 231 // we need SSL - only newer HTTPClients check that themselves 232 if(!in_array('ssl', stream_get_transports())) { 233 $this->_warn($this->getLang('vs_ssl')); 234 $ok = false; 235 } 236 237 // get the available version 238 $http = new DokuHTTPClient(); 239 $tgzversion = trim($http->get($this->tgzversion)); 240 if(!$tgzversion) { 241 $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error)); 242 $ok = false; 243 } 244 $tgzversionnum = $this->dateFromVersion($tgzversion); 245 if($tgzversionnum === 0) { 246 $this->_warn($this->getLang('vs_tgzno')); 247 $ok = false; 248 } else { 249 self::_say($this->getLang('vs_tgz'), $tgzversion); 250 } 251 252 // get the current version 253 $versiondata = getVersionData(); 254 $version = trim($versiondata['date']); 255 $versionnum = $this->dateFromVersion($version); 256 self::_say($this->getLang('vs_local'), $version); 257 258 // compare versions 259 if(!$versionnum) { 260 $this->_warn($this->getLang('vs_localno')); 261 $ok = false; 262 } else if($tgzversionnum) { 263 if($tgzversionnum < $versionnum) { 264 $this->_warn($this->getLang('vs_newer')); 265 $ok = false; 266 } elseif($tgzversionnum == $versionnum && $tgzversion == $version) { 267 $this->_warn($this->getLang('vs_same')); 268 $ok = false; 269 } 270 } 271 272 // check plugin version 273 $pluginversion = $http->get($this->pluginversion); 274 if($pluginversion) { 275 $plugininfo = linesToHash(explode("\n", $pluginversion)); 276 $myinfo = $this->getInfo(); 277 if($plugininfo['date'] > $myinfo['date']) { 278 $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']); 279 $ok = false; 280 } 281 } 282 283 // check if PHP is up to date 284 $minphp = '5.6'; 285 if(version_compare(phpversion(), $minphp, '<')) { 286 $this->_warn($this->getLang('vs_php'), $minphp, phpversion()); 287 $ok = false; 288 } 289 290 return $ok; 291 } 292 293 /** 294 * Redirect to the start page 295 */ 296 private function _step_done() { 297 if (function_exists('opcache_reset')) { 298 opcache_reset(); 299 } 300 echo $this->getLang('finish'); 301 echo "<script type='text/javascript'>location.href='".DOKU_URL."';</script>"; 302 } 303 304 /** 305 * Download the tarball 306 * 307 * @return bool 308 */ 309 private function _step_download() { 310 self::_say($this->getLang('dl_from'), $this->tgzurl); 311 312 @set_time_limit(300); 313 @ignore_user_abort(); 314 315 $http = new DokuHTTPClient(); 316 $http->timeout = 300; 317 $data = $http->get($this->tgzurl); 318 319 if(!$data) { 320 $this->_warn($http->error); 321 $this->_warn($this->getLang('dl_fail')); 322 return false; 323 } 324 325 if(!io_saveFile($this->tgzfile, $data)) { 326 $this->_warn($this->getLang('dl_fail')); 327 return false; 328 } 329 330 self::_say($this->getLang('dl_done'), filesize_h(strlen($data))); 331 332 return true; 333 } 334 335 /** 336 * Unpack the tarball 337 * 338 * @return bool 339 */ 340 private function _step_unpack() { 341 self::_say('<b>'.$this->getLang('pk_extract').'</b>'); 342 343 @set_time_limit(300); 344 @ignore_user_abort(); 345 346 try { 347 $tar = new VerboseTar(); 348 $tar->open($this->tgzfile); 349 $tar->extract($this->tgzdir, 1); 350 $tar->close(); 351 } catch (Exception $e) { 352 $this->_warn($e->getMessage()); 353 $this->_warn($this->getLang('pk_fail')); 354 return false; 355 } 356 357 self::_say($this->getLang('pk_done')); 358 359 self::_say( 360 $this->getLang('pk_version'), 361 hsc(file_get_contents($this->tgzdir.'/VERSION')), 362 getVersion() 363 ); 364 return true; 365 } 366 367 /** 368 * Check permissions of files to change 369 * 370 * @return bool 371 */ 372 private function _step_check() { 373 self::_say($this->getLang('ck_start')); 374 $ok = $this->_traverse('', true); 375 if($ok) { 376 self::_say('<b>'.$this->getLang('ck_done').'</b>'); 377 } else { 378 $this->_warn('<b>'.$this->getLang('ck_fail').'</b>'); 379 } 380 return $ok; 381 } 382 383 /** 384 * Copy over new files 385 * 386 * @return bool 387 */ 388 private function _step_copy() { 389 self::_say($this->getLang('cp_start')); 390 $ok = $this->_traverse('', false); 391 if($ok) { 392 self::_say('<b>'.$this->getLang('cp_done').'</b>'); 393 $this->_rmold(); 394 self::_say('<b>'.$this->getLang('finish').'</b>'); 395 } else { 396 $this->_warn('<b>'.$this->getLang('cp_fail').'</b>'); 397 } 398 return $ok; 399 } 400 401 /** 402 * Delete outdated files 403 */ 404 private function _rmold() { 405 global $conf; 406 407 $list = file($this->tgzdir.'data/deleted.files'); 408 foreach($list as $line) { 409 $line = trim(preg_replace('/#.*$/', '', $line)); 410 if(!$line) continue; 411 $file = DOKU_INC.$line; 412 if(!file_exists($file)) continue; 413 414 // check that the given file is an case sensitive match 415 if(basename(realpath($file)) != basename($file)) { 416 self::_say($this->getLang('rm_mismatch'), hsc($line)); 417 continue; 418 } 419 420 if((is_dir($file) && $this->_rdel($file)) || 421 @unlink($file) 422 ) { 423 self::_say($this->getLang('rm_done'), hsc($line)); 424 } else { 425 $this->_warn($this->getLang('rm_fail'), hsc($line)); 426 } 427 } 428 // delete install 429 @unlink(DOKU_INC.'install.php'); 430 431 // make sure update message will be gone 432 @touch(DOKU_INC.'doku.php'); 433 @unlink($conf['cachedir'].'/messages.txt'); 434 } 435 436 /** 437 * Traverse over the given dir and compare it to the DokuWiki dir 438 * 439 * Checks what files need an update, tests for writability and copies 440 * 441 * @param string $dir 442 * @param bool $dryrun do not copy but only check permissions 443 * @return bool 444 */ 445 private function _traverse($dir, $dryrun) { 446 $base = $this->tgzdir; 447 $ok = true; 448 449 $dh = @opendir($base.'/'.$dir); 450 if(!$dh) return false; 451 while(($file = readdir($dh)) !== false) { 452 if($file == '.' || $file == '..') continue; 453 $from = "$base/$dir/$file"; 454 $to = DOKU_INC."$dir/$file"; 455 456 if(is_dir($from)) { 457 if($dryrun) { 458 // just check for writability 459 if(!is_dir($to)) { 460 if(is_dir(dirname($to)) && !is_writable(dirname($to))) { 461 $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 462 $ok = false; 463 } 464 } 465 } 466 467 // recursion 468 if(!$this->_traverse("$dir/$file", $dryrun)) { 469 $ok = false; 470 } 471 } else { 472 $fmd5 = md5(@file_get_contents($from)); 473 $tmd5 = md5(@file_get_contents($to)); 474 if($fmd5 != $tmd5 || !file_exists($to)) { 475 if($dryrun) { 476 // just check for writability 477 if((file_exists($to) && !is_writable($to)) || 478 (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) 479 ) { 480 481 $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 482 $ok = false; 483 } else { 484 self::_say($this->getLang('tv_upd'), hsc("$dir/$file")); 485 } 486 } else { 487 // check dir 488 if(io_mkdir_p(dirname($to))) { 489 // remove existing (avoid case sensitivity problems) 490 if(file_exists($to) && !@unlink($to)) { 491 $this->_warn('<b>'.$this->getLang('tv_nodel').'</b>', hsc("$dir/$file")); 492 $ok = false; 493 } 494 // copy 495 if(!copy($from, $to)) { 496 $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file")); 497 $ok = false; 498 } else { 499 self::_say($this->getLang('tv_done'), hsc("$dir/$file")); 500 } 501 } else { 502 $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir")); 503 $ok = false; 504 } 505 } 506 } 507 } 508 } 509 closedir($dh); 510 return $ok; 511 } 512 513 /** 514 * Figure out the release date from the version string 515 * 516 * @param $version 517 * @return int|string returns 0 if the version can't be read 518 */ 519 public function dateFromVersion($version) { 520 if(preg_match('/(^|[^\d])(\d\d\d\d-\d\d-\d\d)([^\d]|$)/i', $version, $m)) { 521 return $m[2]; 522 } 523 return 0; 524 } 525} 526 527// vim:ts=4:sw=4:et:enc=utf-8: 528