134aae6dbSAndreas Gohr<?php 234aae6dbSAndreas Gohr/** 334aae6dbSAndreas Gohr * DokuWiki Plugin update (Admin Component) 434aae6dbSAndreas Gohr * 534aae6dbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 634aae6dbSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 734aae6dbSAndreas Gohr */ 834aae6dbSAndreas Gohr 934aae6dbSAndreas Gohr// must be run within Dokuwiki 1034aae6dbSAndreas Gohrif (!defined('DOKU_INC')) die(); 1134aae6dbSAndreas Gohr 1234aae6dbSAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 1334aae6dbSAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 1434aae6dbSAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 1534aae6dbSAndreas Gohr 1634aae6dbSAndreas Gohrrequire_once DOKU_PLUGIN.'admin.php'; 1734aae6dbSAndreas Gohrrequire_once DOKU_PLUGIN.'update/VerboseTarLib.class.php'; 1834aae6dbSAndreas Gohr 1934aae6dbSAndreas Gohrclass admin_plugin_update extends DokuWiki_Admin_Plugin { 2034aae6dbSAndreas Gohr private $tgzurl; 2134aae6dbSAndreas Gohr private $tgzfile; 2234aae6dbSAndreas Gohr private $tgzdir; 2334aae6dbSAndreas Gohr 2434aae6dbSAndreas Gohr public function __construct(){ 2534aae6dbSAndreas Gohr global $conf; 2634aae6dbSAndreas Gohr 2734aae6dbSAndreas Gohr $branch = 'stable'; 2834aae6dbSAndreas Gohr 2934aae6dbSAndreas Gohr $this->tgzurl = 'http://github.com/splitbrain/dokuwiki/tarball/'.$branch; 3034aae6dbSAndreas Gohr $this->tgzfile = $conf['tmpdir'].'/dokuwiki-update.tgz'; 3134aae6dbSAndreas Gohr $this->tgzdir = $conf['tmpdir'].'/dokuwiki-update/'; 3234aae6dbSAndreas Gohr } 3334aae6dbSAndreas Gohr 3434aae6dbSAndreas Gohr function getMenuSort() { return 555; } 3534aae6dbSAndreas Gohr 3634aae6dbSAndreas Gohr function handle() { 3734aae6dbSAndreas Gohr } 3834aae6dbSAndreas Gohr 3934aae6dbSAndreas Gohr public function html() { 4034aae6dbSAndreas Gohr $abrt = false; 4134aae6dbSAndreas Gohr $next = false; 4234aae6dbSAndreas Gohr 4334aae6dbSAndreas Gohr echo '<h1>' . $this->getLang('menu') . '</h1>'; 4434aae6dbSAndreas Gohr#FIXME check and abort on safemode 4534aae6dbSAndreas Gohr 4634aae6dbSAndreas Gohr $this->_stepit(&$abrt, &$next); 4734aae6dbSAndreas Gohr 4834aae6dbSAndreas Gohr#FIXME add security check 4934aae6dbSAndreas Gohr echo '<form action="" method="get">'; 5034aae6dbSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 5134aae6dbSAndreas Gohr echo '<input type="hidden" name="page" value="update" />'; 5234aae6dbSAndreas Gohr if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" />'; 5334aae6dbSAndreas Gohr if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" />'; 5434aae6dbSAndreas Gohr echo '</form>'; 5534aae6dbSAndreas Gohr } 5634aae6dbSAndreas Gohr 5734aae6dbSAndreas Gohr private function _stepit(&$abrt, &$next){ 5834aae6dbSAndreas Gohr if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){ 5934aae6dbSAndreas Gohr $step = array_shift(array_keys($_REQUEST['step'])); 6034aae6dbSAndreas Gohr }else{ 6134aae6dbSAndreas Gohr $step = ''; 6234aae6dbSAndreas Gohr } 6334aae6dbSAndreas Gohr 6434aae6dbSAndreas Gohr if($step == 'cancel'){ 6534aae6dbSAndreas Gohr # cleanup 6634aae6dbSAndreas Gohr @unlink($this->tgzfile); 676b2d1b30SAndreas Gohr $this->_rdel($this->tgzdir); 6834aae6dbSAndreas Gohr $step = ''; 6934aae6dbSAndreas Gohr } 7034aae6dbSAndreas Gohr 7134aae6dbSAndreas Gohr if($step){ 7234aae6dbSAndreas Gohr $abrt = true; 7334aae6dbSAndreas Gohr $next = false; 7434aae6dbSAndreas Gohr $this->_say('<div id="plugin__update">'); 7534aae6dbSAndreas Gohr if(!file_exists($this->tgzfile)){ 7634aae6dbSAndreas Gohr if($this->_step_download()) $next = 'unpack'; 7734aae6dbSAndreas Gohr }elseif(!is_dir($this->tgzdir)){ 7834aae6dbSAndreas Gohr if($this->_step_unpack()) $next = 'check'; 7934aae6dbSAndreas Gohr }elseif($step != 'upgrade'){ 8034aae6dbSAndreas Gohr if($this->_step_copy(true)) $next = 'upgrade'; 8134aae6dbSAndreas Gohr }elseif($step == 'upgrade'){ 8234aae6dbSAndreas Gohr if($this->_step_copy(false)) $next = 'cancel'; 8334aae6dbSAndreas Gohr }else{ 8434aae6dbSAndreas Gohr #continue 8534aae6dbSAndreas Gohr echo 'huh'; 8634aae6dbSAndreas Gohr } 8734aae6dbSAndreas Gohr $this->_say('</div>'); 8834aae6dbSAndreas Gohr }else{ 8934aae6dbSAndreas Gohr # first time run, show intro 9034aae6dbSAndreas Gohr echo $this->locale_xhtml('step0'); 9134aae6dbSAndreas Gohr $abrt = false; 9234aae6dbSAndreas Gohr $next = 'download'; 9334aae6dbSAndreas Gohr } 9434aae6dbSAndreas Gohr } 9534aae6dbSAndreas Gohr 9634aae6dbSAndreas Gohr private function _say(){ 9734aae6dbSAndreas Gohr $args = func_get_args(); 9834aae6dbSAndreas Gohr echo vsprintf(array_shift($args)."<br />\n",$args); 9934aae6dbSAndreas Gohr flush(); 10034aae6dbSAndreas Gohr ob_flush(); 10134aae6dbSAndreas Gohr } 10234aae6dbSAndreas Gohr 1036b2d1b30SAndreas Gohr /** 1046b2d1b30SAndreas Gohr * Recursive delete 1056b2d1b30SAndreas Gohr * 106*9285faa5SAndreas Gohr * @author Jon Hassall 107*9285faa5SAndreas Gohr * @link http://de.php.net/manual/en/function.unlink.php#87045 1086b2d1b30SAndreas Gohr */ 109*9285faa5SAndreas Gohr private function _rdel($dir) { 110*9285faa5SAndreas Gohr if(!$dh = @opendir($dir)) { 111*9285faa5SAndreas Gohr return; 112*9285faa5SAndreas Gohr } 113*9285faa5SAndreas Gohr while (false !== ($obj = readdir($dh))) { 114*9285faa5SAndreas Gohr if($obj == '.' || $obj == '..') continue; 115*9285faa5SAndreas Gohr 116*9285faa5SAndreas Gohr if (!@unlink($dir . '/' . $obj)) { 117*9285faa5SAndreas Gohr $this->_rdel($dir.'/'.$obj); 118*9285faa5SAndreas Gohr } 119*9285faa5SAndreas Gohr } 120*9285faa5SAndreas Gohr closedir($dh); 121*9285faa5SAndreas Gohr @rmdir($dir); 1226b2d1b30SAndreas Gohr } 1236b2d1b30SAndreas Gohr 12434aae6dbSAndreas Gohr private function _step_download(){ 12534aae6dbSAndreas Gohr $this->_say('Downloading from %s',$this->tgzurl); 12634aae6dbSAndreas Gohr 12734aae6dbSAndreas Gohr @set_time_limit(120); 12834aae6dbSAndreas Gohr @ignore_user_abort(); 12934aae6dbSAndreas Gohr 13034aae6dbSAndreas Gohr $http = new DokuHTTPClient(); 13134aae6dbSAndreas Gohr $http->timeout = 120; 13234aae6dbSAndreas Gohr $data = $http->get($this->tgzurl); 13334aae6dbSAndreas Gohr 13434aae6dbSAndreas Gohr if(!$data){ 13534aae6dbSAndreas Gohr $this->_say($http->error); 13634aae6dbSAndreas Gohr $this->_say("Download failed."); 13734aae6dbSAndreas Gohr return false; 13834aae6dbSAndreas Gohr } 13934aae6dbSAndreas Gohr 14034aae6dbSAndreas Gohr $this->_say('Received %d bytes',strlen($data)); 14134aae6dbSAndreas Gohr 14234aae6dbSAndreas Gohr if(!io_saveFile($this->tgzfile,$data)){ 14334aae6dbSAndreas Gohr $this->_say("Failed to save download."); 14434aae6dbSAndreas Gohr return false; 14534aae6dbSAndreas Gohr } 14634aae6dbSAndreas Gohr 14734aae6dbSAndreas Gohr return true; 14834aae6dbSAndreas Gohr } 14934aae6dbSAndreas Gohr 15034aae6dbSAndreas Gohr private function _step_unpack(){ 15134aae6dbSAndreas Gohr global $conf; 15234aae6dbSAndreas Gohr $this->_say('Extracting the archive...'); 15334aae6dbSAndreas Gohr 15434aae6dbSAndreas Gohr @set_time_limit(120); 15534aae6dbSAndreas Gohr @ignore_user_abort(); 15634aae6dbSAndreas Gohr 15734aae6dbSAndreas Gohr $tar = new VerboseTarLib($this->tgzfile); 15834aae6dbSAndreas Gohr if($tar->_initerror < 0){ 15934aae6dbSAndreas Gohr $this->_say($tar->TarErrorStr($tar->_initerror)); 16034aae6dbSAndreas Gohr $this->_say('Extraction failed on init'); 16134aae6dbSAndreas Gohr return false; 16234aae6dbSAndreas Gohr } 16334aae6dbSAndreas Gohr 16434aae6dbSAndreas Gohr $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/'); 16534aae6dbSAndreas Gohr if($ok < 1){ 16634aae6dbSAndreas Gohr $this->_say($tar->TarErrorStr($ok)); 16734aae6dbSAndreas Gohr $this->_say('Extraction failed'); 16834aae6dbSAndreas Gohr return false; 16934aae6dbSAndreas Gohr } 17034aae6dbSAndreas Gohr 17134aae6dbSAndreas Gohr $this->_say('Extraction done.'); 17234aae6dbSAndreas Gohr return true; 17334aae6dbSAndreas Gohr } 17434aae6dbSAndreas Gohr 17534aae6dbSAndreas Gohr private function _step_copy($dryrun=true){ 17634aae6dbSAndreas Gohr $ok = $this->_traverse('',$dryrun); 17734aae6dbSAndreas Gohr if($dryrun){ 17834aae6dbSAndreas Gohr if($ok){ 17934aae6dbSAndreas Gohr $this->_say('<b>All files are writable, ready to upgrade</b>'); 18034aae6dbSAndreas Gohr }else{ 18134aae6dbSAndreas Gohr $this->_say('<b>Some files aren\'t writable. Uprade not possible.</b>'); 18234aae6dbSAndreas Gohr } 18334aae6dbSAndreas Gohr }else{ 18434aae6dbSAndreas Gohr if($ok){ 18534aae6dbSAndreas Gohr $this->_say('<b>All files upgraded successfully</b>'); 18634aae6dbSAndreas Gohr 18734aae6dbSAndreas Gohr #FIXME delete unused files 18834aae6dbSAndreas Gohr }else{ 18934aae6dbSAndreas Gohr $this->_say('<b>Some files couldn\'t be upgraded. Uh-oh. Better check manually.</b>'); 19034aae6dbSAndreas Gohr } 19134aae6dbSAndreas Gohr } 19234aae6dbSAndreas Gohr return $ok; 19334aae6dbSAndreas Gohr } 19434aae6dbSAndreas Gohr 19534aae6dbSAndreas Gohr private function _traverse($dir,$dryrun){ 19634aae6dbSAndreas Gohr $base = $this->tgzdir; 19734aae6dbSAndreas Gohr $ok = true; 19834aae6dbSAndreas Gohr 19934aae6dbSAndreas Gohr $dh = @opendir($base.'/'.$dir); 20034aae6dbSAndreas Gohr if(!$dh) return; 20134aae6dbSAndreas Gohr while(($file = readdir($dh)) !== false){ 20234aae6dbSAndreas Gohr if($file == '.' || $file == '..') continue; 20334aae6dbSAndreas Gohr $from = "$base/$dir/$file"; 20434aae6dbSAndreas Gohr $to = DOKU_INC."$dir/$file"; 20534aae6dbSAndreas Gohr 20634aae6dbSAndreas Gohr if(is_dir($from)){ 20734aae6dbSAndreas Gohr if($dryrun){ 20834aae6dbSAndreas Gohr // just check for writability 20934aae6dbSAndreas Gohr if(!is_dir($to)){ 21034aae6dbSAndreas Gohr if(is_dir(dirname($to)) && !is_writable(dirname($to))){ 21134aae6dbSAndreas Gohr $this->_say("<b>%s is not writable</b>",hsc("$dir/$file")); 21234aae6dbSAndreas Gohr $ok = false; 21334aae6dbSAndreas Gohr } 21434aae6dbSAndreas Gohr } 21534aae6dbSAndreas Gohr } 21634aae6dbSAndreas Gohr 21734aae6dbSAndreas Gohr // recursion 21834aae6dbSAndreas Gohr if(!$this->_traverse("$dir/$file",$dryrun)){ 21934aae6dbSAndreas Gohr $ok = false; 22034aae6dbSAndreas Gohr } 22134aae6dbSAndreas Gohr }else{ 222f2fa6d10SAndreas Gohr $fmd5 = md5(@file_get_contents($from)); 223f2fa6d10SAndreas Gohr $tmd5 = md5(@file_get_contents($to)); 22434aae6dbSAndreas Gohr if($fmd5 != $tmd5){ 22534aae6dbSAndreas Gohr if($dryrun){ 22634aae6dbSAndreas Gohr // just check for writability 22734aae6dbSAndreas Gohr if( (file_exists($to) && !is_writable($to)) || 22834aae6dbSAndreas Gohr (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){ 22934aae6dbSAndreas Gohr 23034aae6dbSAndreas Gohr $this->_say("<b>%s is not writable</b>",hsc("$dir/$file")); 23134aae6dbSAndreas Gohr $ok = false; 23234aae6dbSAndreas Gohr }else{ 23334aae6dbSAndreas Gohr $this->_say("%s needs update",hsc("$dir/$file")); 23434aae6dbSAndreas Gohr } 23534aae6dbSAndreas Gohr }else{ 23634aae6dbSAndreas Gohr // check dir 23734aae6dbSAndreas Gohr if(io_mkdir_p(dirname($to))){ 23834aae6dbSAndreas Gohr // copy 23934aae6dbSAndreas Gohr if(!copy($from,$to)){ 24034aae6dbSAndreas Gohr $this->_say("<b>%s couldn't be copied</b>",hsc("$dir/$file")); 24134aae6dbSAndreas Gohr $ok = false; 24234aae6dbSAndreas Gohr }else{ 24334aae6dbSAndreas Gohr $this->_say("%s updated",hsc("$dir/$file")); 24434aae6dbSAndreas Gohr } 24534aae6dbSAndreas Gohr }else{ 24634aae6dbSAndreas Gohr $this->_say("<b>failed to create %s</b>",hsc("$dir")); 24734aae6dbSAndreas Gohr $ok = false; 24834aae6dbSAndreas Gohr } 24934aae6dbSAndreas Gohr } 25034aae6dbSAndreas Gohr } 25134aae6dbSAndreas Gohr } 25234aae6dbSAndreas Gohr } 25334aae6dbSAndreas Gohr closedir($dh); 25434aae6dbSAndreas Gohr return $ok; 25534aae6dbSAndreas Gohr } 25634aae6dbSAndreas Gohr} 25734aae6dbSAndreas Gohr 25834aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8: 259