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