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 $this->_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 $this->_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 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 = $http->get($this->tgzversion); 240 if(!$tgzversion) { 241 $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error)); 242 $ok = false; 243 } 244 if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) { 245 $this->_warn($this->getLang('vs_tgzno')); 246 $ok = false; 247 $tgzversionnum = 0; 248 } else { 249 $tgzversionnum = $m[2]; 250 $this->_say($this->getLang('vs_tgz'), $tgzversion); 251 } 252 253 // get the current version 254 $version = getVersion(); 255 if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) { 256 $versionnum = 0; 257 } else { 258 $versionnum = $m[2]; 259 } 260 $this->_say($this->getLang('vs_local'), $version); 261 262 // compare versions 263 if(!$versionnum) { 264 $this->_warn($this->getLang('vs_localno')); 265 $ok = false; 266 } else if($tgzversionnum) { 267 if($tgzversionnum < $versionnum) { 268 $this->_warn($this->getLang('vs_newer')); 269 $ok = false; 270 } elseif($tgzversionnum == $versionnum) { 271 $this->_warn($this->getLang('vs_same')); 272 $ok = false; 273 } 274 } 275 276 // check plugin version 277 $pluginversion = $http->get($this->pluginversion); 278 if($pluginversion) { 279 $plugininfo = linesToHash(explode("\n", $pluginversion)); 280 $myinfo = $this->getInfo(); 281 if($plugininfo['date'] > $myinfo['date']) { 282 $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']); 283 $ok = false; 284 } 285 } 286 287 // check if PHP is up to date 288 $minphp = '5.6'; 289 if(version_compare(phpversion(), $minphp, '<')) { 290 $this->_warn($this->getLang('vs_php'), $minphp, phpversion()); 291 $ok = false; 292 } 293 294 return $ok; 295 } 296 297 /** 298 * Redirect to the start page 299 */ 300 private function _step_done() { 301 if (function_exists('opcache_reset')) { 302 opcache_reset(); 303 } 304 echo $this->getLang('finish'); 305 echo "<script type='text/javascript'>location.href='".DOKU_URL."';</script>"; 306 } 307 308 /** 309 * Download the tarball 310 * 311 * @return bool 312 */ 313 private function _step_download() { 314 $this->_say($this->getLang('dl_from'), $this->tgzurl); 315 316 @set_time_limit(300); 317 @ignore_user_abort(); 318 319 $http = new DokuHTTPClient(); 320 $http->timeout = 300; 321 $data = $http->get($this->tgzurl); 322 323 if(!$data) { 324 $this->_warn($http->error); 325 $this->_warn($this->getLang('dl_fail')); 326 return false; 327 } 328 329 if(!io_saveFile($this->tgzfile, $data)) { 330 $this->_warn($this->getLang('dl_fail')); 331 return false; 332 } 333 334 $this->_say($this->getLang('dl_done'), filesize_h(strlen($data))); 335 336 return true; 337 } 338 339 /** 340 * Unpack the tarball 341 * 342 * @return bool 343 */ 344 private function _step_unpack() { 345 $this->_say('<b>'.$this->getLang('pk_extract').'</b>'); 346 347 @set_time_limit(300); 348 @ignore_user_abort(); 349 350 try { 351 $tar = new VerboseTar(); 352 $tar->open($this->tgzfile); 353 $tar->extract($this->tgzdir, 1); 354 $tar->close(); 355 } catch (Exception $e) { 356 $this->_warn($e->getMessage()); 357 $this->_warn($this->getLang('pk_fail')); 358 return false; 359 } 360 361 $this->_say($this->getLang('pk_done')); 362 363 $this->_say( 364 $this->getLang('pk_version'), 365 hsc(file_get_contents($this->tgzdir.'/VERSION')), 366 getVersion() 367 ); 368 return true; 369 } 370 371 /** 372 * Check permissions of files to change 373 * 374 * @return bool 375 */ 376 private function _step_check() { 377 $this->_say($this->getLang('ck_start')); 378 $ok = $this->_traverse('', true); 379 if($ok) { 380 $this->_say('<b>'.$this->getLang('ck_done').'</b>'); 381 } else { 382 $this->_warn('<b>'.$this->getLang('ck_fail').'</b>'); 383 } 384 return $ok; 385 } 386 387 /** 388 * Copy over new files 389 * 390 * @return bool 391 */ 392 private function _step_copy() { 393 $this->_say($this->getLang('cp_start')); 394 $ok = $this->_traverse('', false); 395 if($ok) { 396 $this->_say('<b>'.$this->getLang('cp_done').'</b>'); 397 $this->_rmold(); 398 $this->_say('<b>'.$this->getLang('finish').'</b>'); 399 } else { 400 $this->_warn('<b>'.$this->getLang('cp_fail').'</b>'); 401 } 402 return $ok; 403 } 404 405 /** 406 * Delete outdated files 407 */ 408 private function _rmold() { 409 global $conf; 410 411 $list = file($this->tgzdir.'data/deleted.files'); 412 foreach($list as $line) { 413 $line = trim(preg_replace('/#.*$/', '', $line)); 414 if(!$line) continue; 415 $file = DOKU_INC.$line; 416 if(!file_exists($file)) continue; 417 418 // check that the given file is an case sensitive match 419 if(basename(realpath($file)) != basename($file)) { 420 $this->_say($this->getLang('rm_mismatch'), hsc($line)); 421 continue; 422 } 423 424 if((is_dir($file) && $this->_rdel($file)) || 425 @unlink($file) 426 ) { 427 $this->_say($this->getLang('rm_done'), hsc($line)); 428 } else { 429 $this->_warn($this->getLang('rm_fail'), hsc($line)); 430 } 431 } 432 // delete install 433 @unlink(DOKU_INC.'install.php'); 434 435 // make sure update message will be gone 436 @touch(DOKU_INC.'doku.php'); 437 @unlink($conf['cachedir'].'/messages.txt'); 438 } 439 440 /** 441 * Traverse over the given dir and compare it to the DokuWiki dir 442 * 443 * Checks what files need an update, tests for writability and copies 444 * 445 * @param string $dir 446 * @param bool $dryrun do not copy but only check permissions 447 * @return bool 448 */ 449 private function _traverse($dir, $dryrun) { 450 $base = $this->tgzdir; 451 $ok = true; 452 453 $dh = @opendir($base.'/'.$dir); 454 if(!$dh) return false; 455 while(($file = readdir($dh)) !== false) { 456 if($file == '.' || $file == '..') continue; 457 $from = "$base/$dir/$file"; 458 $to = DOKU_INC."$dir/$file"; 459 460 if(is_dir($from)) { 461 if($dryrun) { 462 // just check for writability 463 if(!is_dir($to)) { 464 if(is_dir(dirname($to)) && !is_writable(dirname($to))) { 465 $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 466 $ok = false; 467 } 468 } 469 } 470 471 // recursion 472 if(!$this->_traverse("$dir/$file", $dryrun)) { 473 $ok = false; 474 } 475 } else { 476 $fmd5 = md5(@file_get_contents($from)); 477 $tmd5 = md5(@file_get_contents($to)); 478 if($fmd5 != $tmd5 || !file_exists($to)) { 479 if($dryrun) { 480 // just check for writability 481 if((file_exists($to) && !is_writable($to)) || 482 (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) 483 ) { 484 485 $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file")); 486 $ok = false; 487 } else { 488 $this->_say($this->getLang('tv_upd'), hsc("$dir/$file")); 489 } 490 } else { 491 // check dir 492 if(io_mkdir_p(dirname($to))) { 493 // remove existing (avoid case sensitivity problems) 494 if(file_exists($to) && !@unlink($to)) { 495 $this->_warn('<b>'.$this->getLang('tv_nodel').'</b>', hsc("$dir/$file")); 496 $ok = false; 497 } 498 // copy 499 if(!copy($from, $to)) { 500 $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file")); 501 $ok = false; 502 } else { 503 $this->_say($this->getLang('tv_done'), hsc("$dir/$file")); 504 } 505 } else { 506 $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir")); 507 $ok = false; 508 } 509 } 510 } 511 } 512 } 513 closedir($dh); 514 return $ok; 515 } 516} 517 518// vim:ts=4:sw=4:et:enc=utf-8: 519