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(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15 16require_once DOKU_PLUGIN.'admin.php'; 17require_once DOKU_PLUGIN.'upgrade/VerboseTarLib.class.php'; 18 19class admin_plugin_upgrade extends DokuWiki_Admin_Plugin { 20 private $tgzurl; 21 private $tgzfile; 22 private $tgzdir; 23 24 public function __construct(){ 25 global $conf; 26 27 $branch = 'stable'; 28 29 $this->tgzurl = 'http://github.com/splitbrain/dokuwiki/tarball/'.$branch; 30 $this->tgzfile = $conf['tmpdir'].'/dokuwiki-upgrade.tgz'; 31 $this->tgzdir = $conf['tmpdir'].'/dokuwiki-upgrade/'; 32 } 33 34 public function getMenuSort() { return 555; } 35 36 public function handle() { 37 if($_REQUEST['step'] && !checkSecurityToken()){ 38 unset($_REQUEST['step']); 39 } 40 } 41 42 public function html() { 43 global $ID; 44 $abrt = false; 45 $next = false; 46 47 echo '<h1>' . $this->getLang('menu') . '</h1>'; 48 49 $this->_say('<div id="plugin__upgrade">'); 50 // enable auto scroll 51 ?> 52 <script language="javascript" type="text/javascript"> 53 var plugin_upgrade = window.setInterval(function(){ 54 var obj = $('plugin__upgrade'); 55 if(obj) obj.scrollTop = obj.scrollHeight; 56 },25); 57 </script> 58 <?php 59 60 // handle current step 61 $this->_stepit($abrt, $next); 62 63 // disable auto scroll 64 ?> 65 <script language="javascript" type="text/javascript"> 66 window.setTimeout(function(){ 67 window.clearInterval(plugin_upgrade); 68 },50); 69 </script> 70 <?php 71 $this->_say('</div>'); 72 73 echo '<form action="" method="get" id="plugin__upgrade_form">'; 74 echo '<input type="hidden" name="do" value="admin" />'; 75 echo '<input type="hidden" name="page" value="upgrade" />'; 76 echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'; 77 if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" class="button continue" />'; 78 if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" class="button abort" />'; 79 echo '</form>'; 80 } 81 82 private function _stepit(&$abrt, &$next){ 83 global $conf; 84 if($conf['safemodehack']){ 85 $abrt = false; 86 $next = false; 87 echo $this->locale_xhtml('safemode'); 88 } 89 90 if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){ 91 $step = array_shift(array_keys($_REQUEST['step'])); 92 }else{ 93 $step = ''; 94 } 95 96 if($step == 'cancel'){ 97 # cleanup 98 @unlink($this->tgzfile); 99 $this->_rdel($this->tgzdir); 100 $step = ''; 101 } 102 103 if($step){ 104 $abrt = true; 105 $next = false; 106 if(!file_exists($this->tgzfile)){ 107 if($this->_step_download()) $next = 'unpack'; 108 }elseif(!is_dir($this->tgzdir)){ 109 if($this->_step_unpack()) $next = 'check'; 110 }elseif($step != 'upgrade'){ 111 if($this->_step_check()) $next = 'upgrade'; 112 }elseif($step == 'upgrade'){ 113 if($this->_step_copy()) $next = 'cancel'; 114 }else{ 115 echo 'uhm. what happened? where am I? This should not happen'; 116 } 117 }else{ 118 # first time run, show intro 119 echo $this->locale_xhtml('step0'); 120 $abrt = false; 121 $next = 'download'; 122 } 123 } 124 125 private function _say(){ 126 $args = func_get_args(); 127 echo vsprintf(array_shift($args)."<br />\n",$args); 128 flush(); 129 ob_flush(); 130 } 131 132 /** 133 * Recursive delete 134 * 135 * @author Jon Hassall 136 * @link http://de.php.net/manual/en/function.unlink.php#87045 137 */ 138 private function _rdel($dir) { 139 if(!$dh = @opendir($dir)) { 140 return; 141 } 142 while (false !== ($obj = readdir($dh))) { 143 if($obj == '.' || $obj == '..') continue; 144 145 if (!@unlink($dir . '/' . $obj)) { 146 $this->_rdel($dir.'/'.$obj); 147 } 148 } 149 closedir($dh); 150 return @rmdir($dir); 151 } 152 153 private function _step_download(){ 154 $this->_say($this->getLang('dl_from'),$this->tgzurl); 155 156 @set_time_limit(120); 157 @ignore_user_abort(); 158 159 $http = new DokuHTTPClient(); 160 $http->timeout = 120; 161 $data = $http->get($this->tgzurl); 162 163 if(!$data){ 164 $this->_say($http->error); 165 $this->_say($this->getLang('dl_fail')); 166 return false; 167 } 168 169 if(!io_saveFile($this->tgzfile,$data)){ 170 $this->_say($this->getLang('dl_fail')); 171 return false; 172 } 173 174 $this->_say($this->getLang('dl_done'),filesize_h(strlen($data))); 175 176 return true; 177 } 178 179 private function _step_unpack(){ 180 global $conf; 181 $this->_say('<b>'.$this->getLang('pk_extract').'</b>'); 182 183 @set_time_limit(120); 184 @ignore_user_abort(); 185 186 $tar = new VerboseTarLib($this->tgzfile); 187 if($tar->_initerror < 0){ 188 $this->_say($tar->TarErrorStr($tar->_initerror)); 189 $this->_say($this->getLang('pk_fail')); 190 return false; 191 } 192 193 $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/'); 194 if($ok < 1){ 195 $this->_say($tar->TarErrorStr($ok)); 196 $this->_say($this->getLang('pk_fail')); 197 return false; 198 } 199 200 $this->_say($this->getLang('pk_done')); 201 202 $this->_say($this->getLang('pk_version'), 203 hsc(file_get_contents($this->tgzdir.'/VERSION')), 204 getVersion()); 205 return true; 206 } 207 208 private function _step_check(){ 209 $this->_say($this->getLang('ck_start')); 210 $ok = $this->_traverse('',true); 211 if($ok){ 212 $this->_say('<b>'.$this->getLang('ck_done').'</b>'); 213 }else{ 214 $this->_say('<b>'.$this->getLang('ck_fail').'</b>'); 215 } 216 return $ok; 217 } 218 219 private function _step_copy(){ 220 $this->_say($this->getLang('cp_start')); 221 $ok = $this->_traverse('',false); 222 if($ok){ 223 $this->_say('<b>'.$this->getLang('cp_done').'</b>'); 224 $this->_rmold(); 225 $this->_say('<b>'.$this->getLang('finish').'</b>'); 226 }else{ 227 $this->_say('<b>'.$this->getLang('cp_fail').'</b>'); 228 } 229 return $ok; 230 } 231 232 private function _rmold(){ 233 $list = file($this->tgzdir.'data/deleted.files'); 234 foreach($list as $line){ 235 $line = trim(preg_replace('/#.*$/','',$line)); 236 if(!$line) continue; 237 $file = DOKU_INC.$line; 238 if(!file_exists($file)) continue; 239 if( (is_dir($file) && $this->_rdel($file)) || 240 @unlink($file)){ 241 $this->_say($this->getLang('rm_done'),hsc($line)); 242 }else{ 243 $this->_say($this->getLang('rm_fail'),hsc($line)); 244 } 245 } 246 } 247 248 private function _traverse($dir,$dryrun){ 249 $base = $this->tgzdir; 250 $ok = true; 251 252 $dh = @opendir($base.'/'.$dir); 253 if(!$dh) return; 254 while(($file = readdir($dh)) !== false){ 255 if($file == '.' || $file == '..') continue; 256 $from = "$base/$dir/$file"; 257 $to = DOKU_INC."$dir/$file"; 258 259 if(is_dir($from)){ 260 if($dryrun){ 261 // just check for writability 262 if(!is_dir($to)){ 263 if(is_dir(dirname($to)) && !is_writable(dirname($to))){ 264 $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file")); 265 $ok = false; 266 } 267 } 268 } 269 270 // recursion 271 if(!$this->_traverse("$dir/$file",$dryrun)){ 272 $ok = false; 273 } 274 }else{ 275 $fmd5 = md5(@file_get_contents($from)); 276 $tmd5 = md5(@file_get_contents($to)); 277 if($fmd5 != $tmd5 || !file_exists($to)){ 278 if($dryrun){ 279 // just check for writability 280 if( (file_exists($to) && !is_writable($to)) || 281 (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){ 282 283 $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file")); 284 $ok = false; 285 }else{ 286 $this->_say($this->getLang('tv_upd'),hsc("$dir/$file")); 287 } 288 }else{ 289 // check dir 290 if(io_mkdir_p(dirname($to))){ 291 // copy 292 if(!copy($from,$to)){ 293 $this->_say('<b>'.$this->getLang('tv_nocopy').'</b>',hsc("$dir/$file")); 294 $ok = false; 295 }else{ 296 $this->_say($this->getLang('tv_done'),hsc("$dir/$file")); 297 } 298 }else{ 299 $this->_say('<b>'.$this->getLang('tv_nodir').'</b>',hsc("$dir")); 300 $ok = false; 301 } 302 } 303 } 304 } 305 } 306 closedir($dh); 307 return $ok; 308 } 309} 310 311// vim:ts=4:sw=4:et:enc=utf-8: 312