xref: /plugin/upgrade/admin.php (revision ff284f1ffc7670448a4f3d06e940359e253184f5)
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
19*ff284f1fSAndreas Gohrclass admin_plugin_upgrade 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
3441163f44SAndreas Gohr    public function getMenuSort() { return 555; }
3534aae6dbSAndreas Gohr
3641163f44SAndreas Gohr    public function handle() {
3741163f44SAndreas Gohr        if($_REQUEST['step'] && !checkSecurityToken()){
3841163f44SAndreas Gohr            unset($_REQUEST['step']);
3941163f44SAndreas Gohr        }
4034aae6dbSAndreas Gohr    }
4134aae6dbSAndreas Gohr
4234aae6dbSAndreas Gohr    public function html() {
4341163f44SAndreas Gohr        global $ID;
4434aae6dbSAndreas Gohr        $abrt = false;
4534aae6dbSAndreas Gohr        $next = false;
4634aae6dbSAndreas Gohr
4734aae6dbSAndreas Gohr        echo '<h1>' . $this->getLang('menu') . '</h1>';
4834aae6dbSAndreas Gohr#FIXME check and abort on safemode
4934aae6dbSAndreas Gohr
5075e9d164SAndreas Gohr        $this->_say('<div id="plugin__update">');
5175e9d164SAndreas Gohr        // enable auto scroll
5275e9d164SAndreas Gohr        ?>
5375e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
5475e9d164SAndreas Gohr            var plugin_update = window.setInterval(function(){
5575e9d164SAndreas Gohr                var obj = $('plugin__update');
5675e9d164SAndreas Gohr                if(obj) obj.scrollTop = obj.scrollHeight;
5775e9d164SAndreas Gohr            },25);
5875e9d164SAndreas Gohr        </script>
5975e9d164SAndreas Gohr        <?php
6075e9d164SAndreas Gohr
6175e9d164SAndreas Gohr        // handle current step
6234aae6dbSAndreas Gohr        $this->_stepit(&$abrt, &$next);
6334aae6dbSAndreas Gohr
6475e9d164SAndreas Gohr        // disable auto scroll
6575e9d164SAndreas Gohr        ?>
6675e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
6775e9d164SAndreas Gohr            window.setTimeout(function(){
6875e9d164SAndreas Gohr                window.clearInterval(plugin_update);
6975e9d164SAndreas Gohr            },50);
7075e9d164SAndreas Gohr        </script>
7175e9d164SAndreas Gohr        <?php
7275e9d164SAndreas Gohr        $this->_say('</div>');
7375e9d164SAndreas Gohr
7475e9d164SAndreas Gohr        echo '<form action="" method="get" id="plugin__update_form">';
7534aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
7634aae6dbSAndreas Gohr        echo '<input type="hidden" name="page" value="update" />';
7741163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
7875e9d164SAndreas Gohr        if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" class="button continue" />';
7975e9d164SAndreas Gohr        if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" class="button abort" />';
8034aae6dbSAndreas Gohr        echo '</form>';
8134aae6dbSAndreas Gohr    }
8234aae6dbSAndreas Gohr
8334aae6dbSAndreas Gohr    private function _stepit(&$abrt, &$next){
8434aae6dbSAndreas Gohr        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){
8534aae6dbSAndreas Gohr            $step = array_shift(array_keys($_REQUEST['step']));
8634aae6dbSAndreas Gohr        }else{
8734aae6dbSAndreas Gohr            $step = '';
8834aae6dbSAndreas Gohr        }
8934aae6dbSAndreas Gohr
9034aae6dbSAndreas Gohr        if($step == 'cancel'){
9134aae6dbSAndreas Gohr            # cleanup
9234aae6dbSAndreas Gohr            @unlink($this->tgzfile);
936b2d1b30SAndreas Gohr            $this->_rdel($this->tgzdir);
9434aae6dbSAndreas Gohr            $step = '';
9534aae6dbSAndreas Gohr        }
9634aae6dbSAndreas Gohr
9734aae6dbSAndreas Gohr        if($step){
9834aae6dbSAndreas Gohr            $abrt = true;
9934aae6dbSAndreas Gohr            $next = false;
10034aae6dbSAndreas Gohr            if(!file_exists($this->tgzfile)){
10134aae6dbSAndreas Gohr                if($this->_step_download()) $next = 'unpack';
10234aae6dbSAndreas Gohr            }elseif(!is_dir($this->tgzdir)){
10334aae6dbSAndreas Gohr                if($this->_step_unpack()) $next = 'check';
10434aae6dbSAndreas Gohr            }elseif($step != 'upgrade'){
10534aae6dbSAndreas Gohr                if($this->_step_copy(true)) $next = 'upgrade';
10634aae6dbSAndreas Gohr            }elseif($step == 'upgrade'){
10734aae6dbSAndreas Gohr                if($this->_step_copy(false)) $next = 'cancel';
10834aae6dbSAndreas Gohr            }else{
10975e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
11034aae6dbSAndreas Gohr            }
11134aae6dbSAndreas Gohr        }else{
11234aae6dbSAndreas Gohr            # first time run, show intro
11334aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
11434aae6dbSAndreas Gohr            $abrt = false;
11534aae6dbSAndreas Gohr            $next = 'download';
11634aae6dbSAndreas Gohr        }
11734aae6dbSAndreas Gohr    }
11834aae6dbSAndreas Gohr
11934aae6dbSAndreas Gohr    private function _say(){
12034aae6dbSAndreas Gohr        $args = func_get_args();
12134aae6dbSAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n",$args);
12234aae6dbSAndreas Gohr        flush();
12334aae6dbSAndreas Gohr        ob_flush();
12434aae6dbSAndreas Gohr    }
12534aae6dbSAndreas Gohr
1266b2d1b30SAndreas Gohr    /**
1276b2d1b30SAndreas Gohr     * Recursive delete
1286b2d1b30SAndreas Gohr     *
1299285faa5SAndreas Gohr     * @author Jon Hassall
1309285faa5SAndreas Gohr     * @link http://de.php.net/manual/en/function.unlink.php#87045
1316b2d1b30SAndreas Gohr     */
1329285faa5SAndreas Gohr    private function _rdel($dir) {
1339285faa5SAndreas Gohr        if(!$dh = @opendir($dir)) {
1349285faa5SAndreas Gohr            return;
1359285faa5SAndreas Gohr        }
1369285faa5SAndreas Gohr        while (false !== ($obj = readdir($dh))) {
1379285faa5SAndreas Gohr            if($obj == '.' || $obj == '..') continue;
1389285faa5SAndreas Gohr
1399285faa5SAndreas Gohr            if (!@unlink($dir . '/' . $obj)) {
1409285faa5SAndreas Gohr                $this->_rdel($dir.'/'.$obj);
1419285faa5SAndreas Gohr            }
1429285faa5SAndreas Gohr        }
1439285faa5SAndreas Gohr        closedir($dh);
1449285faa5SAndreas Gohr        @rmdir($dir);
1456b2d1b30SAndreas Gohr    }
1466b2d1b30SAndreas Gohr
14734aae6dbSAndreas Gohr    private function _step_download(){
14834aae6dbSAndreas Gohr        $this->_say('Downloading from %s',$this->tgzurl);
14934aae6dbSAndreas Gohr
15034aae6dbSAndreas Gohr        @set_time_limit(120);
15134aae6dbSAndreas Gohr        @ignore_user_abort();
15234aae6dbSAndreas Gohr
15334aae6dbSAndreas Gohr        $http = new DokuHTTPClient();
15434aae6dbSAndreas Gohr        $http->timeout = 120;
15534aae6dbSAndreas Gohr        $data = $http->get($this->tgzurl);
15634aae6dbSAndreas Gohr
15734aae6dbSAndreas Gohr        if(!$data){
15834aae6dbSAndreas Gohr            $this->_say($http->error);
15934aae6dbSAndreas Gohr            $this->_say("Download failed.");
16034aae6dbSAndreas Gohr            return false;
16134aae6dbSAndreas Gohr        }
16234aae6dbSAndreas Gohr
16334aae6dbSAndreas Gohr        $this->_say('Received %d bytes',strlen($data));
16434aae6dbSAndreas Gohr
16534aae6dbSAndreas Gohr        if(!io_saveFile($this->tgzfile,$data)){
16634aae6dbSAndreas Gohr            $this->_say("Failed to save download.");
16734aae6dbSAndreas Gohr            return false;
16834aae6dbSAndreas Gohr        }
16934aae6dbSAndreas Gohr
17034aae6dbSAndreas Gohr        return true;
17134aae6dbSAndreas Gohr    }
17234aae6dbSAndreas Gohr
17334aae6dbSAndreas Gohr    private function _step_unpack(){
17434aae6dbSAndreas Gohr        global $conf;
17575e9d164SAndreas Gohr        $this->_say('<b>Extracting the archive...</b>');
17634aae6dbSAndreas Gohr
17734aae6dbSAndreas Gohr        @set_time_limit(120);
17834aae6dbSAndreas Gohr        @ignore_user_abort();
17934aae6dbSAndreas Gohr
18034aae6dbSAndreas Gohr        $tar = new VerboseTarLib($this->tgzfile);
18134aae6dbSAndreas Gohr        if($tar->_initerror < 0){
18234aae6dbSAndreas Gohr            $this->_say($tar->TarErrorStr($tar->_initerror));
18334aae6dbSAndreas Gohr            $this->_say('Extraction failed on init');
18434aae6dbSAndreas Gohr            return false;
18534aae6dbSAndreas Gohr        }
18634aae6dbSAndreas Gohr
18734aae6dbSAndreas Gohr        $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/');
18834aae6dbSAndreas Gohr        if($ok < 1){
18934aae6dbSAndreas Gohr            $this->_say($tar->TarErrorStr($ok));
19034aae6dbSAndreas Gohr            $this->_say('Extraction failed');
19134aae6dbSAndreas Gohr            return false;
19234aae6dbSAndreas Gohr        }
19334aae6dbSAndreas Gohr
19434aae6dbSAndreas Gohr        $this->_say('Extraction done.');
195738c0102SAndreas Gohr
196738c0102SAndreas Gohr        $this->_say('Version <b>%s</b> ready to install. Your current version is <b>%s</b>.',
197738c0102SAndreas Gohr                    hsc(file_get_contents($this->tgzdir.'/VERSION')),
198738c0102SAndreas Gohr                    getVersion());
19934aae6dbSAndreas Gohr        return true;
20034aae6dbSAndreas Gohr    }
20134aae6dbSAndreas Gohr
20234aae6dbSAndreas Gohr    private function _step_copy($dryrun=true){
20334aae6dbSAndreas Gohr        $ok = $this->_traverse('',$dryrun);
20434aae6dbSAndreas Gohr        if($dryrun){
20534aae6dbSAndreas Gohr            if($ok){
20634aae6dbSAndreas Gohr                $this->_say('<b>All files are writable, ready to upgrade</b>');
20734aae6dbSAndreas Gohr            }else{
20834aae6dbSAndreas Gohr                $this->_say('<b>Some files aren\'t writable. Uprade not possible.</b>');
20934aae6dbSAndreas Gohr            }
21034aae6dbSAndreas Gohr        }else{
21134aae6dbSAndreas Gohr            if($ok){
21234aae6dbSAndreas Gohr                $this->_say('<b>All files upgraded successfully</b>');
21334aae6dbSAndreas Gohr
21434aae6dbSAndreas Gohr                #FIXME delete unused files
21534aae6dbSAndreas Gohr            }else{
21634aae6dbSAndreas Gohr                $this->_say('<b>Some files couldn\'t be upgraded. Uh-oh. Better check manually.</b>');
21734aae6dbSAndreas Gohr            }
21834aae6dbSAndreas Gohr        }
21934aae6dbSAndreas Gohr        return $ok;
22034aae6dbSAndreas Gohr    }
22134aae6dbSAndreas Gohr
22234aae6dbSAndreas Gohr    private function _traverse($dir,$dryrun){
22334aae6dbSAndreas Gohr        $base = $this->tgzdir;
22434aae6dbSAndreas Gohr        $ok = true;
22534aae6dbSAndreas Gohr
22634aae6dbSAndreas Gohr        $dh = @opendir($base.'/'.$dir);
22734aae6dbSAndreas Gohr        if(!$dh) return;
22834aae6dbSAndreas Gohr        while(($file = readdir($dh)) !== false){
22934aae6dbSAndreas Gohr            if($file == '.' || $file == '..') continue;
23034aae6dbSAndreas Gohr            $from = "$base/$dir/$file";
23134aae6dbSAndreas Gohr            $to   = DOKU_INC."$dir/$file";
23234aae6dbSAndreas Gohr
23334aae6dbSAndreas Gohr            if(is_dir($from)){
23434aae6dbSAndreas Gohr                if($dryrun){
23534aae6dbSAndreas Gohr                    // just check for writability
23634aae6dbSAndreas Gohr                    if(!is_dir($to)){
23734aae6dbSAndreas Gohr                        if(is_dir(dirname($to)) && !is_writable(dirname($to))){
23834aae6dbSAndreas Gohr                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
23934aae6dbSAndreas Gohr                            $ok = false;
24034aae6dbSAndreas Gohr                        }
24134aae6dbSAndreas Gohr                    }
24234aae6dbSAndreas Gohr                }
24334aae6dbSAndreas Gohr
24434aae6dbSAndreas Gohr                // recursion
24534aae6dbSAndreas Gohr                if(!$this->_traverse("$dir/$file",$dryrun)){
24634aae6dbSAndreas Gohr                    $ok = false;
24734aae6dbSAndreas Gohr                }
24834aae6dbSAndreas Gohr            }else{
249f2fa6d10SAndreas Gohr                $fmd5 = md5(@file_get_contents($from));
250f2fa6d10SAndreas Gohr                $tmd5 = md5(@file_get_contents($to));
25134aae6dbSAndreas Gohr                if($fmd5 != $tmd5){
25234aae6dbSAndreas Gohr                    if($dryrun){
25334aae6dbSAndreas Gohr                        // just check for writability
25434aae6dbSAndreas Gohr                        if( (file_exists($to) && !is_writable($to)) ||
25534aae6dbSAndreas Gohr                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){
25634aae6dbSAndreas Gohr
25734aae6dbSAndreas Gohr                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
25834aae6dbSAndreas Gohr                            $ok = false;
25934aae6dbSAndreas Gohr                        }else{
26034aae6dbSAndreas Gohr                            $this->_say("%s needs update",hsc("$dir/$file"));
26134aae6dbSAndreas Gohr                        }
26234aae6dbSAndreas Gohr                    }else{
26334aae6dbSAndreas Gohr                        // check dir
26434aae6dbSAndreas Gohr                        if(io_mkdir_p(dirname($to))){
26534aae6dbSAndreas Gohr                            // copy
26634aae6dbSAndreas Gohr                            if(!copy($from,$to)){
26734aae6dbSAndreas Gohr                                $this->_say("<b>%s couldn't be copied</b>",hsc("$dir/$file"));
26834aae6dbSAndreas Gohr                                $ok = false;
26934aae6dbSAndreas Gohr                            }else{
27034aae6dbSAndreas Gohr                                $this->_say("%s updated",hsc("$dir/$file"));
27134aae6dbSAndreas Gohr                            }
27234aae6dbSAndreas Gohr                        }else{
27334aae6dbSAndreas Gohr                            $this->_say("<b>failed to create %s</b>",hsc("$dir"));
27434aae6dbSAndreas Gohr                            $ok = false;
27534aae6dbSAndreas Gohr                        }
27634aae6dbSAndreas Gohr                    }
27734aae6dbSAndreas Gohr                }
27834aae6dbSAndreas Gohr            }
27934aae6dbSAndreas Gohr        }
28034aae6dbSAndreas Gohr        closedir($dh);
28134aae6dbSAndreas Gohr        return $ok;
28234aae6dbSAndreas Gohr    }
28334aae6dbSAndreas Gohr}
28434aae6dbSAndreas Gohr
28534aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
286