1<?php 2/** 3 * DokuWiki Plugin update (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.'update/VerboseTarLib.class.php'; 18 19class admin_plugin_update 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-update.tgz'; 31 $this->tgzdir = $conf['tmpdir'].'/dokuwiki-update/'; 32 } 33 34 function getMenuSort() { return 555; } 35 36 function handle() { 37 } 38 39 public function html() { 40 $abrt = false; 41 $next = false; 42 43 echo '<h1>' . $this->getLang('menu') . '</h1>'; 44#FIXME check and abort on safemode 45 46 $this->_say('<div id="plugin__update">'); 47 // enable auto scroll 48 ?> 49 <script language="javascript" type="text/javascript"> 50 var plugin_update = window.setInterval(function(){ 51 var obj = $('plugin__update'); 52 if(obj) obj.scrollTop = obj.scrollHeight; 53 },25); 54 </script> 55 <?php 56 57 // handle current step 58 $this->_stepit(&$abrt, &$next); 59 60 // disable auto scroll 61 ?> 62 <script language="javascript" type="text/javascript"> 63 window.setTimeout(function(){ 64 window.clearInterval(plugin_update); 65 },50); 66 </script> 67 <?php 68 $this->_say('</div>'); 69 70#FIXME add security check 71 echo '<form action="" method="get" id="plugin__update_form">'; 72 echo '<input type="hidden" name="do" value="admin" />'; 73 echo '<input type="hidden" name="page" value="update" />'; 74 if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" class="button continue" />'; 75 if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" class="button abort" />'; 76 echo '</form>'; 77 } 78 79 private function _stepit(&$abrt, &$next){ 80 if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){ 81 $step = array_shift(array_keys($_REQUEST['step'])); 82 }else{ 83 $step = ''; 84 } 85 86 if($step == 'cancel'){ 87 # cleanup 88 @unlink($this->tgzfile); 89 $this->_rdel($this->tgzdir); 90 $step = ''; 91 } 92 93 if($step){ 94 $abrt = true; 95 $next = false; 96 if(!file_exists($this->tgzfile)){ 97 if($this->_step_download()) $next = 'unpack'; 98 }elseif(!is_dir($this->tgzdir)){ 99 if($this->_step_unpack()) $next = 'check'; 100 }elseif($step != 'upgrade'){ 101 if($this->_step_copy(true)) $next = 'upgrade'; 102 }elseif($step == 'upgrade'){ 103 if($this->_step_copy(false)) $next = 'cancel'; 104 }else{ 105 echo 'uhm. what happened? where am I? This should not happen'; 106 } 107 }else{ 108 # first time run, show intro 109 echo $this->locale_xhtml('step0'); 110 $abrt = false; 111 $next = 'download'; 112 } 113 } 114 115 private function _say(){ 116 $args = func_get_args(); 117 echo vsprintf(array_shift($args)."<br />\n",$args); 118 flush(); 119 ob_flush(); 120 } 121 122 /** 123 * Recursive delete 124 * 125 * @author Jon Hassall 126 * @link http://de.php.net/manual/en/function.unlink.php#87045 127 */ 128 private function _rdel($dir) { 129 if(!$dh = @opendir($dir)) { 130 return; 131 } 132 while (false !== ($obj = readdir($dh))) { 133 if($obj == '.' || $obj == '..') continue; 134 135 if (!@unlink($dir . '/' . $obj)) { 136 $this->_rdel($dir.'/'.$obj); 137 } 138 } 139 closedir($dh); 140 @rmdir($dir); 141 } 142 143 private function _step_download(){ 144 $this->_say('Downloading from %s',$this->tgzurl); 145 146 @set_time_limit(120); 147 @ignore_user_abort(); 148 149 $http = new DokuHTTPClient(); 150 $http->timeout = 120; 151 $data = $http->get($this->tgzurl); 152 153 if(!$data){ 154 $this->_say($http->error); 155 $this->_say("Download failed."); 156 return false; 157 } 158 159 $this->_say('Received %d bytes',strlen($data)); 160 161 if(!io_saveFile($this->tgzfile,$data)){ 162 $this->_say("Failed to save download."); 163 return false; 164 } 165 166 return true; 167 } 168 169 private function _step_unpack(){ 170 global $conf; 171 $this->_say('<b>Extracting the archive...</b>'); 172 173 @set_time_limit(120); 174 @ignore_user_abort(); 175 176 $tar = new VerboseTarLib($this->tgzfile); 177 if($tar->_initerror < 0){ 178 $this->_say($tar->TarErrorStr($tar->_initerror)); 179 $this->_say('Extraction failed on init'); 180 return false; 181 } 182 183 $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/'); 184 if($ok < 1){ 185 $this->_say($tar->TarErrorStr($ok)); 186 $this->_say('Extraction failed'); 187 return false; 188 } 189 190 $this->_say('Extraction done.'); 191 192 $this->_say('Version <b>%s</b> ready to install. Your current version is <b>%s</b>.', 193 hsc(file_get_contents($this->tgzdir.'/VERSION')), 194 getVersion()); 195 return true; 196 } 197 198 private function _step_copy($dryrun=true){ 199 $ok = $this->_traverse('',$dryrun); 200 if($dryrun){ 201 if($ok){ 202 $this->_say('<b>All files are writable, ready to upgrade</b>'); 203 }else{ 204 $this->_say('<b>Some files aren\'t writable. Uprade not possible.</b>'); 205 } 206 }else{ 207 if($ok){ 208 $this->_say('<b>All files upgraded successfully</b>'); 209 210 #FIXME delete unused files 211 }else{ 212 $this->_say('<b>Some files couldn\'t be upgraded. Uh-oh. Better check manually.</b>'); 213 } 214 } 215 return $ok; 216 } 217 218 private function _traverse($dir,$dryrun){ 219 $base = $this->tgzdir; 220 $ok = true; 221 222 $dh = @opendir($base.'/'.$dir); 223 if(!$dh) return; 224 while(($file = readdir($dh)) !== false){ 225 if($file == '.' || $file == '..') continue; 226 $from = "$base/$dir/$file"; 227 $to = DOKU_INC."$dir/$file"; 228 229 if(is_dir($from)){ 230 if($dryrun){ 231 // just check for writability 232 if(!is_dir($to)){ 233 if(is_dir(dirname($to)) && !is_writable(dirname($to))){ 234 $this->_say("<b>%s is not writable</b>",hsc("$dir/$file")); 235 $ok = false; 236 } 237 } 238 } 239 240 // recursion 241 if(!$this->_traverse("$dir/$file",$dryrun)){ 242 $ok = false; 243 } 244 }else{ 245 $fmd5 = md5(@file_get_contents($from)); 246 $tmd5 = md5(@file_get_contents($to)); 247 if($fmd5 != $tmd5){ 248 if($dryrun){ 249 // just check for writability 250 if( (file_exists($to) && !is_writable($to)) || 251 (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){ 252 253 $this->_say("<b>%s is not writable</b>",hsc("$dir/$file")); 254 $ok = false; 255 }else{ 256 $this->_say("%s needs update",hsc("$dir/$file")); 257 } 258 }else{ 259 // check dir 260 if(io_mkdir_p(dirname($to))){ 261 // copy 262 if(!copy($from,$to)){ 263 $this->_say("<b>%s couldn't be copied</b>",hsc("$dir/$file")); 264 $ok = false; 265 }else{ 266 $this->_say("%s updated",hsc("$dir/$file")); 267 } 268 }else{ 269 $this->_say("<b>failed to create %s</b>",hsc("$dir")); 270 $ok = false; 271 } 272 } 273 } 274 } 275 } 276 closedir($dh); 277 return $ok; 278 } 279} 280 281// vim:ts=4:sw=4:et:enc=utf-8: 282